hi,
you indeed need a loop to program this. Ararys only will not do it.
Below the code from how I understand it from your text.
regards, Ed
*procedure* sell_proc(*Buy*,*BuyPrice*,stp,hhvstp)
{
*global* BuyAdjusted;
*global* BuyPriceAdjusted;
*global* SellAdjusted;
*global* SellPriceAdjusted;
*global* stopArray;
BuyAdjusted = 0;
BuyPriceAdjusted = 0;
SellAdjusted = 0;
SellPriceAdjusted = 0;
stopArray = *Null*;
delay = 1;
slip = 0.0;
*for*( i = 1; i < *BarCount*; i++ )
{
*if* ( *Buy*[ i ])
{
BuyAdjusted[ i ] = 1;
BuyPriceAdjusted[ i ] = *BuyPrice*[ i ] + slip;
stopArray[ i ] = BuyPriceAdjusted[ i ] + stp[ i ];
*for* (j = i + delay; j < *BarCount*; j++)
{
*if* (hhvstp[ j ])
{
stopArray[ j ] = stopArray[ j - 1 ] + abs(*C*[j - 1] -
*C*[j - 2]);
}
*else*
{
stopArray[ j ] = stopArray[ j - 1 ] - abs(*C*[j - 1] -
*C*[j - 2]);
}
*if* (*C*[ j ] > stopArray[ j ])
{
SellAdjusted[ j ] = 1;
SellPriceAdjusted[ j ] = *C*[ j ] - slip;
i = j;
*break*;
}
*else* *if* (j == *BarCount* - 1)
{
i = *BarCount*;
}
}
}
}
}
SetBarsRequired(10000, 10000);
*Buy* = Cross(*C*,MA(*C*, 100)); *Buy* = Ref(*Buy*,-1);
*BuyPrice* = *O*;
// set initial exit limit at buy
stp = Ref(ATR(14)*4.5 + abs(*C*-Ref(*C*,-1)),-1);
// hhvstp is 1 when closing price is highest of the past 20 days, else 0
hhvstp = Ref(HHV(*C*,20) == *C*,-1);
sell_proc(*Buy*,*BuyPrice*,stp,hhvstp);
*Buy* = BuyAdjusted;
*BuyPrice* = BuyPriceAdjusted;
*Sell* = SellAdjusted;
*SellPrice* = SellPriceAdjusted;
SetChartOptions(0, *chartShowDates*);
*GraphXSpace* = 5;
Plot(*C*,"C",1,64);
Plot(stopArray,"stopArray",*colorWhite*,1);
Plot(MA(*C*,100),"ma",*colorYellow*,1);
PlotShapes(IIf(*Buy*,*shapeUpArrow*,*shapeNone*),*colorGreen*,0,*L*,-15);
PlotShapes(IIf(*Buy*,*shapeHollowUpArrow*,*shapeNone*),*colorWhite*,0,*L*,-15);
PlotShapes(IIf(*Buy*,*shapeHollowSmallCircle*,*shapeNone*),*colorWhite*,0,*BuyPrice*,0);
PlotShapes(IIf(*Sell*,*shapeDownArrow*,*shapeNone*),*colorRed*,0,*H*,-15);
PlotShapes(IIf(*Sell*,*shapeHollowDownArrow*,*shapeNone*),*colorWhite*,0,*H*,-15);
PlotShapes(IIf(*Sell*,*shapeHollowSmallCircle*,*shapeNone*),*colorWhite*,0,*SellPrice*,0);
----- Original Message -----
*From:* Anthony C. Abry <mailto:[email protected]>
*To:* [email protected] <mailto:[email protected]>
*Sent:* Tuesday, December 30, 2008 6:01 AM
*Subject:* [amibroker] Top Stop Exit programming question
Hi,
I am trying to program the Top Stop Exit as described by Volker
Knapp in
the September 08 edition of Active Trader Magazine.
The entry rule is a basic moving average cross (for the trend
strategy)
over but the exit is giving me trouble.
The exit uses a limit order which adjusts up or down depending on the
volatility.
The rules are:
Limit exit: 4.5 times the ATR(14) plus the absolute value of todays
closing price minus yesterdays closing price.
If the closing price is highest of the past 20 days, raise the
exit by
the absolute value of todays closing price minus yesterdays
closing price.
If the price fails to make a new 20 day high, lower the exit price by
the same amount.
Any help would be appreciated.
Thank you.
/* from active trader mag. sep 08, p.54 */
SetTradeDelays(1,0,1,0);
BuyPrice = C;
Buy = Cross(C,MA(C, 100));
Entryprice = 0;
Limitexitprice = 0;
for (i=0; i < BarCount; i++)
{
if (Entryprice == 0 AND Buy[i] == 1)
{
Entryprice = BuyPrice[i];
Limitexitprice = Entryprice + ATR(14)*4.5 + abs(C-Ref(C,-1));
}
else
if (Entryprice > 0 AND H[i] > Limitexitprice[i])
{
Sell[i] =1;
SellPrice[i] = Limitexitprice;
Entryprice = 0;
}
else
if (Entryprice > 0)
{
Limitexitprice = IIf(C > HHV(C,20),
Limitexitprice + ATR(14)*4.5 + abs(C-Ref(C,-1)),
Limitexitprice + ATR(14)*4.5 - abs(C-Ref(C,-1)));
}
}
Sell = Cross(C, Limitexitprice);
Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);