Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Move stop to breakeven

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

    Move stop to breakeven

    Hi

    When I'm in a short position I'd like to move my stop loss to breakeven once the trade has gone a certain distance in my favour. Any ideas how I do this, I've been trying the below, but struggling to get any traction. Assume I'm missing something

    So I'm in a short position named "Go Short" which has Profit target 50 ticks and Stop Loss of 50 ticks

    if (position.marketposition==marketposition.short) && Close [0]< AvgPrice - 15*TickSize

    SetStopLoss("GoShort",CalculationMode.Price,AvgPri ce,false)

    #2
    Hello,

    Thank you for posting.

    I wanted to check, what is the current result of the code you provided? Are you not seeing the existing stop move or are you seeing it move to an unexpected location?

    Assuming the AvgPrice value you are using is a valid Price to place the stop at, it would appear this syntax is correct.

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Hi

      Just nothing is happening- the stops are not rolling. I've put a sample of the code I'm using below. Any suggested amendments would be really appreciated.

      protected override void Initialize()
      {
      Add(RSI(7, 1));
      Add(RSI(7, 1));
      Add(MACD(12, 26, 9));
      Add(RSI(7, 1));
      Add(RSI(7, 1));
      Add(MACD(12, 26, 9));
      SetProfitTarget("GoShort", CalculationMode.Ticks, Profit);
      SetStopLoss("GoShort", CalculationMode.Ticks, StopShort, false);
      SetProfitTarget("GoLong", CalculationMode.Ticks, Profit);
      SetStopLoss("GoLong", CalculationMode.Ticks, StopLong, false);
      SetStopLoss("GoShort", CalculationMode.Ticks,rollStop, false);
      SetStopLoss("GoLong", CalculationMode.Ticks,rollStop, false);
      CalculateOnBarClose = true;
      }

      /// <summary>
      /// Called on each bar update event (incoming tick)
      /// </summary>
      protected override void OnBarUpdate()
      {
      // Condition set 1
      if (CrossAbove(RSI(7, 1).Avg, RSIOverbought, 20)
      && RSI(7, 1).Avg[0] < RSIGoShort
      && MACD(12, 26, 9).Diff[0] <= 0
      && Close[0] > Open[0])
      {
      EnterShort(DefaultQuantity, "GoShort");
      }

      // Condition set 2
      if (CrossBelow(RSI(7, 1).Avg, RSIOversold, 20)
      && RSI(7, 1).Avg[0] > RSIGoLong
      && MACD(12, 26, 9).Diff[0] >= 0
      && Close[0] < Open[0])
      {
      EnterLong(DefaultQuantity, "GoLong");
      }

      // Condition set 3
      if (Position.MarketPosition == MarketPosition.Short
      && Close[0] + 5 * TickSize <= Position.AvgPrice)
      {
      SetStopLoss("GoShort", CalculationMode.Ticks,rollStop, false);
      }

      // Condition set 4
      if (Position.MarketPosition == MarketPosition.Long
      && Close[0] >= Position.AvgPrice + 5 * TickSize)
      {
      SetStopLoss("GoLong", CalculationMode.Ticks,rollStop, false);
      }
      }

      Comment


        #4
        I'm .NET Solution Architect & Technical lead to a team of 50 Software Engineers. I would reject code like yours from one of my engineers because its hard to debug and understand. One of my coding standard rules, is "never pass logic into an If statement or a return statement. Assign it to a local variable first." If you change your code to something like this it will be much easier to debug:

        Code:
        var isRsiOverBought = CrossAbove(RSI(7, 1).Avg, RSIOverbought, 20);
        var isRsiBelowGoShortThreshhold = RSI(7, 1).Avg[0] < RSIGoShort;
        var isMacdDiffBelowZero = MACD(12, 26, 9).Diff[0] <= 0;
        var isBarBullish = Close[0] > Open[0];
        
        // Property Value variable to easily enable/disable debug via Checkbox
        if (DebugEnabled) 
        {
        	var debug = string.Format(@"SHORT_ENTRY_CONDITIONS: 
                    isRsiOverBought:{0} 
                    isRsiBelowGoShortThreshold:{1} 
                    isMacdDiffBelowZero:{2} 
                    isBarBullish:{3}", 
                    isRsiOverBought, isRsiBelowGoShortThreshhold, isMacdDiffBelowZero, isBarBullish);
        
               Print(debug);
        }
        
        if (isRsiOverBought && isRsiBelowGoShortThreshhold && isMacdDiffBelowZero && isBarBullish)
        {
        	if (DebugEnabled)
        	{
        		var debug = string.Format("Short Condition has been met; Going short with Quantity {0}", quantity);
        		Print(debug);
        	}
        	EnterShort(DefaultQuantity, "GoShort");
        }
        An inexperienced engineer will say "No, you're wasting resources because you're assigning unnecessary variables" But it is not true. Part of the multi-stage compilation process is Code Optimization. Among other things the compiler's Optimization stage strips-out unnecessary variables and references. Therefore the code you posted will ultimately result in IDENTICAL - and I mean absolutely IDENTICAL bit-for-bit output to the code I posted (except for the additional debug code I added).



        If you adopt this approach to your coding style you will quickly find which condition or parameter is causing your strategy to have unexpected results. It also makes it easier to switch different conditions in and out and tweak your strategy.

        Its unfortunate that ALL the Ninjatrader Indicators and Code examples have such appalling code quality; they're all written in exactly the same way as the code you posted. Its unfortunate because people like you are learning terrible coding practices; and producing code which is hard to understand and debug. I cry myself to sleep about such things.

        If the Ninjatrader Application code is also written in this way it would explain why the product is so incredibly buggy and why the development process is not agile.

        I don't use NT7 so I can't help much further, but I'm sure if you adopt this approach you will quickly find the problem.
        Last edited by reach4thelasers; 06-14-2017, 04:05 AM.

        Comment


          #5
          Hello lancasterstephen,

          Thank you for your response.

          In the Intialize() functions you have the GoLong and GoShort Stop Losses set twice. It would be recommended to do this once.
          Code:
          SetProfitTarget("GoShort", CalculationMode.Ticks, Profit);
          [B]SetStopLoss("GoShort", CalculationMode.Ticks, StopShort, false);[/B]
          SetProfitTarget("GoLong", CalculationMode.Ticks, Profit);
          [B]SetStopLoss("GoLong", CalculationMode.Ticks, StopLong, false);[/B]
          [B]SetStopLoss("GoShort", CalculationMode.Ticks,rollStop, false);
          SetStopLoss("GoLong", CalculationMode.Ticks,rollStop, false)[/B];
          Can you provide the code that sets 'rollStop'?

          In addition, if you add Print()s to the conditions to move the Stop Losses do you see the Print()s in the Output window?

          For information on Print() please visit the following link: http://ninjatrader.com/support/helpGuides/nt7/print.htm
          The Output window is opened under Tools > Output.

          I look forward to your response.

          Comment


            #6
            reach4thelasers
            Thanks, good concern! I agree.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by kulwinder73, Today, 10:31 AM
            1 response
            8 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by RookieTrader, Today, 09:37 AM
            3 responses
            15 views
            0 likes
            Last Post NinjaTrader_ChelseaB  
            Started by terofs, Yesterday, 04:18 PM
            1 response
            24 views
            0 likes
            Last Post terofs
            by terofs
             
            Started by CommonWhale, Today, 09:55 AM
            1 response
            6 views
            0 likes
            Last Post NinjaTrader_Erick  
            Started by Gerik, Today, 09:40 AM
            2 responses
            8 views
            0 likes
            Last Post Gerik
            by Gerik
             
            Working...
            X