prev_trade_dir = curr_trade_dir;
curr_trade_dir = "";these should be placed after 'myCanTrade = false'
within that last curly brace.
you only want to make these changes if you make a new trade otherwise there are potentially instances when prev_trade_dir = curr_trade_dir = "", wiping out any sense of direction.
Curiosities: if the old trade is stopped out, based on the code presented, how does myCanTrade get set to true? What about partial fills, do those create multiple filled orders?
Q. If the old trade is stopped out, do you still care about its direction?
i would decouple 'myCanTrade' from the action of closing old trades.
the initial for loop and if statements are dangerous as if there are multiple orders in that order list with one
not in the 'FILLED' state following one that is, you will set myCanTrade to false via the 'else' and prevent a trade just because of the ordering/state of orders in the list. You might consider the following example which doesn't require you to completely overhaul your logic:
_barrier = 0; // count filled trades we find and kill
//kill any and all trades in the filled position
for (IOrder my_order : myEngine.getOrders(myInstrument))
{
if(my_order != null && my_order.getState() == IOrder.State.FILLED)
{
_barrier++; //we found one - doesn't matter position in list
if(!curr_trade_dir.equals(prev_trade_dir)) //since we found one only kill it based on your rules
{
my_order.close();
my_order.waitForUpdate(IOrder.State.CLOSED); //wait till the order is closed
myConsole.getOut().println("Order " + my_order.getLabel() + " is closed");
_barrier--; //we killed one - doesn't matter position in list
}
else
{
myConsole.getOut().println("Next order is in same direction as this order found");
}
}
}
//at this point if barrier = 0 we have no more trades or had none when the loop started
myCanTrade = (0 == _barrier) && (!curr_trade_dir.equals(prev_trade_dir)); //1
myCanTrade = (0 == _barrier); //2
//if you answered Yes to the Q. above, use //1 as the rules caused the close(s) OR (the the old trade(s) was(were) stopped out AND you still care about the old direction)
//if you answered No to the Q. above, use //2 as the rules caused the close(s) OR the the old trade(s) was(were) stopped out
// place new trade order if required
if((myCanTrade) && (myCurrPositionValue > 0))
{
if(curr_trade_dir.equals("UP")){ //bullish
myOrderDirection = OrderCommand.BUY;
double takeProfitPrice = myHistory.getLastTick(myInstrument) .getBid() + myInstrument.getPipValue() * myTakeProfit;
placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
}
else if(curr_trade_dir.equals("DOWN"))
{ //bearish
myOrderDirection = OrderCommand.SELL;
double takeProfitPrice = myHistory.getLastTick(myInstrument) .getAsk() - myInstrument.getPipValue() * myTakeProfit;
placeBuyorSell(myOrderDirection, 0, takeProfitPrice);
}
//I would duplicate and move these after each of placeBuyorSell since strictly curr_trade_dir = "", "ABC", "123" would trigger them here
//Keep them here if your idea of "prev trade" / "curr trade" is about the trade "opportunity" that just came up (long vs short)
//consider duplicating/moving them if your idea of "prev trade" / "curr trade" is about actual trades you manage to take (long vs short)
prev_trade_dir = curr_trade_dir;
curr_trade_dir = "";
//no matter what no more trading (just closing) since we should have just made a trade if we reached here
myCanTrade = false;
}