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, 08: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:

            Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.


            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, 02:43 AM.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by xiinteractive, 04-09-2024, 08:08 AM
            3 responses
            11 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by Johnny Santiago, 10-11-2019, 09:21 AM
            95 responses
            6,193 views
            0 likes
            Last Post xiinteractive  
            Started by Irukandji, Today, 09:34 AM
            1 response
            3 views
            0 likes
            Last Post NinjaTrader_Clayton  
            Started by RubenCazorla, Today, 09:07 AM
            1 response
            6 views
            0 likes
            Last Post RubenCazorla  
            Started by TraderBCL, Today, 04:38 AM
            3 responses
            26 views
            0 likes
            Last Post NinjaTrader_Jesse  
            Working...
            X