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

Last updated