KingData Open Platform
  • Open Platform Introduction
  • How to develop an indicator
    • Step1: Clone Repo
    • Step2: Write Definition.md
    • Step3: Write Logic Code
    • Step4: Write Template
    • Step5: Test and Pull Request
  • Examples
    • Demo with JSON data
    • Demo with HTML data
    • Demo with Socket Data
  • ⚠️ Attention && FAQ
    • How to store data
  • Related Links
  • API DOC
    • Integrate KingData content into your app
Powered by GitBook
On this page
  1. How to develop an indicator

Step3: Write Logic Code

To achieve an indicator, we usually consider the following issues

  1. What data source is used

  2. What is the processing logic and how to set the threshold

  3. Output parameters

  4. Render template

Create a new new_coin_spider.py file in the directory crawlers/indicators/spiders/mainstream_coin_long_short_ratio. The code are as follows

from crawlers.utils import SpiderBase
from jinja2 import Template
from crawlers.utils.group_alarm import catch_except


def build_coin_info(data):
    return {
        'symbol': data['symbol'],
        'long_short_rate': round(data['longRate'] / data['shortRate'], 2),
        'long_rate': data['longRate'],
        'short_rate': data['shortRate'],
        'list': [{
            'exchange_name': exchange_info['exchangeName'],
            'long_rate': exchange_info['longRate'],
            'short_rate': exchange_info['shortRate']
        } for exchange_info in data['list'][:3]]
    }


class ContractPositionRatio(SpiderBase):
    name = 'idx-contract-position-ratio'

    start_urls = [
        'https://fapi.coinglass.com/api/futures/longShortRate?symbol=BTC&timeType=3',
        'https://fapi.coinglass.com/api/futures/longShortRate?symbol=ETH&timeType=3'
    ]

    @catch_except
    def parse(self, response, **kwargs):
        data = response.json()['data'][0]
        params = {
            'info': [build_coin_info(data)]
        }

Next, we need to define the template of the broadcast, and the template is used to render the variables in params

PreviousStep2: Write Definition.mdNextStep4: Write Template

Last updated 2 years ago