The script uses two Bars series and starts with Calculate.OnBarClose then changes to .OnPriceChange when the order is placed. I notice that the problem happens when I revert back to .OnBarClose when the order is closed with the .OnExectionUpdate method
Here is my code:
#region Variables
private Order EntryOrder = null;
#endregion
protected override void OnStateChange(){
if (State == State.SetDefaults)
{
Description = @"";
Name = "AddSeriesTest";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
EntryHandling = EntryHandling.AllEntries;
IsExitOnSessionCloseStrategy = true;
ExitOnSessionCloseSeconds = 30;
IsFillLimitOnTouch = false;
MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
OrderFillResolution = OrderFillResolution.Standard;
Slippage = 0;
StartBehavior = StartBehavior.WaitUntilFlat;
TimeInForce = TimeInForce.Gtc;
TraceOrders = false;
RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
BarsRequiredToTrade = 0;
IsInstantiatedOnEachOptimizationIteration = true;
StopTargetHandling = StopTargetHandling.PerEntryExecution;
}
///////// Adds secondary series, primary series is ES 03-14 loaded by chart
else if (State == State.Configure)
{
AddDataSeries("ES 06-14", Data.BarsPeriodType.Minute, 2, Data.MarketDataType.Last);
}
}
protected override void OnBarUpdate(){
////////// Checks which Bars is calling the bar update based on the day
if( BarsInProgress == 1 && ToDay(Time[0]) < 20140314) //ES 06-14
return;
////////// Submits order on a specific bar in the asigned Bars series and sets Calculate method to OnPriceChange
if(BarsInProgress == 0 && ToDay(Time[0]) == 20140312 && ToTime(Time[0]) == ToTime(14,28,00) )
{
SetProfitTarget(CalculationMode.Ticks,6);
SetStopLoss(CalculationMode.Ticks,9);
EntryOrder = EnterShortLimit(0,true, 1, Close[0],"Entry");
Calculate = Calculate.OnPriceChange;
}
}
protected override void OnExecutionUpdate (Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time){
if(execution.Order.Name == "Entry")
{
Print(ToDay(Time[0]) + @" " + ToTime(Time[0]) + @" " + execution.Order.OrderState + @" in full By Bars: " + BarsInProgress);
}
///////// Reverts back to .OnBarClose on either target or stoploss
if(execution.Order.Name == "Profit target")
{
Calculate = Calculate.OnBarClose;
Print(@"W in Bars: " + BarsInProgress);
}
if(execution.Order.Name == "Stop loss")
{
Calculate = Calculate.OnBarClose;
Print(@"L in Bars: "+ BarsInProgress);
}
}
}
}
Comment