Hi,
I am porting a system from another platform to Amibroker and I am running into problems with the ATR / position sizing part of the code.
I nshort: the ATR values seem to be wrong hence the position sizing, stop loss and risk rewards are all wrong.
When I output the ATR from outside the code (I call AddColumn(ATR(20), "ATR"); directly ), the correct ATR value shows. When I call the variable storing the ATR value inside the code it is incorrect.
Any idea as per why?
Thanks
I am porting a system from another platform to Amibroker and I am running into problems with the ATR / position sizing part of the code.
I nshort: the ATR values seem to be wrong hence the position sizing, stop loss and risk rewards are all wrong.
When I output the ATR from outside the code (I call AddColumn(ATR(20), "ATR"); directly ), the correct ATR value shows. When I call the variable storing the ATR value inside the code it is incorrect.
Any idea as per why?
Thanks
Code:
#include "C:\Program Files\AmiBroker\Formulas\Custom\RSI_Solver.afl"
//Filter = Status( "LastBarInTest" );
risk = 0.05;
/* Position sizing */
Account_Size = 10000;
Risk_Lvl = Account_Size * risk;
// --- detect watchlist ---
wlnumber = GetOption( "FilterIncludeWatchlist" );
symlist = GetCategorySymbols( categoryWatchlist, wlnumber );
if ( Status( "stocknum" ) == 0 )
{
// delete static variables
// fill input static arrays
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != ""; i++ )
{
SetForeign( sym );
GSPC = Foreign("GSPC", "C");
vol = ATR(20);
Ratio = (C / GSPC);
MA_Ratio = MA(ratio, 20);
Sdev_Ratio = StDev(Ratio, 20);
Diff20 = Ratio - MA_Ratio;
ZScore20 = Diff20 / Sdev_Ratio;
B1 = Zscore20 < 0;
B2 = barssince( Zscore20 > 0 ) > 3;
//B3 = L < BBandBot(10,2);
B4 = C > MA(C, 100);
B5 = RSI(2) < 15;
Cond = B1 AND B2 /*AND B3*/ AND B4 AND B5;
/* Entry Price based on targeted RSI */
Limit_Entry = Min (C , ReverseRSI(2,5) );
//StaticVarSet("vol" + sym, vol);
//vol = StaticVarGet ("vol" + sym);
/* Stop price based on ATR */
Stop_Amt = 2 * vol;
Stop_Price = Limit_Entry - Stop_Amt;
//ApplyStop(stopTypeLoss , stopModePoint, Stop_Price);
/* Number of shares */
Shares = Risk_Lvl / (vol / C) / C ;
Position_Size = Limit_Entry * Shares;
/*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("vol" + sym, vol);
StaticVarSet("zscore" + sym, ZScore20);
StaticVarSet("shares" + sym, Shares);
StaticVarSet("limit_entry" + sym, Limit_Entry);
StaticVarSet("stop_price" + sym, Stop_Price);
StaticVarSet("cond" + sym, Cond);
StaticVarSet("rr" + sym, rr);
}
}
sym = Name();
cond = StaticVarGet("cond" + sym);
Filter=1 AND cond == 1 ;
vol = StaticVarGet("vol" + sym);
zscore = StaticVarGet("zscore" + sym);
shares = StaticVarGet("shares" + sym);
limit_entry = StaticVarGet("limit_entry" + sym);
stop_price = StaticVarGet("stop_price" + sym);
rr = StaticVarGet("rr" + sym);
//AddColumn(zscore,"Zscore");
//AddColumn(cond, "Cond");
AddColumn(RSI(2), "RSI2");
AddColumn(ATR(20), "ATR(20)"); //Correct ATR value
AddColumn(vol, "ATR in code"); // incorrect ATR value (the one used inside the code)
AddColumn(C, "Closing Price");
AddColumn(shares, "Shares to Buy");
AddColumn(limit_entry,"Limit Entry");
AddColumn(reverseRSI(2,90),"Profit Target");
AddColumn(stop_price,"Stop Loss");
AddColumn(100*rr,"Risk/Reward");
SetSortColumns( -10 );
//AddRankColumn();
/*
AddColumn(Shares, "Shares to Buy");
AddColumn(RR,"Risk/Reward");
SetSortColumns( -11 );
AddRankColumn();
AddColumn(Position_Size,"Position Size");
*/