Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem Executing exits only on next bar

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

    Problem Executing exits only on next bar


    Hey,

    I need help.

    I added manual code that is modifying StopLoss after Profit level activation. It's work. I'm calculating OnEachTick. When I'm back testing, The problem is that when the condition met it's exit only on the next bar(Minute). why It's not closing the position immediately?
    It seems that setStoploss and exitlong()/short is working only on bar per minute and can't execute in the on each bar... why it's waitting until the next bar (in my case 1 minute) to exit?

    Rami


    #2
    Hello RamiRK86,

    Backtests are run OnBarClose so the condition is only evaluated at the close of the bar. Fills are also only evaluated at the close of the bar so the targets will also fill on bar close.



    Comment


      #3
      Thank you. I have another problem.
      I have this code that should exit all positions:
      I have 4 kinds of positions, 2 shorts and 2 longs, only one work in the same time.
      I tried to add another IF to determine which position ID is open and to call the exit with it's name but it doesn't work.

      any way, my code look like this:

      if (Position.MarketPosition == MarketPosition.Long)

      ExitLong("ExitLongOrginal", "Long Entry Orginal");

      ExitLong("ExitLongNew", "Long Entry New");

      if (Position.MarketPosition == MarketPosition.Short)

      ExitShort("ExitShortOrginal", "Short Entry Orginal");

      ExitShort("ExitShortNew", "Short Entry New");


      the problem is that sometimes the exit done twice at the same time and instead of close the opened position it buys the opposite position (for example if the position was short, now when the condition of exit met it do it twice, so one exit close the short and the second exit buys new position long).

      I'm trying to understand the problem, and this information will help me understand more:

      I want to understand whats happen when there is no Long Entry New​ open and the script arrives to ExitLong("ExitLongNew", "Long Entry New") ?
      is it will buy new position ?

      Is there another way to close all open positions?

      Do you know why sometime it closes the opened position without problem and sometimes it do the exit twice at the same time and enter new opposite position..

      Comment


        #4
        Hello RamiRK86,

        The if statements you are using do not have curly braces so only the first line of code for each if will be executed for the condition, the other 2 lines will be executed for every update. You likely need to change it to


        if (Position.MarketPosition == MarketPosition.Long)
        {
        ExitLong("ExitLongOrginal", "Long Entry Orginal");
        ExitLong("ExitLongNew", "Long Entry New");
        }
        if (Position.MarketPosition == MarketPosition.Short)
        {
        ExitShort("ExitShortOrginal", "Short Entry Orginal");
        ExitShort("ExitShortNew", "Short Entry New");
        }

        When you use signal names the exits will be ignored if an entry order with that signal name is not present. With the code you provided you would need to submit 2 entry orders with the names you specified in the exit orders as the from entry signal name. ​

        Comment


          #5
          Hey Jessy,

          Thanks for your attention to the curly braces.
          anyway, how can the Exit “buy” new position ? It should only close the previous position if it is available.

          Please look on the chart and see the two exit orders that happened at the same time. One closed the short and the second opened a long!! ( that I have immediately sold).

          IS not that strange that Exit order “buy” new contract?

          You say:

          When you use signal names the exits will be ignored if an entry order with that signal name is not present.​

          SO how it happens? Not only the Exit did not ignored but it also "Buy" new contract !!

          I want that the Exit will only close the open position and not in any circumstances buy/sell new contract.

          Pleas help.
          Last edited by RamiRK86; 05-16-2024, 07:47 PM.

          Comment


            #6
            Hello RamiRK86,

            If you have called an incorrect number of exits or mixed up the signal names for the position that could be a reason why you exit into a new position.

            For the two exits you have shown you should have two separate entries with those names, is that what you have now? The names of your orders in your code don't really make sense to me as I can't see your entry logic, this is what it would look like if we simplify the names:

            Code:
            if(some entry condition)
            {
            EnterLong("MyEntry1");
            EnterLong("MyEntry2");
            }
            if(some exit condition)
            {
            ExitLong("LongExit1", "MyEntry1");
            ExitLong("LongExit2", "MyEntry2");
            ​}

            Comment


              #7
              Hey,

              I have problem on Chart. I see the entry and exist not on the chart. time time and the price of the exit is correct, but I see the Sign above the Chart. The problem is that the sign is correct (the price and the minute is correct) and the "Chart" is in irrelevant place below that. whats the problem?

              Attached Files

              Comment


                #8
                Hello RamiRK86,

                From the image that may be due to missing data in the chart or if your PC clock is out of sync. I would suggest trying to sync the PC clock and then reload all historical data for that instrument.

                Comment


                  #9
                  Hey,

                  I have problem that when I login to my Ninjatrader account (https://account.ninjatrader.com/welcome) after I put the password and login, it's enter to my account but it takes 2 seconds and automatically log me out.

                  I have tried too much times, the same.

                  What's the problem ?

                  Rami

                  Comment


                    #10
                    OK

                    Now it's ok.

                    I have another problem, With the same account, in my first PC (Ninjatrader software) have no delay of 10 min in chart, and the other PC have delay. but both I have entered with my same account. Is it something in configuration in NinjaTrader software ?

                    Thanks,
                    Rami

                    Comment


                      #11
                      Hello RamiRK86,

                      It looks like these questions are not related to your original question in this thread, in the future please make sure to create new threads for any new questions that you have.

                      I would not be sure why there would be a delay between the two pcs, I would suggest to use the platforms Help -> email support feature on the pc that is having the delay and ask this question again. Make sure the log and trace files are selected so that our support team can check for any log messages that may relate to delayed data.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by NullPointStrategies, Yesterday, 05:17 AM
                      0 responses
                      64 views
                      0 likes
                      Last Post NullPointStrategies  
                      Started by argusthome, 03-08-2026, 10:06 AM
                      0 responses
                      139 views
                      0 likes
                      Last Post argusthome  
                      Started by NabilKhattabi, 03-06-2026, 11:18 AM
                      0 responses
                      75 views
                      0 likes
                      Last Post NabilKhattabi  
                      Started by Deep42, 03-06-2026, 12:28 AM
                      0 responses
                      45 views
                      0 likes
                      Last Post Deep42
                      by Deep42
                       
                      Started by TheRealMorford, 03-05-2026, 06:15 PM
                      0 responses
                      50 views
                      0 likes
                      Last Post TheRealMorford  
                      Working...
                      X