Basically, there are 3 price levels involved in this strategy: entryLevel, exitLevel, and stopLevel. On the long side, once price crosses entrylevel, a long order is triggered. If the price reaches pulls back and reaches the stopLevel, the long is closed. If the price reaches the price target, I wanted a trailing stop issued. I wanted the trail to be the same distance between the entryLevel and stopLevel. Additionally, I wanted there to be a stop for a minimal gain to be ready when I am long and the price exceeds 2 points above the entryLevel.
Here is the code. The parts that I have added and subsequently had troubles with are bolded. This is all in OnBarUpdate() by the way:
if (Position.MarketPosition == MarketPosition.Flat)
{
if (MyIndicator.LongChanceInProgress)
{
if (GetCurrentAsk() > MyIndicator.entryLevel && Open [0] <=MyIndicator.entryLevel && GetCurrentAsk() < MyIndicator.exitLevelLong)
{
SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Buy Signal - Target"+" "+MyIndicator.exitLevelLong);
EnterLong(1);
}
}
}
else if (Position.MarketPosition == MarketPosition.Long)
{
if (GetCurrentAsk() > MyIndicator.exitLevelLong)
{
SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Long Target reached - Trailing Stop Set");
[B]SetTrailStop(CalculationMode.Ticks, (MyIndicator.entryLevel - MyIndicator.stopLevelLong)); //used to be just ExitLong(1)[/B]
}
else if (GetCurrentAsk() < MyIndicator.stopLevelLong)
{
SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Close Long - Stopped Out");
ExitLong(1);
}
[B]else if (Close [0] <= (MyIndicator.entryLevel + 2) && Open [0] >= (MyIndicator.entryLevel + 2))[/B]
{
SendMail("[email protected]","[email protected]", Instrument.MasterInstrument.Name, "Close Long - Stopped for minimal gain");
ExitLong(1);
}
}
What ends up happening when I test this strategy is that if the target is reached, the strategy immediately exits the position instead of setting a trailing stop.
Also, When the price is ascending and reaches entryLevel +2 it immediately exits my long instead of what its supposed to do, which is exiting my long if the price is descending and hits entryLevel + 2. So in essence the price can never reach the target with me being long. (I had to disable this part in order to test if the trailing stop worked)
I have stared at this code for hours and am unable to figure out what I did wrong. Help?

thanks again
Comment