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

Strategy: Sell Orders are not executed

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

    Strategy: Sell Orders are not executed

    Hi
    I am working on a smiple strategy where if my entry condition is satisfied I open 2 orders on the high and low of the candle that just finished.

    I use these as Order entries

    Code:
    private Order longOrder;
    private Order shortOrder;
    
    protected override void OnBarUpdate()
    {
    if(CurrentBar == 0)
    return;
    
    scenarios.Add(GetScenario());
    
    //if(!IsSetup)
    IsStratPattern();
    
    
    }
    
    private void IsStratPattern()
    {
    if(scenarios.Count<3)
    return ;
    
    if(scenarios[scenarios.Count-1] == "1")
    {
    IsSetup = true;
    SetupTrade();
    }
    if(scenarios[scenarios.Count-1] == "3")
    {
    IsSetup = true;
    SetupTrade();
    }
    }
    
    
    private void SetupTrade()
    {
    longOrder = EnterLongStopMarket(High[0] + TickSize*1);
    shortOrder = EnterShortStopMarket(Low[0] - TickSize*1);
    
    
    
    }
    
    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if(Position.MarketPosition == MarketPosition.Long)
    {
    CancelOrder(shortOrder);
    longOrder = null;
    shortOrder = null;
    
    }
    else if(Position.MarketPosition == MarketPosition.Short)
    {
    CancelOrder(longOrder);
    longOrder = null;
    shortOrder = null;
    
    }
    
    SetProfitTarget(CalculationMode.Ticks,10 );
    SetStopLoss(CalculationMode.Ticks, 8);
    
    };
    the Long orders are triggered fine when expected but no short orders are ever triggered eventhough there are several instances where they should be triggered.

    Any idea why ?

    #2
    Hello SuneSorgenfrei,

    Thanks for your post.

    When a script is not working as expected, please check the NinjaTrader control center's "Log" tab for any error messages.

    If you are using the managed approach you will probably see error messages about violating "Internal Order Handling Rules that Reduce Unwanted Positions". In the managed approach you cannot submit both a long and a short order at the same time. Please see: https://ninjatrader.com/support/help...antedPositions

    Here is a link to the Managed Approach: https://ninjatrader.com/support/help...d_approach.htm

    Please note that limit type orders, in the managed approach are automatically canceled if not filled in the bar they submitted. You can either resubmit on a bar by bar basis or use the method overload that allow a "Live until canceled" bool where you would need to ensure that the order is filled or you cancel the order using CancelOrder().
    References:



    One way that you can handle this simply and stay in the managed approach is to use market orders instead of limit orders. Your script would determine the two levels needed and then if price touches either level, submit a market order. It just takes a small amount of logic to do this.

    Alternately, you may want to use the UnManaged Approach "The Unmanaged approach is reserved for VERY EXPERIENCED programmers. In place of the convenience layer that the Managed approach offered, the Unmanaged approach instead offers ultimate flexibility in terms of order submission and management. This section will discuss some of the basics of working with Unmanaged order methods."
    Reference: https://ninjatrader.com/support/help...d_approach.htm
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Hi Paul

      Thanks for your input. I was not aware of that restriction in the manged approach. I will use unmanged for this strategy.

      Comment


        #4
        Hi Again

        So now the strategy works but I am still facing 1 problem. All the sudden the strategy stops working without any errors posted to the log.
        The Strategy is enabled but stops generating new orders. Any idea why this could be ? Click image for larger version

Name:	StrategyStopsLog.JPG
Views:	104
Size:	78.7 KB
ID:	1186945Click image for larger version

Name:	StrategyStops.JPG
Views:	101
Size:	120.8 KB
ID:	1186946

        Comment


          #5
          Hello SuneSorgenfrei,

          Thanks for your post.

          It looks like your log does show an error about an incorrectly placed stop order and advises that subsequent orders may be ignored.

          At this point, you would need to start debugging the strategy to understand why it is placing orders at an incorrect value.

          We recommend using print statements and in this case, you would want to print out the bar time, the price value being determined to place the order.

          Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm
          Paul H.NinjaTrader Customer Service

          Comment


            #6
            Hi Paul

            Here is the error:
            Strategy 'Strat212/254396802': An order has been ignored since the stop price ‘85.22’ near the bar stamped ‘20-01-2022 03:00:00’ i
            s invalid based on the price range of the bar. This is an invalid order and subsequent orders may also be ignored.
            The strange thing is though that the last order is on the 20.01.2022 21:00 (9 PM) so many hours and orders later. There is no error for that point in time only the one much earlier.

            Comment


              #7
              Hello SuneSorgenfrei,

              Thanks for your reply.

              After re-reading the error, this is most often seen with Standard Order Fill Resolution where the order fills are estimated using "virtual bars" and formation of the virtual bars indicate that the order cannot be filled with that price action. Setting Order Fill Resolution to High and using a 1 tick data series as the fill series or submitting orders to a single tick data series would be a good way to avoid those types of errors. Please retest by using High order fill set to 1 tick.


              Paul H.NinjaTrader Customer Service

              Comment


                #8
                Hi Again

                I did change the settings but no change in the way the strategy behaves
                Click image for larger version  Name:	OrderFillResolution.JPG Views:	0 Size:	231.8 KB ID:	1187059
                Last edited by SuneSorgenfrei; 01-25-2022, 02:00 PM.

                Comment


                  #9
                  Hello SuneSorgenfrei,

                  Thanks for your reply.

                  I would suggest returning to the debugging process using print statements.

                  You can add a print statement just ahead of the code that places orders and print the value of variables used in the order.

                  Here is a link to our debugging tips: https://ninjatrader.com/support/help...script_cod.htm
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi
                    I solved the issues. First issue it did not take the gap in between 11pm 00:30 CET into consideration so I had orders hanging that never got filled and that stopped the loop.
                    Kind of same issue between 05.30 and 09.30 CET there is no data today in CL 03-22, so I build a disaster recovery function that resets the strategy if a hole in the data is found.
                    Thanks for your help. Click image for larger version

Name:	PartIssue.JPG
Views:	122
Size:	34.3 KB
ID:	1187088

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by fx.practic, 10-15-2013, 12:53 AM
                    5 responses
                    5,404 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by Shai Samuel, 07-02-2022, 02:46 PM
                    4 responses
                    95 views
                    0 likes
                    Last Post Bidder
                    by Bidder
                     
                    Started by DJ888, Yesterday, 10:57 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post DJ888
                    by DJ888
                     
                    Started by MacDad, 02-25-2024, 11:48 PM
                    7 responses
                    159 views
                    0 likes
                    Last Post loganjarosz123  
                    Started by Belfortbucks, Yesterday, 09:29 PM
                    0 responses
                    8 views
                    0 likes
                    Last Post Belfortbucks  
                    Working...
                    X