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

Stop loss that changes as the market moves in my favor

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

    Stop loss that changes as the market moves in my favor

    Hi Ninjatrader programmers,

    I took a screenshot of my IDE to make it easier to read. I'm also going to paste the code below in case someone wants to copy and paste it to an IDE to try to figure it out. What I'm trying to do is create a stop-loss that is set to:

    -20 at the beginning of trade
    Breakeven when the market has moved in my favor by 20 pips
    +20 when the market has moved in my favor by +40 pips
    +40 when the market has moved in my favor by +60 pips
    Trailing stop loss of -10 once price has moved in my favor by 70 pips.

    This code below looks like it should work. If you would like to get more information about my backtest results, please let me know. This code is simple, I just don't know why I can't get it to work. Thanks a million.





    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Resets stop-loss when market becomes flat.
    SetStopLoss(CalculationMode.Ticks, stoplossticks);

    } else if (Position.MarketPosition == MarketPosition.Long){
    // Once the price is greater than entry price+20 ticks, set stop loss to breakeven
    if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
    SetStopLoss(CalculationMode.Price, Position.AvgPrice);

    }
    } else if (Position.MarketPosition == MarketPosition.Long){
    // Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
    if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);

    }
    } else if (Position.MarketPosition == MarketPosition.Long) {
    // Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
    if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize);

    }

    } else if ( Position.MarketPosition == MarketPosition.Long) {
    // Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
    if(Close[0] > Position.AvgPrice + 70 * TickSize) {
    SetTrailStop(CalculationMode.Ticks, 10);

    }
    }

    #2
    In case it is helpful, I wrote that code under OnBarUpdate()

    Comment


      #3
      Hello BernWillChris,

      Thanks for your post and welcome to the Forum!

      Thank-you for sharing your code.

      When you post code snippets, if you use the "go advanced" you can then use code tags to make it all fit and be easier to read, for example:

      Code:
      if (Position.MarketPosition == MarketPosition.Flat)
      {
      // Resets stop-loss when market becomes flat.
      SetStopLoss(CalculationMode.Ticks, stoplossticks);
      
      } else if (Position.MarketPosition == MarketPosition.Long){
      // Once the price is greater than entry price+20 ticks, set stop loss to breakeven
      if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
      SetStopLoss(CalculationMode.Price, Position.AvgPrice);
      
      }
      } else if (Position.MarketPosition == MarketPosition.Long){
      // Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
      if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);
      
      }
      } else if (Position.MarketPosition == MarketPosition.Long) {
      // Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
      if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
      SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize); 
      
      }
      
      } else if ( Position.MarketPosition == MarketPosition.Long) {
      // Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
      if(Close[0] > Position.AvgPrice + 70 * TickSize) {
      SetTrailStop(CalculationMode.Ticks, 10);
      
      }
      }

      From a coding standpoint you are using SetTrailStop and regrettably you can only use SetTrailStop() or SetStopLoss(). When both are used, the SetStopLoss will override (unless they are applied to different entry signals).

      Here is the helpguide reference: http://ninjatrader.com/support/helpG...ttrailstop.htm Please observe the first line in the "Tips" section.
      Paul H.NinjaTrader Customer Service

      Comment


        #4
        Originally posted by BernWillChris View Post
        Hi Ninjatrader programmers,

        I took a screenshot of my IDE to make it easier to read. I'm also going to paste the code below in case someone wants to copy and paste it to an IDE to try to figure it out. What I'm trying to do is create a stop-loss that is set to:

        -20 at the beginning of trade
        Breakeven when the market has moved in my favor by 20 pips
        +20 when the market has moved in my favor by +40 pips
        +40 when the market has moved in my favor by +60 pips
        Trailing stop loss of -10 once price has moved in my favor by 70 pips.

        This code below looks like it should work. If you would like to get more information about my backtest results, please let me know. This code is simple, I just don't know why I can't get it to work. Thanks a million.





        if (Position.MarketPosition == MarketPosition.Flat)
        {
        // Resets stop-loss when market becomes flat.
        SetStopLoss(CalculationMode.Ticks, stoplossticks);

        } else if (Position.MarketPosition == MarketPosition.Long){
        // Once the price is greater than entry price+20 ticks, set stop loss to breakeven
        if (Close[0] > Position.AvgPrice + 20 * TickSize && Close[0] < Position.AvgPrice + 40 * TickSize){
        SetStopLoss(CalculationMode.Price, Position.AvgPrice);

        }
        } else if (Position.MarketPosition == MarketPosition.Long){
        // Once the price is greater than entry price+40 ticks, set stop loss to 20 ticks from entry
        if(Close[0] > Position.AvgPrice + 40 * TickSize && Close[0] < Position.AvgPrice + 60 * TickSize){
        SetStopLoss(CalculationMode.Price, Position.AvgPrice + 20 * TickSize);

        }
        } else if (Position.MarketPosition == MarketPosition.Long) {
        // Once the price is greater than entry price+60 ticks, set stop loss to 40 ticks from entry
        if(Close[0] > Position.AvgPrice + 60 * TickSize && Close[0] < Position.AvgPrice + 70 * TickSize) {
        SetStopLoss(CalculationMode.Price, Position.AvgPrice + 40 * TickSize);

        }

        } else if ( Position.MarketPosition == MarketPosition.Long) {
        // Once the price is greater than entry price+70 ticks, set trailing stop loss to 10 ticks.
        if(Close[0] > Position.AvgPrice + 70 * TickSize) {
        SetTrailStop(CalculationMode.Ticks, 10);

        }
        }
        Without looking at any other considerations, you are processing your code in the wrong order. You must process from highest value filter to lowest, otherwise you will always satisfy the lowest filter, precisely because it is the lowest. A value that is bigger than 40 is necessarily bigger than 20, so will get processed by the >20 filter, as it is the first in order.

        You will also have to create a manual trailing stop, as you cannot use both SetStop() and SetTrailStop() in the same class file. There is an example in the subforum that lists reference sample code.

        ref: http://ninjatrader.com/support/forum...ead.php?t=3222

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by inanazsocial, Today, 01:15 AM
        1 response
        6 views
        0 likes
        Last Post NinjaTrader_Jason  
        Started by rocketman7, Today, 02:12 AM
        0 responses
        10 views
        0 likes
        Last Post rocketman7  
        Started by dustydbayer, Today, 01:59 AM
        0 responses
        2 views
        0 likes
        Last Post dustydbayer  
        Started by trilliantrader, 04-18-2024, 08:16 AM
        5 responses
        23 views
        0 likes
        Last Post trilliantrader  
        Started by Davidtowleii, Today, 12:15 AM
        0 responses
        3 views
        0 likes
        Last Post Davidtowleii  
        Working...
        X