Dynamic Trailing Stop


Category:
0
0

  I am trying to script a dynamic trailing stop on a daily chart. I can plot the current price with a horizontal line and plot a second line to follow as the price rises and falls. What I can’t figure out is how to get the second line to stop when the current price line falls. Any help would be greatly appreciated. Here is the code I have written so far:

input StopDiff = 00.00;

def Diff = StopDiff;

def vClose = close;

def nan = double.NaN;

def highestClose = HighestAll(if IsNaN(vClose[-1]) then highest (vClose) else nan);

plot hc = highestClose;

hc.SetPaintingStrategy(PaintingStrategy.Horizontal);

hc.SetDefaultColor(Color.DARK_GREEN);

hc.SetLineWeight(2);

hc.HideBubble();

hc.HideTitle();

def TrailingStop = if high<=(hc) then (hc)-Diff else high – diff;

plot TS = hc-Diff;

TS.SetPaintingStrategy(PaintingStrategy.Horizontal);

TS.SetDefaultColor(Color.Blue);

TS.SetLineWeight(2);

Marked as spam
Posted by (Questions: 1, Answers: 0)
Asked on April 20, 2020 9:11 pm
139 views
0
Private answer

You have light-years to go before you are close to solving this. There are two built-in chart studies that plot trailing stops. I'm not sure what you mean by "dynamic" trailing stop. But if you want to solve this I suggest you examine the code in the following studies included with Thinkorswim:

ATRTrailingStop and ParabolicSAR

If those don't provide the solution you are seeking I don't have anything more to offer. In order to solve this for you would take far more time to complete than I can provide free of charge in the Q&A forum.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on April 21, 2020 7:32 am