Login Page - Create Account

Support Board


Date/Time: Thu, 06 Mar 2025 10:11:47 +0000



[Programming Help] - Revising Kiwi's ATR Trailstop to take over average calculation

View Count: 1941

[2022-01-17 21:02:47]
tuanluu20902 - Posts: 24
Hi there, and Kiwi, if you are reading this, would you please help me? I would appreciate it so much.
Anyway, I am currently using Kiwi's trailing stop and it is a wonderful indicator. However, I would like to make it more similar to the ATR Trailing Stop offered in Thinkorswim and that is the ability to select the average type that we want to use toward the ATR calculation (see picture).
After reviewing the source code for Kiwi's trailing stop, it seems that he had hard coded the calculations to be using the Wilders method (if I'm not wrong). Is there any easy solution for me to change it to using the Simple method?
Unfortunately, I am not very proficient in coding nor C++, I can only edit basic coding so I have to ask for your help. Very much appreciated.


if (sc.UpdateStartIndex == 0)
  {
    Subgraph_direction[sc.UpdateStartIndex] = 1;
    Subgraph_loclose[sc.UpdateStartIndex] = sc.Close[sc.UpdateStartIndex];
    Subgraph_hiclose[sc.UpdateStartIndex] = Subgraph_loclose[sc.UpdateStartIndex];

    Array_e1[sc.UpdateStartIndex]=sc.High[sc.UpdateStartIndex]-sc.Low[sc.UpdateStartIndex];
    Array_e2[sc.UpdateStartIndex]=Array_e1[sc.UpdateStartIndex];
    Array_e3[sc.UpdateStartIndex]=Array_e1[sc.UpdateStartIndex];
    Array_e4[sc.UpdateStartIndex]=Array_e1[sc.UpdateStartIndex];
    Array_e5[sc.UpdateStartIndex]=Array_e1[sc.UpdateStartIndex];
    Array_e6[sc.UpdateStartIndex]=Array_e1[sc.UpdateStartIndex];
  }

  if(!Input_Vervoort.GetYesNo())
  {
    b=0.5;          // 0.5
    b2=(b*b);        // 0.25
    b3=(b2*b);        // 0.125

    c1=-b3;          // - 0.125
    c2=(3*(b2+b3));      // 0.45
    c3=(-3*(2*b2+b+b3));
    c4=(1+3*b+b3+3*b2);
    w1 = 2/(Input_ATRLength.GetFloat()+1);
    w2 = 1-w1;

    for (pos=max(sc.UpdateStartIndex, 1); pos < sc.ArraySize; pos++)
    {
      float temp = max(sc.High[pos]-sc.Low[pos], sc.High[pos]-sc.Close[pos-1]);
      float P = max(temp,sc.Close[pos-1]-sc.Low[pos]);

      Array_e1[pos] = w1*P + w2*Array_e1[pos - 1];
      Array_e2[pos] = w1*Array_e1[pos] + w2*Array_e2[pos - 1];
      Array_e3[pos] = w1*Array_e2[pos] + w2*Array_e3[pos - 1];
      Array_e4[pos] = w1*Array_e3[pos] + w2*Array_e4[pos - 1];
      Array_e5[pos] = w1*Array_e4[pos] + w2*Array_e5[pos - 1];
      Array_e6[pos] = w1*Array_e5[pos] + w2*Array_e6[pos - 1];

      Ave = c1*Array_e6[pos] + c2*Array_e5[pos] + c3*Array_e4[pos] + c4*Array_e3[pos];

      if((Subgraph_direction[pos-1]==1 && sc.Close[pos]<(Subgraph_loclose[pos-1]))
        ||
        (Subgraph_direction[pos-1]==-1 && sc.Close[pos]>(Subgraph_hiclose[pos-1])))
      {
        if(Subgraph_direction[pos-1]==1)
        {
          Subgraph_direction[pos] = -1; //reverse short
          Subgraph_hiclose[pos] = sc.Close[pos]+(Ave*Input_ATRFactor.GetFloat());
          Subgraph_loclose[pos] = 0;
        }
        else
        {
          Subgraph_direction[pos] = 1; //reverse long
          Subgraph_loclose[pos] = sc.Close[pos]-(Ave*Input_ATRFactor.GetFloat());
          Subgraph_hiclose[pos] = 0;
        }
      }
      else
      {
        if(Subgraph_direction[pos-1]==1)
        {
          if(sc.Close[pos]-(Ave*Input_ATRFactor.GetFloat())>Subgraph_loclose[pos-1])
          {
            Subgraph_loclose[pos] = sc.Close[pos]-(Ave*Input_ATRFactor.GetFloat());
            Subgraph_hiclose[pos] = Subgraph_hiclose[pos-1];
          }
          else
          {
            Subgraph_loclose[pos] = Subgraph_loclose[pos-1];
            Subgraph_hiclose[pos] = Subgraph_hiclose[pos-1];
          }
        }
        else
        {
          if(sc.Close[pos]+(Ave*Input_ATRFactor.GetFloat())<Subgraph_hiclose[pos-1])
          {
            Subgraph_loclose[pos] = Subgraph_loclose[pos-1];
            Subgraph_hiclose[pos] = sc.Close[pos]+(Ave*Input_ATRFactor.GetFloat());
          }
          else
          {
            Subgraph_loclose[pos] = Subgraph_loclose[pos-1];
            Subgraph_hiclose[pos] = Subgraph_hiclose[pos-1];
          }
        };

        Subgraph_direction[pos] = Subgraph_direction[pos-1]; // no change
      };

      if(Subgraph_direction[pos]==1)
      {
        if(Input_Use2Subgraphs.GetYesNo() == false)
        {
          Subgraph_TSUp[pos] = (Subgraph_loclose[pos]);
          Subgraph_TSUp.DataColor[pos] = Subgraph_TSUp.PrimaryColor;
        }
        else
        {
          Subgraph_TSUp[pos] = (Subgraph_loclose[pos]);
          Subgraph_TSDown[pos] = 0;
        }
      }
      else
      {
        if(Input_Use2Subgraphs.GetYesNo() == false)
        {
          Subgraph_TSUp[pos] = (Subgraph_hiclose[pos]);
          Subgraph_TSUp.DataColor[pos] = Subgraph_TSUp.SecondaryColor;
        }
        else
        {
          Subgraph_TSDown[pos] = (Subgraph_hiclose[pos]);
          Subgraph_TSUp[pos] = 0;
        }
      }  
    }

Date Time Of Last Edit: 2022-01-17 21:04:52
imageTOS ATR example.PNG / V - Attached On 2022-01-17 20:59:45 UTC - Size: 11.68 KB - 279 views
[2022-01-17 21:48:39]
ertrader - Posts: 682
Have your tried the Volatility trend indicator study? It may work for you as it is similar with the different MA types too.
Date Time Of Last Edit: 2022-01-17 21:53:09
[2022-01-17 22:42:58]
Kiwi - Posts: 375
In the code you'll find two lines (spaces may be different but):

Subgraph_diff2[pos] = max(Lref , max(HiLo, Href));
sc.MovingAverage(Subgraph_diff2, Subgraph_atrmod, MOVAVGTYPE_WILDERS, pos, period);

Just change MOVAVGTYPE_WILDERS to MOVAVGTYPE_SIMPLE to change the moving average type.
[2022-01-22 16:25:38]
User61576 - Posts: 445
What this atr trail stop do? Moving the stop based on recent atr?
[2022-01-24 21:48:27]
Kiwi - Posts: 375
Yes. Its also called a Chandelier Trailing stop and was based on Chuck Le Beau's work.

https://duckduckgo.com/?q=chuck+le+beau+trailing+stop&t=braveed&ia=web
Date Time Of Last Edit: 2022-01-24 21:49:56
[2022-01-27 17:49:28]
tuanluu20902 - Posts: 24
Thanks a lot for the help kiwi! Yeah I thought of that solution but really did not know if that would do it since I did not understand half of your codes haha.

To post a message in this thread, you need to log in with your Sierra Chart account:

Login

Login Page - Create Account