Fibo Candles 趋势策略
该策略采用自定义的 Fibo Candles 技术来判断趋势方向。 指标根据斐波那契比例比较当前收盘价与最近的最高/最低区间,将每根K线着色。 颜色转换被视为潜在反转信号:当颜色变成看涨时,策略平掉空头并开多; 当颜色变成看跌时,平掉多头并开空。
通过可调的回溯期和斐波那契级别,方法能够适应市场波动。 每笔交易都通过固定点数的止损和止盈进行保护。
详情
- 入场条件:
- 做多:当前K线颜色从看跌变为看涨。
- 做空:当前K线颜色从看涨变为看跌。
- 方向:双向。
- 离场条件:
- 当出现相反颜色时关闭持仓。
- 止损/止盈:通过
StartProtection设置固定点数的止损和止盈。 - 默认参数:
Period= 10(用于计算高低区间的K线数量)。Fibo Level= 0.236(用于判断趋势的比例)。Stop Loss= 1000 点。Take Profit= 2000 点。
- 过滤器:
- 分类:趋势跟随
- 方向:双向
- 指标:Highest, Lowest
- 止损:是
- 复杂度:中等
- 时间框架:默认1小时
- 季节性:否
- 神经网络:否
- 背离:否
- 风险级别:中等
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Strategy based on color changes of the Fibo Candles indicator.
/// The indicator colors candles according to Fibonacci ratios and trend direction.
/// A change from bearish to bullish color triggers a long entry and closes short positions.
/// A change from bullish to bearish color triggers a short entry and closes long positions.
/// </summary>
public class FiboCandlesTrendStrategy : Strategy
{
public enum FiboLevels
{
Level1 = 1,
Level2,
Level3,
Level4,
Level5
}
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _period;
private readonly StrategyParam<FiboLevels> _fiboLevel;
private readonly StrategyParam<int> _stopLoss;
private readonly StrategyParam<int> _takeProfit;
private Highest _highest;
private Lowest _lowest;
private int _trend;
private int? _previousColor;
private decimal _levelMultiplier;
/// <summary>
/// Type and timeframe of candles used by the strategy.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Lookback period for high and low calculations.
/// </summary>
public int Period
{
get => _period.Value;
set => _period.Value = value;
}
/// <summary>
/// Fibonacci level used by the indicator logic.
/// </summary>
public FiboLevels Level
{
get => _fiboLevel.Value;
set => _fiboLevel.Value = value;
}
/// <summary>
/// Stop loss in points.
/// </summary>
public int StopLoss
{
get => _stopLoss.Value;
set => _stopLoss.Value = value;
}
/// <summary>
/// Take profit in points.
/// </summary>
public int TakeProfit
{
get => _takeProfit.Value;
set => _takeProfit.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="FiboCandlesTrendStrategy"/>.
/// </summary>
public FiboCandlesTrendStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Type and timeframe of candles", "General");
_period = Param(nameof(Period), 10)
.SetGreaterThanZero()
.SetDisplay("Period", "Lookback period for high/low", "FiboCandles")
.SetOptimize(5, 30, 5);
_fiboLevel = Param(nameof(Level), FiboLevels.Level1)
.SetDisplay("Fibo Level", "Fibonacci ratio level", "FiboCandles");
_stopLoss = Param(nameof(StopLoss), 1000)
.SetGreaterThanZero()
.SetDisplay("Stop Loss", "Stop loss in points", "Risk")
.SetOptimize(500, 2000, 500);
_takeProfit = Param(nameof(TakeProfit), 2000)
.SetGreaterThanZero()
.SetDisplay("Take Profit", "Take profit in points", "Risk")
.SetOptimize(1000, 4000, 500);
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_highest = null;
_lowest = null;
_trend = 0;
_previousColor = null;
_levelMultiplier = 0m;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_highest = new Highest { Length = Period };
_lowest = new Lowest { Length = Period };
_levelMultiplier = GetLevelMultiplier(Level);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_highest, _lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal highest, decimal lowest)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (!_highest.IsFormed || !_lowest.IsFormed)
return;
var range = highest - lowest;
var trend = _trend;
var open = candle.OpenPrice;
var close = candle.ClosePrice;
if (open > close)
{
if (!(trend < 0 && range * _levelMultiplier < close - lowest))
trend = 1;
else
trend = -1;
}
else
{
if (!(trend > 0 && range * _levelMultiplier < highest - close))
trend = -1;
else
trend = 1;
}
var color = trend == 1 ? 1 : 0;
if (_previousColor.HasValue)
{
if (color == 1 && _previousColor.Value == 0)
{
BuyMarket();
}
else if (color == 0 && _previousColor.Value == 1)
{
SellMarket();
}
}
_previousColor = color;
_trend = trend;
}
private static decimal GetLevelMultiplier(FiboLevels level)
{
return level switch
{
FiboLevels.Level1 => 0.236m,
FiboLevels.Level2 => 0.382m,
FiboLevels.Level3 => 0.500m,
FiboLevels.Level4 => 0.618m,
FiboLevels.Level5 => 0.762m,
_ => 0.236m
};
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class fibo_candles_trend_strategy(Strategy):
def __init__(self):
super(fibo_candles_trend_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Type and timeframe of candles", "General")
self._period = self.Param("Period", 10) \
.SetDisplay("Period", "Lookback period for high/low", "FiboCandles")
self._fibo_level = self.Param("Level", 1) \
.SetDisplay("Fibo Level", "Fibonacci ratio level (1-5)", "FiboCandles")
self._stop_loss = self.Param("StopLoss", 1000) \
.SetDisplay("Stop Loss", "Stop loss in points", "Risk")
self._take_profit = self.Param("TakeProfit", 2000) \
.SetDisplay("Take Profit", "Take profit in points", "Risk")
self._highest = None
self._lowest = None
self._trend = 0
self._previous_color = None
self._level_multiplier = 0.0
@property
def candle_type(self):
return self._candle_type.Value
@property
def period(self):
return self._period.Value
@property
def fibo_level(self):
return self._fibo_level.Value
@property
def stop_loss(self):
return self._stop_loss.Value
@property
def take_profit(self):
return self._take_profit.Value
def _get_level_multiplier(self, level):
levels = {1: 0.236, 2: 0.382, 3: 0.500, 4: 0.618, 5: 0.762}
return levels.get(level, 0.236)
def OnReseted(self):
super(fibo_candles_trend_strategy, self).OnReseted()
self._highest = None
self._lowest = None
self._trend = 0
self._previous_color = None
self._level_multiplier = 0.0
def OnStarted2(self, time):
super(fibo_candles_trend_strategy, self).OnStarted2(time)
self._highest = Highest()
self._highest.Length = self.period
self._lowest = Lowest()
self._lowest.Length = self.period
self._level_multiplier = self._get_level_multiplier(self.fibo_level)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._highest, self._lowest, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawOwnTrades(area)
def process_candle(self, candle, highest_val, lowest_val):
if candle.State != CandleStates.Finished:
return
highest_val = float(highest_val)
lowest_val = float(lowest_val)
if not self._highest.IsFormed or not self._lowest.IsFormed:
return
rng = highest_val - lowest_val
trend = self._trend
o = float(candle.OpenPrice)
c = float(candle.ClosePrice)
if o > c:
if not (trend < 0 and rng * self._level_multiplier < c - lowest_val):
trend = 1
else:
trend = -1
else:
if not (trend > 0 and rng * self._level_multiplier < highest_val - c):
trend = -1
else:
trend = 1
color = 1 if trend == 1 else 0
if self._previous_color is not None:
if color == 1 and self._previous_color == 0:
self.BuyMarket()
elif color == 0 and self._previous_color == 1:
self.SellMarket()
self._previous_color = color
self._trend = trend
def CreateClone(self):
return fibo_candles_trend_strategy()