So Simple, why doesn't it work? A Bug?
So Simple, why doesn't it work? A Bug?
09 Feb 2016, 14:34
Hi,
With limit orders, I want to be able to move my target price, WITHOUT the stop loss or take profit moving (which cTrader does by default).
I've written a simple little bot called LockSLTP. When it's initialised, it saves the SL and TP, then when you move the Target price, every tick it checks if the SL or TP has moved, and if it has, it moves them back (if anyone knows a better event trigger, I'm all ears).
This sounds simple enough, but for some strange reason, it moves the SL and TP to very close to the original, but not quite exact, which means the modify keeps firing. Below is my code, I'd love it if someone could tell me what is going on?
protected override void OnStart()
{
var n = 0;
foreach (var order in PendingOrders)
{
if (order.SymbolCode == Symbol.Code)
{
dSL = order.StopLoss.Value;
dTP = order.TakeProfit.Value;
nOrderIndex = n;
}
n++;
}
// Put your initialization logic here
}
protected override void OnTick()
{
var dSLPips = 0.0;
var dTPPips = 0.0;
var str = "";
if ((PendingOrders[nOrderIndex].StopLoss.Value != dSL) || (PendingOrders[nOrderIndex].TakeProfit.Value != dTP))
{
//These 3 lines are Debug Code only
str = "TARGET = " + PendingOrders[nOrderIndex].TargetPrice + "\nOrder SL = " + PendingOrders[nOrderIndex].StopLoss.Value + "\ndSL = " + dSL + "\nOrder TP = " + PendingOrders[nOrderIndex].TakeProfit.Value + "\ndTP = " + dTP;
str = str + "\ndSLPips = " + dSLPips + "\ndTPPips = " + dTPPips + "\nTick Size = " + Symbol.TickSize;
ChartObjects.DrawText("Lock", str, StaticPosition.TopLeft, Colors.Turquoise);
dSLPips = Math.Abs(((PendingOrders[nOrderIndex].TargetPrice - dSL)) / (Symbol.TickSize * 10));
dTPPips = Math.Abs((PendingOrders[nOrderIndex].TargetPrice - dTP) / (Symbol.TickSize * 10));
ModifyPendingOrder(PendingOrders[nOrderIndex], PendingOrders[nOrderIndex].TargetPrice, dSLPips, dTPPips, PendingOrders[nOrderIndex].ExpirationTime);
}
}
cre8iveq
09 Feb 2016, 17:54
In case anyone else comes across it, I found the problem, and it is a bug in cAlgo. Turn out it rounds "stoplosspips" and "takeprofitpips" in the ModifyPendingOrder function. The arguments are doubles, but they are rounded anyway...
So for example, if you call ModifyPendingOrder with SL pips of 22.3, then after you execute, take a look at PendingOrders[OrderIndex].StopLossPips, and you will find that even though it's a double, the value will be 22.
@cre8iveq