Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to See the Value of SetStopLoss

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

    How to See the Value of SetStopLoss

    Is there a way to "see" the value of SetStopLoss? I have a strategy that updates the stop when price moves a certain number of points. The problem is sometimes the price moves and the stop is moved to a previous level. I only want the stop to be moved up for a long position. I want to compare the existing stop loss and only update if it is lower than what the new stop will be. How might I do that?
    Here is a chunk of my code:
    // If Entry + 54, Move stop to Entry + 28
    else if (High[0] > Position.AvgPrice + 54)
    {
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 28);
    DrawText("tag1", "longS3", 0, Position.AvgPrice + 28, Color.Red);
    }
    // If Entry + 18, Move stop to Entry + 3
    else if (High[0] > Position.AvgPrice + 18)
    {
    ExitLong("Long 1a");
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 3);
    DrawText("tag2", "longS2", 0, Position.AvgPrice + 3, Color.Red);


    Thanks, Tim

    #2
    Hi Tim,

    The issue you're seeing is the conditions for the lower stop loss movements continue to evaluate true. There are a few different approaches you can take for this. You could work with bool flags so that the lower movements happen only once. You check the bool flag in your condition and set in the opposite direction, resetting at a later time (when flat).

    Another solution is using else if and if statements in the appropriate order. Below is an example of this:

    if (High[0] > Position.AvgPrice + 30 * TickSize)
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 25 * TickSize);

    else if (High[0] > Position.AvgPrice + 20 * TickSize)
    SetStopLoss(CalculationMode.Price, Position.AvgPrice + 9 * TickSize);

    Note that the second level movement is evaluated first, and else if is used for the first movement. This means that the first if statement must evaluate false before the first stop loss movement is executed.
    Ryan M.NinjaTrader Customer Service

    Comment


      #3
      Ryan, I am setting the stop using else if statements like you suggest in your second approach. Using your code as an example, the problem occurs when High[0] is less than 30 (your target) and greater than 25 (your stop). When this happens, the if statement is false and the else if statement is evaluated. The else if is true because High[0] is greater than 20 (your target) so your stop is set to +9. I would like to add something like && newStopLoss > oldStopLoss. Wait a sec, I may have just answered my own question. I'll be back...

      Comment


        #4
        I just created my own variable called newStopLoss. Each time SetStopLoss is called, I update newStopLoss to the new value. I then compare newStopLoss to the value that I want to update it to. For a long, if newStopLoss is less than what I want to update it to, I update it, if not, I don't.

        else if (High[0] > Position.AvgPrice + target2)
        {
        if (newStopLoss < Position.AvgPrice + stop3)
        {
        SetStopLoss(CalculationMode.Price, Position.AvgPrice + stop3);
        newStopLoss = Position.AvgPrice + stop3;
        }
        }

        Seems to work fine.

        Tim

        Comment


          #5
          Yes, definitely more than one way to approach this. Capturing the value of your stop loss when it moves and comparing to a previous value is also another way. Glad to hear you were able to work this out.

          I can see what you're saying about my earlier suggestion. If the market starts heading back down after breaching 30, it would then adjust back to the first level and might not work for you.
          Ryan M.NinjaTrader Customer Service

          Comment


            #6
            isNewStop( ref double oldStop, double newStop, bool up)

            FWIW, Tim, this is how I did it....

            Code:
            private bool isNewStop( ref double oldStop, double newStop, bool up)
                    {
                        if ((oldStop==0) || ( (up) ? newStop > oldStop : newStop < oldStop) )
                        {    
                            newStop = Instrument.MasterInstrument.Round2TickSize(newStop);
                            if (_debug) Print(header+Time[0].ToString()+ "   previous "+((up) ? "LONG" : "SHORT")+ " Stop = "+oldStop +"  new Stop = "+newStop);
                            SetStopLoss((up) ? "Long 1a" : "Short 1a", CalculationMode.Price, newStop, false);  // false puts order at broker
                            PlaySound(@"C:\Program Files\NinjaTrader 6.5\sounds\AutoTrail.wav");
                            DrawDiamond( (up ? "L":"S")+CurrentBar, true, 0, newStop, Color.Black);
                            oldStop = newStop;   // ref so it can be changed and returned
                            return true;
                        }                            
                        return false;  // did not change
                    }
            Since I did not use Instrument.MasterInstrument.Compare, it will sometimes update the stop to the same value. I don't think I care.... Note the ref -- you will have to specify ref on the calling line as well for that one variable (your newStopLoss).

            Comment


              #7
              Great! Thanks for sharing, Lost Trader.
              Ryan M.NinjaTrader Customer Service

              Comment


                #8
                Thanks Lost Trader. You have a lot of stuff going on there that I have no idea what it means. Still lots to learn.

                Tim

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by trilliantrader, 04-18-2024, 08:16 AM
                5 responses
                22 views
                0 likes
                Last Post trilliantrader  
                Started by Davidtowleii, Today, 12:15 AM
                0 responses
                3 views
                0 likes
                Last Post Davidtowleii  
                Started by guillembm, Yesterday, 11:25 AM
                2 responses
                9 views
                0 likes
                Last Post guillembm  
                Started by junkone, 04-21-2024, 07:17 AM
                9 responses
                70 views
                0 likes
                Last Post jeronymite  
                Started by mgco4you, Yesterday, 09:46 PM
                1 response
                14 views
                0 likes
                Last Post NinjaTrader_Manfred  
                Working...
                X