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

Strategy: Moving StopLoss

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

    #16
    Paul,

    I contained two conditions within (IsFirstTickOfBar) and I am sometimes getting the conditions to trigger when the conditions have not fully played out. It seems most of the time I will get a "bar close" behavior but other times the line I have set to cross will fall short of crossing, yet the strategy is executing entries prematurely.

    Here's what I have:

    if (IsFirstTickOfBar)
    {
    // Set 1
    if (CrossAbove(MACD1.Diff, 0, 1))
    {
    EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
    }

    if (CrossAbove(MACD1.Diff, 0, 1))
    {
    EnterShort(Convert.ToInt32(Position.Quantity), @"Short Zero");
    }

    Every now and then the entries will not trigger at all.

    Does this sound normal?
    Last edited by Rogue_Two; 04-16-2018, 07:05 AM.

    Comment


      #17
      Hello Rogue_Two,

      Thanks for your reply.

      You may want to change the lookback period to 2 from 1 in the crossabove and crossbelow statements.

      For a diagnostic process, for the crossabove and crossbelow statements, you may want to add a Draw statement to draw a dot on the chart when the cross above condition is true. This would allow you to see when the statement is true regardless of an order as there may be other reasons that an order may not be placed.
      Paul H.NinjaTrader Customer Service

      Comment


        #18
        I'll try that and see how it goes.

        One more question for now.

        Is it possible in the strategy to allow me to manually move stops without having them revert back to their original positions as well as not disabling the strategy when they get hit?

        Comment


          #19
          Hello Rogue_Two,

          Thanks for your reply.

          No, in your strategy if you move the stop/target that would be expected and disabling of the strategy can occur.

          If you wish to have that capability then you would need to use different strategy methods that interact with an ATM template. There is an example of the coding required in your NinjaTrader called SampleATMstrategy which is well documented internally by comments. In addition, here is a link to the helpguide section on Ninjascript ATMstrategyMethods: https://ninjatrader.com/support/help...gy_methods.htm Basically your strategy would create the entry conditions, then the ATM template would be in play after the entry order is filled. Once a profit or stop is hit, control returns to the ninjascript strategy.

          A limitation is that you would not be able to use the strategy in the strategy analyzer as ATMs cannot be backtested (except for using playback with Market replay data).
          Paul H.NinjaTrader Customer Service

          Comment


            #20
            Got it.

            So I changed the lookback to 2 and the behavior is still the same. Now I'm finding executions seem to be triggering on touch of the 0 line instead of on "bar close" even though the conditions and entries are contained withing (IsFirstTickOfBar).

            While I try and learn how to add in the Draw statements you mentioned, do you have any idea why the entries are getting triggered on touch rather than close?

            Could using a Heiken-Ashi Tick chart have anything to do with it?

            Thanks for your help and patience Paul!
            Last edited by Rogue_Two; 04-16-2018, 11:17 AM.

            Comment


              #21
              Hello Rogue_Two,

              You may want to look at your code again as in post #16 it appears you are using CrossAbove for both conditions.
              Paul H.NinjaTrader Customer Service

              Comment


                #22
                Oops, that happened as I was copying it over but in the actual strategy it is correct.

                Comment


                  #23
                  Hello Rogue_Two,

                  Here is an alternative way to determine if the diff line has crossed the zero line.

                  Code:
                  if (IsFirstTickOfBar)
                  			{
                  				if (MACD1.Diff[1] > 0 && MACD1.Diff[2] < 0)
                  				{
                  					EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
                  					Draw.Square (this, "testb"+CurrentBar, true, 0, Low[0] - 8 * TickSize, Brushes.Blue);
                  				}
                  				
                  				if (MACD1.Diff[1] < 0 && MACD1.Diff[2] >0)
                  				{
                  					EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
                  					Draw.Dot (this, "testc"+CurrentBar, true, 0, High[0] + 8 * TickSize, Brushes.Magenta);
                  				}
                  }
                  The difference here is that the conditional statements are looking at the closed bars that would not change and does not include the currently forming bar.
                  Paul H.NinjaTrader Customer Service

                  Comment


                    #24
                    My strategy is coming along thanks to you Paul! Although the crosses seem to be triggering my entries correctly on shorter timeframes, but I'm still getting some rogue triggers and even late triggers for some reason on longer tick charts.

                    ***edit***

                    I will do more testing but it actually seems like the print outs are just moving bars after entries. For example, the "Long Zero" or "Short Zero" are shifting around to other bars when crosses occur.

                    My stop losses are now moving at least. My long entries stop loss modification is occurring how I want it to in two stages.

                    Once the market gets to 80 ticks above entry, the stop gets moved +40 ticks into profit and then another modification of the stop loss occurs when the market gets to +160 ticks above entry.

                    The same behavior is supposed to occur on the short side but only the first stage seems to be trigger the stop to move. I can't seem to get it to move +80 ticks into profit once the market has reached 160 ticks above entry. Can you please take a look at this at your convenience and see if you can spot anything that may be preventing this from occurring?

                    Thanks Paul!

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

                    if (CurrentBars[0] < BarsRequiredToTrade)
                    return;

                    if (IsFirstTickOfBar)
                    {
                    //Enter Longs
                    if (MACD1.Diff[1] > 0 && MACD1.Diff[2] < 0)
                    {
                    EnterLong(Convert.ToInt32(Position.Quantity), @"Long Zero");
                    Draw.Square (this, "testb"+CurrentBar, true, 0, Low[0] - 8 * TickSize, Brushes.Blue);
                    }

                    //Enter Shorts
                    if (MACD1.Diff[1] < 0 && MACD1.Diff[2] >0)
                    {
                    EnterShort(Convert.ToInt32(Position.Quantity), @"Short Zero");
                    Draw.Dot (this, "testc"+CurrentBar, true, 0, High[0] + 8 * TickSize, Brushes.Magenta);
                    }
                    }


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

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

                    else if (Position.MarketPosition == MarketPosition.Long && SecondMove == true)
                    {
                    if (Close[0] > Position.AveragePrice + 160 * TickSize)
                    {
                    SetStopLoss("", CalculationMode.Price, Position.AveragePrice + 80 * TickSize, false);
                    SecondMove = false;
                    }
                    }

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

                    else if (Position.MarketPosition == MarketPosition.Short && SecondMove)
                    {
                    if (Close[0] < Position.AveragePrice - 160 * TickSize)
                    {
                    SetStopLoss("", CalculationMode.Price, Position.AveragePrice - 80 * TickSize, false);
                    SecondMove = false;
                    }
                    }


                    }
                    }

                    //This section is to help with OCO rejected orders due to fast market movement.
                    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
                    {
                    if(error == ErrorCode.OrderRejected)
                    {
                    if(order.Name == "Stop loss" && Position.MarketPosition == MarketPosition.Long)
                    {
                    ExitLong();
                    Print("Stop Loss Rejected Exiting Long");

                    }

                    if(order.Name == "Stop loss" && Position.MarketPosition == MarketPosition.Short)
                    {
                    ExitShort();
                    Print("Stop Loss Rejected Exiting Short");

                    }

                    }

                    if(order.Name == "Stop loss" && order.OrderState == OrderState.Accepted)
                    {

                    Print("Stop Loss Accepted");
                    Last edited by Rogue_Two; 04-20-2018, 07:41 AM.

                    Comment


                      #25
                      Hello Rogue_Two,

                      Thanks for your post.

                      I don't see anything obvious. What you can do is to add print statements in your stop moving area to show the state of the two bools, the position, and the close price.
                      Paul H.NinjaTrader Customer Service

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by michi08, 10-05-2018, 09:31 AM
                      2 responses
                      739 views
                      0 likes
                      Last Post Denver_Wayne  
                      Started by sightcareclickhere, Today, 01:55 PM
                      0 responses
                      1 view
                      0 likes
                      Last Post sightcareclickhere  
                      Started by Mindset, 05-06-2023, 09:03 PM
                      9 responses
                      258 views
                      0 likes
                      Last Post ender_wiggum  
                      Started by Mizzouman1, Today, 07:35 AM
                      4 responses
                      18 views
                      0 likes
                      Last Post Mizzouman1  
                      Started by philmg, Today, 01:17 PM
                      1 response
                      8 views
                      0 likes
                      Last Post NinjaTrader_ChristopherJ  
                      Working...
                      X