Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Price of Stop Loss in format double

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

    Price of Stop Loss in format double

    Hello,
    Please, tell me:
    I need to receive a stop order price in format double in method OnBarUpdate() so that I can conduct mathematical calculations. How can I do it?

    Regards,

    Yury

    #2
    Hello,

    Thank you for the note.

    You can override OnOrderUpdate() to get the price of your stop. The best way to save it for OnOrderUpdate() would be to make a class variable.

    Code:
    public class MyStrategy : Strategy
    
    {
    
            private double myStopPrice;
    
            ....
    
            protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
            {
        
                    	if(order.Name == "Stop loss")
    			{
    				myStopPrice = order.StopPrice;
    			}
            
            }
    
    }
    https://ninjatrader.com/support/help...rderupdate.htm - OnOrderUpdate()

    https://ninjatrader.com/support/help...-us/?order.htm - Oder object

    Please let us know if we may be of any further assistance.

    Comment


      #3
      Thank you for fast reply.
      This way I realize. I need price of stop loss in metod OnBarUpdate() for stop trailing

      Comment


        #4
        Hello,

        Thank you for the reply.

        To find the current price of an order generated from a Set method, it is necessary to capture the order in OnOrderUpdate and assign this to a variable. Then access the price through the order object from any other methods such as OnBarUpdate. This is why we set up a class variable named myStopOrder. The variable will persist throughout class member method calls. This means you can assign myStopOrder in OnOrderUpdate then use it in OnBarClose. Notice in my example I am looking for the signal name of the stop loss, which is case sensitive. Stop loss orders will always have the signal name "Stop loss". I am also providing a more well rounded example, where I save the entire Stop loss order object in OnOrderUpdate, then I make sure the Stop loss order object is initialized before I use it In OnBarUpdate.

        Code:
        public class MyStrategy : Strategy
        	{
        		
        		private double myStopOrder;
        ...
        
        protected override void OnBarUpdate()
        		{
        			...
        			
        			if(myStopOrder != null)
        			{
        				Print(myStopOrder.StopPrice.ToString());
        			}
        		}
        		
        		protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
        		{
        			Print(order.StopPrice);
        			Print(order.Name);
        			if(order.Name == "Stop loss")
        			{
        				myStopOrder = order;
        			}
        		}
        Here is the documentation on the Order object:


        Also, keep in mind that we provide the SetTrailStop method for trail stops:


        For more information on C# classes, please see this publicly available link:
        Learn about class types, how to use classes, and how to create new class type declarations for your app.


        Please let us know if we may be of any further assistance.

        Comment


          #5
          Thank you for fast reply.
          This way I realized too. I need price of stop loss in metod in OnBarUpdate() for my logic for stop trailing:
          double newPriceStopLoss = LastPriceStop + 2 * TickSize
          Your way call string format but I need double

          Comment


            #6
            Hello,

            Thank you for the reply.

            I am casting the double to a string in my example. The property is a double originally so you would use the double returned as you would normally.

            Code:
            double newPriceStopLoss = myStopOrder.StopPrice + 2 * TickSize;
            Please let us know if you have any questions.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by NullPointStrategies, Today, 05:17 AM
            0 responses
            50 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            126 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            69 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            42 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            46 views
            0 likes
            Last Post TheRealMorford  
            Working...
            X