Trailing Stop
Created at 10 Aug 2020, 22:08
SA
Trailing Stop
10 Aug 2020, 22:08
My code was working perfectly until I added a trailing stop and now nothing else works. Could someone tell me how I can solve this problem????
Thankss
using System; using System.Linq; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; using cAlgo.Indicators; namespace cAlgo.Robots { [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class bot : Robot { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } [Parameter("Source")] public DataSeries Source { get; set; } [Parameter("Periods", DefaultValue = 14)] public int Periods { get; set; } [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)] public int StopLoss { get; set; } [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)] public int Volume { get; set; } [Parameter("Take_Profit", DefaultValue = 45, MinValue = 10)] public int TakeProfit { get; set; } [Parameter("Begin Trading Hour", DefaultValue = 5.0)] public double Begin { get; set; } [Parameter("Ending Trading Hour", DefaultValue = 19.0)] public double Ending { get; set; } [Parameter("Trigger When Gaining", DefaultValue = 50)] public double TriggerWhenGaining { get; set; } [Parameter("Trailing Stop Loss Distance", DefaultValue = 1)] public double TrailingStopLossDistance { get; set; } [Parameter("Step (pips)", DefaultValue = 10)] public double Step { get; set; } [Parameter("Buy")] public bool Buy { get; set; } private double _highestGain; private bool _isTrailing; private DateTime startTime; private DateTime endTime; public RelativeStrengthIndex _rsi; private ExponentialMovingAverage _Ema1; protected override void OnStart() { } protected override void OnTick() { { startTime = Server.Time.Date.AddHours(Begin); endTime = Server.Time.Date.AddHours(Ending); if (Trade.IsExecuting) return; bool tradeTime = false; if (Begin < Ending) tradeTime = Server.Time.Hour >= Begin && Server.Time.Hour < Ending; if (Ending < Begin) tradeTime = Server.Time.Hour >= Begin || Server.Time.Hour <= Ending; if (!tradeTime) return; } _rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 48); _Ema1 = Indicators.ExponentialMovingAverage(Bars.ClosePrices, 6); { if (_rsi.Result.LastValue < 45) if (_Ema1.Result.LastValue == Symbol.Ask) ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "bot", StopLoss, TakeProfit); } { if (_rsi.Result.LastValue > 55) if (_Ema1.Result.LastValue == Symbol.Bid) ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "bot", StopLoss, TakeProfit); var position = Positions.Find("SampleTrailing"); if (position == null) { Stop(); return; } //If the trigger is reached, the robot starts trailing if (position.Pips >= TriggerWhenGaining) { //Based on the position's direction, we calculate the new stop loss price and we modify the position if (position.TradeType == TradeType.Buy) { var newSLprice = Symbol.Ask - (Symbol.PipSize * TrailingStopLossDistance); if (newSLprice > position.StopLoss) { ModifyPosition(position, newSLprice, null); } } else { var newSLprice = Symbol.Bid + (Symbol.PipSize * TrailingStopLossDistance); if (newSLprice < position.StopLoss) { ModifyPosition(position, newSLprice, null); } } TriggerWhenGaining += Step; } } } protected override void OnStop() { // Put your deinitialization logic here } } }
firemyst
13 Aug 2020, 09:20
RE: my code worked perfectly until ...
samuel.jus.cornelio said:
Whether or not this is the issue, your code will fail and not move the SL if the Symbol.Spread is greater than the Trailing stop distance specified.
For example, with your parameters I see you have a default value of "1" for trailing stop loss distance.
If the spread is 1.1 pips of more, how can you trail a stop loss by 1 pip from the Ask price? You'd be inside the spread, and thus be knocked out of your position (if it even lets you set that).
So in your code you also need to check that
is also > Symbol.Spread distance between the Ask and Bid prices.
Basically, put a check in there that it's also lower than the bid price.
Example:
Similarly:
@firemyst