在 GitHub 上查看

平衡力量策略

平衡力量策略通过比较收盘价与价格区间来衡量多空力量。当该值突破正阈值时,表示买方力量强劲。

当平衡力量上穿设定的 Threshold 时,策略做多;当指标跌破负阈值时平仓。

细节

  • 入场条件:
    • 平衡力量上穿 Threshold。
  • 多空方向:仅做多。
  • 离场条件:
    • 平衡力量跌破 -Threshold。
  • 止损:无。
  • 默认值:
    • Threshold = 0.8
  • 过滤器:
    • 类别:动量
    • 方向:多头
    • 指标:Balance of Power
    • 止损:无
    • 复杂度:低
    • 时间周期:任意
    • 季节性:否
    • 神经网络:否
    • 背离:否
    • 风险等级:中
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>
/// Balance of Power strategy.
/// Long only: buys when Balance of Power crosses above Threshold and closes the long when it crosses below -Threshold.
/// </summary>
public class BalanceOfPowerStrategy : Strategy
{
	private readonly StrategyParam<decimal> _threshold;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevBop;

	/// <summary>
	/// Balance of Power level whose upward cross opens a long.
	/// </summary>
	public decimal Threshold
	{
		get => _threshold.Value;
		set => _threshold.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public BalanceOfPowerStrategy()
	{
		_threshold = Param(nameof(Threshold), 0.8m)
			.SetNotNegative()
			.SetDisplay("Threshold", "Balance of Power level whose upward cross opens a long", "Indicators");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).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();
		_prevBop = null;
	}

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

		_prevBop = null;

		var bop = new BalanceOfPower();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(bop, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);

			var oscillators = CreateChartArea();
			if (oscillators != null)
				DrawIndicator(oscillators, bop);
		}
	}

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

		// A candle without range has no Balance of Power value.
		if (!bopValue.IsFormed || bopValue.IsEmpty)
			return;

		var bop = bopValue.GetValue<decimal>();
		var prev = _prevBop;
		_prevBop = bop;

		if (prev is not decimal prevBop)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (Position == 0 && prevBop <= Threshold && bop > Threshold)
			BuyMarket(Volume);
		else if (Position > 0 && prevBop >= -Threshold && bop < -Threshold)
			SellMarket(Position);
	}
}