在 GitHub 上查看

Loco 策略

该策略实现了最初在 MQL5 中编写的 “Loco” 指标。指标分析 K 线价格并分配颜色(绿色或品红)。颜色变化表示趋势反转。

逻辑

  • 指标使用可配置价格(默认收盘价)和回溯长度计算序列。
  • 当颜色从品红变为绿色时,策略平掉空头并开多。
  • 当颜色从绿色变为品红时,策略平掉多头并开空。

参数

  • Candle Type – 策略所用的 K 线类型。
  • Length – 用于比较价格的柱数。
  • Price Type – 指标计算使用的价格类型。

说明

策略使用 Loco 指标的自定义实现。暂未提供 Python 版本。

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 the Loco indicator (price direction detector).
/// </summary>
public class LocoStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _length;

	private readonly Queue<decimal> _prices = new();
	private decimal? _prev;
	private int _prevColor = -1;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int Length { get => _length.Value; set => _length.Value = value; }

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

		_length = Param(nameof(Length), 1)
			.SetDisplay("Length", "Lookback length", "Indicator")
			.SetOptimize(1, 10, 1);
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prices.Clear();
		_prev = null;
		_prevColor = -1;
	}

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

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(ProcessCandle).Start();
	}

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

		var price = candle.ClosePrice;
		_prices.Enqueue(price);

		if (_prices.Count <= Length)
		{
			_prev = price;
			return;
		}

		var series1 = _prices.Dequeue();
		var prev = _prev ?? price;
		decimal result;
		int color;

		if (price == prev)
		{
			result = prev;
			color = 0;
		}
		else if (series1 > prev && price > prev)
		{
			result = Math.Max(prev, price * 0.999m);
			color = 0;
		}
		else if (series1 < prev && price < prev)
		{
			result = Math.Min(prev, price * 1.001m);
			color = 1;
		}
		else
		{
			if (price > prev)
			{
				result = price * 0.999m;
				color = 0;
			}
			else
			{
				result = price * 1.001m;
				color = 1;
			}
		}

		_prev = result;

		if (_prevColor == -1)
		{
			_prevColor = color;
			return;
		}

		if (color != _prevColor)
		{
			if (color == 1)
			{
				if (Position < 0)
					BuyMarket();
				if (Position <= 0)
					BuyMarket();
			}
			else
			{
				if (Position > 0)
					SellMarket();
				if (Position >= 0)
					SellMarket();
			}
		}

		_prevColor = color;
	}
}