在 GitHub 上查看

Custom Signal Oscillator 自定义信号振荡器

该策略使用两个价格信号的差值。当振荡器上穿零线时做多,下穿零线时做空。启用仅做多模式时,振荡器下穿零线会平仓。

详情

  • 入场条件: 振荡器穿越零线。
  • 多空方向: 双向或仅做多。
  • 退出条件: 反向信号或仅做多模式下的零线穿越。
  • 止损: 无。
  • 默认值:
    • LongOnly = false
    • CandleType = TimeSpan.FromMinutes(5)
  • 过滤器:
    • 类型: 振荡器
    • 方向: 双向
    • 指标: 无
    • 止损: 无
    • 复杂度: 基础
    • 时间框架: 日内 (5m)
    • 季节性: 无
    • 神经网络: 无
    • 背离: 无
    • 风险等级: 中
using System;
using System.Collections.Generic;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Custom Signal Oscillator strategy.
/// The oscillator is the difference between two price signals of the candle, its close and its open.
/// A cross above zero goes long and a cross below zero goes short, reversing an opposite position.
/// In long-only mode a cross below zero only closes the long.
/// </summary>
public class CustomSignalOscillatorStrategy : Strategy
{
	private readonly StrategyParam<bool> _longOnly;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevOscillator;

	/// <summary>
	/// Trade only long positions.
	/// </summary>
	public bool LongOnly
	{
		get => _longOnly.Value;
		set => _longOnly.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public CustomSignalOscillatorStrategy()
	{
		_longOnly = Param(nameof(LongOnly), false)
			.SetDisplay("Long Only", "Trade only long positions", "General");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

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

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

		_prevOscillator = null;

		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 oscillator = candle.ClosePrice - candle.OpenPrice;
		var prev = _prevOscillator;
		_prevOscillator = oscillator;

		if (prev is not decimal prevOscillator)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var crossUp = prevOscillator <= 0m && oscillator > 0m;
		var crossDown = prevOscillator >= 0m && oscillator < 0m;

		if (crossUp && Position <= 0)
		{
			BuyMarket(Volume + Math.Abs(Position));
		}
		else if (crossDown)
		{
			if (LongOnly)
			{
				if (Position > 0)
					SellMarket(Position);
			}
			else if (Position >= 0)
			{
				SellMarket(Volume + Math.Abs(Position));
			}
		}
	}
}