Skip to content

Commit 3131d3a

Browse files
authored
Add files via upload
1 parent 10739a9 commit 3131d3a

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

SuperTrendPure.py

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import logging
2+
from numpy.lib import math
3+
from freqtrade.strategy.interface import IStrategy
4+
from freqtrade.strategy.hyper import IntParameter
5+
from pandas import DataFrame
6+
import talib.abstract as ta
7+
import numpy as np
8+
import freqtrade.vendor.qtpylib.indicators as qtpylib
9+
10+
11+
class SuperTrendPure(IStrategy):
12+
13+
# ROI table:
14+
minimal_roi = {
15+
"0": 0.087,
16+
"372": 0.058,
17+
"861": 0.029,
18+
"2221": 0
19+
}
20+
# Stoploss:
21+
stoploss = -0.265
22+
23+
# Trailing stop:
24+
trailing_stop = True
25+
trailing_stop_positive = 0.05
26+
trailing_stop_positive_offset = 0.144
27+
trailing_only_offset_is_reached = False
28+
29+
timeframe = '1h'
30+
31+
startup_candle_count = 50
32+
33+
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
34+
35+
supertrend = self.supertrend(dataframe, 2, 8)
36+
dataframe['st'] = supertrend['ST']
37+
dataframe['stx'] = supertrend['STX']
38+
39+
return dataframe
40+
41+
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
42+
dataframe.loc[
43+
(
44+
(qtpylib.crossed_above(dataframe['close'], dataframe['st'])) &
45+
(dataframe['volume'].gt(0))
46+
),
47+
'buy'] = 1
48+
49+
return dataframe
50+
51+
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
52+
dataframe.loc[
53+
(
54+
(qtpylib.crossed_below(dataframe['close'], dataframe['st'])) &
55+
(dataframe['volume'].gt(0))
56+
),
57+
'sell'] = 1
58+
59+
return dataframe
60+
61+
"""
62+
Supertrend Indicator; adapted for freqtrade
63+
from: https://github.com/freqtrade/freqtrade-strategies/issues/30
64+
"""
65+
def supertrend(self, dataframe: DataFrame, multiplier, period):
66+
df = dataframe.copy()
67+
68+
df['TR'] = ta.TRANGE(df)
69+
df['ATR'] = ta.SMA(df['TR'], period)
70+
71+
st = 'ST_' + str(period) + '_' + str(multiplier)
72+
stx = 'STX_' + str(period) + '_' + str(multiplier)
73+
74+
# Compute basic upper and lower bands
75+
df['basic_ub'] = (df['high'] + df['low']) / 2 + multiplier * df['ATR']
76+
df['basic_lb'] = (df['high'] + df['low']) / 2 - multiplier * df['ATR']
77+
78+
# Compute final upper and lower bands
79+
df['final_ub'] = 0.00
80+
df['final_lb'] = 0.00
81+
for i in range(period, len(df)):
82+
df['final_ub'].iat[i] = df['basic_ub'].iat[i] if df['basic_ub'].iat[i] < df['final_ub'].iat[i - 1] or df['close'].iat[i - 1] > df['final_ub'].iat[i - 1] else df['final_ub'].iat[i - 1]
83+
df['final_lb'].iat[i] = df['basic_lb'].iat[i] if df['basic_lb'].iat[i] > df['final_lb'].iat[i - 1] or df['close'].iat[i - 1] < df['final_lb'].iat[i - 1] else df['final_lb'].iat[i - 1]
84+
85+
# Set the Supertrend value
86+
df[st] = 0.00
87+
for i in range(period, len(df)):
88+
df[st].iat[i] = df['final_ub'].iat[i] if df[st].iat[i - 1] == df['final_ub'].iat[i - 1] and df['close'].iat[i] <= df['final_ub'].iat[i] else \
89+
df['final_lb'].iat[i] if df[st].iat[i - 1] == df['final_ub'].iat[i - 1] and df['close'].iat[i] > df['final_ub'].iat[i] else \
90+
df['final_lb'].iat[i] if df[st].iat[i - 1] == df['final_lb'].iat[i - 1] and df['close'].iat[i] >= df['final_lb'].iat[i] else \
91+
df['final_ub'].iat[i] if df[st].iat[i - 1] == df['final_lb'].iat[i - 1] and df['close'].iat[i] < df['final_lb'].iat[i] else 0.00
92+
# Mark the trend direction up/down
93+
df[stx] = np.where((df[st] > 0.00), np.where((df['close'] < df[st]), 'down', 'up'), np.NaN)
94+
95+
# Remove basic and final bands from the columns
96+
df.drop(['basic_ub', 'basic_lb', 'final_ub', 'final_lb'], inplace=True, axis=1)
97+
98+
df.fillna(0, inplace=True)
99+
100+
return DataFrame(index=df.index, data={
101+
'ST' : df[st],
102+
'STX' : df[stx]
103+
})

0 commit comments

Comments
 (0)