• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

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 mrwarkhan, Yesterday, 03:45 PM
                1 response
                20 views
                0 likes
                Last Post koganam
                by koganam
                 
                Started by onevefiefe, Today, 07:42 AM
                0 responses
                2 views
                0 likes
                Last Post onevefiefe  
                Started by J_o_s, Today, 06:09 AM
                0 responses
                8 views
                0 likes
                Last Post J_o_s
                by J_o_s
                 
                Started by jeronymite, 03-23-2020, 12:27 AM
                3 responses
                34 views
                0 likes
                Last Post leontancfa  
                Started by asmmbillah, Today, 04:53 AM
                0 responses
                5 views
                0 likes
                Last Post asmmbillah  
                Working...
                X