Step4: Write Template

The template can clearly explain the alert data, making it easier for users to understand.

Because the data fields referenced in each indicator are different, there is no reusability. In order to improve the flexibility of the data and make the expression of the alert more understandable, we have separated the processing of the indicator data from the broadcast text, and introduced the template engine. We create an alert template corresponding to the language for each indicator, and the data that needs to be used in the template is replaced by variables. After the logic of the indicators is processed, the corresponding data variables are passed to the template, which can be easily compiled into broadcast text in different languages.

We are using Jinjia2 templating engine

Jinja is a fast, expressive, extensible templating engine. Special placeholders in the template allow writing code similar to Python syntax. Then the template is passed data to render the final document. More info: https://github.com/pallets/jinja

For Example

The corresponding EN template is:

According to KingData monitoring, {% for coin in info%}In the past 4 hours, {{coin.symbol}} Long/Short Ratio across network is {{coin.long_short_rate}}, with {{coin.long_rate}}% longs and  {{coin.short_rate}}% shorts. {%if coin.long_short_rate>1%}Longs outweigh shorts{% else %}Shorts outweigh Longs{% endif %}.
Among leading exchanges:{% for exchange in coin.list %}
{{exchange.exchange_name}}: long {{exchange.long_rate}}%, short {{exchange.short_rate}}%{% endfor %}

{% endfor %}

The rendered EN text is:

According to KingData monitoring, In the past 4 hours, BTC Long/Short Ratio across network is 1.08, with 51.86% longs and  48.14% shorts. Longs outweigh shorts.
Among leading exchanges:
Binance: long 46.4%, short 53.6%
OKX: long 57.96%, short 42.04%
Bitget: long 50.53%, short 49.47%

The corresponding CN template is:

The rendered CN text is:

So the complete code is

Last updated