Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement SuperTrend V.1 Super trend Line system by python

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how python implements the SuperTrend V.1 super trend line system. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

System introduction

CMC Markets New Generation Intelligent Trading system-- Super trend Line (Supertrend)

Here is an article about the system.

In the new generation intelligent trading system in CMC Markets, you can use it by selecting "super trend line" from the technical index.

As shown in the picture, you can adjust the "color and thickness" to the rising signal and the falling signal according to your preference.

So what is the overtrend indicator? Before understanding the hypertrend indicator formula, it is necessary to understand the ATR, because the hypertrend uses ATR values to calculate the index values.

There is also a picture below to introduce the main algorithms.

At a general glance, the main description is the channel HL2 (k-line average price) multiplied by n times ATR. Make a trend breakthrough.

But the article is relatively brief. There is no detailed algorithm. Then I thought of the best community, Tradingview.

It's not surprising. It does have it.

From the picture, it is more in line with the trend. But unfortunately, it is only an Alert alarm signal.

Learning source code

Seeing that the code is not too long, let's translate it and have a try. (thanks omega) "✎ baby"!

The complete pine code is as above.

IV. Code conversion

Here we create a new policy in FMZ, named SuperTrade

Then let's set two parameters: Factor and Pd.

In order to simplify the operation of the code and make it easy to understand, it is necessary to use python's advanced data expansion package pandas

At lunch, I asked Mr. Mengmeng if FMZ supported this library. It can be used in the afternoon.

Teacher Mengmeng is really amazing.

1. We need to import the pandas library, the time library.

two。 Set the quarterly contract in the main function (mainly run okex)

3. Set a cycle doTicker () to check once every 15 minutes.

Run the code in a 15-minute cycle

Then we write the main strategy in doTicker ().

4. We want to get back the OHCLV of k-line, so use GetRecords ()

5. We import the retrieved data into pandas M15 = pd.DataFrame (records)

6. We need to modify the header label of the table. M15.columns = ['time','open','high','low','close','volume','OpenInterest']

In fact, it is to change the first letter of 'open','high','low','close' to lowercase, so as to make it easier to write code later without capitalization and lowercase.

7. Add a column of hl2 hl2= (high+low) / 2 to the data set

8. Then let's calculate the ATR.

Because the calculation of ATR imports a variable length, whose value is Pd

Then, by referring to the Mai language manual, the algorithm steps for the average amplitude of ATR real waves are as follows:

TR: MAX (MAX ((HIGH-LOW), ABS (REF (CLOSE,1)-HIGH)), ABS (REF (CLOSE,1)-LOW))

ATR: RMA (TR,N)

The value of TR takes the largest one of the following three differences.

1. The HIGH-LOW between the highest price and the lowest price on the current trading day

2. The volatility between the closing price of the previous trading day and the highest price of the current trading day REF (CLOSE,1)-HIGH)

3. The volatility between the closing price of the previous trading day and the lowest price of the current trading day REF (CLOSE,1)-LOW)

So TR: MAX (MAX ((HIGH-LOW), ABS (REF (CLOSE,1)-HIGH)), ABS (REF (CLOSE,1)-LOW))

In python calculation

First, set up a prev_close to fetch the data of close on the previous line, that is, move the close to the right to set a new parameter.

Then define an array of intermediate variables that record the three logarithms of TR. (HIGH-LOW) (high-prev_close) (low-prev_close)

We define a new column in the data set, named TR,TR, whose value is the largest of the absolute values of the intermediate variables, using the abs () and max () functions

Finally, we want to calculate the value of ATR, ATR: RMA (TR,N). It is found that RMA's algorithm is actually a fixed-value variant EMA algorithm.

N is the variable we imported, where the default parameter for ATR is 14. Here we import the reciprocal of alpha=length.

=

Then use ewm algorithm to calculate ema

The complete ATR calculation process is as follows

Calculate Up and Dn from 9

Up=hl2-(Factor * atr)

Dn=hl2 + (Factor * atr)

Isn't it easy.

The following is the core code snippet of 15-21 lines in TV

The main meaning of this paragraph is to express

If you are in the bullish phase, (lower line) TrendUp = max (Up,TrendUp [1])

TrendDown=min (Dn,TrendDown [1]) if you are in a downward phase.

In other words, in a trend, the value of ATR has been using a technique similar to the robber Bollinger strategy.

Keep narrowing the other side of the channel

Here, each calculation of TrendUp and TrendDown requires self-iteration.

That is, you have to calculate each step by yourself.

So loop through the data set.

Here, we need to create a new field TrendUp,TrendDown,Trend,linecolor for the data collection. And give them an initial value.

Then use the fillna (0) syntax to fill in the data with null values in the previously calculated results.

Enable a for loop

Using python trinomial operation in the loop

Calculate TrendUp

TrendUp = MAX (Up,TrendUp [- 1]) if close [- 1] > TrendUp [- 1] else Up

It roughly means that if the previous close > the previous TrendUp, take the maximum value between the Up and the previous TrendUp, not the up value, and pass it to the current TrendUp

In the same way, calculate TrendDown

TrendDown=min (Dn,TrendDown [- 1]) if close [- 1] the previous TrendDown takes 1 (bullish) does not hold x

If the closing price

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report