I took a screenshot of my IDE to make it easier to read. I'm also going to paste the code below in case someone wants to copy and paste it to an IDE to try to figure it out. What I'm trying to do is create a stop-loss that is set to:
-20 at the beginning of trade
Breakeven when the market has moved in my favor by 20 pips
+20 when the market has moved in my favor by +40 pips
+40 when the market has moved in my favor by +60 pips
Trailing stop loss of -10 once price has moved in my favor by 70 pips.
This code below looks like it should work. If you would like to get more information about my backtest results, please let me know. This code is simple, I just don't know why I can't get it to work. Thanks a million.
if (Position.MarketPosition == MarketPosition.Flat)
{
// Resets stop-loss when market becomes flat.
SetStopLoss(CalculationMode.Ticks, stoplossticks);
} else if (Position.MarketPosition == MarketPosition.Long){
// Once the price is greater than entry price+20 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
} else if (Position.MarketPosition == MarketPosition.Long){
// Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);
}
} else if (Position.MarketPosition == MarketPosition.Long) {
// Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize);
}
} else if ( Position.MarketPosition == MarketPosition.Long) {
// Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
if(Close[0] > Position.AvgPrice + 70 * TickSize) {
SetTrailStop(CalculationMode.Ticks, 10);
}
}
Comment