# Slight modification in a Awesome Oscillator code

Hello,

I got this code from Internet:

``````/*
Bill William's Alligator System II Awesome Oscillator, modified from williamAlligator2.
Reference Website:
==================
http://www.alpari-idc.com/en/market-analysis-guide/chaos-theory/alligator-and-gator.html

Original by by Steve Wiser - slwiserr@erols.com
Modified by TohMz  on June 9th, 2008
*/
SetChartOptions( 0, chartShowArrows|chartShowDates );

_SECTION_BEGIN("BW Alligator");
/*** The trend indicators ***/

P= ParamList("Price", "Close|(H+L)/2|(H+C+L)/3",1);

if (P=="Close")
A = C;

else
if (P=="(H+C+L)/3")
A = (H+C+L)/3;
else
A = (H+L)/2;

AlligatorJaw   = Ref(Wilders(A,13),-8);
AlligatorTeeth = Ref(Wilders(A,8), -5);
AlligatorLips  = Ref(Wilders(A,5), -3);

AO=MA(A,5)-MA(A,34);
AOcolor=IIf(AO>Ref(AO,-1),colorGreen,colorRed);

AC=AO-MA(AO,5);
ACcolor=IIf(AC>Ref(AC,-1),colorGreen,colorRed);

Plot(AO,"AO",AOcolor,styleHistogram|styleThick);

_SECTION_END();

_SECTION_BEGIN("BW Fractal");

UpFractal= ValueWhen(
(Ref(H,-2) > Ref(H, -4)) AND
(Ref(H,-2) > Ref(H, -3)) AND
(Ref(H,-2) > Ref(H, -1)) AND
(Ref(H,-2) > H), Ref(H,-2));

DownFractal= ValueWhen(
(Ref(L,-2) <= Ref(L, -4)) AND
(Ref(L,-2) <=  Ref(L, -3)) AND
(Ref(L,-2) <=  Ref(L, -1)) AND
(Ref(L,-2) <=  L), Ref(L,-2));

_SECTION_END();

_SECTION_BEGIN("Exploration");

/*
Buy:  Scan stocks only breakout..maxbreakout (1~30%, default) and Trend is bullish
Sell: Scan stocks only breakout..maxbreakout (1~30%, default) and Trend is bearish

*/
//== Price Increment Value - depend on different country
Inc = 1;

//== Set the Price Range for stock to scan
PriceFrom = Param("Price From:", 50,   1, 200000, Inc);
PriceTo   = Param("Price To:",   200000, 1, 200000, Inc);
MaxBreakOut = Param("Max Breakout (%)", 5, 1, 30);
MaxBreakOut = MaxBreakOut/100;

Buy  = C>UpFractal AND C<=(1+MaxBreakOut)*UpFractal AND AlligatorTeeth>AlligatorJaw; /* AND AlligatorLips>AlligatorTeeth; */
Sell = C<DownFractal AND C>=(1-MaxBreakOut)*DownFractal AND AlligatorTeeth<AlligatorJaw; /* AND AlligatorLips<AlligatorTeeth; */

Filter = (Buy OR Sell) AND  (MA(V,3)/EMA(V,17))>1;

AddTextColumn(FullName(), "Security", 1.0, colorDefault, colorDefault, 200);

SetSortColumns(-2);

_SECTION_END();

GfxSetOverlayMode(1);
GfxSelectFont("Tahoma", Status("pxheight")/6 );
GfxSetTextAlign( 6 );// center alignment
GfxSetTextColor( ParamColor( "Warna", colorLightGrey ) );
GfxSetBkMode(0); // transparent
GfxTextOut( "Awesome", Status("pxwidth")/2, Status("pxheight")/2 );
``````

It plots histograms on the chart and generates buy and sell signals based on combination of certain conditions. I am more interested in the exploration part of it.

After setting the time range as 27th March, it generates a few results - one of the stock is Apollo Tyre, the hourly chart of which is as below: If the histograms start forming above 0, it's a bullish trend and vice versa. But the code also considers other conditions before generating buy signal. But I want is simple - the moment histogram crosses above 0, it should give me buy signal.

For example - if the last histogram was at -4 and the next one starts forming above 0, that's when I want a buy signal to be generated. I have added some annotations onto the same chart as above to better illustrate what I'm looking for: As you can see in the above screenshot, I am only looking for crossovers, i.e, when histogram which was formed below 0 starts forming new one above 0 will create buy signal and vice versa for sell.

Thanks!

@sarupria what you need is to change your signals to be triggered by the crossing above and below zero for your indicator that is used in that histogram. So read up and practice using the `Cross` function.

http://www.amibroker.com/guide/afl/cross.html

And make some slight changes in your code, then have fun with it and experiment with other possible changes ``````Buy = Cross(AO, 0);
Sell = Cross(0, AO);
`````` 2 Likes

Hi,

I'm trying to add a few more conditions to the above code like checking if MACD is above Signal, supertrend direction is up and vice versa to generate signal. Please note I'm looking for crossover of all these indicators at the same time but if one crossover happens like if MACD crosses Signal then I check what is the current state of Supertrend, if it is also in uptrend along with Awesome Oscillator then buy signal is generated.

I'm using the below code:

``````P= ParamList("Price", "Close|(H+L)/2|(H+C+L)/3",1);

if (P=="Close")
A = C;

else
if (P=="(H+C+L)/3")
A = (H+C+L)/3;
else
A = (H+L)/2;

AlligatorJaw   = Ref(Wilders(A,13),-8);
AlligatorTeeth = Ref(Wilders(A,8), -5);
AlligatorLips  = Ref(Wilders(A,5), -3);

AO=MA(A,5)-MA(A,34);

// Supertrend Section

HalfLife	= param("channel halflife", 5, 1, 20, 1);
ChanLen	= param("channel length", 20 , 1, 20, 1);

shrink		= 2^(-HalfLife);
shrink2	= 1 - shrink;

topChan	= High;
botChan	= Low;

HH	= HHV(High, ChanLen);
LL	= LLV(Low, Chanlen);

for( i = 1; i < BarCount-1; i++ ) {
topChan[i] = shrink * H[i] + shrink2 * topChan[i-1] ;
botChan[i] = shrink * L[i] + shrink2 * botChan[i-1] ;
if (HH[i] >= topChan[i])	topChan[i] = HH[i];
if (LL[i] <= botChan[i])	botChan[i] = LL[i];
}

Up=topChan;

Dn=botChan;

TrendUp=TrendDown=Null;

trend=1;

changeOfTrend=0;

flag=flagh=0;

for (i = 1; i <BarCount-1; i++) {

TrendUp[i] = Null;

TrendDown[i] = Null;

trend[i]=1;

if (Close[i]>Up[i-1]) {

trend[i]=1;

if (trend[i-1] == -1) changeOfTrend = 1;

}

else if (Close[i]<Dn[i-1]) {

trend[i]=-1;

if (trend[i-1] == 1) changeOfTrend = 1;

}

else if (trend[i-1]==1) {

trend[i]=1;

changeOfTrend = 0;

}

else if (trend[i-1]==-1) {

trend[i]=-1;

changeOfTrend = 0;

}

if (trend[i]<0 && trend[i-1]>0) {

flag=1;

}

else {

flag=0;

}

if (trend[i]>0 && trend[i-1]<0) {

flagh=1;

}

else {

flagh=0;

}

if (trend[i]>0 && Dn[i]<Dn[i-1]){

Dn[i]=Dn[i-1];

}

if (trend[i]<0 && Up[i]>Up[i-1])

{ Up[i]=Up[i-1];

}

if (flag==1)

{  Up[i]=topchan[i];;

}

if (flagh==1)

{ Dn[i]=botChan[i];;

}

if (trend[i]==1) {

TrendUp[i]=Dn[i];

if (changeOfTrend == 1) {

TrendUp[i-1] = TrendDown[i-1];

changeOfTrend = 0;

}

}

else if (trend[i]==-1) {

TrendDown[i]=Up[i];

if (changeOfTrend == 1) {

TrendDown[i-1] = TrendUp[i-1];

changeOfTrend = 0;

}

}

}

supertrendUp = trend==1;

supertrendDown = trend==-1;

// Supertrend Section End
tenBarMA = MA(Close, 10) > MA(Close, 20);

Buy = (Cross(AO, 0) AND MACD() > Signal()) OR (Cross(MACD(), Signal()) AND AO > 0) AND tenBarMA AND supertrendUp;
Sell = (Cross(0, AO) AND MACD() < Signal()) OR (Cross(Signal(), MACD()) AND AO < 0) AND !tenBarMA AND supertrendDown;

SetSortColumns(-3);

``````

I am not getting the intented results, for example - I am getting results in which 1 or 2 conditions are met but not all and they are still showing in scan results. For example - If Supertrend is up and AO is also greater than 0 but if tenBarMA is false, it will still show as Buy.

A few of the results do match the criteria, i.e, all 3 conditions are satisfied but most of the times it doesn't happen.

Please look into the above code as I'm unable to find the error.

@sarupria, after having only a quick glance at your code, and issue description, here is what I think you are missing.

Realize that Cross is an impulse signal, and ">" is a State signal. If you use State signals, you can use "Exrem" function to eliminate the duplicate signals.

Additionally, you appear to be doing looping, instead of taking advantage of the array processing power of AFL. I know it is a bit of thinking shift, but it would probably simplify your code immensely.

Last but not least, if you use the Exploration, you can show the variable and states of the signals and see where you don't get the results you want.

Hi,

I have modified the code a bit and it now works:

``````Buy = (Cross(AO, 0) AND MACD() > Signal() AND trend == 1 AND MA(Close, 10) > MA(Close, 20)) OR (Cross(MACD(), Signal()) AND AO > 0 AND trend == 1 AND MA(Close, 10) > MA(Close, 20));
Sell = (Cross(0, AO) AND MACD() < Signal() AND trend == -1 AND MA(Close, 10) < MA(Close, 20)) OR (Cross(Signal(), MACD()) AND AO < 0 AND trend == -1 AND MA(Close, 10) < MA(Close, 20));
``````

The above code works and is giving me desired results but I still don't understand why this works and the previous one doesn't because to me, both code looks pretty much same.

1 Like

@sarupria, While I can't quickly find a reference in the manual, I am (reasonably) sure that the AND and the OR logical operators work on the two adjacent conditions.

You would have to very carefully dissect the logic to see any differences between the two. Watch for misplaced end brackets that drastically change how the logic is evaluated.