Michael,
I have this problem also and came up with this code with the help of
TJ's example code from AB Help. It uses loops to find the actual
entry details. All comments and insights welcome. Using array AFL
only works if there are no redundant buy signals and exrem is used.
Yes horse & cart argument, since sell signal not known if money
management only used ie. sell=0;. Hence the need for loops. I would
like to take this opportunity to thank you for all your help on this
and other forums.
franc
//-------------------------------------------------------------------------------------
x = Cum(1);
buy = put your signals here
sell = 0;
InitialStop = put your code here
//-------------------------------------------------------------------------------------
//STOPS - worst first
//-------------------------------------------------------------------------------------
//Breakeven
//Stop at breakeven price once threshold reached
//BB-BarAtBuy, bar-current bar, perc-threshold percent
function BrkEvenStop(bar,BB,perc)
{
BrkEven = 0;
threshold = BuyPrice[BB]*(1+perc);
if( C[bar] > InitialStop[BB] )
{
//search back for threshold, then set BrkEven,
//within limits j=0...barcount
for( j=Bar; j>=BB AND j>=0 AND j<BarCount AND BrkEven==0; j-- )
{
if( C[j] >= threshold ) //threshold found
{ BrkEven = 1; }
}
}
return ( C[bar] <= BuyPrice[BB] AND BrkEven )
OR C[bar] <= InitialStop[BB];
}
//-------------------------------------------------------------------------------------
//MAEStop
//Exit once Adverse Trade by perc amount
function MAEStop(bar,BB,perc)
{
return L[bar] <= BuyPrice[BB]*(1-perc);
}
//-------------------------------------------------------------------------------------
//MaxTradeExit
//Exit once a Target High has been reached
function MaxTradeExit(bar,BB,perc)
{
return H[bar] >= BuyPrice[BB]*(1+perc);
}
//-------------------------------------------------------------------------------------
//Check for valid Stops and Exits
//loops needed to find entryprice, for the following reasons
//1. cannot find entry or exit condition without stable flip operation
//2. Flip func messed up since Sell=0 (all exits are MoneyManagement)
//3. multiple Buy/Sell, (redundant signals present)
// since AA may enter 2 or 3rd buy andso not same bar as flip func
//4. exrem loses redundant entries all except 1st buy since no sells
BarAtBuy = 0;
MAECutoff = 0.025; //MAE percent
MaxExitValue = 0.50; //Target Exit percent
BrkEvenPt = 0.15; //Breakeven threshold percent
MAECode = 11; //select exit code(s)
MaxExitCode = 12;
BrkEvenCode = 13;
//from least to most important,
//because subsequent stop will over-write previous Exitcode
for( i=0; i<BarCount; i++ )
{
if( Buy[i] AND BarAtBuy==0 ) {BarAtBuy=x[i]-1;}
//cum(1)-1->>0..barcount
if( BarAtBuy>0 ) //allows same bar exit
{
BuyBar[i] = BarAtBuy;
if( MaxTradeExit(i,BarAtBuy,MaxExitValue) ){ExitCode=MaxExitCode;}
if( MAEStop(i,BarAtBuy,MAECutoff) ) {ExitCode=MAECode;}
if( BrkEvenStop(i,BarAtBuy,BrkEvenPt) ) {ExitCode=BrkEvenCode;}
}
else
{
Sell[i] = 0;
ExitCode = 0;
//cleans redundant sell signals
}
if(ExitCode>0)
{
Buy[i] = 0;
Sell[i] = ExitCode; //mark appropriate exit code
BarAtBuy = 0; //reset price
ExitCode = 0;
}
}
//-------------------------------------------------------------------------------------
--- In [email protected], "Michael.S.G." <[EMAIL PROTECTED]> wrote:
>
> I seem to be stuck in a rut and can not figure this out. Any help
would
> be greatly appreciated.
>
> I would like to correctly calculate the bars since a buy or sell
signal,
> Then find the HHV or LLV since that Buy or Sell signal.
> Seems simple enough, But my code is not producing the required results.
> ie. The BarsSinceBuy is NOT reseting after a Buy Sig.
> Here are two pictures showing the problem. And the code following.
>
> 1) Bar count after sell is correct. (ie Bars since SellSig = 7)
>
>
>
> 1) Bar count after buy is incorrect and should = 7. (ie Bars since
> BuySig = 195)
>
>
> CODE:
> _SECTION_BEGIN("TestSystem");
> //Init
> SetBarsRequired(10000,10000);
> Buy[0]=0;
> Sell[0]=0;
>
> // optimize values
> CMAV = Optimize("Common MA value", 20, 20, 80, 1 );
> BuyHook = Optimize("Buy Hook percent", 1, 1, 20, 1 );
> SellHook = Optimize("Sell Hook percent", 1, 1, 20, 1 );
>
> //Manual Overides
> CMAV = Param("Common MA value", 20, 10, 80, 1 );
> BuyHook = Param("Buy Hook percent", 2, 1, 20, 1 );
> SellHook = Param("Sell Hook percent", 2, 1, 20, 1 );
>
> z = Param("BuySell Delay", 1, 0, 20, 1 );
>
> // Calc
> CMA = MA(C,CMAV);
>
> BarsSinceBuy = BarsSince(Buy);
> SMAhhv = HHV(CMA, BarsSinceBuy);
> SMAmod = 1-((SellHook/1000));
> SMAadj = SMAmod * SMAhhv;
> Sell = Cross(SMAadj,CMA);
>
> BarsSinceSell = BarsSince(Sell);
> BMAllv = LLV(CMA, BarsSinceSell);
> BMAmod = ((BuyHook/1000))+1;
> BMAadj = BMAmod * BMAllv;
> Buy = Cross(CMA,BMAadj);
>
> Buy = ExRem(Buy,Sell);
> Sell = ExRem(Sell,Buy);
>
> Buyplt=Ref(Buy,-z);
> Sellplt=Ref(Sell,-z);
>
> // plot & title
> Color = IIf(O > C, colorBlack, colorYellow);
> Plot( Close, "Price", color, styleCandle);// | styleOwnScale );
> Plot( CMA, "Common MA", colorBlack, styleLine);
> PlotShapes( shapeSmallUpTriangle*Buyplt, colorGreen, 0, O, 10 );
> PlotShapes( shapeSmallDownTriangle*Sellplt, colorRed, 0, O, -10 );
>
> _N(Title = "Test System " + Date() + " Open "+O+" Hi "+H+" Low "+L+"
> Close "+C+" Volume "+WriteVal(V,1) + "\nBase Index:" + GetBaseIndex() +
> "\nDebug" +
> "\nBuy = "+Buy + " Sell = "+Sell +
> "\n" +
> "\nBars since BuySig: " + BarsSinceBuy +
> "\nSMAmod : " + SMAmod +
> "\nSMAhhv : " + SMAhhv +
> "\nCMA : " + CMA +
> "\nSMAadj : " + SMAadj +
> "\n" +
> "\nBars since SellSig: " + BarsSinceSell +
> "\nBMAmod : " + BMAmod +
> "\nBMAllv : " + BMAllv +
> "\nCMA : " + CMA +
> "\nBMAadj : " + BMAadj +
> "\n");
> _SECTION_END();
>
Please note that this group is for discussion between users only.
To get support from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
For other support material please check also:
http://www.amibroker.com/support.html
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/amibroker/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/