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

Switching positions in strategy

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

    Switching positions in strategy

    Let's say I have the following code in my onBarUpdate() method:

    Code:
    if (Position.MarketPosition == MarketPosition.Long && myLongTrigger) {
       exitOrder = ExitLong(0, DefaultQuantity, EXIT_CONDITION, LONG_POSITION);
    }
    						
    if (Position.MarketPosition == MarketPosition.Short && myShortTrigger) {
      exitOrder = ExitShort(0, DefaultQuantity, EXIT_CONDITION, SHORT_POSITION);
    }
    Now what happens here is that if my long signal is triggered and I'm short then it will trigger an exit. When I'm long and a short signal is triggered it'll exit as well. So far so good.

    My problem is that the ensuing regular long triggers are not being called on the same candle. The reason for that is that I'm checking for the current market position.

    Code:
    if (myLongTrigger) {
       if (Position.MarketPosition == MarketPosition.Flat) {
    	entryOrder = EnterLong(DefaultQuantity, LONG_POSITION);
       }
    }
    					
    else if (myShortTrigger) {
       if (Position.MarketPosition == MarketPosition.Flat) {
         entryOrder = EnterShort(DefaultQuantity, SHORT_POSITION);
       }
    }
    Now, I know why this is happening. The strategy cannot guarantee that the previous order has been completely exited. Thus the inverse entry fails as MarketPosition.Flat == false.

    So this leads me to my question. Is there a way in NT7 to swap positions? Basically when I'm already in a trade and the inverse signal is being triggered I would like to exit and immediately drop into an inverse position. Hope this makes sense.

    Perhaps I should instead check for entryOrder == null as opposed what I'm doing right now. I have seen both examples in the past and I prefer MarketPosition.Flat as my 'all clear' flag. But I'm open to any approaches that allow me to swap positions quickly within the same minute candle.

    Thanks in advance.

    #2
    Hello molecool,

    Using the Managed Approach if you are currently long and then you call an enter short position NinjaTrader will automatically send an order to exit your position, then send an order to enter you in your short position. So if you would like to reverse or switch positions you would just want to call your short entry order.
    JCNinjaTrader Customer Service

    Comment


      #3
      Interestingly that very thought just popped into my mind while I was taking a shower - LOL :-)

      I'm going to try it and report back if I run into any trouble. Many thanks for the timely response.

      Comment


        #4
        Thanks

        Working fine now - thanks.

        Comment


          #5
          I have a question that maybe you have solved?
          I want to reverse the position if the original position is closing due to
          being stopped out.
          Where is the best place to capture that situation?

          Comment


            #6
            You can track the IOrder state for your Stop order and once that order has been filled, you can submit an EnterLong/Short

            Please see our Reference Sample on Monitoring Stop-Loss and Profit Target orders:

            MatthewNinjaTrader Product Management

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by elirion, Today, 09:32 PM
            0 responses
            5 views
            0 likes
            Last Post elirion
            by elirion
             
            Started by cre8able, Today, 09:15 PM
            1 response
            7 views
            0 likes
            Last Post bltdavid  
            Started by cummish, Today, 08:43 PM
            0 responses
            13 views
            0 likes
            Last Post cummish
            by cummish
             
            Started by Option Whisperer, Today, 07:58 PM
            4 responses
            21 views
            0 likes
            Last Post Option Whisperer  
            Started by ETFVoyageur, 05-07-2024, 07:05 PM
            13 responses
            87 views
            0 likes
            Last Post ETFVoyageur  
            Working...
            X