Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Add to- or Reverse Trade Position Not Working

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

    Add to- or Reverse Trade Position Not Working

    I have a strategy which trades proprietary signals I receive. The signal is in the form of a sum, called signalSum (integer), which can range from -4 to 4.
    For example:
    It receives a sum of -2, and will open a short position for 2 contracts
    Then it receives a sum of -1 and opens a third short contract
    Then it receives a sum of 2 and closes two of the short contracts, leaving 1 short
    Then it receives a sum of 3 and opens a long position for 2 (-1 + 3 = 2) contracts (this should automatically close the short position)

    I get logging that shows it is in the code where it is doing a reversal, but the actual trade never happens -the existing position just remains.

    Relevant code is attached; 152 lines seems a bit long to post inline.

    No issues with opening a position, but I have seen problems with reversing and adding to position. Both of those are handled starting on line 136 [if (needsOpen) ].

    It's my understanding that if call EnterLong() it will automatically close any short position and vice versa.

    But the Enter calls do nothing, regardless of whether I am adding lots or reversing.

    Also attached is a sample Log output from today.
    Attached Files

    #2
    Hello DerkWehler,

    Have you tried using TraceOrers to check if the order is being ignored?
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello DerkWehler,

      Have you tried using TraceOrers to check if the order is being ignored?
      No. I will look into it.
      Did you look over the code and not see any issues?
      Last edited by DerkWehler; 08-21-2024, 03:57 PM.

      Comment


        #4
        Hello DerkWehler,

        Yes however without running the code I couldnt really say what may be happening. Using trace orders would be a good first step to just see if the orders are being ignored for any reason.
        JesseNinjaTrader Customer Service

        Comment


          #5
          I enabled TraceOrders = true;

          But I am not seeing anything in the output windows except my own debugging. I added a few lines though:

          EA #0 - ES 09-24 - [ParseResponse] - 06:35:53 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          Signal received @ 22-Aug-24 09:33:00 : Sum = 1

          EA #0 - ES 09-24 - [OnBarUpdate] - 06:35:53 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
          Occam Signal Received @ 22-Aug-24 06:35:53 - New Direction = Long, Signal Sum = 1, old Pos Sum = 2, New Pos Sum = 3, QuantityMult = 1

          Signal to Increase: ENTERING ADDITIONAL 1 CONTRACTS LONG
          Before EntryLong/Short(), Position: Long, Quantity: 2
          Attempting to enter Long position for 1 contracts
          After EnterLong/Short(), Position: Long, Quantity: 2


          From this code:

          Code:
          /// Open new pos (or reverse)
          if (needsOpen)
          {
             Log(logStr);
             Log($"Before EntryLong/Short(), Position: {Position.MarketPosition}, Quantity: {Position.Quantity}");
             Log($"Attempting to enter {signalDir} position for {qty} contracts");
             if (signalDir == "Long")
                EnterLong(qty, "Long");
             else
                EnterShort(qty, "Short");
             Log($"After EnterLong/Short(), Position: {Position.MarketPosition}, Quantity: {Position.Quantity}");
             lastTradeDir = signalDir;
          }
          ​
          So not seeing any trace order stuff and it plainly is not opening my additional lot.​

          Comment


            #6
            Hello DerkWehler,

            After enabling that did you remove and re apply the strategy? Any changes to SetDefaults or OnStateChange requires removing and re applying the strategy.

            Also I see that you used a variable signalDir, based on the print that should have been ignored. The position was long before that condition, the signalDir was long so you tried to enter long again.

            Before EntryLong/Short(), Position: Long, Quantity: 2
            Attempting to enter Long position for 1 contracts​

            To do another long you need to increase the entries per direction to allow for that
            JesseNinjaTrader Customer Service

            Comment


              #7
              Oh.. EntriesPerDirection... I created this using another strategy as template, but the other one only entered once. There is really no limit in the strategy. How do I make it infinite; just make it large?

              The strategy is always adjusting... like "Open 2 Long, now add 1, now reduce by 2, now reverse for 1, now add 1 more Short, now add 2 Short, now reduce by 4 (close)", ...etc

              After adjusting EntriesPerDirection, I assume the rest of the code is correct. The progression above, for example, it does as:

              EnterLong(2, "Long");

              EnterLong(1, "Long");

              ExitLong(2, "SigReduce", "Long");

              EnterShort(1, "Short"); // Will close long position and go short

              EnterShort(1, "Short");

              ​EnterShort(2, "Short");

              ExitShort("SigFlat", "Short");
              ​​​​​
              Correct?

              Comment


                #8
                Hello DerkWehler,

                Yes you would have to have some kind of idea how many entries it will place and then increase the value to that amount.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  Originally posted by NinjaTrader_Jesse View Post
                  Hello DerkWehler,

                  Yes you would have to have some kind of idea how many entries it will place and then increase the value to that amount.
                  Okay, I will just set it to be large. That seems to solve the problem, thank you.

                  Comment


                    #10
                    I have converted my 2 strategies over to Unmanaged. But I am having trouble with the one that opens additional / closes partial positions. I have put in quite a bit of logging, but still cannot figure out why it behaves as it does. Here is a sample of how it is meant to run; it operates on a signal sum, so:

                    Sum = +1 : Open one contract long
                    Sum = +1 : Open another long contract
                    Sum = -1 : Close one long
                    Sum = -2 : Close remaining long, and open one short
                    Sum = +1 : Close short contract (flatten)
                    (etc)

                    But it seems to close out everything instead. I have attached entire strategy.
                    If you run it, there will be buttons at the bottom of the chart. Press "Increase Sum" or "Decrease Sum".
                    Press Increase once and wait for it to open. Then press it again, and it will open a second contract, but then seems to immediately close.
                    I have TraceOrders enabled, but still have not been able to figure what is going on.

                    Can anyone tell me why / what I have done wrong?
                    Attached Files
                    Last edited by DerkWehler; 09-10-2024, 06:11 AM. Reason: Update source

                    Comment


                      #11
                      Hello DerkWehler,

                      By looking at the script I would say that a good starting point would be to trace which specific part of the logic is being called when the entire position is closed. I see that you have a close position method that does have the ability to close out the whole position so it may just be that you need to adjust one of the method calls to avoid that.
                      JesseNinjaTrader Customer Service

                      Comment


                        #12
                        Thanks for your reply.

                        Did you try running it?

                        I have put quite a lot of log lines in, and have not been able to derive what it is doing; how it is closing. There are log lines for when it tries to change the SL & TP quantity (line 392), but I don't know if the TP gets set... and then it just closes (no where near the target price that was previously set or just modified).

                        I don't know what causes the close, when I am actually increasing lots, not decreasing or closing.

                        Comment


                          #13
                          Hello DerkWehler,

                          I have not run the script as I cannot debug it for you but I can use the script to get an idea of what type of logging you added.

                          Based on your reply the goal at this point should be to find out what is happening specifically. You mentioned that you are unsure how it is closing so finding that out would be the primary goal so we know what code is involved. To find out which part of the logic is working at that time you would need to add a print into each area where you are submitting or modifying orders. After doing that you would run the script in the use case where the problem happens and then collect the prints. That will let you know in what order your logic was called so you can have a better idea of what logic was working when the position gets closed. Knowing that will let us know what part of the code we need to look at.
                          JesseNinjaTrader Customer Service

                          Comment


                            #14
                            I was hoping to have someone look at the unmanaged code logic, to see if I had made some incorrect assumption in how I am using it to open and close (partial or full) trades.

                            Or perhaps some flaw in my logic due to the multi-threaded nature of order placement.

                            But if you cannot help, thanks anyway. I'll keep banging my head against the wall until it gives :-)

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Scalper1969, 10-09-2024, 10:37 AM
                            14 responses
                            49 views
                            0 likes
                            Last Post NinjaTrader_Erick  
                            Started by Thomas79, Today, 02:03 PM
                            0 responses
                            6 views
                            0 likes
                            Last Post Thomas79  
                            Started by Mirasol, Today, 11:32 AM
                            2 responses
                            19 views
                            0 likes
                            Last Post Mirasol
                            by Mirasol
                             
                            Started by MikM45, 10-12-2024, 01:12 PM
                            1 response
                            19 views
                            0 likes
                            Last Post NinjaTrader_LuisH  
                            Started by CDXTrader, Today, 01:46 PM
                            0 responses
                            8 views
                            0 likes
                            Last Post CDXTrader  
                            Working...
                            X