Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Wrong Quantity and Orders

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

    Wrong Quantity and Orders

    I am testing a strategy, so I have user variable ShareQuantity which I defined during the strategy builder step and kept its default value as 1, Now whenever a buy/sell signals are generated I call:
    EnterLong(Convert.ToInt32(ShareQuantity), @"Long Entry") / EnterShort(Convert.ToInt32(ShareQuantity), @"Short Entry");

    but before entering Long I check if currently I am short or not, if I am then I call:

    ExitShort(Convert.ToInt32(ShareQuantity), @"PnL", @"Short Entry"); and during Short I exit Long by calling: ExitLong(Convert.ToInt32(ShareQuantity), @"PnL", @"Long Entry");

    All this works great my signals are getting called properly but chart and order page are showing wrong informations, chart shows "Close Position" with quantity as 2, similarly on the order page some orders have quantity as 2.

    One more thing, I have a condition in my strategy which checks If my target for today has been achieved or not and only enter in a trade if we are short on target, but even after filling my target and getting out of trades, strategy page shows I am Long/short and this gets filled on "ExitOnSessionCloseSeconds".

    Thanks & Regards.
    Attached Files

    #2
    Hello.

    Thanks for the post.

    If you ExitShort then EnterLong in the same bar, the position hasn't updated yet and you would get two exits and one entry. When you use EnterLong or EnterShort NinjaTrader will already try to close out of an opposite position, so checking if you are long/short before your call EnterLong/EnterShort is resulting in a double exit.

    You can check your position with OnPositionUpdate to ensure that you get the most current information on your position.



    You will need to make sure your position is flat in OnPositionUpdate before your next entry in OnBarUpdate. For example, you can make a class level boolean variable and toggle that variable in OnPositionUpdate for use in OnBarUpdate.

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

    Comment


      #3
      Thanks @NinjaTrader_ChrisL, that clears all of my doubts, I will remove the exit calls as it is handled internally by NT already. I have one another query though, when we enable a strategy on a chart of days of data, strategy will fire orders for all the past eligible conditions, is there a way so that though they may be marked on the chart but the orders don't fire for those?

      Thanks and Regards.

      Comment


        #4
        Hello.

        Thanks for the reply.

        The historical orders that are marked on your chart by the strategy represent historically calculated executions based on historical calculations of the strategy. You can do the following in OnBarUpdate to ignore the historical calculations and only run the strategy on real-time data; this will not plot the executions though.

        Code:
        if(State ==State.Historical) return;
        Please also read this section of the help guide on syncing your account position when you start your strategy. The option "Immediately submit" will cause the strategy to start placing live orders to the account immediately. We recommend reviewing all the options available to you when starting your strategy.



        Please let us know if you have any questions.
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Thanks again @NinjaTrader_ChrisL, you are such a time saver, I would though require your help one more time, can we execute our code on ExitOnSessionClose, if yes then please let me know how, is it a bool condition or something else? Also If we are placing market orders how can we get our entry and exit prices?

          Thanks & Regards
          Last edited by RC007; 03-29-2018, 05:08 AM.

          Comment


            #6
            Hello RC007,

            thanks for the reply.

            Exit On Close is a setting used by NinjaTrader to close out the strategies positions at the end of the trading session, there is no way of accessing this code.

            You can check if the working bar is the last bar of the session if your indicator calculates on bar close with IsLastBarOfSession:



            If that will not work you can also get the actual end of the session with a session iterator:



            When you get the proper time, you can run some code once that time is reached.

            You can use the times array to get the time while your strategy is running:



            Please let us know if we may be of any further assistance.
            Last edited by NinjaTrader_ChrisL; 03-29-2018, 08:59 AM.
            Chris L.NinjaTrader Customer Service

            Comment


              #7
              Thanks NinjaTrader_ChrisL, I have decide to use time array and is working fine, please also let me know, if I am placing market orders how can I get the entry and exit prices, especially when the exits are being handled by NT internally?

              Thanks & Regards.

              Comment


                #8
                Hello RC007,

                I am responding to you on behalf of Chris who is out of the office today.

                The fill prices for market orders can be seen in the OnOrderUpdate() or OnExecutionUpdate() methods.

                As an example, the fill prices for these orders are printed in OnOrderUpdate() and OnExecutionUpdate():
                Code:
                protected override void OnBarUpdate()
                {
                	if(Position.MarketPosition == MarketPosition.Flat)
                		EnterLong("MyEntry");
                	if(Position.MarketPosition == MarketPosition.Long)
                		ExitLong("MyExit", "MyEntry");
                }
                
                protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity, 
                	Cbi.MarketPosition marketPosition, string orderId, DateTime time)
                {
                	if(execution.Order.Name == "MyEntry" || execution.Order.Name == "MyExit")
                		Print(execution.Price);
                }
                
                protected override void OnOrderUpdate(Cbi.Order order, double limitPrice, double stopPrice, 
                	int quantity, int filled, double averageFillPrice, 
                	Cbi.OrderState orderState, DateTime time, Cbi.ErrorCode error, string comment)
                {
                	if(orderState == OrderState.Filled)
                	{
                		if(order.Name == "MyEntry" || order.Name == "MyExit")
                			Print(order.AverageFillPrice);
                	}
                }
                I've included publicly available documentation for these methods below.

                OnOrderUpdate() - https://ninjatrader.com/support/help...rderupdate.htm

                OnExecutionUpdate() - https://ninjatrader.com/support/help...tionupdate.htm

                If you have any additional questions, please don't hesitate to ask.
                JimNinjaTrader Customer Service

                Comment


                  #9
                  Thanks NinjaTrader_Jim, that was very helpful.

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by Christopher_R, Today, 12:29 AM
                  0 responses
                  9 views
                  0 likes
                  Last Post Christopher_R  
                  Started by sidlercom80, 10-28-2023, 08:49 AM
                  166 responses
                  2,235 views
                  0 likes
                  Last Post sidlercom80  
                  Started by thread, Yesterday, 11:58 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post thread
                  by thread
                   
                  Started by jclose, Yesterday, 09:37 PM
                  0 responses
                  8 views
                  0 likes
                  Last Post jclose
                  by jclose
                   
                  Started by WeyldFalcon, 08-07-2020, 06:13 AM
                  10 responses
                  1,415 views
                  0 likes
                  Last Post Traderontheroad  
                  Working...
                  X