在 GitHub 上查看

Jpalonso Modoki 策略

Jpalonso Modoki 策略使用简单移动平均线和百分比包络线构建价格通道。 当价格触及下轨或位于通道上半部分时做多;相反条件下做空。固定的止盈和止损保护仓位。

细节

  • 入场条件: 价格低于下轨或在中线与上轨之间做多;价格高于上轨或在中线与下轨之间做空。
  • 多空: 双向。
  • 出场条件: 反向信号或止损止盈。
  • 止损: 有,止盈和止损均以点数表示。
  • 默认值:
    • CandleType = 1 分钟
    • SmaPeriod = 200
    • Deviation = 0.35%
    • TakeProfit = 127 点
    • StopLoss = 77 点
  • 过滤器:
    • 分类: 通道
    • 方向: 双向
    • 指标: SMA, Envelopes
    • 止损: 有
    • 复杂度: 基础
    • 时间框架: 日内
    • 季节性: 否
    • 神经网络: 否
    • 背离: 否
    • 风险等级: 中
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Strategy based on price position relative to SMA envelopes.
/// Buys when price is below the lower envelope, sells when above upper.
/// </summary>
public class JpalonsoModokiStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _smaPeriod;
	private readonly StrategyParam<decimal> _deviation;
	private readonly StrategyParam<Unit> _takeProfit;
	private readonly StrategyParam<Unit> _stopLoss;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int SmaPeriod { get => _smaPeriod.Value; set => _smaPeriod.Value = value; }
	public decimal Deviation { get => _deviation.Value; set => _deviation.Value = value; }
	public Unit TakeProfit { get => _takeProfit.Value; set => _takeProfit.Value = value; }
	public Unit StopLoss { get => _stopLoss.Value; set => _stopLoss.Value = value; }

	public JpalonsoModokiStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");

		_smaPeriod = Param(nameof(SmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("SMA Period", "Length of the moving average", "Envelopes");

		_deviation = Param(nameof(Deviation), 0.35m)
			.SetDisplay("Deviation %", "Envelope deviation from SMA in percent", "Envelopes");

		_takeProfit = Param(nameof(TakeProfit), new Unit(3000, UnitTypes.Absolute))
			.SetDisplay("Take Profit", "Take profit in points", "Risk Management");

		_stopLoss = Param(nameof(StopLoss), new Unit(5000, UnitTypes.Absolute))
			.SetDisplay("Stop Loss", "Stop loss in points", "Risk Management");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		StartProtection(takeProfit: TakeProfit, stopLoss: StopLoss);

		var sma = new SimpleMovingAverage { Length = SmaPeriod };

		SubscribeCandles(CandleType)
			.Bind(sma, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal ma)
	{
		if (candle.State != CandleStates.Finished)
			return;

		var upper = ma * (1 + Deviation / 100m);
		var lower = ma * (1 - Deviation / 100m);
		var close = candle.ClosePrice;

		var buy = close <= lower;
		var sell = close >= upper;

		if (buy && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (sell && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
	}
}