宽松版斐波那契策略 - 增加交易机会
Timeframe
15m
Direction
Long Only
Stoploss
-6.0%
Trailing Stop
Yes
ROI
0m: 5.0%, 30m: 3.0%, 60m: 2.0%, 120m: 1.0%
Interface Version
3
Startup Candles
N/A
Indicators
3
freqtrade/freqtrade-strategies
Strategy 003 author@: Gerald Lonlas github@: https://github.com/freqtrade/freqtrade-strategies
# 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 Dict, Optional, Union, Tuple
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,
AnnotationType,
)
# --------------------------------
# Add your lib to import here
import talib.abstract as ta
from technical import qtpylib
class RelaxedFibonacciStrategy(IStrategy):
"""
宽松版斐波那契策略 - 增加交易机会
策略特点:
1. 大幅放宽交易条件
2. 增加斐波那契容忍度
3. 简化确认条件
4. 提高交易频率
"""
# Strategy interface version
INTERFACE_VERSION = 3
# 策略时间框架 - 15分钟
timeframe = "15m"
# 是否支持做空
can_short: bool = False
# 最小ROI设置
minimal_roi = {
"120": 0.01, # 2小时后1%收益
"60": 0.02, # 1小时后2%收益
"30": 0.03, # 30分钟后3%收益
"0": 0.05 # 立即5%收益
}
# 止损设置
stoploss = -0.06 # 6%止损
# 追踪止损
trailing_stop = True
trailing_stop_positive = 0.02
trailing_stop_positive_offset = 0.03
trailing_only_offset_is_reached = True
# 只处理新K线
process_only_new_candles = True
# 策略参数
use_exit_signal = True
exit_profit_only = False
ignore_roi_if_entry_signal = False
# 策略启动所需的K线数量
startup_candle_count: int = 50
# 宽松的参数设置
fib_period = IntParameter(20, 80, default=30, space="buy")
rsi_period = IntParameter(10, 20, default=14, space="buy")
rsi_oversold = IntParameter(35, 55, default=45, space="buy")
rsi_overbought = IntParameter(45, 65, default=55, space="sell")
fib_tolerance = DecimalParameter(0.05, 0.20, default=0.10, space="buy")
# 订单类型
order_types = {
"entry": "limit",
"exit": "limit",
"stoploss": "market",
"stoploss_on_exchange": False
}
# 订单时间
order_time_in_force = {
"entry": "GTC",
"exit": "GTC"
}
@property
def plot_config(self):
return {
"main_plot": {
"fib_0.236": {"color": "red", "type": "line"},
"fib_0.382": {"color": "orange", "type": "line"},
"fib_0.5": {"color": "yellow", "type": "line"},
"fib_0.618": {"color": "green", "type": "line"},
"fib_0.786": {"color": "blue", "type": "line"},
},
"subplots": {
"RSI": {
"rsi": {"color": "purple"},
}
}
}
def informative_pairs(self):
return []
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
计算技术指标
"""
# 计算RSI
dataframe["rsi"] = ta.RSI(dataframe, timeperiod=self.rsi_period.value)
# 计算斐波那契回归水平
period = self.fib_period.value
dataframe["highest"] = dataframe["high"].rolling(window=period).max()
dataframe["lowest"] = dataframe["low"].rolling(window=period).min()
dataframe["price_range"] = dataframe["highest"] - dataframe["lowest"]
# 计算斐波那契回归水平
dataframe["fib_0.236"] = dataframe["highest"] - 0.236 * dataframe["price_range"]
dataframe["fib_0.382"] = dataframe["highest"] - 0.382 * dataframe["price_range"]
dataframe["fib_0.5"] = dataframe["highest"] - 0.5 * dataframe["price_range"]
dataframe["fib_0.618"] = dataframe["highest"] - 0.618 * dataframe["price_range"]
dataframe["fib_0.786"] = dataframe["highest"] - 0.786 * dataframe["price_range"]
# 计算EMA
dataframe["ema_20"] = ta.EMA(dataframe, timeperiod=20)
# 计算MACD
macd = ta.MACD(dataframe)
dataframe["macd"] = macd["macd"]
dataframe["macdsignal"] = macd["macdsignal"]
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
宽松的买入信号
"""
tolerance = self.fib_tolerance.value
# 基础条件
base_conditions = (
(dataframe["volume"] > 0) &
(dataframe["price_range"] > 0) &
(dataframe["highest"].notna()) &
(dataframe["lowest"].notna())
)
# 斐波那契条件(大幅放宽)
fib_conditions = (
# 价格接近任何斐波那契支撑位
(
((dataframe["close"] <= dataframe["fib_0.618"] * (1 + tolerance)) &
(dataframe["close"] >= dataframe["fib_0.618"] * (1 - tolerance))) |
((dataframe["close"] <= dataframe["fib_0.786"] * (1 + tolerance)) &
(dataframe["close"] >= dataframe["fib_0.786"] * (1 - tolerance))) |
((dataframe["close"] <= dataframe["fib_0.5"] * (1 + tolerance)) &
(dataframe["close"] >= dataframe["fib_0.5"] * (1 - tolerance)))
)
)
# RSI条件(放宽)
rsi_conditions = (
(dataframe["rsi"] <= self.rsi_oversold.value)
)
# 趋势条件(简化)
trend_conditions = (
(dataframe["close"] > dataframe["ema_20"])
)
# 综合买入条件
dataframe.loc[
base_conditions &
fib_conditions &
rsi_conditions &
trend_conditions,
"enter_long"
] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
"""
宽松的卖出信号
"""
tolerance = self.fib_tolerance.value
# 基础条件
base_conditions = (
(dataframe["volume"] > 0) &
(dataframe["price_range"] > 0) &
(dataframe["highest"].notna()) &
(dataframe["lowest"].notna())
)
# 斐波那契条件(大幅放宽)
fib_conditions = (
# 价格接近任何斐波那契阻力位
(
((dataframe["close"] >= dataframe["fib_0.382"] * (1 - tolerance)) &
(dataframe["close"] <= dataframe["fib_0.382"] * (1 + tolerance))) |
((dataframe["close"] >= dataframe["fib_0.236"] * (1 - tolerance)) &
(dataframe["close"] <= dataframe["fib_0.236"] * (1 + tolerance))) |
((dataframe["close"] >= dataframe["fib_0.5"] * (1 - tolerance)) &
(dataframe["close"] <= dataframe["fib_0.5"] * (1 + tolerance)))
)
)
# RSI条件(放宽)
rsi_conditions = (
(dataframe["rsi"] >= self.rsi_overbought.value)
)
# 综合卖出条件
dataframe.loc[
base_conditions &
fib_conditions &
rsi_conditions,
"exit_long"
] = 1
return dataframe