Rubiks
Rubiks

Reputation: 481

Coding divergence in AFL

I am trying to code divergence using AFL in amibroker. The program is a block based divergence. So it should loop through each block and compare the value of the blocks as it searched for peaks and troughs. It will then store them into peak and trough arrays. It will then loop through the arrays and check for positive and negative divergence and place buy and sell signals. The program should place buy signals on negative divergence and sell signals on positive divergence. Right now the program places buy and sell signals ever where. Any help would be appreciated.

indi= (MACD(12,26) - Signal(12,26,9));

indiPeakIndex =1;
indiPeakCounter = 1;
indiTroughIndex =1;
indiTroughCounter = 1;



for(i = 4; i < BarCount-1; i++)
{
    if((indi[i-1] > indi[i]) AND (indi[i-1] > indi[i-2]) AND (indi[i-2] > indi[i-3]))//Find Peaks
    {
        indiPeakIndex[indiPeakCounter] = i -1; //Store peaks
        indiPeakCounter++;
    }

    else if((indi[i-1] < indi[i]) AND (indi[i-1] < indi[i-2]) AND (indi[i-2] < indi[i-3])) //Find troughs
    {
        indiTroughIndex[indiTroughCounter] = i -1; //Store troughs
        indiTroughCounter++;
    }
}

for(i =1; i < indiPeakCounter-1; i++) //Loop through peaks and check for negative divergence
{
    if((indi[indiPeakIndex[i]] > indi[indiPeakIndex[i+1]]) AND (High[indiPeakIndex[i]] < high[indiPeakIndex[i+1]])) 
    {
        Buy[i] = Open[indiPeakIndex[i+1] + 1];  


    }
}
for(i =1; i < indiTroughCounter-1; i++) //Positive Divergence
{
    if((indi[indiTroughIndex[i]] < indi[indiTroughIndex[i+1]]) AND (High[indiTroughIndex[i]] > high[indiTroughIndex[i+1]])) //Loop through troughs and check for positive divergence
    {

        Sell[i] = Open[indiTroughIndex[i+1] + 1];   
    }
}


buy = ExRem( buy, sell );
sell = ExRem( sell, buy );

Upvotes: 0

Views: 1363

Answers (1)

Sethmo011
Sethmo011

Reputation: 601

Rather stay away from loops if you can help it. Take advantage of the fact that Amibroker operates on arrays. How AFL Works

Not tested, but try something like:

//set the peak condition
peakCond= (Ref(indi,-1) > indi) AND (Ref(indi,-1) > Ref(indi,-2)) AND (Ref(indi,-2) > Ref(indi,-3));

//set all the places the peaks occur
indiPeaks = IIf(peakCond,1, 0);

Then

Buy = indiPeaks;  

Upvotes: 0

Related Questions