Hello.
I am using a stratedy on AUDNZD on H1. I place limit orders and on each tick try to correct the TP if it not corresponding with calculated price.
TP - is a rounded EMA:
private static double round(double amount, int decimalPlaces) {
int tmp = 1;
for(int k=1;k <= decimalPlaces;k++)
tmp*=10;
double result = amount*tmp;
result = Math.round(result);
result = result/tmp;
return result;
}
TP = round(TPEMA, _MyInstrument.getPipScale()+1);
I try to correct TP in onTick(), when EMA recalculated:
delta=1;
for(int k=1;k<=MyInstrument.getPipScale();k++)
delta/=10;
spread=0.0005;
if (order.getState() == IOrder.State.FILLED)
{
if(order.isLong() == true)
{
bought = true;
if (Math.abs(order.getTakeProfitPrice() - TP) > delta);
{
order.setTakeProfitPrice(TP);
}
}
else
{
sold = true;
if (Math.abs(order.getTakeProfitPrice() - TP - spread) > delta)
{
order.setTakeProfitPrice(TP + spread);
}
}
}
Changing of TP for the sell orders is correct (first correcting is good and on next ticks correcting is ignored: difference between existing and calculated TP less then delta), but for the buy orders on each tick robot try to change current TP to a new TP even if they are equal. This correcting ends after order is closed (it can be after several bars H1), or robot crashed.
Help me, please: where can be the problem?