Thanks a lot, JW - much appreciated!
Best regards,
Thomas
On 25.01.2016 16:38 trendxplorer@gmail.com wrote:
> [Attachment(s) <#TopText> from trendxplorer@gmail.com [amibroker]
> included below]
>
> Pontric,
>
> The attached code (copied below for review) re-balances a stock/bond
> portfolio (default: SPY/TLT, but adjustable through parameter window)
> every month, but it assumes trading at the close of the month. The code
> is for monthly data.
> The model's equity curve is saved as composite symbol ( "~~~BH_60_40" ).
> The exploration view shows monthly gain/loss per symbol.
>
> JW
>
> /* --- BH_60_40_v1.afl ---
> **
> ** model re-balances SPY/TLT portfolio 60/40
> ** trading at the close of every month
> **
> */
>
> // --- start of code ---
>
> // --- backtester settings ---
> SetOption( "CommissionAmount", 0.00 );
> SetOption( "InitialEquity", 100000 );
> SetTradeDelays( 0, 0, 0, 0 );
> SetOption( "WorstRankHeld", 2 );
> SetOption( "MaxOpenLong", 2 );
> SetOption( "MaxOpenPositions", 2);
> SetOption( "AllowPositionShrinking", True );
> SetBacktestMode( backtestRegular );
>
> // --- inputs ---
> _StockSymbol = ParamStr( "Stock Symbol?", "SPY" );
> _BondSymbol = ParamStr( "Bond Symbol?" , "TLT" );
> StockPerc = Param( "Stock Percentage (%)?", 60, 0, 100 );
> BondPerc = 100 - StockPerc;
>
> // --- set lot size ---
> RoundLotSize = 1;
>
> // --- detect tradelist ---
> wlnumber = GetOption( "FilterIncludeWatchlist" );
> watchlist = GetCategorySymbols( categoryWatchlist, wlnumber );
>
> // --- init ---
> PosSize = 0;
>
> // --- position allocation routine ---
> if ( Status( "stocknum" ) == 0 )
> {
> StaticVarRemove( "Pos*" );
> StaticVarRemove( "ret*" );
>
> for ( i = 0; ( symbol = StrExtract( watchlist, i ) ) != ""; i++ )
> {
> SetForeign( symbol );
> ret1m = ROC( Close, 1 );
> RestorePriceArrays();
>
> if ( symbol == _StockSymbol ) PosSize = StockPerc;
> if ( symbol == _BondSymbol ) PosSize = BondPerc;
>
> StaticVarSet( "ret1m" + symbol, ret1m );
> StaticVarSet( "PosSize" + symbol, PosSize );
> }
> }
>
> // --- get values and ranks for Momentum ---
> symbol = Name();
> ret1m = StaticVarGet( "ret1m" + symbol );
> PosSize = StaticVarGet( "PosSize" + symbol );
>
> // --- trade logic ---
> Buy = PosSize > 0;
> Sell = 0;
> Short = Cover = 0;
> BuyPrice = Close;
> SellPrice = Close;
>
> // --- set position sizes ---
> PositionSize = -PosSize; // "-" sign for spsPercentOfEquity
>
> // --- exit at EoM for rebalancing ---
> ApplyStop( stopTypeNBar, stopModeBars, numbars = 1, exitatstop = 0 );
>
> // --- exploration filter ---
> ExploreFilter = ParamToggle( "ExploreFilter", "LastBarInTest|All", 1 );
> if ( ExploreFilter )
> Filter = 1;
> else
> Filter = Status( "LastBarInTest" );
>
> // --- sort for exploration only (not on backtest) ---
> if ( Status( "actionex" ) == actionExplore )
> {
> SetSortColumns( 2, 1 );
>
> ColorRet = IIf( ret1m > 0, colorLime, colorRed );
>
> AddColumn( Close , "Close (current)", 1.2 );
> AddColumn( Ref( Close, -1 ), "Close (prior)" , 1.2 );
> AddColumn( ret1m , "ret1m%" , 3.3, 1, ColorRet );
> AddColumn( PosSize , "PosSize (%)" , 1.0 );
> }
>
> // --- save portfolio equity ---
> _PortfolioName = ParamStr( "~~~PortfolioName", "~~~BH_60_40" );
>
> SetCustomBacktestProc("");
>
> if( Status("action") == actionPortfolio )
> {
> bo = GetBacktesterObject();
> bo.Backtest();
> AddToComposite( bo.EquityArray,
> _PortfolioName, "X",
> atcFlagDeleteValues | atcFlagEnableInPortfolio );
> }
>
> // --- end of code ---
>
>
Posted by: Thomas Ludwig <Thomas.Ludwig@gmx.de>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (14) |
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
EmoticonEmoticon