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

Target and Stop Loss Management

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

    #16
    Hello Cormick,

    Yes, you can use the same approach outlined in the in my MultiStepBreakevenStrategyBuilder example for long and short side.

    Some items to keep in mind:

    Order prices for Long orders would need to be flipped to be on the other side of the market to be valid for short side.

    You cannot have a pending Long entry and Short entry at the same time unless you use the Unmanaged Approach because having pending orders for long and short entry would not be allowed by the Managed Approach. The Strategy Builder generates Managed Approach code.

    Internal Rules of the Managed Approach - https://ninjatrader.com/support/help...antedPositions

    Any short side target and stop logic should be checked with Position checks for Short side, similarly to how the Long position checks are made.

    If you want to use Set methods for Long/Short side, I recommend using separate Entry Signal names and having separate Set methods tied to each unique entry.

    For creating a strategy that captures the account position. I suggest unlocking the code, skipping historical processing with if (State == State.Historical) return; at the top of OnBarUpdate, and then checking the captured Position in OnStateChange when State == State.Realtime to place target and stop based on the captured position. I have attached Managed and Unmanaged examples.
    Attached Files
    JimNinjaTrader Customer Service

    Comment


      #17
      Hi Jim,

      Thanks a lot for the reply! I just noticed it today.

      I've updated my code.
      It's working now both for Long and Short with the simple If() Else If() structure.

      I'll do some more testing and be back with definitive results asap.

      Be well!

      EDIT:

      About the Quantity parameter of the SubmitOrderUnmanaged() method:

      Documentation:


      It states:
      quantity Sets the number of contracts to submit with the order

      Is it possible to get the SubmitOrderUnmanaged() auto-detect the quantity from the Manually entered order?—

      —Thanks a lot for the AdoptAccountPositionTestUnmanaged.zip sample above, great new concepts with the OnExecutionUpdate() method and the sumFilled variable to track quantities.
      I also found the 'Position.Quantity' way to set the quantity in the (State == State.Realtime) method. That should do!

      I'll try figuring it out from there and be back later with result!

      Be well!


      Last edited by Cormick; 07-26-2021, 07:14 AM. Reason: EDIT: sumFilled variable to track quantities, OnExecutionUpdate(), (State == State.Realtime), Position.Quantity

      Comment


        #18
        Hi Jim,

        After further tests, the Strategy auto-disables itself.

        Here's the test post:
        I'll start from scratch tomorrow back to your strategy trying to add the Short side and see if I can make it work.

        I'll be back with the results and we'll see from there.

        Be well!

        Comment


          #19
          Hello Cormick,

          You can use PositionAccount to monitor an account position instead of a strategy position. PositionAccount.Quantity will tell you the current quantity of the account position.

          PositionAccount - https://ninjatrader.com/support/help...ionaccount.htm
          JimNinjaTrader Customer Service

          Comment


            #20
            Hi Jim,

            Thanks for the direction.

            I've added the Short side to your MultistepBreakeven strategy,
            to a new Strategy MultistepBreakevenLongAndShort.

            After testing, the chart shows both Long and Short trades,
            so it seems it's working as intended.

            Screenshot:

            Click image for larger version  Name:	vmplayer_VYiJZa0I1J.png Views:	0 Size:	736.9 KB ID:	1165472

            Full Code:

            Zip code NT8 Export Code attached.

            I finally grasped how to nest the multiple if() statements within both the If() Else If() statements structure.

            The 1st if() statement's action/scope can/needs to be be treated as an independent scope (without if() statement—
            it's if is the Top one/The one from the If() Else if() structure.

            if ((Position.MarketPosition == MarketPosition.Flat) && (Close[0] > Open[0]))
            {
            {
            EnterLong(Convert.ToInt32(DefaultQuantity), "");
            StopLossModeLong = 0;
            }

            If(X)
            {
            do A;
            }

            If(Y)
            {
            do B;
            }

            If(Z)
            {
            do C;
            }
            }
            else if ((Position.MarketPosition == MarketPosition.Flat) && (Close[0] < Open[0]))
            {
            {
            EnterShort(Convert.ToInt32(DefaultQuantity), "");
            StopLossModeShort = 0;
            }

            If(P)
            {
            do D;
            }
            If(Q)
            {
            do E;
            }

            If(R)
            {
            do F;
            }
            }




            I'll tackle next testing the Unmanaged Order approach, then the PositionAccount.Quantity method.
            Attached Files
            Last edited by Cormick; 07-28-2021, 01:43 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by KirkHammett, Today, 03:23 AM
            0 responses
            2 views
            0 likes
            Last Post KirkHammett  
            Started by eleven, Yesterday, 10:06 PM
            0 responses
            35 views
            0 likes
            Last Post eleven
            by eleven
             
            Started by ezrollin, Yesterday, 08:12 PM
            7 responses
            72 views
            0 likes
            Last Post bltdavid  
            Started by kyamato, 11-23-2021, 10:51 PM
            6 responses
            91 views
            0 likes
            Last Post jeronymite  
            Started by fscabrera03, Yesterday, 07:14 PM
            0 responses
            49 views
            0 likes
            Last Post fscabrera03  
            Working...
            X