hi, I can only suggest a way by writing your own stopcode, so it is a bit complicated maybe. I would not know how to do this using Applystop, you might be able to solve it using the ReEntryDelay parameter but I wouldn't know how.
What you need to do is trick the backtester to sell when an actual sell signal
occurs, so at the cross defined by sell = cross(ma(c,50),c) but use the sell
price that follows from the 5% stop. This way the backtester will think it
still owns the stock while in reality you already sold at the stop price. You
will have to set SetOption("PriceBoundChecking", False);
I wrote the code see below. Also a chart is added. In the chart you see a buy
above the green arrow. Three bars later the stop is hit and the stock is sold
at the price of the yellow circle. However, the actuall sell defined by the
cross occurs more than 4 months later. This is where you tell the backtester it
is sold. So the backtester thinks it is sold 4 months later for the price shown
at the stopline (or yellow circle).
Now lets hope I didn't misunderstood your question :)
rgds, Ed
p.s. didn't do a backtest check.
procedure sell_proc(Buy,Sell,stopPercentage)
{
global BuyAdjusted;
global BuyPriceAdjusted;
global SellAdjusted;
global SellPriceAdjusted;
global longStopArray;
global SellHelp;
global SellPriceHelp;
BuyAdjusted = 0;
BuyPriceAdjusted = 0;
SellAdjusted = 0;
SellPriceAdjusted = 0;
longStopArray = Null;
SellPriceHelp = 0;
SellHelp = 0;
delay = 1;
slip = 0.0;
for( i = 1; i < BarCount; i++ )
{
if ( Buy[ i ])
{
BuyAdjusted[ i ] = 1;
BuyPriceAdjusted[ i ] = BuyPrice[ i ] + slip;
longStopArray[ i ] = BuyPriceAdjusted[ i ] - BuyPriceAdjusted[ i
]/100*stopPercentage;
flag = 0;
for (j = i + delay; j < BarCount; j++)
{
longStopArray[ j ] = longStopArray[ i ];
if (L[ j ] < longStopArray[ j ] AND flag == 0)
{
flag = 1;
sellPriceHelpVar = Min(O[ j ],longStopArray[ j ]);
SellPriceHelp[ j ] = Min(O[ j ],longStopArray[ j ]);
SellHelp[ j ] = 1;
}
else if (Sell[ j ] AND flag == 0)
{
SellAdjusted[ j ] = 1;
SellPriceAdjusted[ j ] = SellPrice[ j ] - slip;
i = j;
break;
}
else if (Sell[ j ] AND flag == 1)
{
SellAdjusted[ j ] = 1;
SellPriceAdjusted[ j ] = sellPriceHelpVar - slip;
i = j;
break;
}
else if (j == BarCount - 1)
{
i = BarCount;
}
}
}
}
}
SetOption("PriceBoundChecking", False);
SetBarsRequired(10000,10000);
SetOption("MaxOpenPositions", 3 );
SetTradeDelays(0,0,0,0);
PositionSize = -100/3;
Buy = Cross(C,MA(C,200)); Buy = Ref(Buy,-1); BuyPrice = O;
Sell = Cross(MA(C,50),C); Sell = Ref(Sell,-1); SellPrice = O;
stopPercentage = 5;
sell_proc(Buy,Sell,stopPercentage);
Buy = BuyAdjusted;
BuyPrice = BuyPriceAdjusted;
Sell = SellAdjusted;
SellPrice = SellPriceAdjusted;
SetChartOptions(0, chartShowDates);
Plot(C,"Last=",colorBlack,64);
PlotShapes(IIf(Buy,shapeUpArrow,shapeNone),colorGreen,0,L,-15);
PlotShapes(IIf(Buy,shapeHollowUpArrow,shapeNone),colorWhite,0,L,-15);
PlotShapes(IIf(Buy,shapeSmallCircle,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,shapeSmallCircle,shapeNone),colorWhite,0,SellPrice,0);
PlotShapes(IIf(SellHelp,shapeSmallDownTriangle,shapeNone),colorRed,0,H,-15);
PlotShapes(IIf(SellHelp,shapeHollowSmallDownTriangle,shapeNone),colorWhite,0,H,-15);
PlotShapes(IIf(SellHelp,shapeCircle,shapeNone),colorYellow,0,SellPriceHelp,0);
Plot(LongStopArray,"longStopArray",colorWhite,1);
Plot(MA(C,200),"MA(C,200)",colorYellow,1);
Plot(MA(C,50),"MA(C,50)",colorAqua,1);
----- Original Message -----
From: onelkm
To: [email protected]
Sent: Sunday, March 08, 2009 2:24 PM
Subject: [amibroker] Re: How to control delaying "some" new buys?
Again ... Can anyone suggest a way to do this?
--- In [email protected], "onelkm" <lkm...@...> wrote:
>
> In the example below, I buy 3 stocks in a watch list based on the Buy
condition. Let's say stock 1 stopped out, so the next day the system buys a new
stock, stock 1A, to replace it and again holds 3 stocks. But, I want to only
buy and replace stock 1 after stock 1 has met the sell condition, which could
be several days later. Thus the system would only hold 2 stocks for a period of
time until the sell condition is met. Is there a way to do this?
>
> SetTradeDelays(1,1,1,1);
> PositionSize = -100/3;
> Buy = Cross(C,MA(C,200));
> Sell = Cross(MA(C,50),C);
> ApplyStop(stopTypeLoss, stopModePercent, 5);
>
> Thanks in advance
> Larry
>
<<pp1.png>>
