Does not provide proper signal

I am totally newbie and don't know anything about coding. I tried to write (rather search, copy paste) AFL codes which does not provide signal properly. It shows many sell, does not cover orders when target or stoploss is reached. Here is the code. Please help.

"_SECTION_BEGIN( "RSI" );

SetChartOptions( 0, chartShowArrows | chartShowDates );
Plot( C, "Close", ParamColor( "Color", colorBlack ), styleNoTitle | ParamStyle( "Style" ) | GetPriceStyle() );

Period = Param("RSI Period", 0, 0, 0, 0); // Define the lookback period for use with RSI

vRSI = RSI(8); //Get the Value of the RSI(8)
vRSI1= RSI(12); //Get the value of RSI(12)

BuySignal1 = Ref(vRSI,-1) <= 20; // Check of 1 bars ago, the value of RSI(8) was less than or equal to 20
BuySignal2 = vRSI > 20; // Check the current bar, the value of RSI(8) still keeps higher than 20
BuySignal3 = vRSI > Ref(vMFI,-1); // Value of RSI(8) should be increasing (higher than previous value)
BuySignal11= Ref(vRSI1, -1) <= 20; //Check of 1 bars ago, the value of RSI(12) was less than or equal to 20
BuySignal22= vRSI1 > 20; // Check the current bar, the value of RSI(12) still keeps higher than 20
BuySignal33 = vRSI1 > Ref(vRSI1,-1); // Value of RSI(12) should be increasing (higher than previous value)
ShortSignal1 = Ref(vRSI,-1) >= 80; // Check of 1 bars ago, the value of RSI(8) was more than or equal to 80
ShortSignal2 = vRSI < 80; // Check the current bar, the value of RSI(8) still keeps lower than 80
ShortSignal3 = vRSI < Ref(vRSI,-1); // Value of RSI(12) should be decreasing (lower than previous value)
ShortSignal11= Ref(vRSI1, -1) >= 80; //Check of 1 bars ago, the value of RSI(12) was more than or equal to 80
ShortSignal22= vRSI1 < 80; // Check the current bar, the value of RSI(12) still keeps lower than 80
ShortSignal33 = vRSI1 < Ref(vRSI1,-1); // Value of RSI(12) should be decreasing (lower than previous value)

TargetPrice = Param("TargetProfit",5,0,0,0);
StopPrice = Param("Stoploss",1,0,0,0);

SetPositionSize(1,spsShares); // Backtest Calculation for No of Points

Buy = BuySignal1 AND BuySignal2 AND BuySignal3 AND BuySignal33; // buy condition 1
Buy = BuySignal11 AND BuySignal22 AND BuySignal33 AND BuySignal3; // buy condition 2
Short = ShortSignal1 AND ShortSignal2 AND ShortSignal3 AND ShortSignal33; // Short condition 1
Short = ShortSignal11 AND ShortSignal22 AND ShortSignal33 AND ShortSignal3; // Short condition 2

Cover = Buy;
Sell = Short;

BuyPrice = ValueWhen(Buy,Close); //Enter at Open of the next candle.
TargetBuy = BuyPrice + TargetPrice;
StopBuy = BuyPrice - StopPrice;

Buy = ExRem(Buy,Sell);
Sell = ExRem(Sell,Buy);
BuyCont = Flip(Buy,Sell);
SellCont = Flip(Sell,Buy);

ShortPrice = ValueWhen(Short,Close);
TargetShort = ShortPrice - TargetPrice;
StopShort = ShortPrice + StopPrice;

Short = ExRem(Short,Cover);
Cover = ExRem(Cover,Short);
ShortCont = Flip(Short,Cover);
CoverCont = Flip(Cover,Short);

Cover = Cross(H,StopShort) OR Cross(TargetShort,L); //Exit if Target/Stop reached
CoverPrice = IIf(Cross(H,StopShort), StopShort, IIf(Cross(TargetShort,L), TargetShort, Close));
Sell = Cross(H,TargetPrice) OR Cross(StopPrice,L); //Exit if Target/Stop reached
SellPrice = IIf(Cross(H,TargetPrice), TargetPrice, IIf(Cross(TargetPrice,H), TargetPrice, Close));

PlotShapes( IIf( Buy, shapeUpArrow, shapeNone ), colorGreen, 0, L, Offset = -25 );
PlotShapes( IIf( Sell, shapeDownArrow, shapeNone ), colorRed, 0, H, Offset = -45 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorRed, 0, H, Offset = 40 );
PlotShapes( IIf( Short, shapeSquare, shapeNone ), colorOrange, 0, H, Offset = 50 );
PlotShapes( IIf( Short, shapeDownArrow, shapeNone ), colorWhite, 0, H, Offset = -45 );
PlotShapes( IIf( Cover, shapeUpArrow, shapeNone ), colorBlue, 0, L, Offset = -45 );

_SECTION_END();"

1 Like

@Rohan please follow the forum rules and post codes properly.

As a new user you should build your code gradually with a few variables or lines and then "debug" them to see if what you have created is correct. Eventually you can put them all together in a longer more complex code. Perhaps run an Exploration each time you create a new variable or equation and see if it is doing what you intended.

And don't try to code a system that goes both Long and Short until you are certain that you have at least half of that correct (for example code the Long only portion and once you are sure it is correct then code the Short portion).

I haven't had a close look at that mess you posted but one obvious error jumps out

BuySignal3 = vRSI > Ref(vMFI,-1); // Value of RSI(8) should be increasing (higher than previous value)

Where did the vMFI come from?

5 Likes

Thank you, will do it in small steps. Being new, i made mistake about posting code. And yes, vMFI was due to copy and paste, which was removed while editing... sorry about that. Will go through through forum first and get back if required.

1 Like

@Rohan,

Good job on taking advice from PortfolioBuilder. Always a challenge to build in stages when you are new and excited about the system and program.

Also, please make sure you check out the License Verified badge.

1 Like

Hi,

Why code below is not working? Your suggestion please.

vRSI1= RSI(12); //Get the value of RSI(12)

BuyCond2 = vRSI1 > Ref(vRSI1,-1) > 20; 

Buy = BuyCond2;

@Rohan,

Because it is incorrect AFL code. You have to connect multi comparisons with AND operator.

Corrected:

BuyCond2 = vRSI1 > Ref(vRSI1,-1) AND Ref(vRSI1,-1) > 20;

Or

BuyCond2 = IIf(Ref(vRSI1,-1) > 20,  vRSI1 > Ref(vRSI1,-1), 0);

3 Likes

Thank you @fxshrat. For helping a newbie like me.