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

Moving Set Stop Loss Code

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

    Moving Set Stop Loss Code

    I am having issues with trying to set up a stop that moves based on market action when I am in a position.

    I took an example from the code I am working on to help explain. FYI I am using generic "custom indicator" instead of using the actual code for the indicator

    So for instance, earlier in the strategy code, I have logic to get long and I get filled. The logic then proceeds to the code below. As the market moves up, past certain market levels, the target stays the same but the stop moves up.

    My problem is in the "second stop move" below and the argument "&& Low[1]>custom indicator[1]". What can happen is Low[1] may move below the custom indicator, so instead of the stop for that logic staying at lower custom indicator[0]-1*TickSize, instead it goes back to the logic in the "first stop move" and moves to lower custom indicator[0]-2*TickSize

    Essentially what needs to be happening is the stops below in the code should be in order, so as the market reaches the logic of the "third stop move", the placement of the stop for that logic ( Position.AveragePrice + 1*TickSize) needs to stay in place, no matter what the market does after that logic becomes true. (same with first and second stop move)

    I am also doing the same thing for a short, just opposite.

    Hopefully that all makes sense. Let me know any questions


    //first stop move

    if(Position.MarketPosition == MarketPosition.Long
    && Position.AveragePrice<lower custom indicator[1]
    && High[0]>=custom indicator[1])

    {
    SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

    SetStopLoss("Long", CalculationMode.Price,lower custom indicator[0]-2*TickSize,true);
    }

    // second stop move

    if(Position.MarketPosition == MarketPosition.Long
    && Position.AveragePrice<lower custom indicator[1]
    && Low[1]>custom indicator[1])

    {
    SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

    SetStopLoss("Long", CalculationMode.Price,lower custom indicator[0]-1*TickSize,true);
    }

    //third stop move

    if(Position.MarketPosition == MarketPosition.Long
    && Position.AveragePrice<custom indicator[1]
    && Low[1]>other custom indicator[1])

    {
    SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

    SetStopLoss("Long", CalculationMode.Price,Position.AveragePrice + 1*TickSize,true);
    }

    #2
    Hello CMillz,

    Thanks for your post.

    To control them in sequence suggest you use 3 bools, one for each set, for example, called A, B, C where A is set to true and B & C are set to false initially.

    In each set you would check for the bool to be true in addition to your other conditions.

    To get into your first condition you would add a check to see that A is true. Once you are done with your first condition you would set A to false and B to true. Once A is false the first condition can no longer effect the stop/target.

    In the 2nd set you could check that B is true until you are done with the second set then set B to false and finally set C to true.

    Once you are in a flat condition you would need to reset A to true, B & C back to false to then start over on the next entry.

    I would advise using different bools for the other side just to keep things straight.
    Paul H.NinjaTrader Customer Service

    Comment


      #3
      Thanks Paul

      Ok think I am following you. So would this be the way to write the bools into the code?

      private bool BuyA;
      private bool BuyB;
      private bool BuyC;

      protected override void OnBarUpdate().......



      if (....logic to go long true......)

      {
      EnterLong(DefaultQuantity, "RCPLong");

      SetProfitTarget(..........);
      SetStopLoss(............);

      }

      //first stop move

      if(Position.MarketPosition == MarketPosition.Long
      && Position.AveragePrice<lower custom indicator[1]
      && High[0]>=custom indicator[1])

      {
      SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

      SetStopLoss("Long", CalculationMode.Price,lower custom indicator[0]-2*TickSize,true);

      BuyA = true;
      }

      // second stop move

      if(Position.MarketPosition == MarketPosition.Long
      && Position.AveragePrice<lower custom indicator[1]
      && Low[1]>custom indicator[1])

      {
      SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

      SetStopLoss("Long", CalculationMode.Price,lower custom indicator[0]-1*TickSize,true);

      BuyA = false;
      BuyB = true;
      }

      //third stop move

      if(Position.MarketPosition == MarketPosition.Long
      && Position.AveragePrice<custom indicator[1]
      && Low[1]>other custom indicator[1])

      {
      SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

      SetStopLoss("Long", CalculationMode.Price,Position.AveragePrice + 1*TickSize,true);

      BuyA = false;
      BuyB = false;
      BuyC = true;
      }

      Comment


        #4
        Hello CMillz,

        Thanks for your reply.

        Not quite. try this:

        private bool BuyA = true;
        private bool BuyB = false;
        private bool BuyC = false;

        .
        .
        .

        //first stop move

        if(Position.MarketPosition == MarketPosition.Long
        && Position.AveragePrice<lower custom indicator[1]
        && High[0]>=custom indicator[1]
        && BuyA)

        {
        SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

        SetStopLoss("Long", CalculationMode.Price,lower custom indicator[0]-2*TickSize,true);

        BuyA = false;
        BuyB = true;
        }
        .
        .
        .

        / second stop move

        if(Position.MarketPosition == MarketPosition.Long
        && Position.AveragePrice<lower custom indicator[1]
        && Low[1]>custom indicator[1] && BuyB)

        {
        SetProfitTarget("Long", CalculationMode.Price,higher custom indicator[0]-1*TickSize,true);

        SetStopLoss("Long", CalculationMode.Price,lower custom indicator[0]-1*TickSize,true);

        BuyB = false;
        BuyC = true;
        }

        .
        .
        .
        etc. etc.
        Paul H.NinjaTrader Customer Service

        Comment


          #5
          Great, thanks Paul that makes sense. Now how about when we switch to the short signal code?

          Do we add:
          private bool BuyA = true;
          private bool BuyB = false;
          private bool BuyC = false;
          private bool SellA = false;
          private bool SellB = false;
          private bool SellC = false;

          For your reference, How the code reads is it starts with the logic for the long, then goes through the long stop moves, then goes to the logic for the short and begins the logic for the short stop moves.


          What if the first trade is short & not long, wouldn't that be a problem since we are starting the code with BuyA being true?

          Comment


            #6
            Hi Paul,

            Please ignore my previous message, I think I have resolved the issue.

            Thanks for your help, will let you know anything further.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by cls71, Today, 04:45 AM
            0 responses
            1 view
            0 likes
            Last Post cls71
            by cls71
             
            Started by mjairg, 07-20-2023, 11:57 PM
            3 responses
            213 views
            1 like
            Last Post PaulMohn  
            Started by TheWhiteDragon, 01-21-2019, 12:44 PM
            4 responses
            544 views
            0 likes
            Last Post PaulMohn  
            Started by GLFX005, Today, 03:23 AM
            0 responses
            3 views
            0 likes
            Last Post GLFX005
            by GLFX005
             
            Started by XXtrader, Yesterday, 11:30 PM
            2 responses
            12 views
            0 likes
            Last Post XXtrader  
            Working...
            X