Can anyone help me with multiple profit targets or part lot sizes? I am trying to backtest a strategy I use for FTSE Futures where I enter the trade with three contracts and then close each contract as the various profit targets get hit. The difficulty I am having is that I can not find a way to close part of a position. I am certain that it is possible but I have trawled the board and can not find any code to do this. Here is the code I am using at present. Currently when the first target is hit it closes out the whole position, so the second profit target vanishes from the cursor window. If the market moves up strongly this does not happen but if the bars retrace after hitting the first target all the positions are closed.
if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
// Check if the long 'Narrow' profit target has been reached/breached
if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
// Profit Target Breached, Execute Cover order.
if(getBarState()==BARSTATE_NEWBAR){
Alert.playSound("choir1.wav");
Strategy.doSell("Long PT-A Exit", Strategy.STOP, Strategy.THISBAR,
Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
ProfitTarget1 = ProfitTarget2;
}
}
}
if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
// Check if the long 'Wide' profit target has been reached/breached
if (high() >= (nTradeEntryPrice + ProfitTarget2)) {
// Profit Target Breached, Execute Cover order.
if(getBarState()==BARSTATE_NEWBAR){
Alert.playSound("choir1.wav");
Strategy.doSell("Long PT-B Exit", Strategy.STOP, Strategy.THISBAR,
Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget2));
}
}
}
if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
// Check if the long 'Narrow' profit target has been reached/breached
if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
// Profit Target Breached, Execute Cover order.
if(getBarState()==BARSTATE_NEWBAR){
Alert.playSound("choir1.wav");
Strategy.doSell("Long PT-A Exit", Strategy.STOP, Strategy.THISBAR,
Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget1));
ProfitTarget1 = ProfitTarget2;
}
}
}
if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
// Check if the long 'Wide' profit target has been reached/breached
if (high() >= (nTradeEntryPrice + ProfitTarget2)) {
// Profit Target Breached, Execute Cover order.
if(getBarState()==BARSTATE_NEWBAR){
Alert.playSound("choir1.wav");
Strategy.doSell("Long PT-B Exit", Strategy.STOP, Strategy.THISBAR,
Strategy.getDefaultLotSize(), (nTradeEntryPrice + ProfitTarget2));
}
}
}
Comment