130 lines
4.1 KiB
Python
130 lines
4.1 KiB
Python
|
|
# --- Do not remove these libs ---
|
|
from freqtrade.strategy.interface import IStrategy
|
|
from typing import Dict, List
|
|
from hyperopt import hp
|
|
from functools import reduce
|
|
from pandas import DataFrame
|
|
# --------------------------------
|
|
|
|
# Add your lib to import here
|
|
import talib.abstract as ta
|
|
|
|
|
|
# Update this variable if you change the class name
|
|
class_name = 'TestStrategy'
|
|
|
|
|
|
class TestStrategy(IStrategy):
|
|
"""
|
|
This is a test strategy to inspire you.
|
|
You can:
|
|
- Rename the class name (Do not forget to update class_name)
|
|
- Add any methods you want to build your strategy
|
|
- Add any lib you need to build your strategy
|
|
|
|
You must keep:
|
|
- the lib in the section "Do not remove these libs"
|
|
- the prototype for the methods: minimal_roi, stoploss, populate_indicators, populate_buy_trend,
|
|
populate_sell_trend, hyperopt_space, buy_strategy_generator
|
|
"""
|
|
|
|
# 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
|
|
}
|
|
|
|
# Optimal stoploss designed for the strategy
|
|
# This attribute will be overridden if the config file contains "stoploss"
|
|
stoploss = -0.10
|
|
|
|
def populate_indicators(self, dataframe: DataFrame) -> DataFrame:
|
|
"""
|
|
Adds several different TA indicators to the given DataFrame
|
|
|
|
Performance Note: For the best performance be frugal on the number of indicators
|
|
you are using. Let uncomment only the indicator you are using in your strategies
|
|
or your hyperopt configuration, otherwise you will waste your memory and CPU usage.
|
|
"""
|
|
|
|
dataframe['adx'] = ta.ADX(dataframe)
|
|
dataframe['blower'] = ta.BBANDS(dataframe, nbdevup=2, nbdevdn=2)['lowerband']
|
|
dataframe['tema'] = ta.TEMA(dataframe, timeperiod=9)
|
|
|
|
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.loc[
|
|
(
|
|
(dataframe['adx'] > 30) &
|
|
(dataframe['tema'] <= dataframe['blower']) &
|
|
(dataframe['tema'] > dataframe['tema'].shift(1))
|
|
),
|
|
'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[
|
|
(
|
|
(dataframe['adx'] > 70) &
|
|
(dataframe['tema'] > dataframe['blower']) &
|
|
(dataframe['tema'] < dataframe['tema'].shift(1))
|
|
),
|
|
'sell'] = 1
|
|
return dataframe
|
|
|
|
def hyperopt_space(self) -> List[Dict]:
|
|
"""
|
|
Define your Hyperopt space for the strategy
|
|
"""
|
|
space = {
|
|
'adx': hp.choice('adx', [
|
|
{'enabled': False},
|
|
{'enabled': True, 'value': hp.quniform('adx-value', 15, 50, 1)}
|
|
]),
|
|
'trigger': hp.choice('trigger', [
|
|
{'type': 'lower_bb'},
|
|
]),
|
|
'stoploss': hp.uniform('stoploss', -0.5, -0.02),
|
|
}
|
|
return space
|
|
|
|
def buy_strategy_generator(self, params) -> None:
|
|
"""
|
|
Define the buy strategy parameters to be used by hyperopt
|
|
"""
|
|
def populate_buy_trend(dataframe: DataFrame) -> DataFrame:
|
|
conditions = []
|
|
# GUARDS AND TRENDS
|
|
if params['adx']['enabled']:
|
|
conditions.append(dataframe['adx'] > params['adx']['value'])
|
|
|
|
# TRIGGERS
|
|
triggers = {
|
|
'lower_bb': dataframe['tema'] <= dataframe['blower'],
|
|
}
|
|
conditions.append(triggers.get(params['trigger']['type']))
|
|
|
|
dataframe.loc[
|
|
reduce(lambda x, y: x & y, conditions),
|
|
'buy'] = 1
|
|
|
|
return dataframe
|
|
|
|
return populate_buy_trend
|