N Candles 策略
N Candles 策略复刻原始的 MQL 智能交易程序:当最近 N 根已完成的蜡烛线方向一致时入场。若窗口内所有蜡烛都收阳,则提交市价买单;若全部收阴,则提交市价卖单。策略本身不包含止盈、止损或离场逻辑,持仓需要人工或其他风控模块来处理。
概览
- 适用市场:更适合出现短期动量爆发的行情。
- 标的范围:外汇、期货、加密货币等连续交易的资产。
- 周期设置:可配置,默认使用 1 小时蜡烛。
- 下单方式:单纯的市价单,无保护性挂单。
工作流程
- 每当一根蜡烛收盘,策略检查最近的
N根蜡烛。 - 若窗口内全部收阳,则按设定手数买入。
- 若全部收阴,则按设定手数卖出。
- 若出现十字星(开盘价等于收盘价),计数会被清零,直到形成新的连续趋势。
- 策略不会主动平仓;在净额账户上,重复信号会不断加仓同一方向。
参数说明
- Consecutive Candles:触发交易所需的连续同向蜡烛数量。
- Volume:每次信号提交的市价单数量。
- Candle Type:用于判定序列的蜡烛类型或周期。
使用建议
- 由于缺乏退出逻辑,应与手动管理或额外的风险控制策略搭配使用。
- 对于波动性较大的市场,可以降低所需蜡烛数量或缩短周期以捕捉更快的走势。
- 持续的信号可能导致仓位迅速增加,请关注杠杆、保证金及账户风险限制。
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>
/// Trades in the direction of consecutive candles of the same color.
/// </summary>
public class NCandlesStrategy : Strategy
{
private readonly StrategyParam<int> _consecutiveCandles;
private readonly StrategyParam<DataType> _candleType;
private int _currentDirection;
private int _streakLength;
/// <summary>
/// Number of identical candles that must appear in a row to trigger an order.
/// </summary>
public int ConsecutiveCandles
{
get => _consecutiveCandles.Value;
set => _consecutiveCandles.Value = value;
}
/// <summary>
/// The type of candles used for analysis.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of the strategy.
/// </summary>
public NCandlesStrategy()
{
_consecutiveCandles = Param(nameof(ConsecutiveCandles), 4)
.SetGreaterThanZero()
.SetDisplay("Consecutive Candles", "Number of identical candles required", "General")
.SetOptimize(2, 6, 1);
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candles to analyze", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_currentDirection = 0;
_streakLength = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var direction = 0;
if (candle.ClosePrice > candle.OpenPrice)
{
direction = 1;
}
else if (candle.ClosePrice < candle.OpenPrice)
{
direction = -1;
}
else
{
// Doji candle breaks the streak just like in the original expert.
_currentDirection = 0;
_streakLength = 0;
return;
}
if (direction == _currentDirection)
{
_streakLength = Math.Min(_streakLength + 1, ConsecutiveCandles);
}
else
{
_currentDirection = direction;
_streakLength = 1;
}
if (_streakLength < ConsecutiveCandles)
return;
if (direction > 0 && Position <= 0)
{
BuyMarket();
}
else if (direction < 0 && Position >= 0)
{
SellMarket();
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Strategies import Strategy
class n_candles_strategy(Strategy):
def __init__(self):
super(n_candles_strategy, self).__init__()
self._consecutive_candles = self.Param("ConsecutiveCandles", 4)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1)))
self._current_direction = 0
self._streak_length = 0
@property
def ConsecutiveCandles(self):
return self._consecutive_candles.Value
@ConsecutiveCandles.setter
def ConsecutiveCandles(self, value):
self._consecutive_candles.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(n_candles_strategy, self).OnStarted2(time)
self._current_direction = 0
self._streak_length = 0
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self.ProcessCandle).Start()
def ProcessCandle(self, candle):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
open_price = float(candle.OpenPrice)
direction = 0
if close > open_price:
direction = 1
elif close < open_price:
direction = -1
else:
self._current_direction = 0
self._streak_length = 0
return
consecutive = int(self.ConsecutiveCandles)
if direction == self._current_direction:
self._streak_length = min(self._streak_length + 1, consecutive)
else:
self._current_direction = direction
self._streak_length = 1
if self._streak_length < consecutive:
return
if direction > 0 and self.Position <= 0:
self.BuyMarket()
elif direction < 0 and self.Position >= 0:
self.SellMarket()
def OnReseted(self):
super(n_candles_strategy, self).OnReseted()
self._current_direction = 0
self._streak_length = 0
def CreateClone(self):
return n_candles_strategy()