Good morning fellow coders!
I have created a few cbots using a trailing stop function. Originally I used the ClickAlgo code which relied on the ModifyPosition function.
However, I was getting error messages back along the lines of FAILED with error "InvalidStopLossTakeProfit"
Thanks to this forum I then upgraded my code to the new code below. However, my problem is now that my broker is saying my bots are using too may server commands. When i look at my log I see its modyfing the TSL function many times.
So, my question is as follows: Is it possible to update the open order to a TSL and then leave that order alone and not constantly modify the SL. I have set the ModifyPosition to "newStopLossPrice, p.TakeProfit,true" in a bid to let the server manage the TSL rather than contently sending a modify request but naturally the bot wants to keep changing.
Does anyone know how I can update the code below to update once to TSL and then leave it alone? 😀
Many thanks all..
Chris
public void SetTrailingStop(Position p)
{
double newStopLossPrice = 0;
try
{
if (p.TradeType == TradeType.Sell)
{
double distance = p.EntryPrice - Symbol.Ask;
if (distance < TrailingStopTrigger * Symbol.PipSize)
return;
newStopLossPrice = Symbol.Ask + TrailingStopStep * Symbol.PipSize;
if (p.StopLoss == null || newStopLossPrice < p.StopLoss)
{
if (p.TakeProfit == null)
{
ModifyPosition(p, newStopLossPrice, null);
}
else
{
ModifyPosition(p, newStopLossPrice, p.TakeProfit,true);
}
}
}
if (p.TradeType == TradeType.Buy)
{
double distance = Symbol.Bid - p.EntryPrice;
if (distance < TrailingStopTrigger * Symbol.PipSize)
return;
newStopLossPrice = Symbol.Bid - TrailingStopStep * Symbol.PipSize;
if (p.StopLoss == null || newStopLossPrice > p.StopLoss)
{
if (p.TakeProfit == null)
{
ModifyPosition(p, newStopLossPrice, null);
}
else
{
ModifyPosition(p, newStopLossPrice, p.TakeProfit,true);
}
}
}
} catch (Exception ex)
{
Print("SetTrailingStop: " + ex.Message);
Print("Entry price: " + p.EntryPrice.ToString());
Print("New stop loss price: " + newStopLossPrice.ToString());
}
}
}
}