Need help for AFL

#1
Hi,

I got this code from amibroker website and would like to include the buy/long condition is reverse to the short . Could someone please help me out ? Thanks

fCl = Ref(C,-1);
fHigh = Ref(H,-1);
fLow = Ref(L,-1);
fAdvDrop = fCl - Ref(C,-2);
fAtr = Ref(ATR(8),-1);
diffHl = fHigh-fLow;
fStoch = Ref(StochD(14),-1);
fBBandTop=Ref(BBandTop(C,20,2),-1);
fOpCond = fCl - 0.5 * fAtr;
fOp = Ref(O,0);
fStopLoss = fHigh + 0.5 * fAtr;

bbTopSell =
// The stock must first puncture and close outside (above) the upper Bollinger Band
IIf( fCl > fBBandTop AND
// The closer the closing price is to the high of the day, the better.
fCl > (fLow + 0.75 * diffHL ) AND
// And the bigger the day's advance, the better
fAdvDrop > (1.5 * fAtr) AND
// On the following day, the stock must 'gap' down below the prior day's close.
// This 'gap down' is crucial as it serves as the most important criteria of
// the entire strategy.
fOp < fOpCond AND
// Overbought condition added
fStoch > 80, 1, 0 );

/* Exploration Columns for Sorting */

NumColumns = 10;

Column0 = fOp;
Column1 = fOpCond;
Column2 = fStopLoss;

Column0Name = "Open";
Column1Name = "OpenCond";
Column2Name = "StopLoss";

Column0Format = 1.2;
Column1Format = 1.2;
Column2Format = 1.2;

Filter = bbTopSell == 1;

Buy = 0;
Sell = bbTopSell>0;
 

beginner_av

Well-Known Member
#2
dear tican2,

i have arranged your formula so that it is more understandable. pls see this. it is for ami4.8 or higher. else replace the addcolumns with the older one with numcolumns.

**************************************

fCl = Ref(C,-1); // close yesterday
fHigh = Ref(H,-1); // high yesterday
fLow = Ref(L,-1); // low yesterday
fAdvDrop = fCl - Ref(C,-2); // difference between yesterday and day before's close
fAtr = Ref(ATR(8),-1); // last 8 days Average True Range till yesterday
diffHl = fHigh-fLow; // difference between yesterday's high and low (range)
fStoch = Ref(StochD(14),-1); //14 day slow stochastics as on yeaterday
fBBandTop=Ref(BBandTop(C,20,2),-1); //bollinger band top
fOpCond = fCl - 0.5 * fAtr; // opening condition gap down by .5 times ATR(8) from yesterday's close
fOp = Ref(O,0); //today's opening
fStopLoss = fHigh + 0.5 * fAtr; // stp loss at yesterday's high + .5 times ATR(8)

// The stock must first puncture and close outside (above) the upper Bollinger Band

bbTopSell = IIf( fCl > fBBandTop AND
// The closer the closing price is to the high of the day, the better.
fCl > (fLow + 0.75 * diffHL ) AND
// And the bigger the day's advance, the better
fAdvDrop > (1.5 * fAtr)
// On the following day, the stock must 'gap' down below the prior day's close.
// This 'gap down' is crucial as it serves as the most important criteria of
// the entire strategy.
AND fOp < fOpCond AND
// Overbought condition added
fStoch > 80, 1, 0 );

/* Exploration Columns for Sorting */

//NumColumns = 10;

//Column0 = fOp;
//Column1 = fOpCond;
//Column2 = fStopLoss;

//Column0Name = "Open";
//Column1Name = "OpenCond";
//Column2Name = "StopLoss";

//Column0Format = 1.2;
//Column1Format = 1.2;
//Column2Format = 1.2;

AddColumn(fOp, "Open", 1.2);
AddColumn(fOpCond, "OpenCond", 1.2);
AddColumn(fStopLoss, "StopLoss", 1.2);
AddTextColumn(FullName(), "Stock Name");

Filter = bbTopSell == 1;

Buy = 0;
Sell = bbTopSell>0;


*************************************


now this formula has a big problem. by bollinger itself it is fine, but it does not take into account the direction of the trend. in case of strong trend, if u understand all the conditions you will see that a gap down of .5 * atr is very normal and prices can recover very rapidly in the direction of the main trend. see the attached charts on the last day for which signals were generated. the macd and all the three MAs are up...so this kind of trade becomes very risky as you are going against the trend EVEN WHEN PRICES ARE TRENDING UP. you are only depending upon an opening gap, which professionals will gladly fill.
and STOC or RSI or any OSC is of no use as during trends, they will remain in overbought zones, often > 80.
in spite of this do you want a downside break formula for buy? if u still want, i can try. btw pls read bb walk in bollinger on bollinger bands, where prices walk across the bb top during strong trends.
 

Attachments

beginner_av

Well-Known Member
#3
// Weekly MACD Histogran
_SECTION_BEGIN("Weekly MACD");
TimeFrameSet( inWeekly );
MACDw = MACD( 3, 10 ) - Signal( 3, 10, 16 );
MACDwLINE = MACD( 3, 10 ) ;
MACDwSignal = Signal( 3, 10, 16);
TimeFrameRestore();
Plot(MACDw,"MACD Weekly",colorDarkOliveGreen,styleHistogram);
Plot(MACDwLINE,"MACD Weekly Line",colorRed,styleLine);
Plot(MACDwSignal,"MACD Weekly Signal Line",colorBlue,styleLine);
_SECTION_END();


please get it checked by someone like sunil or praveen

daily macd

// Daily MACD Histogram
_SECTION_BEGIN("MACD");
r1 = Param( "Fast avg", 12, 2, 200, 1 );
r2 = Param( "Slow avg", 26, 2, 200, 1 );
r3 = Param( "Signal avg", 9, 2, 200, 1 );
Plot( ml = MACD(r1, r2), StrFormat(_SECTION_NAME()+"(%g,%g)", r1, r2),
ParamColor("MACD color", colorRed ), ParamStyle("MACD style") );
Plot( sl = Signal(r1,r2,r3), "Signal" + _PARAM_VALUES(), ParamColor("Signal
color", colorBlue ), ParamStyle("Signal style") );
Plot( ml-sl, "MACD Histogram", ParamColor("Histogram color", colorBlack ),
styleNoTitle | ParamStyle("Histogram style", styleHistogram | styleNoLabel,
maskHistogram ) );
_SECTION_END();

this was posted by someone in this forum...is it casoni?
 
#4
hi beginner_av,

Thanks you for your help and comments . I understand the risk there . What i am after is the coding for the reverse ( buy position ) .
Thanks
Tican2
 
#6
by reverse position do u mean buy when prices go and hit bb bottom and then gap up?
Hello Avi,

I think Tican is asking to edit the AFL, so that it gives BUY signal. If you see the AFL, it gives SELL signal alone.

I've not went through the AFL; so can't comment much on it.

Praveen.
 

beginner_av

Well-Known Member
#8
This is the exact reverse of what u have given - buy when prices go and hit bb bottom and then gap up


fCl = Ref(C,-1); // close yesterday
fHigh = Ref(H,-1); // high yesterday
fLow = Ref(L,-1); // low yesterday
fAdvDrop = Ref(C,-2) - fCl; // difference between yesterday and day before's close
//where yesterday's close < day b4 yesterday close

fAtr = Ref(ATR(8),-1); // last 8 days Average True Range till yesterday
diffHl = fHigh-fLow; // difference between yesterday's high and low (range)
fStoch = Ref(StochD(14),-1); //14 day slow stochastics as on yeaterday

fOpCond = fCl + 0.5 * fAtr; // opening condition gap up by .5 times ATR(8) from yesterday's Close

fOp = Ref(O,0); //today's opening

fStopLoss = fLow - 0.5 * fAtr; // stp loss at yesterday's low - .5 times ATR(8)

fBBandBottom=Ref(BBandBot(C,20,2),-1); //bollinger band bottom *****

// The stock must first puncture and close outside (below) the lower Bollinger Band
bbBottomBuy = IIf( fCl < fBBandBottom AND
// The closer the closing price is to the low of the day, the better.
fCl < (fHigh - 0.75 * diffHL ) AND
// And the bigger the previous day's decline, the better
fAdvDrop > (1.5 * fAtr) AND
// On the following day, the stock must 'gap' up above the prior day's close.
// This 'gap up' is crucial as it serves as the most important criteria of
// the entire strategy.

fOp > fOpCond AND

// Oversold condition added
fStoch < 80, 1, 0 );


/* Exploration Columns for Sorting */

AddColumn(fOp, "Open", 1.2);
AddColumn(fOpCond, "OpenCond", 1.2);
AddColumn(fStopLoss, "StopLoss", 1.2);
AddTextColumn(FullName(), "Stock Name");


Filter = bbBottomBuy == 1;

Sell = 0;
Buy = bbBottomBuy>0;


in case u r looking for something else, do let me know.
 
#9
hi Avi,

Thank you so much . That is what i really need . You have been a great help.
Just a quick question . In the code you mentioned :
// Oversold condition added
fStoch < 80, 1, 0 );

Don't you mean :
// Oversold condition added
fStoch < 30, 1, 0 );


Thanks
Tican
 

Similar threads