-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathMACDCrossoverWithTrend.py
77 lines (59 loc) · 2.51 KB
/
MACDCrossoverWithTrend.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
from freqtrade.strategy import IStrategy, merge_informative_pair
from pandas import DataFrame
import talib.abstract as ta
import freqtrade.vendor.qtpylib.indicators as qtpylib
import numpy # noqa
class MACDCrossoverWithTrend(IStrategy):
"""
MACDCrossoverWithTrend
author@: Paul Csapak
github@: https://github.com/paulcpk/freqtrade-strategies-that-work
How to use it?
> freqtrade download-data --timeframes 1h --timerange=20180301-20200301
> freqtrade backtesting --export trades -s MACDCrossoverWithTrend --timeframe 1h --timerange=20180301-20200301
> freqtrade plot-dataframe -s MACDCrossoverWithTrend --indicators1 ema100 --timeframe 1h --timerange=20180301-20200301
"""
# Minimal ROI designed for the strategy.
# This attribute will be overridden if the config file contains "minimal_roi"
# minimal_roi = {
# "40": 0.0,
# "30": 0.01,
# "20": 0.02,
# "0": 0.04
# }
# This attribute will be overridden if the config file contains "stoploss"
stoploss = -0.2
# Optimal timeframe for the strategy
timeframe = '1h'
# trailing stoploss
trailing_stop = False
trailing_stop_positive = 0.03
trailing_stop_positive_offset = 0.04
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
macd = ta.MACD(dataframe)
dataframe['macd'] = macd['macd']
dataframe['macdsignal'] = macd['macdsignal']
dataframe['macdhist'] = macd['macdhist']
dataframe['ema100'] = ta.EMA(dataframe, timeperiod=100)
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[
(
(dataframe['macd'] < 0) & # MACD is below zero
# Signal crosses above MACD
(qtpylib.crossed_above(dataframe['macd'], dataframe['macdsignal'])) &
(dataframe['low'] > dataframe['ema100']) & # Candle low is above EMA
# Ensure this candle had volume (important for backtesting)
(dataframe['volume'] > 0)
),
'buy'] = 1
return dataframe
def populate_sell_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe.loc[
(
# MACD crosses above Signal
(qtpylib.crossed_below(dataframe['macd'], 0)) |
(dataframe['low'] < dataframe['ema100']) # OR price is below trend ema
),
'sell'] = 1
return dataframe