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

ATM scale out within a strategy

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

    ATM scale out within a strategy

    Hello,

    How would I go about entering a position with multiple contracts (lets say 3) with a trailing stop. Then when the target to sell one contract is hit, the trail stop updates the amount of contracts form 3 to 2, and moves to a break even position. Then, once target 2 is hit and another contract is sold, the trail stop is again updated and moved to the previous target of the first contract sold? I am able to do this with the SuperDOM, but not in my strategy. Would this be hard to code, or would calling a preset ATM strategy within my automated strategy be an easier way to go about this?

    Sorry for the confusion. Hopefully I made it as clear as possible.

    Thanks,
    Chris

    #2
    Hello Chrisca,

    Thank you for your note.

    You would be able to program this logic into a strategy without using an ATM strategy template, however would require writing the logic. It sounds like you already have an ATM template set up for this, so calling that ATM strategy template would require far less lines of code. With the ATM strategy template you would lose the ability to backtest the strategy however.

    Below is a link to calling ATM’s from a Strategy,



    If you were interested in going the Non-ATM route, you may find the following samples at the link below helpful, 1, 5, 7, 11, and 14.

    Following are links to all available NinjaScript reference samples within this forum section: Strategy Reference Samples (NinjaTrader 8) - also applicable to NT7 Using a time filter to limit trading hours (http://www.ninjatrader.com/support/forum/showthread.php?t=3226) Using multiple entry/exit signals simultaneously


    You can also contact a professional NinjaScript Consultants who would be eager to create or modify this script at your request or assist you with your script. Please let me know if you would like our business development follow up with you with a list of professional NinjaScript Consultants who would be happy to create this script or any others at your request.

    Please let us know if you need further assistance.
    Alan P.NinjaTrader Customer Service

    Comment


      #3
      Thank you Alan,

      So the entry of my order is off from where I would like it be though. I have a strategy that draws an arrow up when conditions are met and I would like the order to be placed using those same conditions. Unfortunately though, the order is being placed at the open of the following bar. Is there a way to fix this?

      My logic looks like this

      // Set 1
      if ((Stochastics1.K[0] < NumberLow)
      && (CrossBelow(Low, Bollinger1.Lower, 1)))
      {
      Draw.ArrowUp(this, @"RedGreenLight Arrow up_1"+CurrentBar,
      false, 0, (Low[0] + (-2 * TickSize)) , Brushes.Lime);
      EnterLong(Convert.ToInt32(2), "");
      }

      I know that the issue may be me using a market order, but when using a limit order, price either doesn't come back to the limit to be filled, or the limit order is not placed where I would like it to be. I would like the order to be filled at the instant both conditions become true.

      Thank you,
      Chris

      Comment


        #4
        Hello chrisca,

        This is due to Calculate being set to OnBarClose, meaning the condition is not true until the bar closes and thus the order is submitted on the next bars open.

        If you'd like your strategy to execute the moment a condition becomes true rather than at the end of the bar, you could add the following to your code under State ==State.SetDefaults.

        Code:
        Calculate = Calculate.OnEachTick;
        or

        Code:
        Calculate = Calculate.OnPriceChange;
        Or when you apply your strategy either via a chart or the strategies tab you could set Calculate to either setting above.

        Please see Calculate section of our helpguide,


        Reference Example covering Calculate,


        Please let us know if you need further assistance.
        Alan P.NinjaTrader Customer Service

        Comment


          #5
          Thanks Alan.

          My strategy is set on OnEachTick, but it's still filling in the same place. Would OnPriceChange fill more accurately, or is OnEachTick my best bet?

          Thanks,
          Chris

          Comment


            #6
            Hello chrisca,

            Are you applying the strategy to a chart and looking historically? If so you should see note 1 at the following link.


            I would suggest running the strategy on market reply to see how changing your Calculate setting impacts the entry bar.

            You should also see the following reference sample for a strategy which adds a secondary tick series for intrabar granularity.
            You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


            Please let us know if you need further assistance.
            Alan P.NinjaTrader Customer Service

            Comment


              #7
              I did run it historically, which may not allow it to fill like it would in real-time. I will check out the links you provided and see if they help.

              Thanks again, Ill keep in touch
              Chris

              Comment


                #8
                Alan,

                So after making a couple adjustments and trying to run the strategy on replay, it only enables for a second or two before turning off again. I've restarted NT which did not help.

                Here is the error I am getting:

                "Strategy 'RGArrow345Auto': Error on calling 'OnBarUpdate' method on bar 35: You are accessing an index with a value that is invalid since it is out-of-range. I.E. accessing a series [barsAgo] with a value of 5 when there are only 4 bars on the chart."

                Please let me know if you have any ideas,
                Chris
                Last edited by chrisca; 01-03-2018, 06:15 PM.

                Comment


                  #9
                  Hello chrisca,

                  Thank you for writing in.

                  I would suggest adding print statements to check for null references. I’ve provided a link to a youtube video which covers an example of using prints to understand behavior:
                  Dive into manipulating C# code from within an unlocked NinjaScript strategy using the NinjaScript Editor.NinjaTrader 7 is an award winning end to end online ...


                  Link to checking for null references,


                  I’ve provided a link covering debugging which you may find helpful.
                  Debugging: http://ninjatrader.com/support/forum...ead.php?t=3418

                  If you try the above and still unable to figure out the issue, please leave the print statements in the script and you can upload a copy and I'll take a look and see if anything jumps out.

                  To export a NinjaScript from NinjaTrader 8 do the following:
                  From the Control Center window select Tools -> Export -> NinjaScript...
                  Click Add>Select the indicator>OK>Export.
                  Then attach that file you saved; under My Docs>NT8>Bin>Custom>Select the downloaded .zip file.
                  Please let us know if you need further assistance.
                  Alan P.NinjaTrader Customer Service

                  Comment


                    #10
                    Thanks Alan,

                    I'm working on debugging, but the strategy runs fine on SIM. For some reason though I can't get it to run on Replay. If I still can't figure it out after I put the prints in, I will send it your way.

                    Thanks again,
                    Chris

                    Comment


                      #11
                      Hello chrisca,

                      I would check the log tab for errors in replay.

                      Also, make sure you have a current bar check which is greater than any historical bar reference. For example if you have if(Close[0] > Close[50]), then make sure your script contains this at the top of OnBarUpdate, if(CurrentBar<51) return;

                      More on this,


                      Please let us know if you need further assistance.
                      Alan P.NinjaTrader Customer Service

                      Comment


                        #12
                        Thanks Alan,

                        I will verify and let you know. Also, I am running a secondary tick series for intrabar granularity, and it seems to be working well. The only downside I see with that is my orders are filling a tick or two off from where I would like them to. So far, an order will generate at the price of the Bollinger Band if price crosses, but still does not fill at the same time as an arrow is formed once a cross is confirmed. Is there a way to fine tune this even more to get orders to fill at the exact price the cross is made and at the exact time an arrow forms? It seems to me that an order being placed is slightly delayed and is filled once price has started pulling back as opposed to the more "extreme" price I would like. Then again, this could be due to me using a market order as opposed to a limit order.

                        Thanks again,
                        Chris
                        Last edited by chrisca; 01-04-2018, 02:32 PM.

                        Comment


                          #13
                          Hello chrisca,

                          A condition becomes true, an arrow is drawn, the order is submitted, on the next tick the order is filled.

                          Expecting some slippage in sim as well as live is reasonable and I'm unaware of a method to combat this.

                          Please let us know if you need further assistance.
                          Alan P.NinjaTrader Customer Service

                          Comment


                            #14
                            Thank you very much Alan!

                            I managed to get everything to work on Replay and SIM. One last issue though...

                            I am using 2 contracts with one target at 4 ticks and the second target at 8 ticks with a stop loss of 8. When target 1 (4 ticks) is hit, the stop loss stays at 8 ticks even though I have it set to move to 4 ticks behind entry price, then move again to my entry price once price moves to 6 ticks from entry. My logic may be a little off, but here is how I have it set so far:

                            protected override void OnBarUpdate()
                            {
                            if (BarsInProgress != 0)
                            return;

                            if (CurrentBars[0] < 1)
                            return;

                            // Resets the stop loss to the original value when all positions are closed
                            if (Position.MarketPosition == MarketPosition.Flat)
                            {
                            SetStopLoss(CalculationMode.Ticks, StopLossTicks);
                            }

                            // If a long position is open, allow for stop loss modification to breakeven
                            else if (Position.MarketPosition == MarketPosition.Long)
                            {
                            // Once the price is greater than entry price + 4 ticks, set stop loss to
                            breakeven
                            if (Close[0] > Position.AveragePrice + 4 * TickSize)
                            {
                            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                            }

                            // Once price is greater than entry price + 6 ticks, set stop loss to
                            entry price
                            if (Close[0] > Position.AveragePrice + 6 * TickSize)
                            {
                            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                            }

                            // If a short position is open, allow for stop loss modification to breakeven
                            else if (Position.MarketPosition == MarketPosition.Short)
                            {
                            // Once the price is less than entry price - 4 ticks, set stop loss to
                            breakeven
                            if (Close[0] > Position.AveragePrice - 4 * TickSize)
                            {
                            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                            }

                            // Once price is less than entry price - 6 ticks, set stop loss to entry
                            price
                            if (Close[0] > Position.AveragePrice - 6 * TickSize)
                            {
                            SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                            }

                            }

                            Any help with this last issue would be most helpful.
                            Thank you again,
                            Chris

                            Comment


                              #15
                              Hello Chris,

                              If you add the following to your code block, are you seeing prints as you'd expect?


                              Code:
                              if (Close[0] > Position.AveragePrice + 4 * TickSize)
                              {
                              [B]Print(Time[0].ToSting()+"This should be changing SL");
                              [/B]SetStopLoss(CalculationMode.Price, Position.AveragePrice);
                              }
                              I look forward to your reply.
                              Alan P.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by The_Sec, Yesterday, 03:37 PM
                              1 response
                              11 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by vecnopus, Today, 06:15 AM
                              0 responses
                              1 view
                              0 likes
                              Last Post vecnopus  
                              Started by Aviram Y, Today, 05:29 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post Aviram Y  
                              Started by quantismo, 04-17-2024, 05:13 PM
                              3 responses
                              27 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Started by ScottWalsh, 04-16-2024, 04:29 PM
                              7 responses
                              36 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X