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

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.
    Chris L.NinjaTrader Customer Service

    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:


        Please let us know if we may be of any further assistance.
        Chris L.NinjaTrader Customer Service

        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.
            Chris L.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by Barry Milan, Today, 10:35 PM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Manfred  
            Started by WeyldFalcon, 12-10-2020, 06:48 PM
            14 responses
            1,428 views
            0 likes
            Last Post Handclap0241  
            Started by DJ888, Yesterday, 06:09 PM
            2 responses
            9 views
            0 likes
            Last Post DJ888
            by DJ888
             
            Started by jeronymite, 04-12-2024, 04:26 PM
            3 responses
            40 views
            0 likes
            Last Post jeronymite  
            Started by bill2023, Today, 08:51 AM
            2 responses
            16 views
            0 likes
            Last Post bill2023  
            Working...
            X