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

Exiting a long position

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

    Exiting a long position

    I can successfuly enter a long position using:
    EnterLong(1, "Signal1");

    But I can't seem to exit the position using:
    ExitLong("Signal1");

    The ExitLong appears to be ignored. What's the best way to exit a long position after using EnterLong?

    #2
    Welcome to our forums!

    Does this occur in real-time, or in backtesting?

    Have you enabled the TraceOrders = true from the Initialize() method of your code? If so, does this specifically say the order was ignored due to the internal handling rules, or does it not get called at all?
    MatthewNinjaTrader Product Management

    Comment


      #3
      I'm using Market Replay to develop my strategy so I assume it's equivalent to real-time.

      I just enabled TraceOrders and it appears that the ExitLong does not get called at all. The long position remains in effect until ExitOnClose.

      I can close the long position by using EnterShort(1, "Signal2"), but of course a short position is opened. As expected, the short position remains in effect until ExitOnClose.

      Thank you for your very prompt reply to my original question.

      Comment


        #4
        You're using the same code block to call EnterShort that you were trying ExitLong? That seems very odd if the Order Entry Handling rules aren't being hit for your Exit.

        How many different entries do you have? If you simply to just 1 long position, can you exit the single long position using the same methods?

        What happens if you remove the signal names and just work with EnterLong() and ExitLong()
        MatthewNinjaTrader Product Management

        Comment


          #5
          One problem solved but another persists.

          I used some Print() statements to track the logic in my strategy and discovered that I had a logic error that was preventing things from being executed properly. I apologize for not having been more thorough.

          However, one problem does appear to persist. Now that I can exit the long, sometimes I want to also enter a short and I'm noticing some strange behavioral differences between running the strategy live as compared to loading the strategy onto a chart with existing data.

          The strategy appears to work properly when loaded onto a chart with pre-existing data but when running live using Market Replay, two short positions appear to be entered. This happens when there is a long position and the code executes ExitLong() followed by EnterShort() within the same OnBarUpdate().

          Comment


            #6
            Hi,

            Glad to hear you got that working.

            If you're using the EnterLong/Short methods, you do not need to use the Exit method to reverse the position. Should the strategy detected you are already in a Long position and you call EnterShort, it will automatically close your long position and put you in a short.

            However, If you call both ExitLong and EnterShort, you may end up with 3 orders which will enter you in a second short position.

            You would only want to use the Exit method should you want to flatten the position and not take another position at that time.
            MatthewNinjaTrader Product Management

            Comment


              #7
              I understand. Thank you very much for your patience and thorough explanation.

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by gravdigaz6, Yesterday, 11:40 PM
              1 response
              7 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by MarianApalaghiei, Yesterday, 10:49 PM
              3 responses
              10 views
              0 likes
              Last Post NinjaTrader_Manfred  
              Started by XXtrader, Yesterday, 11:30 PM
              0 responses
              4 views
              0 likes
              Last Post XXtrader  
              Started by love2code2trade, 04-17-2024, 01:45 PM
              4 responses
              28 views
              0 likes
              Last Post love2code2trade  
              Started by funk10101, Yesterday, 09:43 PM
              0 responses
              9 views
              0 likes
              Last Post funk10101  
              Working...
              X