Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

GTC orders open over session boundaries getting filled?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    GTC orders open over session boundaries getting filled?

    Open orders are getting hit outside of data series if the order spans session boundaries.





    Some demo code:

    Code:
    namespace NinjaTrader.NinjaScript.Strategies
    {
        public class DemoCrossSessionGTC : Strategy
        {
            //order objects
            private Order longEntryLimitOrder = null;
            private Order longEntryStopMar****rder = null;
            private Order longExitLimitOrder = null;
            private Order longExitStopMar****rder = null;
    
            private int sumLongFilled = 0;
            private int contracts = 1;
            private double longStopTicks = 8.0;
            private double longTargetTicks = 8.0; 
    
            protected override void OnStateChange()
            {
                if (State == State.SetDefaults)
                {
                    Description                                    = @"GTC Orders being executed when no data should trigger entry/exits";
                    Name                                        = "DemoCrossSessionGTC";
                    Calculate                                    = Calculate.OnBarClose;
                    EntriesPerDirection                            = 2;
                    EntryHandling                                = EntryHandling.AllEntries;
                    IsExitOnSessionCloseStrategy                = false;
                    ExitOnSessionCloseSeconds                    = 0;
                    IsFillLimitOnTouch                            = true;
                    MaximumBarsLookBack                            = MaximumBarsLookBack.TwoHundredFiftySix;
                    OrderFillResolution                            = OrderFillResolution.High;
                    OrderFillResolutionType                        = BarsPeriodType.Tick;
                    OrderFillResolutionValue                    = 1;
                    Slippage                                    = 1;
                    StartBehavior                                = StartBehavior.WaitUntilFlat;
                    TimeInForce                                    = TimeInForce.Gtc;
                    TraceOrders                                    = true;
                    RealtimeErrorHandling                        = RealtimeErrorHandling.IgnoreAllErrors;
                    StopTargetHandling                            = StopTargetHandling.ByStrategyPosition;
                    BarsRequiredToTrade                            = 20;
                    IsInstantiatedOnEachOptimizationIteration    = true;
                }
                else if (State == State.Configure)
                {
                }
            }
    
            protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
                Cbi.MarketPosition marketPosition, string orderId, DateTime time)
            {
                if ((longEntryStopMar****rder != null && longEntryStopMar****rder == execution.Order) ||
                    (longEntryLimitOrder != null && longEntryLimitOrder == execution.Order))
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled
                        && execution.Order.Filled > 0))
                    {
                        // We sum the quantities of each execution making up the entry order
                        sumLongFilled += execution.Quantity;
    
                        // add stop exit order or if if exists already update volume as partfills occur
                        if ( longExitStopMar****rder == null)            
                        {
                            Print ("Entering GTC Stop Exit Order on "+ Position.AveragePrice + (longTargetTicks * TickSize).ToString());
                            longExitStopMar****rder = ExitLongStopMarket(0, true, sumLongFilled, Position.AveragePrice - (longStopTicks * TickSize), "ExitLStpMrkt", "LongEntry");
                        }
                        else
                        {
                            Print ("Changing GTC Stop Exit Order on "+ CurrentBar.ToString());
                            ChangeOrder(longExitStopMar****rder, sumLongFilled,  0.0 , longExitStopMar****rder.StopPrice);                            
                        }
    
                        if ( longExitLimitOrder == null)
                        {
                            Print ("Entering GTC Target Exit Order on "+  Position.AveragePrice + (longTargetTicks * TickSize).ToString());
                            longExitLimitOrder = ExitLongLimit(0, true, sumLongFilled, Position.AveragePrice + (longTargetTicks * TickSize), "ExitLLimit", "LongEntry");
                        }
                        else
                        {
                            Print ("Changing GTC Target Exit Order on "+ CurrentBar.ToString());
                            ChangeOrder(longExitLimitOrder, sumLongFilled , Position.AveragePrice + (longTargetTicks * TickSize), 0.0);                                    
                        }
    
    
                        if (execution.Order.OrderState == OrderState.Filled && sumLongFilled == contracts)
                        {
                            if(longEntryLimitOrder == execution.Order)
                            {
                                longEntryLimitOrder  = null;
                                if (longEntryStopMar****rder != null) CancelOrder( longEntryStopMar****rder );
                            }
                            if(longEntryStopMar****rder == execution.Order)
                            {
                                longEntryStopMar****rder = null;
                                if (longEntryLimitOrder != null) CancelOrder( longEntryLimitOrder );
                            }
                        }
    
    
                        if (execution.Order.OrderState == OrderState.Filled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {
                            sumLongFilled = 0;
                        }
                    }
                }
    
                //handle exits, if one of target or stop are filled, cancel the other
                if  ((longExitStopMar****rder != null && longExitStopMar****rder == execution.Order) ||
                    (longExitLimitOrder != null && longExitLimitOrder == execution.Order ))
                {
                    if (execution.Order.OrderState == OrderState.Filled )
                    {
                        if (longExitStopMar****rder == execution.Order)
                        {
                            longExitStopMar****rder = null;
    
                            if (longExitLimitOrder != null) CancelOrder (longExitLimitOrder);
                        }
                        else if  (longExitLimitOrder == execution.Order)                
                        {
                            longExitLimitOrder = null;
                            if ( longExitStopMar****rder!= null) CancelOrder (longExitStopMar****rder);
                        }
                    }
                }
            }
    
            protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice,
                int quantity, int filled, double averageFillPrice,
                Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
            {
                //assign long entry orders  objects to vars and tidy
                if (order.Name == "LongEntry")
                {
                    if (order.IsStopMarket) longEntryStopMar****rder = order;
                    if (order.IsLimit) longEntryLimitOrder = order;
    
                    // Reset the entryOrder object to null, if Orderstate.Filled then reset in OnExecutionUpdate
                    if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Rejected || order.OrderState == OrderState.Unknown)
                    {
                        if (order.IsStopMarket) longEntryStopMar****rder = null;
                        if (order.IsLimit) longEntryLimitOrder = null;
                    }
                }
    
                //assign and tidy exit orders
                if (order.Name == "ExitLStpMrkt")
                {
                    longExitStopMar****rder = order;
    
                    // Reset the exit order object to null. if filled reset in OnExecutionUpdate
                    if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Rejected
                        || order.OrderState == OrderState.Unknown)
                    {
                        longExitStopMar****rder = null;
                    }
                }
                if (order.Name == "ExitLLimit")
                {
                    longExitLimitOrder = order;
    
                    // Reset the exit order object to null. if filled reset in OnExecutionUpdate
                    if (order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Rejected
                        || order.OrderState == OrderState.Unknown )
                    {
                        longExitLimitOrder = null;
                    }
                }
            }
    
            protected override void OnBarUpdate()
            {
                //place GTC stop and limit long entry orders if they dont exist and we're flat + enough bars
                if (longEntryStopMar****rder == null && longEntryLimitOrder == null)
                {
                    if (Position.MarketPosition == MarketPosition.Flat && CurrentBar > BarsRequiredToTrade)
                    {
                        //assign to an object var in OnOrderUpdate
                        Print("Entering GTC Long Entry Orders on "+ CurrentBar.ToString());
                        EnterLongLimit(0, true, contracts, GetCurrentBid() - 1.5, "LongEntry");
                        EnterLongStopMarket(0, true, contracts, GetCurrentAsk() + 1.5, "LongEntry");
                    }
                }
            }
        }
    }

    #2
    Hello lavalampmj,

    Thanks for your post.

    Something must have triggered the forums filter's to leave the post unapproved. We have notified IT to look into this.

    As you have opened a ticket with us over email, we will assist you further there and then report back any conclusion or useful information here.

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by bortz, 11-06-2023, 08:04 AM
    47 responses
    1,607 views
    0 likes
    Last Post aligator  
    Started by jaybedreamin, Today, 05:56 PM
    0 responses
    9 views
    0 likes
    Last Post jaybedreamin  
    Started by DJ888, 04-16-2024, 06:09 PM
    6 responses
    19 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by Jon17, Today, 04:33 PM
    0 responses
    6 views
    0 likes
    Last Post Jon17
    by Jon17
     
    Started by Javierw.ok, Today, 04:12 PM
    0 responses
    15 views
    0 likes
    Last Post Javierw.ok  
    Working...
    X