PureBytes Links
Trading Reference Links
|
Hi,
The MA function returns an array, yet you are trying to store it in a scaler (i.e. a non array variable). Similarly, MA expects an array as argument, but you are passing it a scaler.
You cannot use arrays to track the trades since you may have more trades than bars (array length is dictated by number of bars). Therefore, you will need to calculate the moving average yourself.
Run the following code and see if it does what you're after. I have not tested it extensively, but it should at least get you started. To improve upon it, add a Parameter instead of hard coding the value 30 everywhere: http://www.amibroker.com/guide/afl/afl_view.php?id=203
Mike
procedure PersistTrade( profit ) { local t;
// Add to first open slot.
for ( t = 0; t < 30; t++ ) { if ( IsNull( VarGet( "t" + t ) ) ) { VarSet( "t" + t, profit ); break; } }
if ( t == 30 ) { // All slots currently occupied, need to bump oldest.
for ( t = 0; t < 29; t++ ) { VarSet( "t" + t, VarGet( "t" + ( t + 1 ) ) ); }
VarSet( "t" + t, profit ); } }
function AvgTrades() { local cumProfit; local t;
cumProfit = 0;
for ( t = 0; t < 30; t++ ) { cumProfit += VarGet( "t" + t ); }
return ( cumProfit / 30 ); }
SetCustomBacktestProc( "" );
if ( Status( "action" ) == actionPortfolio ) { for ( t = 0; t < 30; t++ ) { VarSet( "t" + t, NULL ); }
bo = GetBacktesterObject();
bo.Backtest( 1 ); // run default backtest at notradelist mode
cumProfit = 0; numTrades = 0;
for ( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) { tradeProfit = trade.GetProfit(); cumProfit += tradeProfit; PersistTrade( tradeProfit );
trade.AddCustomMetric( "custom cum profit", cumProfit ); // value of this metrics should be same as the built - in "cum.profit"
numTrades++;
if ( numtrades >= 30 ) { trade.AddCustomMetric( "MA30 of cum profit", AvgTrades() ); } else { trade.AddCustomMetric( "MA30 of cum profit", NULL ); } }
bo.ListTrades(); }
//===========a simple trading system ================
fast = Optimize( "fast", 12, 5, 20, 1 ); slow = Optimize( "slow", 26, 10, 25, 1 );
Buy = Cross( MACD( fast, slow ), Signal( fast, slow ) ); Sell = Cross( Signal( fast, slow ), MACD( fast, slow ) );
Short = Sell; Cover = Buy;
--- In amibroker@xxxxxxxxxxxxxxx, "huanyanlu" <huanyan2000@xxx> wrote: > > Hi, > > After some reading, now I am able to put up some codes as attached. I > want to add two custom per-trade metrics. One is "custom cum profit", > this metrics already appear in the built-in backtester, I add this as > custom metrics in order to confirm that I am in the right direction. > The second custom metrics is what I really need to add, that is "MA30 > of cum profit". > > I succeeded in adding the first custom metrics "custom cum profit", > but failed in getting the second done. The backtester shows zero > for "MA30 of cum profit"" the first 30 trades and blank from the > 31th trade. Did I miss something here, why I cannot use the function > MA() here correctly ? > > thanks for any help > > huanyan > > ========================================================== > > SetCustomBacktestProc(""); > > > if( Status("action") == actionPortfolio ) > { > bo = GetBacktesterObject(); > > bo.Backtest(1); // run default backtest at notradelist mode > > Cumprofit = 0; > NumTrades = 0; > MA30_profit=0; > > // iterate through closed trades first > for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade > () ) > { > > Cumprofit = Cumprofit+ trade.Getprofit(); > > trade.AddCustomMetric("custom cum profit",Cumprofit ); // > value of this metrics should be same as the built-in "cum.profit" > > > > NumTrades++; > > if(numtrades>=30) > {MA30_profit=MA(Cumprofit,30);} > > trade.AddCustomMetric("MA30 of cum profit",MA30_profit ); > } > > bo.ListTrades(); > > } > > > //===========a simple trading system ================ > > fast = Optimize("fast", 12, 5, 20, 1 ); > slow = Optimize("slow", 26, 10, 25, 1 ); > Buy=Cross(MACD(fast,slow),Signal(fast,slow)); > Sell=Cross(Signal(fast,slow),MACD(fast,slow)); > > Short=Sell; > Cover=Buy; > > > > > > > --- In amibroker@xxxxxxxxxxxxxxx, "Mike" sfclimbers@ wrote: > > > > Start with the document titled "AmiBroker Custom Backtester > > Interface.pdf" found in the Files section of this group published > by > > gp_sydney: > > > > http://f1.grp.yahoofs.com/v1/sPtkSfaX2ek2RCVbqqJOCJA2R_- > > armYEr2K2MmIWnAHp_8p2ZKxwE4WR0554peVNTIdd-- > > CzFINIbYE5z51vkgAozgCxi0yI/AmiBroker%20Custom%20Backtester% > > 20Interface.pdf > > > > Mike > > > > --- In amibroker@xxxxxxxxxxxxxxx, "huanyanlu" <joesan99@> wrote: > > > > > > Hi, > > > I have a simple question about custom backtesting. > > > I want to add a column of 30-trades average of the cum of profit > > into > > > the tradelist of the backtest result. How to implement this in > AFL ? > > > > > > Thanks for any help > > > > > > huanlan > > > > > >
__._,_.___
**** IMPORTANT ****
This group is for the discussion between users only.
This is *NOT* technical support channel.
*********************
TO GET TECHNICAL SUPPORT from AmiBroker please send an e-mail directly to
SUPPORT {at} amibroker.com
*********************
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
For other support material please check also:
http://www.amibroker.com/support.html
*********************************
__,_._,___
|