117 lines
3.6 KiB
Python
117 lines
3.6 KiB
Python
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
|
||
# flake8: noqa: F401
|
||
# isort: skip_file
|
||
# --- Do not remove these imports ---
|
||
import numpy as np
|
||
import pandas as pd
|
||
from datetime import datetime, timedelta, timezone
|
||
from pandas import DataFrame
|
||
from typing import Optional, Union
|
||
from freqtrade.strategy import (
|
||
IStrategy,
|
||
Trade,
|
||
Order,
|
||
PairLocks,
|
||
informative, # @informative decorator
|
||
# Hyperopt Parameters
|
||
BooleanParameter,
|
||
CategoricalParameter,
|
||
DecimalParameter,
|
||
IntParameter,
|
||
RealParameter,
|
||
# timeframe helpers
|
||
timeframe_to_minutes,
|
||
timeframe_to_next_date,
|
||
timeframe_to_prev_date,
|
||
# Strategy helper functions
|
||
merge_informative_pair,
|
||
stoploss_from_absolute,
|
||
stoploss_from_open,
|
||
)
|
||
import logging
|
||
|
||
logger = logging.getLogger(__name__)
|
||
# --------------------------------
|
||
# Add your lib to import here
|
||
import talib.abstract as ta
|
||
from technical import qtpylib
|
||
|
||
|
||
class MACDStrategy(IStrategy):
|
||
# 策略参数
|
||
INTERFACE_VERSION = 3
|
||
|
||
minimal_roi = {"0": 100}
|
||
stoploss = -1
|
||
trailing_stop = False
|
||
timeframe = '15m'
|
||
|
||
use_exit_signal = True
|
||
exit_profit_only = False
|
||
ignore_roi_if_entry_signal = False
|
||
|
||
def TD(self, dataframe:DataFrame):
|
||
close = dataframe['close'].to_list()
|
||
td = [0,0,0,0]
|
||
up = 0
|
||
down = 0
|
||
for i in range(4, len(close)):
|
||
if close[i] > close[i-4]:
|
||
up += 1
|
||
down = 0
|
||
td.append(up)
|
||
else:
|
||
down -= 1
|
||
up = 0
|
||
td.append(down)
|
||
return td
|
||
|
||
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||
|
||
macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9,)
|
||
dataframe["macd"] = macd["macd"]
|
||
dataframe["macdsignal"] = macd["macdsignal"]
|
||
dataframe["macdhist"] = macd["macdhist"]
|
||
dataframe['cci'] = ta.CCI(dataframe, 26)
|
||
dataframe['TD'] = self.TD(dataframe)
|
||
return dataframe
|
||
|
||
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||
# 入场:1d与4h CCI < -100,且4h CCI上升(当前 > 前一根)
|
||
dataframe.loc[
|
||
(
|
||
# (dataframe['macdhist'] < 0) &
|
||
# (dataframe['macdhist'] > dataframe['macdhist'].shift(1)) &
|
||
# (dataframe['macdsignal'] < 0) &
|
||
# (dataframe['macd'] < dataframe['macdsignal']) &
|
||
# (dataframe['cci'] < -100) &
|
||
# (dataframe['cci'].shift(1) < dataframe['cci']) &
|
||
# (dataframe['macdhist'] < 0) &
|
||
(dataframe['TD'] == 1) &
|
||
(dataframe['volume'] > 0)
|
||
),
|
||
'enter_long',
|
||
] = 1
|
||
|
||
return dataframe
|
||
|
||
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||
# 离场:1d与4h CCI > 100,且4h CCI下降(当前 < 前一根)
|
||
dataframe.loc[
|
||
(
|
||
# (dataframe['macdhist'] > 0) &
|
||
# (dataframe['macdhist'] < dataframe['macdhist'].shift(1)) &
|
||
# (dataframe['macdsignal'] > 0) &
|
||
# (dataframe['macd'] > dataframe['macdsignal']) &
|
||
# (dataframe['cci'] > 100 )&
|
||
# (dataframe['cci'].shift(1) > dataframe['cci']) &
|
||
# (dataframe['macdhist'] > 0) &
|
||
(dataframe['TD'] == -1) &
|
||
(dataframe['volume'] > 0)
|
||
),
|
||
'exit_long',
|
||
] = 1
|
||
|
||
return dataframe
|
||
|