Home › Forums › Trading System Mentor Course Community › AmiBroker Coding and AFL › Position Sizing
- This topic is empty.
-
AuthorPosts
-
November 5, 2017 at 11:17 pm #101724Nick RadgeKeymaster
Anyone know if its possible to do position sizing in a backtest using yesterday close rather than the next days open?
November 6, 2017 at 4:34 am #107947LeeDanelloParticipantUse the ref function or Ref(C,-1) = yesterdays close where -1 is the shift relative to the last close in the test
November 6, 2017 at 9:24 am #107948ScottMcNabParticipantWould this not need access to the portfolio equity during the backtest ? I did not think this was possible.
November 7, 2017 at 12:45 pm #107950ZachSwannMemberI am not 100% sure if this is relevant bit beyond me at the moment but I found this when I poking around looking for something else
http://www.amibroker.com/docs/Houston2.pdfNovember 7, 2017 at 3:09 pm #107951JulianCohenParticipantThat was a good find Zach. Thanks
November 18, 2017 at 12:16 pm #107949ZachSwannMemberHas anyone already coded position sizing for rotational systems
For example if ranked
1st 30%
2nd 20%
3rd 10%
etc
Have been looking in this link http://www.amibroker.com/docs/Houston2.pdf
I think excluding of top positions is a way to figure out the rank as 1,2,3,4 etc but its still a bit of a foreign language to me at the momentNovember 19, 2017 at 7:16 am #108002ZachSwannMemberI answered most of my own question but any ideas how to Param sig.PosSize??
//=================================================================================
//Backtesting Parameters
//=================================================================================
posqty = Param(“# Positions”,20,1,100,1);
Eq = Param(“Initial Equity”,100000,1,10000000,1);
SetOption(“InitialEquity”,Eq);
SetOption(“MaxOpenPositions”,posqty);
SetOption(“UsePrevBarEquityForPosSizing”,True);
SetOption(“AccountMargin”,100);
SetTradeDelays(0,0,0,0);
SetOption(“UseCustomBacktestProc”, True );if( Status(“action”) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
for( bar = 0; bar < BarCount; bar++ )
{
Cnt = 1;for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) ) // Cnt Ends up being the rank
{
if( Cnt == 1) sig.PosSize = -20; // sig.PosSize If -20 is 20% of portfolio If +20 is $20
if( Cnt == 2) sig.PosSize = -5; // -param wouldn’t work for me not sure why
if( Cnt == 3) sig.PosSize = -5;
if( Cnt == 4) sig.PosSize = -5;
if( Cnt == 5) sig.PosSize = -5;
if( Cnt == 6) sig.PosSize = -5;
if( Cnt == 7) sig.PosSize = -5;
if( Cnt == sig.PosSize = -5;
if( Cnt == 9) sig.PosSize = -5;
if( Cnt == 10) sig.PosSize = -5;
if( Cnt == 11) sig.PosSize = -5;
if( Cnt == 12) sig.PosSize = -5;
if( Cnt == 13) sig.PosSize = -5;
if( Cnt == 14) sig.PosSize = -5;
if( Cnt == 15) sig.PosSize = -5;
if( Cnt == 16) sig.PosSize = -5;
if( Cnt == 17) sig.PosSize = -2.5;
if( Cnt == 18) sig.PosSize = -2.5;
if( Cnt == 19) sig.PosSize = -2.5;
if( Cnt >= 20) sig.PosSize = -2.5;
Cnt++;
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}wr = Param(“Worst Rank”,20,1,20,1);
SetBacktestMode(backtestRotational);
SetOption(“WorstRankHeld”,wr);
EOM = Month() != Ref(Month(),-1);//First trading day of the month
score = Ref(Rank,-1);//score is the rotational criteria
Score = IIf(LE,score,0);
PositionScore = IIf(Year()>=1985 AND EOM,Score,scoreNoRotate);
//
November 19, 2017 at 7:35 pm #108003SaidBitarMemberZach Swann wrote:Has anyone already coded position sizing for rotational systems
For example if ranked
1st 30%
2nd 20%
3rd 10%
etc
Have been looking in this link http://www.amibroker.com/docs/Houston2.pdf
I think excluding of top positions is a way to figure out the rank as 1,2,3,4 etc but its still a bit of a foreign language to me at the momentit is doable i did not try it before i will check it if i have some time this week and will post the code
November 19, 2017 at 7:38 pm #108006SaidBitarMemberZach Swann wrote:I answered most of my own question but any ideas how to Param sig.PosSize??
//=================================================================================
//Backtesting Parameters
//=================================================================================
posqty = Param(“# Positions”,20,1,100,1);
Eq = Param(“Initial Equity”,100000,1,10000000,1);
SetOption(“InitialEquity”,Eq);
SetOption(“MaxOpenPositions”,posqty);
SetOption(“UsePrevBarEquityForPosSizing”,True);
SetOption(“AccountMargin”,100);
SetTradeDelays(0,0,0,0);
SetOption(“UseCustomBacktestProc”, True );if( Status(“action”) == actionPortfolio )
{
bo = GetBacktesterObject();
bo.PreProcess();
for( bar = 0; bar < BarCount; bar++ )
{
Cnt = 1;for( sig = bo.GetFirstSignal( bar ); sig; sig = bo.GetNextSignal( bar ) ) // Cnt Ends up being the rank
{
if( Cnt == 1) sig.PosSize = -20; // sig.PosSize If -20 is 20% of portfolio If +20 is $20
if( Cnt == 2) sig.PosSize = -5; // -param wouldn’t work for me not sure why
if( Cnt == 3) sig.PosSize = -5;
if( Cnt == 4) sig.PosSize = -5;
if( Cnt == 5) sig.PosSize = -5;
if( Cnt == 6) sig.PosSize = -5;
if( Cnt == 7) sig.PosSize = -5;
if( Cnt == sig.PosSize = -5;
if( Cnt == 9) sig.PosSize = -5;
if( Cnt == 10) sig.PosSize = -5;
if( Cnt == 11) sig.PosSize = -5;
if( Cnt == 12) sig.PosSize = -5;
if( Cnt == 13) sig.PosSize = -5;
if( Cnt == 14) sig.PosSize = -5;
if( Cnt == 15) sig.PosSize = -5;
if( Cnt == 16) sig.PosSize = -5;
if( Cnt == 17) sig.PosSize = -2.5;
if( Cnt == 18) sig.PosSize = -2.5;
if( Cnt == 19) sig.PosSize = -2.5;
if( Cnt >= 20) sig.PosSize = -2.5;
Cnt++;
}
bo.ProcessTradeSignals( bar );
}
bo.PostProcess();
}wr = Param(“Worst Rank”,20,1,20,1);
SetBacktestMode(backtestRotational);
SetOption(“WorstRankHeld”,wr);
EOM = Month() != Ref(Month(),-1);//First trading day of the month
score = Ref(Rank,-1);//score is the rotational criteria
Score = IIf(LE,score,0);
PositionScore = IIf(Year()>=1985 AND EOM,Score,scoreNoRotate);
//
not sure this is the way
i used ranking before but it should look different
i believe you need to use StaticVarGenerateRanksNovember 20, 2017 at 5:06 am #108011ZachSwannMemberStaticVarGenerateRanks I didn’t even know that existed.
That would be before all the backtesting code I gather?November 20, 2017 at 9:23 am #108020SaidBitarMemberhttps://www.amibroker.com/guide/afl/staticvargenerateranks.html
this can give you idea how to do it
in short you put the conditions then you put the part for the custom backtest it will be used to generate the rank and later to read the rank based on the rank you set the position sizeNovember 25, 2017 at 12:03 pm #108021ZachSwannMemberI have had a bit of a play with it and this is what I don’t know
symlist part is there an easy way to get a whole watchlist as a string?
The static variable below I think it puts them in order and returns a string. I think you create a loop similar to Cnt loop in a previous post but I am not sure what to use instead of getfirstsignal and getnextsignal because if that is used I can’t use an array which I want too.symlist = “C,CAT,DD,GE,IBM,INTC,MSFT”;
// delete static variables – DO NOT forget the asterisk (wildcard) at the end
StaticVarRemove( “ValuesToSort*” );// fill input static arrays
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != “”; i++ )
{
SetForeign( sym );
Value = ROC( C, 10 );
RestorePriceArrays();
StaticVarSet( “ValuesToSort” + sym, Value );
}// perform ranking
StaticVarGenerateRanks( “rank”, “ValuesToSort”, 0, 1224 ); // normal rank mode// read ranking
rk = 0;
for ( i = 0; ( sym = StrExtract( symlist, i ) ) != “”; i++ )
{
StaticVarGet( “RankValuesToSort” + sym );
rk++;
}
//=================================================================================
_SECTION_END();This is current code doesn’t work but maybe I am not seeing something dead simple
November 26, 2017 at 11:44 am #108058SaidBitarMemberIn order to read watchlist you can use the following function “GetCategorySymbols” and you need to assign the watchlist ID. I remember that there was even easier way that will take the selected watchlist but i can not remember it now.
Code:symlist = GetCategorySymbols(categoryWatchlist ,18,0);for me watchlist number 18 is the sp500 current and past
November 26, 2017 at 12:02 pm #108059SaidBitarMemberthere is still an error it is driving me crazy because i can plot it but the backtest breaks.
i will try to find it but here is what i had in mindCode:_SECTION_BEGIN (“Index Filter”);
IndexSwitch = ParamToggle(“Index Filter”,”On|Off”,0);
“”;
“Foreign Ticker Symbol” ;
IndexCode = ParamStr(“Index Code”,”$SPX”);
Index = Foreign(IndexCode,”C”);
IndMA = Param(“Index Filter MA Period”,200,10,300,10);
IndexFilter = Index > MA(Index,IndMA);
IndexFilt = IIf(IndexSwitch,1,IndexFilter);IndexFilterDown = Index < MA(Index,IndMA); _SECTION_END(); posqty = Param("# Positions",20,1,100,1); Eq = Param("Initial Equity",100000,1,10000000,1); SetOption("InitialEquity",Eq); SetOption("AllowPositionShrinking",True); SetOption("MaxOpenPositions",posqty); SetOption("UsePrevBarEquityForPosSizing",True); SetOption("AccountMargin",100); SetTradeDelays(0,0,0,0); SetOption("UseCustomBacktestProc", True ); wlnum = GetOption( "FilterIncludeWatchlist" ); List = CategoryGetSymbols( categoryWatchlist, wlnum ) ; StaticVarRemove( "rank*" ); StaticVarRemove( "values*" ); categoryList = ","; for( n = 0; ( symbol = StrExtract( List, n ) ) != ""; n++ ) { SetForeign( symbol ); values = Roc( Close, 252 ); RestorePriceArrays(); StaticVarSet( "ValuesToSort" + symbol, values ); } StaticVarGenerateRanks( "rank", "ValuesToSort", 0, 1224 ); Symbol = Name(); value = StaticVarGet( "ValuesToSort" + name() ); rank = StaticVarGet( "RankValuesToSort" + name() ); PositionSize = IIf(rank == 1,-20, IIf(rank ==2, -10,IIf( rank > 2 AND rank <=10,-5,-2.5))); wr = Param("Worst Rank",20,1,20,1); SetBacktestMode(backtestRotational); SetOption("WorstRankHeld",wr); LE = Ref(ROC(C,252),-1) > 0 AND IndexFilt AND Ref(rank,-1) <= wr; EOM = Month() != Ref(Month(),-1); score = Ref(ROC(C,252),-1); Score = IIf(LE,score,0); PositionScore = IIf(Year()>=1985 AND EOM,Score,scoreNoRotate);
November 26, 2017 at 12:19 pm #108060SaidBitarMemberSaid Bitar wrote:In order to read watchlist you can use the following function “GetCategorySymbols” and you need to assign the watchlist ID. I remember that there was even easier way that will take the selected watchlist but i can not remember it now.Code:symlist = GetCategorySymbols(categoryWatchlist ,18,0);for me watchlist number 18 is the sp500 current and past
here it is
wlnum = GetOption( “FilterIncludeWatchlist” );
List = CategoryGetSymbols( categoryWatchlist, wlnum ) ; -
AuthorPosts
- You must be logged in to reply to this topic.