Login Page - Create Account

Support Board


Date/Time: Thu, 06 Mar 2025 09:57:08 +0000



Post From: Revising Kiwi's ATR Trailstop to take over average calculation

[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