I've find two solutions:
the first is specifical for this aim:
ApplyStop( stopTypeNBar , stopModeBars, 2, 0 ) ;
Buy = MA(Close,10) > 0;
Sell = False;
Short = False;
Cover = False;
The second is useless in this case, but it suggests solutions for other similar
problems. In general using arrays in an initial for cycle can make possible
complex strategies:
FLAT = 0;
LONG_POS = 1;
Short_POS = 2;
position[0] = FLAT;
SetBarsRequired( sbrAll ) ;
Buycondition [0] = False;
Sellcondition[0] = False;
movingAv = MA(Close,10);
for( i = 1; i < BarCount; i++ )
{
Buycondition [i] = Close[i] > movingAv [i];
if(position[i-1] == FLAT AND Buycondition [i])
{
position[i] = LONG_POS;
}
else if(position[i-1] == LONG_POS)
{
if(Sellcondition[i])
{
position[i] = FLAT;
}
}
if(position[i] AND i+2 < BarCount )
{
Sellcondition[i+2] = True;
}
}
Buy = Ref(position,-1) == FLAT AND Ref(position, 0) == LONG_POS;
Sell = Sellcondition;
Short = False;
Cover = False;
PositionSize = MarginDeposit = 1;
________________________________
Da: reinsley <[email protected]>
A: [email protected]
Inviato: Ven 13 agosto 2010, 11:44:40
Oggetto: Re: [amibroker] How can I sell at N bars after buy?
maybe :
date of the last buy
DateArray = DateTime();
DateLastEntry = Ref( DateArray,-BarsSince(Buy) );
Plot(DateLastEntry ,"DateLastEntry ", colorWhite,styleLine+styleOwnScale);
>