• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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.
    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:
        https://ninjatrader.com/support/help...-us/?order.htm

        Also, keep in mind that we provide the SetTrailStop method for trail stops:
        https://ninjatrader.com/support/help...ttrailstop.htm

        For more information on C# classes, please see this publicly available link:
        https://docs.microsoft.com/en-us/dot...tructs/classes

        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 sdauteuil, Today, 11:02 PM
            1 response
            2 views
            0 likes
            Last Post sdauteuil  
            Started by Jschmelz, Today, 04:45 PM
            1 response
            7 views
            0 likes
            Last Post NinjaTrader_Marco  
            Started by sisenberg, Today, 08:06 PM
            1 response
            8 views
            0 likes
            Last Post sisenberg  
            Started by priceisking, Today, 07:44 PM
            0 responses
            5 views
            0 likes
            Last Post priceisking  
            Started by saltminer, Today, 06:06 PM
            0 responses
            5 views
            0 likes
            Last Post saltminer  
            Working...
            X