What's wrong with my AFL

Hi, i am newbie of AFL. I want to write a code about RSI.
The Buy Condition are:

  1. The close of day below RSI 30
  2. The next close of day goes up and over RSI 30
  3. The final close of day keeps over RSI 30 <- and then I buy.
    If Short:
  4. The close of day over RSI 70
  5. The next close of day goes down and below RSI 70
  6. The final close of day keeps over RSI 70 <- and then I short.

I want to try myself, here is my code:

Period = Param( "RSI period", 10, 2, 30, 1 );

OverBought = Period = 70;
OverSold = Period = 30;

BuySignal1 = Ref (Close, -2) < OverSold;
BuySignal2 = Ref (Close, -1) > OverSold;
BuySignal3 = Close > OverSold;
Buy = BuySignal1 + BuySignal2 + BuySignal3;

ShortSignal1= Ref (Close,-2) < Overbought;
ShortSignal2 = Ref (Close, -1) < Overbought;
ShortSignal3 = Close < Overbought;
Short = ShortSignal1 + ShortSignal2 + ShortSignal3;

Buy = ExRem( Buy, Short );
Short = ExRem( Short, Buy );

Cover = Buy;
Sell = Short;

SetPositionSize(1,spsShares);

The Code checking on Amibroker is fine, but it does not work at backtesting, could anyone tell my what's wrong with my AFL, many thanks.

I would strongly recommend reading Howard Bandy’s free PDF book


Then read his other excellent books if you intend real trading this may give you some insights in how to develop robust trading systems that won’t loose your shirt.

There is also lots of materials on the Amibroker knowledge base and manual.

Your code is a long way short of what is needed to work correctly
You need to use the RSI () function to generate buy,sell signals.

Hi @ken20111,

As a newbie AFL coder, I hope you will take the following suggestions as helpful...

  1. I am happy to see you have logical steps (1-6).
  2. Next time try using the CODE BUTTON ("</>" ) when posting your AFL. It helps keep it neater and allows others to copy it and directly use it.
  3. Variables: You are using "Period" but define/redefine it several times.
  4. Comparison: Your comparison to generate your BuySignalx is comparing Close Prices to an input/redefined number (Ref(Close,-2 ) < Oversold )
  5. Use of "+" instead of "AND"
  6. Comment your code - makes it easier when reviewing later, or as things get more complicated

Now I don't want you to feel overwhelmed, so let's look at some fixes...

This code is untested and WILL NOT WORK as is. Hopefully it shows you where you have gone astray, and points you in the direction for you to solve your own issues.

Period = Param("RSI Period", 10, 2, 30,1);  // Define the lookback period for use with RSI
vRSI = RSI(Period);  //Get the Value of the RSI

BuySignal1 = Ref(vRSI,-2) < 30; // Check of 2 bars ago, the value of RSI was less than 30
// you try your other signals

Buy = BuySignal1 AND BuySignal2 AND BuySignal3; //  AND together the Signals for the BUY Signal

//Hope this gets you started


Good job on defining your logic, and displaying your code. A Good example for others to see.

Hope you learn lots working through this.

Thanks for your recommendation, i read it before, but may because i don't have any programming background or my mother tongue is not English. I don't think I fully understand it. (even i may not notice "what part I don't understand") But thanks your reply anyway. may read it again later.

Hi snoopy.pa30,

Thank so much your kind help with your great patience, you taught me step by step that is really helpful to me.
With your help, it seems that i can code it myself.

Period = Param("RSI Period", 10, 2, 30,1);  // Define the lookback period for use with RSI
vRSI = RSI(Period);  //Get the Value of the RSI

BuySignal1 = Ref(vRSI,-2) < 30; // Check of 2 bars ago, the value of RSI was less than 30

BuySignal2 = Ref(vRSI,-1) > 30; // Check of 1 bar, the value of RSI was higher than 30

BuySignal3 = vRSI > 30; // Check the Trade Day, the value of RSI still keeps higher than 30 

Buy = BuySignal1 AND BuySignal2 AND BuySignal3; //  Use of "+" instead of "AND"

ShortSignal1 = Ref(vRSI,-2) > 70; // Check of 2 bars ago, the value of RSI was higher than 70

ShortSignal2 = Ref(vRSI,-1) < 70; // Check of 2 bars ago, the value of RSI was less than 70

ShortSignal3 = vRSI < 70; // Check the Trade Day, the value of RSI still keeps less than 70

Short = ShortSignal1 AND ShortSignal2 AND ShortSignal3;  //  Use of "+" instead of "AND"

Cover = Buy;
Sell = Short;

SetPositionSize(1,spsShares);

Do you mind to have a look, see is it 100% correct? thank a lot.