However, I want to set a profit target, so if price goes above the SMA it enters long and it will set a profit target of around 8 ticks. However, if price does not reach 8 ticks and price crosses below the SMA it will exit the Long order.
When I do backtesting, the EnterLong() works and it price hits the setprofittarget of 8ticks it gets out of the trade for a 8 tick profit. However, the issue is that if price does not reach 8 ticks and it goes below the SMA then the strategy does the ExitLong(), but then the strategy stop working and never enters back to EnterLong() every again. There must be something with the SetProfitTarget() that maybe creating an issue if it does not get trigger and then the ExitLong() gets trigger.
I have attach the code on the attachment, but below is also part of the code. Also I got the same issue if I put the SetProfitTarget(CalculationMode.Ticks, 8); on the OnBarUpdate() instead of putting it in the State.Configure as it is below.
else if (State == State.DataLoaded)
{
smatesting = SMA(smatestingnumber);
x = SMA (smatestingnumber) [0];
smatesting.Plots[0].Brush = Brushes.Goldenrod;
AddChartIndicator(smatesting);
}
if(State == State.Configure)
{
SetProfitTarget(CalculationMode.Ticks, 8);
}
}
protected override void OnBarUpdate()
{
//Add your custom strategy logic here.
if(CrossAbove(smatesting,Close,1)) //What this is saying is "if price cross from above"
{
//CancelOrder();//("Long 1");
ExitLong();
} //then you want to close position
if (CrossBelow(smatesting,Close,1))
{
EnterLong();
// SetProfitTarget(CalculationMode.Ticks, 8);
//put in here a enter long that says get out after x ticks/points
}
}
Comment