GitHub で見る

Nifty Options Trendy Markets with TSL Strategy

Breakout strategy using Bollinger Bands with ADX and Supertrend filters. Entries require a volume spike. Positions close on MACD crossovers, ADX weakening or an ATR based trailing stop.

Details

  • Entry Criteria:
    • Long: price crosses above upper Bollinger Band && ADX > threshold && volume spike && price above Supertrend
    • Short: price crosses below lower Bollinger Band && ADX > threshold && volume spike && price below Supertrend
  • Long/Short: Both
  • Exit Criteria: MACD cross, ADX drop or ATR trailing stop
  • Stops: ATR trailing stop
  • Default Values:
    • BollingerPeriod = 20
    • BollingerMultiplier = 2m
    • AdxLength = 14
    • AdxEntryThreshold = 25m
    • AdxExitThreshold = 20m
    • SuperTrendLength = 10
    • SuperTrendMultiplier = 3m
    • MacdFast = 12
    • MacdSlow = 26
    • MacdSignal = 9
    • AtrLength = 14
    • AtrMultiplier = 1.5m
    • VolumeSpikeMultiplier = 1.5m
    • CandleType = TimeSpan.FromMinutes(15).TimeFrame()
  • Filters:
    • Category: Trend
    • Direction: Both
    • Indicators: Bollinger Bands, ADX, Supertrend, MACD, ATR
    • Stops: Yes
    • Complexity: Intermediate
    • Timeframe: Mid-term
    • Seasonality: No
    • Neural Networks: No
    • Divergence: No
    • Risk Level: Medium
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class NiftyOptionsTrendyMarketsWithTslStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public NiftyOptionsTrendyMarketsWithTslStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		var fast = new ExponentialMovingAverage { Length = 14 };
		var slow = new ExponentialMovingAverage { Length = 40 };
		var rsi = new RelativeStrengthIndex { Length = 14 };
		var prevF = 0m; var prevS = 0m; var init = false;
		var lastSignal = DateTimeOffset.MinValue;
		var cooldown = TimeSpan.FromMinutes(120);
		var sub = SubscribeCandles(CandleType);
		sub.Bind(fast, slow, rsi, (c, f, s, r) =>
		{
			if (c.State != CandleStates.Finished || !fast.IsFormed || !slow.IsFormed || !rsi.IsFormed) return;
			if (!init) { prevF = f; prevS = s; init = true; return; }
			if (c.OpenTime - lastSignal >= cooldown)
			{
				if (prevF <= prevS && f > s && r > 50 && Position <= 0) { BuyMarket(); lastSignal = c.OpenTime; }
				else if (prevF >= prevS && f < s && r < 50 && Position > 0) { SellMarket(); lastSignal = c.OpenTime; }
			}
			prevF = f; prevS = s;
		}).Start();
		var area = CreateChartArea();
		if (area != null) { DrawCandles(area, sub); DrawIndicator(area, fast); DrawIndicator(area, slow); DrawOwnTrades(area); }
	}
}