AFL stoch, macd, overbought

AFL stoch, macd, overbought

  1. StochK<30=>checking stoch crossup and macd crossup=>buy
  2. if line of stoch and line of macd cross up from 30 to 50 in range=>buy
  3. if next bar or next few bar can't crossup over 50 and drop down below 30=>sell
  4. if stoch crossdown and macd crossdown StochK>70=>sell

I coded as below and ran it in amibroker. The result was wrong.
I dont know where i coded wrong.

I would appreciate some assistance.

Thanks.
Tim

r1 = Param( "Macd Fast avg", 12, 2, 200, 1 );
r2 = Param( "Macd Slow avg", 26, 2, 200, 1 );
r3 = Param( "Macd Signal avg", 9, 2, 200, 1 ); 
myMacd = MACD(r1,r2);
mySignal = Signal(r1,r2,r3);
crossUpMACD = Cross( MACD(), Signal() );
crossDownMACD = Cross( Signal(), MACD() );
upMacd = IIf(myMacd  > mySignal, 1, 0);

sk=StochK(10,5);
sd=StochD(10,5,3);
uptrend= IIf( StochK(10,5) > StochD(10,5,3), 1, 0 );
downtrend=IIf( StochK(10,5)<StochD(10,5,3), 1, 0 );
crossBuy=Cross(StochK(10,5),StochD(10,5,3));
crossSell=Cross(StochD(10,5,3),StochK(10,5));

for(i=0;i<BarCount;i++){
    if(i>1){
        if(sk[i] <=30){
            bottomPos[i]=1;
            bmiddlePos[i]=0;
            bmiddlePos[i]=0;
            topPos[i]=0;
            if ((myMacd[i]  > mySignal[i]) AND (sk[i]>sd[i])){
                actionBuy[i]=1;
            }
            if(actionBuy[i] AND bmiddlePos[i]){
                actionSell[i]=1;
                bmiddlePos[i]=0;
            }
        }
        if(sk[i] >31 && sk[i] <=50){
            if (bottomPos[i]){
                bmiddlePos[i]=1;
                if ((myMacd[i]  > mySignal[i]) AND (sk[i]>sd[i])){
                    actionBuy[i]=1;
}
            }
            }else if(tmiddlePos[i]){
                bottomPos[i]=0;
                bmiddlePos[i]=0;
                tmiddlePos[i]=0;
                topPos[i]=0;
            }
        }    
        if(sk[i] >50 && sk[i] <=70){
            if (topPos[i]){
                tmiddlePos[i]=1;
            }else if(tmiddlePos[i]){
                bottomPos[i]=0;
                bmiddlePos[i]=0;
                tmiddlePos[i]=0;
                topPos[i]=0;
            }        
        }
        if(stochasticK[i] >70){
            bottomPos[i]=0;
            bmiddlePos[i]=0;
            tmiddlePos[i]=0;
            topPos[i]=1;
            if ((myMacd[i]  < mySignal[i]) AND (sk[i]<sd[i])){
                actionSell[i]=1;
            }
        }
    }
}

Buy =  actionBuy ;
Sell = actionSell; 
 
Buy=ExRem(Buy, Sell)
Sell=ExRem(Sell,Buy);

@colokit Tim,

Welcome to the forum.

Can't tell how much you know, only can see that you joined recently and have read for an hour. Good job on posting your code properly.

My first impression is that you don't really understand that AmiBroker uses Arrays processing. While I have not gone through your code, it is a common issue for people transitioning to think in terms of looping.

Generally with AFL and Array Processing, you will only very RARELY need loops.

So, my suggestions are to look at the following to get you going in the right direction:



Hopefully this will be a good start. THEN use the SEARCH tool (the magnifying glass in the top right) to find similar topics.

Hope this gets you going.

4 Likes