Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

long short same bar

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

    long short same bar

    the strategy has the following logic.
    longshort intraday.calc on barclose true.
    go long after an upbar high+3 ticks.go short after downbar low - 3ticks
    the startegy works fine.but no short or long trade is taken on the same bar after a long or short is exited on this same bar.
    pls advise...
    here is the code
    Code:
    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Strategy;
    #endregion
    
    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
        /// <summary>
        /// Enter the description of your strategy here
        /// </summary>
        [Description("longshort intraday.go long after upbar high+3 ticks.go short after downbar low - 3ticks")]
        public class projectminisessiondaytrading1longshort : Strategy
     
        {
            #region Variables
            private IOrder longentryOrder     = null; // This variable holds an object representing our long entry order
            private IOrder shortentryOrder     = null; // This variable holds an object representing our short entry order
            private IOrder stopOrder     = null; // This variable holds an object representing our stop loss order
            private IOrder targetOrder     = null; // This variable holds an object representing our profit target order
            private int barNumberOfOrder = 0;
            private int cancelBars = 2;          // number of bars to cancel order after
    
            #endregion
    
            /// <summary>
            /// This method is used to configure the strategy and is called once before any strategy method is called.
            /// </summary>
            protected override void Initialize()
            {
                TraceOrders = false;
                CalculateOnBarClose = true;
                
            }
            
            /// <summary>
            /// Called on each bar update event (incoming tick)
            /// </summary>
            protected override void OnBarUpdate()
            {
                // Submit an longentry limit order if we currently don't have an entry order open
                if (longentryOrder == null && Close[0] > Open[0] && (ToTime(Time[0]) < 135900)) 
                {
                    
                    longentryOrder = EnterLongStop(0, true,1,High[0] + 6 * TickSize,"LONGEntry");
                barNumberOfOrder = CurrentBar;
    
    
                }
                
                //  cancel the entry order
                else if (longentryOrder != null 
            && ( (Close[0] < Open[0]))
            )        
        
             CancelOrder(longentryOrder);
                
                
                
                if (Position.MarketPosition == MarketPosition.Long 
                    
                    && (Close[0] < Open[0])
                    && (ToTime(Time[0]) < 135900)
                    
                    )
                {
                    // Checks to see if our Stop Order has been submitted already
                    if (stopOrder != null && stopOrder.StopPrice < Position.AvgPrice)
                    {
                        // Modifies stop-loss
                        stopOrder = ExitLongStop(0, true, stopOrder.Quantity, Low[0] - 6 * TickSize, "LONGStopmodified", "LONGEntry");
                    }
                }
                
                
                /*********************now look for short trades*****************************/
                // Submit an short entry  order if we currently don't have an entry order open
                if (shortentryOrder == null && Close[0] < Open[0] && (ToTime(Time[0]) < 135900)) 
                {
                    shortentryOrder = EnterShortStop(0, true,1,Low[0] - 6 * TickSize,"SHORTEntry");
                barNumberOfOrder = CurrentBar;
    
    
                }
                
                //  cancel the shortentry order
                else if (shortentryOrder != null 
            && ((Close[0] > Open[0]))
            )        
        
             CancelOrder(shortentryOrder);
                
                    if (Position.MarketPosition == MarketPosition.Short 
                    
                    && (Close[0] > Open[0])
                    && (ToTime(Time[0]) < 135900)
                    
                    )
                {
                    // Checks to see if our Stop Order has been submitted already
                    if (stopOrder != null && stopOrder.StopPrice > Position.AvgPrice)
                    {
                        // Modifies stop-loss 
                        stopOrder = ExitShortStop(0, true, stopOrder.Quantity, High[0] + 6 * TickSize, "SHORTStopmodified", "SHORTEntry");
                    }
                }
                
                
            }
    
            /// <summary>
            /// Called on each incoming order event
            /// </summary>
            protected override void OnOrderUpdate(IOrder order)
            {
                //  The longentryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                if (longentryOrder != null && longentryOrder == order)
                {    
                    // Reset the longentryOrder object to null if order was cancelled without any fill
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                    {
                        longentryOrder = null;
                    }
                }
                //  The shortentryOrder object allows us to identify that the order that is calling the OnOrderUpdate() method is the entry order.
                if (shortentryOrder != null && shortentryOrder == order)
                {    
                    // Reset the shortentryOrder object to null if order was cancelled without any fill
                    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
                    {
                        shortentryOrder = null;
                    }
                }
            }
            
            /// <summary>
            /// Called on each incoming execution
            /// </summary>
            protected override void OnExecution(IExecution execution)
            {
                if (longentryOrder != null && longentryOrder == execution.Order)
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {
                        // Stop-Loss order  below our entry price
                        stopOrder     = ExitLongStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 22 * TickSize, "LONGStop", "LONGEntry");
                        
                        // Target order  above our entry price
                        targetOrder = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 22 * TickSize, "LONGTarget", "LONGEntry");
                        
                        // Resets the longentryOrder object to null after the order has been filled
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                            longentryOrder     = null;
                        }
                    }
                }
                 if (shortentryOrder != null && shortentryOrder == execution.Order)
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                    {
                        // Stop-Loss order  above our entry price
                        stopOrder     = ExitShortStop(0, true, execution.Order.Filled, execution.Order.AvgFillPrice + 22 * TickSize, "SHORTStop", "SHORTEntry");
                        
                        // Target order   below our entry price
                        targetOrder = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AvgFillPrice - 22 * TickSize, "SHORTTarget", "SHORTEntry");
                        
                        // Resets the shortentryOrder object to null after the order has been filled
                        if (execution.Order.OrderState != OrderState.PartFilled)
                        {
                            shortentryOrder     = null;
                        }
                    }
                }
                
                // Reset our stop order and target orders' IOrder objects after our position is closed.
                if ((stopOrder != null && stopOrder == execution.Order) || (targetOrder != null && targetOrder == execution.Order))
                {
                    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled )
                    {
                        stopOrder = null;
                        targetOrder = null;
                    }
                    
                }
            }
    
            /// <summary>
            /// Called on each incoming position event
            /// </summary>
            protected override void OnPositionUpdate(IPosition position)
            {
                // Print our current position to the lower right hand corner of the chart
                DrawTextFixed("Tag", position.ToString(), TextPosition.BottomRight);
            }
    
            #region Properties
             #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public int BarNumberOfOrder
            {
                get { return barNumberOfOrder; }
                set { barNumberOfOrder = Math.Max(1, value); }
            }
            [Description("")]
            [GridCategory("Parameters")]
            public int CancelBars
            {
                get { return cancelBars; }
                set { cancelBars = Math.Max(1, value); }
            }
            #endregion
            #endregion
        }
    }

    #2
    easyfying, correct we would expect that for same bar orders - more discussion on this topic could be found here in a related thread on this item -

    Comment


      #3
      thank you for providing the link.however it discusses managed approach .my stratetgy uses unmanaged approach.
      r the rules th e same for managed and unmanaged approach.i thought same bar entries r possible using unmanaged approach.
      another question is--if a trade is open using unmanaged approach and has stop and profit orders attatched to it ,.do i have to close it myself before entering trade in opposite direction or does the opposite trade cancels this trade itself.

      Comment


        #4
        That's not correct here easyfying, the strategy uses the managed mode Enter() methods so the discussed limitations will unfortunately apply.

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Geovanny Suaza, 02-11-2026, 06:32 PM
        0 responses
        558 views
        0 likes
        Last Post Geovanny Suaza  
        Started by Geovanny Suaza, 02-11-2026, 05:51 PM
        0 responses
        324 views
        1 like
        Last Post Geovanny Suaza  
        Started by Mindset, 02-09-2026, 11:44 AM
        0 responses
        101 views
        0 likes
        Last Post Mindset
        by Mindset
         
        Started by Geovanny Suaza, 02-02-2026, 12:30 PM
        0 responses
        545 views
        1 like
        Last Post Geovanny Suaza  
        Started by RFrosty, 01-28-2026, 06:49 PM
        0 responses
        547 views
        1 like
        Last Post RFrosty
        by RFrosty
         
        Working...
        X