Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

OnMarketData(MarketDataEventArgs e

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

    OnMarketData(MarketDataEventArgs e

    Hello,

    My code below creates a bracket trade. I have then utilized OnExecution to create a stop loss ( see 2nd part of code ). I then do a few things in the OnBarUpdate() section...basically I keep the Stoploss adjusted tick by tick.

    The problem however is that when the Stop Loss is activated the system does not always go back to " OnMarketData(MarketDataEventArgs e" to create a new Bracket trade.

    sometimes it does... most of the time it doesn't

    Please help




    protected override void Initialize()
    {
    CalculateOnBarClose = false;
    Unmanaged = true;
    }

    protected override void OnMarketData(MarketDataEventArgs e)
    {
    if
    ( longOrder == null || shortOrder == null)
    {
    // Create the Bracket on Initialization
    // Automatically recreates a Bracket when the previous one has run its course!!!

    longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, GetCurrentBid() +23 * TickSize, GetCurrentBid() +10 * TickSize, "Oil", "long limit entry");
    shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, GetCurrentBid() -23 * TickSize, GetCurrentBid() -10 * TickSize, "Oil", "Short limit entry");
    }

    }


    /* 2nd Part of code */

    protected override void OnExecution(IExecution execution)
    {



    pTickPrice = Close[0]; // Keep a record of the Execution TickPrice


    if (longOrder != null && longOrder == execution.Order) // we need a short position Stop loss

    {

    stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, 1, Position.AvgPrice +1 * TickSize, Position.AvgPrice +1 * TickSize, "protectLong", "Stop loss long");

    }


    else if (shortOrder != null && shortOrder == execution.Order)
    //(Position.MarketPosition == MarketPosition.Short && stopOrder == null)
    {
    // (2)
    stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.StopLimit, 1, pTickPrice -1 * TickSize, pTickPrice -1 * TickSize, "Oil", "Stop loss short");
    }


    // (4) if stopOrder is executed then the long / short position must have been closed

    if (stopOrder != null && stopOrder == execution.Order)

    {
    //shortOrder = longOrder = null;
    }

    #2
    Hello cocopod,

    Thank you for your post.

    Please try setting the entry order IOrder objects to null when they are filled and when you submit the Stop Loss.

    Then set the set up for the bracket to check Position.MarketPosition == MarketPosition.Flat.

    For information on Position please visit the following link: http://www.ninjatrader.com/support/h...7/position.htm

    Comment


      #3
      Admittedly I have only been coding in Ninjascript for 4 days or so and to be honest I don't have a clue what you are telling me to do

      Comment


        #4
        Hello cocpod,

        Thank you for your response.

        Change the code to the following:
        Code:
        protected override void Initialize()
                {
                    CalculateOnBarClose = false;
                    Unmanaged = true;
        		}	
        		
        		protected override void OnMarketData(MarketDataEventArgs e) 
        		{
        			if 
        				( Position.MarketPosition == MarketPosition.Flat)
        			{
        			// Create the Bracket on Initialization
        		       // Automatically recreates a Bracket when the previous one has run its course!!!		
        	
        			longOrder = SubmitOrder(0, OrderAction.Buy, OrderType.StopLimit, 1, GetCurrentBid() +23 * TickSize, GetCurrentBid() +10 * TickSize, "Oil", "long limit entry");
        			shortOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, GetCurrentBid() -23 * TickSize, GetCurrentBid() -10 * TickSize, "Oil", "Short limit entry");
        			}	
        
        		}
        
        
        /*   2nd Part of code */
        
        protected override void OnExecution(IExecution execution)
        		{
        
        			   
        			
                                pTickPrice = Close[0];  // Keep a record of the Execution TickPrice
        
        	        
        			if (longOrder != null && longOrder == execution.Order && execution.OrderState == OrderState.Filled) // we need a short position Stop loss
        
        			    {
        
        				stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Stop, 1,  Position.AvgPrice +1 * TickSize,  Position.AvgPrice +1 * TickSize, "protectLong", "Stop loss long");	
        				longOrder = null;
        			    }
        				
        						
        			   else if (shortOrder != null && shortOrder == execution.Order && execution.OrderState == OrderState.Filled)
        				//(Position.MarketPosition == MarketPosition.Short && stopOrder == null)
        			     {
        				// (2)
        				  stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.StopLimit, 1,  pTickPrice -1 * TickSize,  pTickPrice -1 * TickSize, "Oil", "Stop loss short");
        				  shortOrder = null;
        				 }

        Comment


          #5
          The system began to create bracket order after order ...nonstop

          protected override void OnMarketData(MarketDataEventArgs e)
          {
          if
          ( Position.MarketPosition == MarketPosition.Flat)

          Comment


            #6
            I did by the way use simulated data as the vol at this hour is non-existent

            Comment


              #7
              This is becoming most unrelaible. I notice now that the system doesn't always create the Stop Loss. I think the appraoch is conventional.. The OnExecution section is called when the Order is filled by the system. Again Sometimes it works...sometimes it doesn't

              protected override void OnExecution(IExecution execution)
              {


              pTickPrice = Close[0]; // Keep a record of the Execution TickPrice

              if
              (Position.MarketPosition == MarketPosition.Long) // not a call after Stop Loss has been created
              {
              // (2)
              stopOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.StopLimit, 1, pTickPrice -1 * TickSize, pTickPrice -1 * TickSize, "Oil", "Short limit entry");
              }

              else if
              (Position.MarketPosition == MarketPosition.Short)
              {
              // (2)
              stopOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.StopLimit, 1, pTickPrice +1 * TickSize, pTickPrice +1 * TickSize, "Oil", "Stop loss short");
              }

              // (4) if stopOrder is executed then the long / short position must have been closed

              if (stopOrder != null && stopOrder == execution.Order)
              {
              shortOrder = longOrder = null;
              }

              Comment


                #8
                OrderState.Filled)

                (shortOrder != null && shortOrder == execution.Order && execution.OrderState == OrderState.Filled)

                The compilation failed on this...... NinjaTrader.Cbi.IExecution does not contain a definition for OrderState...

                Comment


                  #9
                  My Kingdom for a solution

                  Can someone look into this please.

                  I did make the code adjustments suggested by Patrick however it wouldn't compile now

                  Comment


                    #10
                    Originally posted by cocopod View Post
                    Can someone look into this please.

                    I did make the code adjustments suggested by Patrick however it wouldn't compile now
                    I think we commented on that in your other thread and you could compile. We will post an update there to your other questions. Thanks.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                    0 responses
                    647 views
                    0 likes
                    Last Post Geovanny Suaza  
                    Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                    0 responses
                    369 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by Mindset, 02-09-2026, 11:44 AM
                    0 responses
                    108 views
                    0 likes
                    Last Post Mindset
                    by Mindset
                     
                    Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                    0 responses
                    572 views
                    1 like
                    Last Post Geovanny Suaza  
                    Started by RFrosty, 01-28-2026, 06:49 PM
                    0 responses
                    573 views
                    1 like
                    Last Post RFrosty
                    by RFrosty
                     
                    Working...
                    X