Trail stop loss

x = HHV(H,5);
y0 = LLV (L,5); // Initial Stop loss

Buy = cross (C, x);
Buyprice = C;
Sell = 0;
Applystop = (stoptypetrailing, stopmodepoint, stoplevel, true);

Having problem to code for stoplevel

My algorithm for calculating stoplevel is as follows:

C0 = buy price
C1 = next day close price
Initial SL = y0

if C1 <=y, then sell at y0
If C1>y and C1<=C0, SL will not change i.e. y1=y0
If C1>C0, new SL will be y1 = y0+ (C1-C0)

C2 = next day close price
if C2<= y1, then sell at y1
If C2>y1 and C2<=C1, SL will not change i.e. y2 = y1
If C2>C1 and C1>C0, new SL will be y2 = y1+ (C2-C1)
If C2>C1 and C1<C0, new SL will be y2 = y1+ (C2-C0)
process continue until C(i+1)<=y(i), then sell at y(i)

Read the whole topic, you will find many hints.

There are plenty of topics that cover these scenarios, search the forum for more.


See How to ask a good question.