system coding issue. code inside

#1
Hi Guys,

I found an interesting system on stockfetcher.com that I am trying to replicate in AFL as an exploration but it doesnt work properly (please see code below). I am turning to the community for some guidance and help.

This exploration is designed to find short term setups in an uptrend where Buy and Sell prices are determined by RSI levels. (The reverseRSI comes from Cesar Alvarez's website). The ranking should done on the risk/reward ratio (well, ideally I would like to rank on at least 2 parameters).

Right now:
- I am not clear whether the code is structured properly (does everything need to be done inside the loop?)
- most values are not output after running the exploration
- No ranking is performed (I think I am missing a part where the ranking is done IF Buy signals are fulfilled but I wouldnt know where to put them).

For the ranking part, I used an "off the shelf" piece of code from Amibroker's KB.
I am not very familiar with its syntax (and I dont really get what the staticvars are doing...)

Later I would like to convert this into a system that can be backtested, some pointers on the difficulties ahead would be great (I expect to be using the "hold()" function quite a bit due to the limit_entry price)


Thanks for looking into this and for your opinion!
Cheers





Code:
#include "C:\Program Files\AmiBroker\Formulas\Custom\RSI_Solver.afl"

symlist = "AA, BA, C, CAT, MSFT";



// delete static variables
StaticVarRemove( "ItemScore*" );

// fill input static arrays

for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    SetForeign( sym );
    GSPC = Foreign("GSPC", "C");
    
    Ratio = (C / GSPC);
    MA_Ratio = MA(ratio, 20);
    Sdev_Ratio = StDev(Ratio, 20);
    Diff20 = Ratio - MA_Ratio;
    ZScore20 = Diff20 / Sdev_Ratio;
   
    risk = 0.05;

    /* Position sizing */
    Account_Size = SetOption("InitialEquity", 10000);
    Risk_Lvl = Account_Size * risk;


    /* Entry Price based on targeted RSI */
    Limit_Entry = Min (C , ReverseRSI(2,5) );

    /* Stop price based on ATR */
    Stop_Amt = 2 * ATR(20);
    Stop_Price = Limit_Entry - Stop_Amt;
    ApplyStop(stopTypeLoss , stopModePoint, Stop_Price);

    /* Number of shares */
    Shares = round( Risk_Lvl / Stop_Amt);
   
    Position_Size = Limit_Entry * shares;
   
    /*SET THE CRITERIA NECESSARY TO TRIGGER A TRADE*/
    B1 = BarsSince(Zscore20 < 0) > 3;
    B2 = L < BBandBot(10,2);
    B3 = C > MA(C, 200);
    B4 = RSI(2) < 15;

    Buy = B1 AND B2 AND B3 AND B4;
   
    /*DETERMINE THE REWARD-TO-RISK RATIO BASED ON THE PROFIT TARGET AT RSI(2) = 90*/
    Reward = ( ReverseRSI(2,90) - Limit_Entry ) * Shares;
    RR = Reward / Risk_Lvl;

    RestorePriceArrays();
    StaticVarSet( "ItemScore" + sym, RR );
}

// perform ranking
StaticVarGenerateRanks( "rank", "ItemScore", 0, 1224 ); // normal rank mode

// read ranking
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
    Plot( StaticVarGet( "RankItemScore" + sym ), sym, colorCustom10 + i );
}


Filter=1;
AddColumn( DateTime(), "Date / Time", formatDateTime );

AddColumn(RSI(2),"RSI(2)");
AddColumn(ATR(20), "ATR(20)");
AddColumn(Shares, "Shares to Buy");
AddColumn(C, "Closing Price");
AddColumn(Limit_Entry,"Limit Entry");
AddColumn(reverseRSI(2,90),"Profit Target");
AddColumn(Stop_Price,"Stop Loss");
AddColumn(RR,"Risk/Reward");

AddColumn(Position_Size,"Position Size");
 

Similar threads