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

i need help to get rid of duplicate positions.

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

    i need help to get rid of duplicate positions.





    people with nt,




    in september of 2019 i decided to adapt some strategies of mine to nt. after an unbelievable amount of hard work, i now finally have a number of strategies that i'm ready to trade with real funds on nt and yesterday i created two charts with one strategy each and enabled them.


    however, today one strategy reversed from a long to a short position and it ended up with two contracts instead of one. this is an error that i thought i had solved back in october of last year when i was evaluating nt on simulator but it has appeared again. consequently, i have suspended all automated trading until i can solve this definitely.



    most of my strategies have one same general structure, i have one condition to enter a short position and its corresponding condition to exit this position. i also have a condition to enter a long position and its corresponding condition to exit this position.



    this issue with duplicate positions will happen if and when both the condition to exit a position and to enter a position in the opposite direction become true at the same time at the close of one same bar.



    for example (this is not what is happening in my code but should be useful to illustrate this situation), if price is 2% lower than the highest high of the last 50 bars, the strategy should exit all long positions. and if price is 2.3% lower than the highest high of the last 50 bars then the strategy should enter a short position. there will be a lot of cases when at the end of one bar both of these conditions will be true at the same time and nt then ends up with a position of twice the size it should be as it executes both an exit and a reversal order.



    in october of 2019 i thought i had solved this problem by adding checks for positions status to my code, and at least on backtests and optimization processes it seemed like this was enough:


    // Set 1
    if ( conditions for short position
    && (Position.MarketPosition != MarketPosition.Short) )
    {
    EnterShort(Convert.ToInt32(Posi), entry1 );
    }


    // Set 2
    if ( (Position.MarketPosition == MarketPosition.Short)
    && conditions to exit short positions )
    {
    ExitShort(Convert.ToInt32(Posi), entry1 );
    ExitShort(Convert.ToInt32(Posi), entry2 );
    }


    // Set 3
    if ( conditions for long position
    && (Position.MarketPosition != MarketPosition.Long) )
    {
    EnterLong(Convert.ToInt32(Posi), entry1 );
    }


    // Set 4
    if ( (Position.MarketPosition == MarketPosition.Long)
    && conditions to exit long positions )
    {
    ExitLong(Convert.ToInt32(Posi), entry1 );
    ExitLong(Convert.ToInt32(Posi), entry2 );
    }





    i now realize that this is not working as it is intended, so i ask from the people with nt, ¿how could i trade my strategies automated and have nt only enter positions in the appropriate size? ¿are there better checks than those i incorporated? ¿is it possible to delay exit orders by fractions of a second so that entries - reversals could execute first? ¿would nesting these conditions inside of one another solve these problems?


    very well, this is a crucial matter for me, if i'm not able to solve these issues i can't trade my strategies automated and would really have no use for nt anymore. thanks, regards.
    Last edited by rtwave; 04-16-2020, 02:15 PM.

    #2
    Hello rtwave,

    Thank you for the post.

    It sounds like you made a correct assement of the situation, if the entry and exit are called together that creates the situation you described. From what I can see the most simple solution is likely to just use else statements. If this is in the builder you can unlock the code to do this or use the other suggestion I made below this:

    Code:
    // Set 2
    if ( (Position.MarketPosition == MarketPosition.Short)
    && conditions to exit short positions )
    {
    ExitShort(Convert.ToInt32(Posi), entry1 );
    ExitShort(Convert.ToInt32(Posi), entry2 );
    }
    
    [B]else [/B]
    
    // Set 3
    if ( conditions for long position
    && (Position.MarketPosition != MarketPosition.Long) )
    {
    EnterLong(Convert.ToInt32(Posi), entry1 );
    }

    That will only allow one of the two conditions to happen in the same bar. The first condition will become true, otherwise if not it will check the second condition. You can order the sets in the way you want the logic to be checked, if it should reverse as priory over exiting you could switch the order of the two sets.

    If you still need the sets separate and checked in the same bar you would have to use a variable to control that. In set 2 you would set a variable to true if you exit. In set 3 as part of the condition you check if the variable is false, only if it is false will the condition work and the previous condition has set it to true preventing the double.

    You can also use a variable logically if you want to enter on the following bar, in the exit action set a variable to true and then on the next bar the set 3 could work. This only works if you were to change the order, set 2 would become set 3 and set 3 would become set 2 so it has to wait a bar to check the result of setting the variable.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3




      Jesse,



      thanks.



      i have been trying to figure out how to fix my code all day today.


      from the possible solutions you suggested, i think that a control variable could be the best. what i have done is place the sets for both entries at the top, i have also declared two variables, which, in the case of short positions, i set to true at the condition for an entry and to false at the condition for a long entry (or reversal).




      private bool ordinaryshortexit = false;


      .....


      // Set 1
      if ( conditions for short position
      && (Position.MarketPosition != MarketPosition.Short) )
      {
      EnterShort(Convert.ToInt32(Posi), entry1);
      ordinaryshortexit = true;
      ordinarylongexit = false;
      }


      // Set 3
      if ( conditions for long position
      && (Position.MarketPosition != MarketPosition.Long) )
      {
      EnterLong(Convert.ToInt32(Posi), entry1);
      ordinarylongexit = true;
      ordinaryshortexit = false;
      }



      // Set 2
      if ( (Position.MarketPosition == MarketPosition.Short)
      && ordinaryshortexit == true
      && conditions to exit short positions )
      {
      ExitShort(Convert.ToInt32(Posi), exit1, entry1);
      ExitShort(Convert.ToInt32(Posi), exit2, entry1);
      }



      i don't know whether this will work and it seems to me like this problem with the code will not occur if one is automating on simulator. this is really bad, i don't know what else to do, i will just have to test this on simulator as i would never risk any real money as long as such an error the code has not been fixed.

      Last edited by rtwave; 04-21-2020, 07:13 AM.

      Comment


        #4
        Hello rtwave,

        You have the right idea however you will need to test it to make sure it works. We can walk through the logic here to try and better visualize it but ultimately it would be best to run this on sim and see what happens.

        How you have it now will be executed like this:

        Flat -> Set1=true -> enter short -> sets ordinaryshortexit=true -> next bar -> Set2 checks ordinaryshortexit and is now true -> exit

        You may additionally need to reset the variables when you call the exit logic:

        ordinaryshortexit = false;

        As long as the variables control when the exit logic is "avaliable" you can prevent the exit from being called in the same bar as the opposite entry. Even if the overall conditions are true at the same time because the variable is false that prevents the whole condition from being true.


        I look forward to being of further assistance.



        JesseNinjaTrader Customer Service

        Comment


          #5




          Jesse,




          i have continued to work on this all through the weekend and every day since last week.


          you were right from the beginning, the best structure to use is if - else - if.


          i have been adjusting my strategies to this structure and currently have this setup:


          if entry in one direction

          else

          if opposite exit



          this structure prioritizes entries and reversals and eliminates duplicate positions, but at the same time it does allow for ordinary exits if the conditions call for them.


          i realized that i also have to use this structure with two indicator variables that i have in every strategy that should be true as long as a downtrend (uptrend) is in progress. consequently i have been implementing a similar logic for those variables.


          now i have to run a lot of tests and optimization processes because the results from these corrected versions of my strategies are slightly different than the ones that the previous code generated (even better if that was still possible). i hope i can have my strategies ready and trading with real funds as soon as possible. and i really hope there will be no more surprises with unwanted performance from my code. i'm getting good at coding and do like it but i would prefer if ninjatrader was easier to master for non experts and i could have been trading automated months ago.

          Last edited by rtwave; 04-21-2020, 07:09 AM.

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by adeelshahzad, Today, 03:54 AM
          4 responses
          25 views
          0 likes
          Last Post adeelshahzad  
          Started by merzo, 06-25-2023, 02:19 AM
          10 responses
          823 views
          1 like
          Last Post NinjaTrader_ChristopherJ  
          Started by frankthearm, Today, 09:08 AM
          5 responses
          17 views
          0 likes
          Last Post NinjaTrader_Clayton  
          Started by jeronymite, 04-12-2024, 04:26 PM
          3 responses
          43 views
          0 likes
          Last Post jeronymite  
          Started by yertle, Today, 08:38 AM
          5 responses
          19 views
          0 likes
          Last Post NinjaTrader_BrandonH  
          Working...
          X