I have utilised Josh's Sample Strategy for Scaling Out of Position which works well for two contracts - I am having problems with > 2 contracts.
My want is as follows:
1/ Kick off with 4x contracts with stop loss at -20 points.
2/ Profit Target for first 2x contracts is +5 at which point stop loss moved to -5.
3/ Profit target for third contract is +10 at which point stop loss moved to breakeven (this is where I am having the problem).
Points 1/ & 2/ work fine during backtest.
For point 3/, sometimes the stop loss works OK (i.e. exit at breakeven, but sometimes it exits at -5 ?!?!?).
Attached is a sample of the strategy that has been deployed:
protectedoverridevoid Initialize()
{
EntriesPerDirection = 1;
EntryHandling = EntryHandling.UniqueEntries;
SetProfitTarget("Long 1a", CalculationMode.Ticks, 5);
SetProfitTarget("Long 1b", CalculationMode.Ticks, 5);
SetProfitTarget("Long 1c", CalculationMode.Ticks, 10);
SetProfitTarget("Long 1d", CalculationMode.Ticks, 20);
SetStopLoss("", CalculationMode.Ticks, 20, false);
CalculateOnBarClose = false;
}
///<summary>
/// Called on each bar update event (incoming tick)
///</summary>
protectedoverridevoid OnBarUpdate()
{
// Condition set 1
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, 20);
}
// If a long position is open, allow for stop loss modification to breakeven
elseif (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+5 ticks, set stop loss to breakeven - 5
if (Close[0] > Position.AvgPrice + 5 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice - 5);
}
if (Close[0] > Position.AvgPrice + 10 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}
if (Conditions x, y & z comply.............
{
if (Position.MarketPosition == MarketPosition.Flat)
{
EnterLong("Long 1a");
EnterLong("Long 1b");
EnterLong("Long 1c");
EnterLong("Long 1d");
}
is there anything I wrong here that is causing this issue with the stop loss on the 4th contract.
many thx
David
Comment