|
| 1 | +namespace StockSharp.Samples.Strategies; |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +using StockSharp.Algo; |
| 7 | +using StockSharp.Algo.Indicators; |
| 8 | +using StockSharp.Algo.Strategies; |
| 9 | +using StockSharp.BusinessEntities; |
| 10 | +using StockSharp.Messages; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// Directional Movement Index crossover strategy. |
| 14 | +/// </summary> |
| 15 | +public class AdxDmiStrategy : Strategy |
| 16 | +{ |
| 17 | + private readonly StrategyParam<DataType> _candleTypeParam; |
| 18 | + private readonly StrategyParam<int> _dmiPeriod; |
| 19 | + private readonly StrategyParam<bool> _allowLong; |
| 20 | + private readonly StrategyParam<bool> _allowShort; |
| 21 | + private readonly StrategyParam<bool> _closeLong; |
| 22 | + private readonly StrategyParam<bool> _closeShort; |
| 23 | + |
| 24 | + private DirectionalIndex _dmi = null!; |
| 25 | + private decimal? _prevPlus; |
| 26 | + private decimal? _prevMinus; |
| 27 | + |
| 28 | + public AdxDmiStrategy() |
| 29 | + { |
| 30 | + _candleTypeParam = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame()) |
| 31 | + .SetDisplay("Candle Type", "Time frame for strategy calculation", "General"); |
| 32 | + |
| 33 | + _dmiPeriod = Param(nameof(DmiPeriod), 14) |
| 34 | + .SetDisplay("DMI Period", "Directional Movement Index period", "Indicators") |
| 35 | + .SetGreaterThanZero(); |
| 36 | + |
| 37 | + _allowLong = Param(nameof(AllowLong), true) |
| 38 | + .SetDisplay("Allow Long", "Enable long entries", "Trading"); |
| 39 | + |
| 40 | + _allowShort = Param(nameof(AllowShort), true) |
| 41 | + .SetDisplay("Allow Short", "Enable short entries", "Trading"); |
| 42 | + |
| 43 | + _closeLong = Param(nameof(CloseLong), true) |
| 44 | + .SetDisplay("Close Long", "Close long positions on opposite signal", "Trading"); |
| 45 | + |
| 46 | + _closeShort = Param(nameof(CloseShort), true) |
| 47 | + .SetDisplay("Close Short", "Close short positions on opposite signal", "Trading"); |
| 48 | + } |
| 49 | + |
| 50 | + public DataType CandleType |
| 51 | + { |
| 52 | + get => _candleTypeParam.Value; |
| 53 | + set => _candleTypeParam.Value = value; |
| 54 | + } |
| 55 | + |
| 56 | + public int DmiPeriod |
| 57 | + { |
| 58 | + get => _dmiPeriod.Value; |
| 59 | + set => _dmiPeriod.Value = value; |
| 60 | + } |
| 61 | + |
| 62 | + public bool AllowLong |
| 63 | + { |
| 64 | + get => _allowLong.Value; |
| 65 | + set => _allowLong.Value = value; |
| 66 | + } |
| 67 | + |
| 68 | + public bool AllowShort |
| 69 | + { |
| 70 | + get => _allowShort.Value; |
| 71 | + set => _allowShort.Value = value; |
| 72 | + } |
| 73 | + |
| 74 | + public bool CloseLong |
| 75 | + { |
| 76 | + get => _closeLong.Value; |
| 77 | + set => _closeLong.Value = value; |
| 78 | + } |
| 79 | + |
| 80 | + public bool CloseShort |
| 81 | + { |
| 82 | + get => _closeShort.Value; |
| 83 | + set => _closeShort.Value = value; |
| 84 | + } |
| 85 | + |
| 86 | + /// <inheritdoc /> |
| 87 | + public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() |
| 88 | + => [(Security, CandleType)]; |
| 89 | + |
| 90 | + /// <inheritdoc /> |
| 91 | + protected override void OnStarted(DateTimeOffset time) |
| 92 | + { |
| 93 | + base.OnStarted(time); |
| 94 | + |
| 95 | + _dmi = new DirectionalIndex |
| 96 | + { |
| 97 | + Length = DmiPeriod |
| 98 | + }; |
| 99 | + |
| 100 | + var subscription = SubscribeCandles(CandleType); |
| 101 | + subscription |
| 102 | + .BindEx(_dmi, ProcessCandle) |
| 103 | + .Start(); |
| 104 | + |
| 105 | + var area = CreateChartArea(); |
| 106 | + if (area != null) |
| 107 | + { |
| 108 | + DrawCandles(area, subscription); |
| 109 | + DrawIndicator(area, _dmi); |
| 110 | + DrawOwnTrades(area); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private void ProcessCandle(ICandleMessage candle, IIndicatorValue dmiValue) |
| 115 | + { |
| 116 | + if (candle.State != CandleStates.Finished) |
| 117 | + return; |
| 118 | + |
| 119 | + var dmi = (DirectionalIndexValue)dmiValue; |
| 120 | + |
| 121 | + if (dmi.Plus is not decimal currentPlus || |
| 122 | + dmi.Minus is not decimal currentMinus) |
| 123 | + return; |
| 124 | + |
| 125 | + if (_prevPlus is null || _prevMinus is null) |
| 126 | + { |
| 127 | + _prevPlus = currentPlus; |
| 128 | + _prevMinus = currentMinus; |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + var buySignal = _prevMinus > _prevPlus && currentMinus <= currentPlus; |
| 133 | + var sellSignal = _prevPlus > _prevMinus && currentPlus <= currentMinus; |
| 134 | + |
| 135 | + if (buySignal) |
| 136 | + { |
| 137 | + if (CloseShort && Position < 0) |
| 138 | + BuyMarket(Math.Abs(Position)); |
| 139 | + |
| 140 | + if (AllowLong && Position <= 0) |
| 141 | + BuyMarket(Volume); |
| 142 | + } |
| 143 | + |
| 144 | + if (sellSignal) |
| 145 | + { |
| 146 | + if (CloseLong && Position > 0) |
| 147 | + SellMarket(Position); |
| 148 | + |
| 149 | + if (AllowShort && Position >= 0) |
| 150 | + SellMarket(Volume); |
| 151 | + } |
| 152 | + |
| 153 | + _prevPlus = currentPlus; |
| 154 | + _prevMinus = currentMinus; |
| 155 | + } |
| 156 | +} |
0 commit comments