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

infinite loop while placing order in OnPositionUpdate

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

    infinite loop while placing order in OnPositionUpdate

    Hi devs,

    i've been struggling with a snippet for a few days and here i come looking for some clarifications.

    I'm not going to show the full code but just the "fun" part.

    The first code i'm showing is working and it is this:

    Code:
    protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                int quantity, Cbi.MarketPosition marketPosition)
            {
    
                //... other part above tested working
    
                if (position.MarketPosition == MarketPosition.Flat && trading_time){ //trading is a range of hours
    
                    Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    
                    if (lastTrade.ProfitPips > 0){ //if last position was profitable open in the same direction
    
                        if (lastentry.Name == "entrylong"){ //lastentry is set in OnOrderUpdate like is shown in all examples of code here XD
    
                             EnterLong(1, "entrylong");
                        }
                     }
    }

    Trivial code right?
    I wanted to test an opposite situation where "if the last position is profitable then open a reverse short". Well, The platform freezes and i need to kill it with task manager.

    The code crashing is this:

    Code:
    protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                int quantity, Cbi.MarketPosition marketPosition)
            {
    
                //... other part above tested working
    
                if (position.MarketPosition == MarketPosition.Flat && trading_time){ //trading is a range of hours
    
                    Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    
                    if (lastTrade.ProfitPips > 0){ //if last position was profitable open REVERSED
    
                        if (lastentry.Name == "entrylong"){ //lastentry is set in OnOrderUpdate like is shown in all examples of code here XD
    
                             EnterShort(1, "entryshort"); //changing the direction here blows up the ninjatrader backtest.
                        }
                     }
    }
    i hope to get an answer and thanks anyway for reading.

    kind regards,

    ross94

    #2
    Hello ross94,

    The only difference is that EnterShort() is called and not EnterLong()?

    I would expect the issue is in a different part of the code that is not included.

    I've added your code to a simple test script and I'm able to run this without any issues.

    Are you experiencing crashing with this test script?
    Attached Files
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi ChelseaB,

      thanks for reply! Tbh your code works smooth like butter as i thought.
      So i put my onPositionUpdate in your example and still it freezes the application.
      I'll copy paste the full "operative" part for you here.

      Code:
      protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                  int quantity, Cbi.MarketPosition marketPosition)
              {
      
                  if (position.MarketPosition == MarketPosition.Flat && trading_time){
      
                      Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
      
                      if (lastTrade.ProfitPips > 0){
      
                          if (lastentry.Name == "entrylong"){
      
                             lastentry = EnterShort(1, "entryshort");
      
                          }else if(lastentry.Name == "entryshort"){
      
                             lastentry = EnterLong(1, "entrylong");
                          }
                       }else{
      
                          if (lastentry.Name == "entrylong"){
      
                             lastentry = EnterLong(1, "entrylong");
      
                          }else if(lastentry.Name == "entryshort"){
      
                             lastentry = EnterShort(1, "entryshort");
                          }
      
      
                       }
      
      
                  }
              }
      Now please try it yourself, replace this OnPositionUpdate in your code example Ross94Test. It won't run any backtest. I tried it on FDAX but whatever instrument is fine... it will end up somehow in an infinite loop because the application never stops loading the backtest and at some point i get "no answer".

      LAST THING: what i noticed is that this code doesn't run the backtest if the OrderFillResolution in Standart! but you put "High" and whatever type and value it will work.

      Kind Regards,

      Ross94
      Last edited by ross94; 06-30-2019, 10:58 AM.

      Comment


        #4
        Hello ross94,

        Look at the logic you have just posted.

        You are forever back and forth going from long to short. This is a broken loop and is bad logic and will make infinite orders.

        However, the code you posted earlier is fine.
        Chelsea B.NinjaTrader Customer Service

        Comment


          #5
          No it is not a broken logic at all.
          It is just supposed to open a new order right after the previous trade is closed depending on the result. If it is a profit reverse it, if it is a loss open in the same direction. There's no other way to accomplish this task?

          And it works exatly as i want if i backtest "orderfillresolution High, Tick, 1". It does what it is meant to do, while in stardart orderfillresolution it freezes.

          If it was a broken logic it should never work neither stadart or high order resolution.

          Kind Regards
          Last edited by ross94; 06-30-2019, 11:50 AM.

          Comment


            #6
            Hello ross94,

            I'm seeing that if the last order was entrylong place the entryshort order, if the order is entryshort place the entrylong order.

            There doesn't seem anything to limit this and I would expect an infinite amount of orders to be placed.

            Is there code you haven't included to prevent this?
            Chelsea B.NinjaTrader Customer Service

            Comment


              #7
              Hi ChelseaB,

              the new order is placed when the marketPosition is FLAT which means it places a new order everytime it hits a stoploss or profit. It's only 1 order per time.

              Go test this in the example your created, it will work without problems with "orderfillresolution standard":

              Code:
                
              protected override void OnPositionUpdate(Cbi.Position position, double averagePrice,
                          int quantity, Cbi.MarketPosition marketPosition)
                      {
              
                          if (position.MarketPosition == MarketPosition.Flat && trading_time && lastentry != null){
              
                              Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
              
                              if (lastTrade.ProfitPips > 0){ //IF LAST TRADE IS PROFITABLE OPEN A NEW TRADE IN THE SAME DIRECTION
              
                                  if (lastentry.Name == "entrylong"){
              
                                     lastentry = EnterLong(1, "entrylong");
              
                                  }else if(lastentry.Name == "entryshort"){
              
                                     lastentry = EnterShort(1, "entryshort");
                                  }
                               }else{ //
              
                                  if (lastentry.Name == "entrylong"){ //IF IT WAS A LOSS OPEN REVERSE
              
                                     lastentry = EnterShort(1, "entryshort");
              
                                  }else if(lastentry.Name == "entryshort"){
              
                                     lastentry = EnterLong(1, "entrylong");
                                  }
              
              
                               }
              
              
                          }
                      }
              Run this code in your example. It will work without any mistake. Try
              The only difference with the one crashing is the code opening the positions "enterlong" and "entershort", not the code itself. The one crashing (above) reverses the position if it is profitable, this one reverses the position only if it is a loss.

              My question is that, one works the other one doesn't, but there's no difference in the code scheme. Why is it crashing?

              Kind Regards,

              Ross94

              Comment


                #8
                I found out something in the Manual but still i don’t get how to make it work as i want.

                Link: https://ninjatrader.com/support/help...d_approach.htm

                Under “’Internal order handling to reduce unwanted positions’

                Thanks for the replies

                Ross94

                Comment


                  #9
                  Hello Ross94,

                  I disagree. With the else, if the trade is not profitable the position is still reversed. This looks like it could happen continuously depending on how the exits occur. I could likely take this logic and make the exit immediate and use this script to completely max out the processor.

                  To export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
                  1. Click Tools -> Export -> NinjaScript...
                  2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
                  3. Click the 'Export' button
                  4. Enter a unique name for the file in the value for 'File name:'
                  5. Choose a save location -> click Save
                  6. Click OK to clear the export location message
                  By default your exported file will be in the following location:
                  • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
                  Below is a link to the help guide on Exporting NinjaScripts.
                  http://ninjatrader.com/support/helpG...-us/export.htm
                  Chelsea B.NinjaTrader Customer Service

                  Comment


                    #10
                    Hi ChelseaB,

                    Well, the point is that i don't get how to exatly enter right after a position is closed. Is it right to wait for MarketPosition.Flat? Is there a better approach for this?
                    My logic is simple, if the last trade is profitable open reversed right there where it took the target profit.

                    Thanks

                    Ross94

                    Comment


                      #11
                      Hello Ross94,

                      I'm not able to assist you with correcting logic, however this thread will remain open for any community members that would like to assist.

                      But what I can do is assist with adding prints to a script to see what is occurring.



                      Unfortunately, with the NinjaTrader Support team it is against our policy to create, debug, or modify, code or logic for our clients. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                      You can also contact a professional NinjaScript Consultant who would be eager to create or modify this script at your request or assist you with your script. The NinjaTrader Ecosystem has affiliate contacts who provide educational as well as consulting services. Please let me know if you would like our business development follow up with you with a list of affiliate consultants who would be happy to create this script or any others at your request.
                      Chelsea B.NinjaTrader Customer Service

                      Comment


                        #12
                        Originally posted by ross94 View Post
                        No it is not a broken logic at all.
                        It is just supposed to open a new order right after the previous trade is closed depending on the result. If it is a profit reverse it, if it is a loss open in the same direction. There's no other way to accomplish this task?xxnx redtube pornktube

                        And it works exatly as i want if i backtest "orderfillresolution High, Tick, 1". It does what it is meant to do, while in stardart orderfillresolution it freezes.

                        If it was a broken logic it should never work neither stadart or high order resolution.

                        Kind Regards
                        thanks for reply! Tbh your code works smooth like butter as i thought.
                        So i put my onPositionUpdate in your example and still it freezes the application.
                        I'll copy paste the full "operative" part for you here.
                        Last edited by amekassa; 07-03-2019, 02:02 PM.

                        Comment

                        Latest Posts

                        Collapse

                        Topics Statistics Last Post
                        Started by Waxavi, Today, 02:10 AM
                        1 response
                        16 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by Kaledus, Today, 01:29 PM
                        5 responses
                        13 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by Waxavi, Today, 02:00 AM
                        1 response
                        11 views
                        0 likes
                        Last Post NinjaTrader_LuisH  
                        Started by alifarahani, Today, 09:40 AM
                        5 responses
                        23 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Started by gentlebenthebear, Today, 01:30 AM
                        3 responses
                        16 views
                        0 likes
                        Last Post NinjaTrader_Jesse  
                        Working...
                        X