Code with 2 conditions: Close above/below 50 Day SMA AND RSI(14) >/< 50

Hello, I've written a code to generate:

  1. A buy signal when the S&P 500 price closes above the 50 Day SMA AND RSI(14) is > 50.
  2. A sell when the S&P 500 price closes below the 50 Day SMA AND RSI(14) is < 50.

My code is below. I checked it via "Code Check & Profile and it did not return any errors. However, my "PlotShapes" were not plotted on the chart. My database does include a symbol named ^SPX.

Any help would be greatly appreciated.

MA1 = MA(C,1); 
MA50 = MA(C,50);

Buy = Cross(MA1,MA50) AND RSI(14)>50;
BuyPrice = Close;

Sell = Cross(MA50,MA1) AND RSI(14)<50;
SellPrice = Close;

Plot (MA1, "MA1", colorGreen, styleDashed, 0, 0, 0, 0); 
Plot (MA50, "MA50", colorRed, styleDashed, 0, 0, 0, 0);

PlotShapes (Buy*shapeUpArrow,colorGreen, 0, Buy);   
PlotShapes (Sell*shapeDownArrow,colorRed, 0, Sell);

@MSJr, please, review the syntax for PlotShapes

You are using wrong values for the yposition - try to use the Close price:

PlotShapes (Buy*shapeUpArrow,colorGreen, 0, C);   
PlotShapes (Sell*shapeDownArrow,colorRed, 0, C);

@beppe, this worked!

I also reviewed the syntax for Plot Shapes.

Thank you!

1 Like

@beppe , after following your instructions in post #2 (reviewing the syntax for PlotShapes and using the Close price for the yposition) my revised code used to generate:

  1. A buy signal when the S&P 500 price closes above the 50 Day SMA AND RSI(14) is > 50
  2. A sell when the S&P 500 price closes below the 50 Day SMA AND RSI(14) is < 50

my revised code (below) worked correctly.

MA1 = MA(C,1); 
MA50 = MA(C,50);

Buy = Cross(MA1,MA50) AND RSI(14)>50;
BuyPrice = Close;

Sell = Cross(MA50,MA1) AND RSI(14)<50;
SellPrice = Close;

Plot (MA1, "MA1", colorGreen, styleDashed, 0, 0, 0, 0); 
Plot (MA10, "MA50", colorRed, styleDashed, 0, 0, 0, 0);

PlotShapes (Buy*shapeUpArrow,colorGreen, 0, C);   
PlotShapes (Sell*shapeDownArrow,colorRed, 0, C);

I then wrote the following code to back test the 50 day SMA with the ROC indicator instead of the RSI indicator (see code below). Sadly and apologetically the code did not work. What might I be missing? Again, any help would be greatly appreciated.

MA1 = MA(C,1); 
MA50 = MA(C,50);

Buy = Cross(MA1,MA50) AND ROC(14)>0;
BuyPrice = Close;

Sell = Cross(MA50,MA1) AND ROC(14)<0;
SellPrice = Close;

Plot (MA1, "MA1", colorGreen, styleDashed, 0, 0, 0, 0); 
Plot (MA50, "MA50", colorRed, styleDashed, 0, 0, 0, 0);

PlotShapes (Buy*shapeUpArrow,colorGreen, 0, C);   
PlotShapes (Sell*shapeDownArrow,colorRed, 0, C);

@MSJr, once again you are calling a function in the wrong way!!!

See the documentation for the ROC() function.
You need to specify the ARRAY (price series) used to calculate the periods rate-of-change expressed as percentage.

Example:

Roc10 = ROC(C, 10);

I kindly suggest saving this link:

AFL Function Reference

and use it to learn more about to properly invoke the Amibroker functions.

@beppe, this worked! I reviewed the AFL Reference documentation again (several times before I posted my latest request) I think the "lights finally came on". I also saved the link to the AFL Function Reference on my electronic scratch pad which I use for writing trial code.

Thank you for your patience, sharing your knowledge, and helping me better understand another aspect of AFL coding.

1 Like

@beppe , I spent all weekend reviewing the the AmiBroker Guide and Forum, google, YouTube, etc. and found errors in my original code posted Oct 4th e.g. AND RSI, Plot (MA10, "MA50", Buy and Sell Plots. So...starting from ground zero...the above code was not correct and therefore no solution achieved. My code below for back testing the S&P 500 "Long" only did work:

MA1 = MA(C,1); 
MA50 = MA(C,50);

Buy = Cross(MA1,MA50) AND RSI(14)>50;
BuyPrice = Close;

Sell = Cross(MA50,MA1) AND RSI(14)<50;
SellPrice = Close;

Plot (MA1, "MA1", colorGreen, styleDashed, 0, 0, 0, 0); 
Plot (MA50, "MA50", colorRed, styleDashed, 0, 0, 0, 0);

PlotShapes (Buy*shapeUpArrow,colorGreen, 0, C);   
PlotShapes (Sell*shapeDownArrow,colorRed, 0, C);

I now must learn how to write code that will enable me to 1) back test "Long" and "Short", 2) back test 2 conditions "Long", and back test 2 conditions "Long and Short".

@beppe , Sorry...A correction to my post 37 minutes ago. I spent all weekend reviewing the the AmiBroker Guide and Forum, google, YouTube, etc. and found errors in my original code posted Oct 4th e.g. AND RSI, Plot (MA10, "MA50", Buy and Sell Plots. So...starting from ground zero...the above code was not correct and therefore no solution achieved. My code below for back testing the S&P 500 "Long" only did work "except for PlotShapes".

I now must learn how to write code that will enable me to 1) back test "Long" and "Short", 2) back test 2 conditions "Long", 3) back test 2 conditions "Long and Short", and 4) PlotShapes.

@beppe has kindly already given you the solution for the code you posted!

@TrendSurfer @beppe , Geez, sorry again. You're absolutely correct. I think I need to get some shuteye and or take a coding break.

Given the code Giuseppe kindly helped me with did indeed provide a solution, is the correct protocol here to delete my last to posts above? I was referred to the wrong code.

The code I meant to refer to is the one I posted on Oct 13 entitled "Sell Plot Shapes are not plotting in Long Short Back Test" . My attempted fixes i.e., variations of Syntax initialization and lines of Buy/Sell, Sell/Short, Cover, code, etc. but have been unsuccessful.

Michael...you replied to that one with "Sell = 0". ??? I am at wits end...can you please help.

Thank you. Michael S Jr