100 lines
3.4 KiB
Python
100 lines
3.4 KiB
Python
# --- Do not remove these libs ---
|
|
from freqtrade.strategy.interface import IStrategy
|
|
from typing import Dict, List
|
|
from functools import reduce
|
|
from pandas import DataFrame, merge, DatetimeIndex
|
|
# --------------------------------
|
|
|
|
import talib.abstract as ta
|
|
import freqtrade.vendor.qtpylib.indicators as qtpylib
|
|
|
|
|
|
class ReinforcedAverageStrategy(IStrategy):
|
|
"""
|
|
|
|
author@: Gert Wohlgemuth
|
|
|
|
idea:
|
|
buys and sells on crossovers - doesn't really perfom that well and its just a proof of concept
|
|
"""
|
|
|
|
# Minimal ROI designed for the strategy.
|
|
# This attribute will be overridden if the config file contains "minimal_roi"
|
|
minimal_roi = {
|
|
"0": 0.5
|
|
}
|
|
|
|
# Optimal stoploss designed for the strategy
|
|
# This attribute will be overridden if the config file contains "stoploss"
|
|
stoploss = -0.2
|
|
|
|
# Optimal ticker interval for the strategy
|
|
ticker_interval = '4h'
|
|
|
|
def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
|
|
macd = ta.MACD(dataframe)
|
|
|
|
dataframe['maShort'] = ta.EMA(dataframe, timeperiod=8)
|
|
dataframe['maMedium'] = ta.EMA(dataframe, timeperiod=21)
|
|
##################################################################################
|
|
# required for graphing
|
|
bollinger = qtpylib.bollinger_bands(dataframe['close'], window=20, stds=2)
|
|
dataframe['bb_lowerband'] = bollinger['lower']
|
|
dataframe['bb_upperband'] = bollinger['upper']
|
|
dataframe['bb_middleband'] = bollinger['mid']
|
|
return dataframe
|
|
|
|
def populate_buy_trend(self, dataframe: DataFrame) -> DataFrame:
|
|
"""
|
|
Based on TA indicators, populates the buy signal for the given dataframe
|
|
:param dataframe: DataFrame
|
|
:return: DataFrame with buy column
|
|
"""
|
|
dataframe = ReinforcedAverageStrategy.resample(dataframe, self.ticker_interval, 12)
|
|
|
|
dataframe.loc[
|
|
(
|
|
qtpylib.crossed_above(dataframe['maShort'], dataframe['maMedium']) &
|
|
dataframe['close'] > dataframe['resample_sma']
|
|
),
|
|
'buy'] = 1
|
|
|
|
return dataframe
|
|
|
|
def populate_sell_trend(self, dataframe: DataFrame) -> DataFrame:
|
|
"""
|
|
Based on TA indicators, populates the sell signal for the given dataframe
|
|
:param dataframe: DataFrame
|
|
:return: DataFrame with buy column
|
|
"""
|
|
dataframe.loc[
|
|
(
|
|
qtpylib.crossed_above(dataframe['maMedium'], dataframe['maShort'])
|
|
),
|
|
'sell'] = 1
|
|
return dataframe
|
|
|
|
@staticmethod
|
|
def resample( dataframe, interval, factor):
|
|
|
|
|
|
# defines the reinforcement logic
|
|
# resampled dataframe to establish if we are in an uptrend, downtrend or sideways trend
|
|
df = dataframe.copy()
|
|
df = df.set_index(DatetimeIndex(df['date']))
|
|
ohlc_dict = {
|
|
'open': 'first',
|
|
'high': 'max',
|
|
'low': 'min',
|
|
'close': 'last'
|
|
}
|
|
df = df.resample(str(int(interval[:-1]) * factor) + 'min').agg(ohlc_dict)
|
|
df['resample_sma'] = ta.SMA(df, timeperiod=50, price='close')
|
|
df = df.drop(columns=['open', 'high', 'low', 'close'])
|
|
df = df.resample(interval[:-1] + 'min')
|
|
df = df.interpolate(method='time')
|
|
df['date'] = df.index
|
|
df.index = range(len(df))
|
|
dataframe = merge(dataframe, df, on='date', how='left')
|
|
return dataframe
|