I´m trying to understand why an order is executing on the wrong Bars series.
In the code below I´m show how I have set for two orders to execute each on a very specific bar. One of these orders should happen within the context of the primary bars series and the second order should happen within the context of the secondary bars series, yet, both execute within the primary series.
Here is my code:
public class AddSeriesTest : Strategy
{
#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 09-14 loaded by chart
else if (State == State.Configure)
{
AddDataSeries("ES 12-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 == 0 && ToDay(Time[0]) > 20140910)
return;
if( BarsInProgress == 1 && ToDay(Time[0]) < 20140911)
return;
Print(@"bar# " + CurrentBar + @"in Bars " + BarsInProgress);
/////////////////// Submits order if on a specific bar in the asigned Bars
if(BarsInProgress == 0 && ToDay(Time[0]) == 20140910 && ToTime(Time[0]) == ToTime(11,58,00) )
{
SetProfitTarget(CalculationMode.Ticks,6);
SetStopLoss(CalculationMode.Ticks,9);
EntryOrder = EnterShortLimit(0,true, 1, Close[0],"Entry");
}
if(BarsInProgress == 1 && ToDay(Time[0]) == 20140911 && ToTime(Time[0]) == ToTime(10,10,00) )
{
SetProfitTarget(CalculationMode.Ticks,6);
SetStopLoss(CalculationMode.Ticks,9);
EntryOrder = EnterShortLimit(0,true, 1, Close[0],"Entry");
}
}
protected override void OnExecutionUpdate (Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time){
if(execution.Order.Name == "Entry")
{
Print(execution.Order.OrderState + @" in full By Bars: " + BarsInProgress);
}
if(execution.Order.Name == "Profit target")
{
EntryOrder = null;
Print(@"W in Bars: " + BarsInProgress);
}
if(execution.Order.Name == "Stop loss")
{
EntryOrder = null;
Print(@"L in Bars: "+ BarsInProgress);
}
}
}
Comment