-
Notifications
You must be signed in to change notification settings - Fork 22
/
Canals.py
executable file
·146 lines (118 loc) · 4.68 KB
/
Canals.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# --- Do not remove these libs ---
from freqtrade.strategy.interface import IStrategy
from typing import Dict, List
from functools import reduce
from pandas import DataFrame
# --------------------------------
import talib.abstract as ta
import numpy as np
import freqtrade.vendor.qtpylib.indicators as qtpylib
import datetime
from technical.util import resample_to_interval, resampled_merge
from freqtrade.strategy import DecimalParameter, IntParameter, BooleanParameter
rangeUpper = 60
rangeLower = 5
def EWO(dataframe, ema_length=5, ema2_length=35):
df = dataframe.copy()
ema1 = ta.EMA(df, timeperiod=ema_length)
ema2 = ta.EMA(df, timeperiod=ema2_length)
emadif = (ema1 - ema2) / df['close'] * 100
return emadif
def valuewhen(dataframe, condition, source, occurrence):
copy = dataframe.copy()
copy['colFromIndex'] = copy.index
copy = copy.sort_values(by=[condition, 'colFromIndex'], ascending=False).reset_index(drop=True)
copy['valuewhen'] = np.where(copy[condition] > 0, copy[source].shift(-occurrence), copy[source])
copy['barrsince'] = copy['colFromIndex'] - copy['colFromIndex'].shift(-occurrence)
copy.loc[
(
(rangeLower <= copy['barrsince']) &
(copy['barrsince'] <= rangeUpper)
)
, "in_range"] = 1
copy['in_range'] = copy['in_range'].fillna(0)
copy = copy.sort_values(by=['colFromIndex'], ascending=True).reset_index(drop=True)
return copy['valuewhen'], copy['in_range'], copy['barrsince']
class Canals(IStrategy):
INTERFACE_VERSION = 2
# Buy hyperspace params:
buy_params = {
}
# Sell hyperspace params:
sell_params = {
}
# ROI table:
minimal_roi = {
"0": 0.05,
}
# Stoploss:
stoploss = -0.08
# Trailing stop:
trailing_stop = True
trailing_stop_positive = 0.005
trailing_stop_positive_offset = 0.02
trailing_only_offset_is_reached = True
# Optimal timeframe for the strategy
timeframe = '5m'
use_custom_stoploss = False
# Number of candles the strategy requires before producing valid signals
startup_candle_count: int = 30
osc = 'close'
len = 14
src = 'close'
lbL = 14
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['RSI'] = ta.RSI(dataframe[self.src], self.len)
dataframe['RSI'] = dataframe['RSI'].fillna(0)
stoch = ta.STOCH(dataframe, fastk_period=10, slowk_period=3, slowk_matype=0, slowd_period=3, slowd_matype=0)
dataframe['slowk'] = stoch['slowk']
dataframe['slowd'] = stoch['slowd']
dataframe['osc'] = dataframe[self.osc]
# plFound = na(pivotlow(osc, lbL, lbR)) ? false : true
dataframe['min'] = dataframe['osc'].rolling(self.lbL).min()
dataframe['prevMin'] = np.where(dataframe['min'] > dataframe['min'].shift(), dataframe['min'].shift(), dataframe['min'])
dataframe.loc[
(
(dataframe['osc'] == dataframe['prevMin'])
)
, 'plFound'] = 1
# phFound = na(pivothigh(osc, lbL, lbR)) ? false : true
dataframe['max'] = dataframe['osc'].rolling(self.lbL).max()
dataframe['prevMax'] = np.where(dataframe['max'] < dataframe['max'].shift(), dataframe['max'].shift(), dataframe['max'])
dataframe.loc[
(
(dataframe['osc'] == dataframe['prevMax'])
)
, 'phFound'] = 1
dataframe['valuewhen_plFound'], dataframe['inrange_plFound'], dataframe['barssince_plFound'] = valuewhen(dataframe,'plFound', 'close', 1)
dataframe['valuewhen_phFound'], dataframe['inrange_phFound'], dataframe['barssince_phFound'] = valuewhen(dataframe, 'phFound', 'close', 1)
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
conditions = []
conditions.append(
(
(dataframe['bullCond'] > 0) &
(dataframe['volume'] > 0)
)
)
if conditions:
dataframe.loc[
reduce(lambda x, y: x | y, conditions),
'buy'
] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
conditions = []
conditions.append(
(
(dataframe['bearCond'] > 0) &
(dataframe['volume'] > 0)
)
)
if conditions:
dataframe.loc[
reduce(lambda x, y: x | y, conditions),
'sell'
] = 1
dataframe.to_csv('user_data/csvs/%s_%s.csv' % (self.__class__.__name__, metadata["pair"].replace("/", "_")))
return dataframe