[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Custom Backtester & AddToComposite help



PureBytes Links

Trading Reference Links

Hello All,
I have been working on determining the portfolio openequity.  The
approach used is mid-level custom backtester.  With some excellent
guidance from TJ's examples I have fixed stupid coding problems.  Now
I can determine the open equity and write the value(s) to a *.csv
file.  To obtain a graphic of the open equity I would prefer to use
AddToComposite. This has stumped me from various approaches.  method#1
write to the composite straight after writing to file. method#2
writing to a variable bar-by-bar, then writing the array to the
composite.  I cannot create the composite using either method.  Could
someone help with suggestions for the code below.
Many thanks for all the great ideas and help provided to all in the
group.  You will notice that I have modified others excellent code and
I would like to thank them for their contributions.

regards
franc

//---------------------------------------------------------------------------------------/
// Functions

function DateNumToStr(fDateNum)
{
 local temp, nYear, nMonth, nDay;

 temp   = fDateNum;
 nYear  = int(temp*0.0001);  // divide by 10000
 temp   = fDateNum - nYear*10000;

 nYear  = nYear + 1900;
 nMonth = int(temp*0.01);    // divide by 100
 nDay   = temp - nMonth*100;

 return StrFormat("%g/%g/%g",nDay,nMonth,nYear);
}


//---------------------------------------------------------------------------------------/
// Custom Backtester - outputs open equity to c:\temp\PortfolioEquity.csv

SetCustomBacktestProc("");


if( Status("action") == actionPortfolio ) 
{ 

   fh = fopen("c:\\temp\\OpenPortfolioEquity.csv", "w" ); 
   if(fh)
fputs("Bar:,Date:,Equity:,NumTrades:,OpenEquity:,EntryValue:,Profit:\n",fh);

   bo = GetBacktesterObject(); 

   bo.PreProcess();  // Initialize backtester 

   DateOfBar = DateNum();
     

   for( bar = 0; bar < BarCount; bar++ ) 
   { 
      OpenEquity[bar]  = 0;
      EntryPosn[bar]   = 0;
      TradeProfit[bar] = 0;
      NumTrades   = 0;
      sBar        = "";
      sDate       = "";
      sTrade      = "";
      ds          = "";

      // this processes current bar signals
      // NEED to do this before trades & equity are able to
exist->then retreived
      bo.ProcessTradeSignals( bar ); 

      CurEquity[bar] = bo.Equity;
      // the code lists positions currently open 
      // (the list includes positions that were open BEFORE executing
current bar signals) 
      for( Openpos = bo.GetFirstOpenPos(); Openpos ; Openpos =
bo.GetNextOpenPos() ) 
      { 
         NumTrades[bar]++;
         OpenEquity[bar]  = OpenEquity[bar]  + OpenPos.GetPositionValue();
         EntryPosn[bar]   = EntryPosn[bar]   + OpenPos.GetEntryValue();
         TradeProfit[bar] = TradeProfit[bar] + OpenPos.GetProfit();
      } 
      sBar   = StrFormat("%4.0f,",bar);
      sDate  = DateNumToStr(DateOfBar[bar]) + ",";
      sTrade = StrFormat("%6.2f,%2.0f,%6.2f,%6.2f,%6.2f\n",
                     
CurEquity[bar],NumTrades,OpenEquity[bar],EntryPosn[bar],TradeProfit[bar]);

      ds     = sBar + sDate + sTrade;
      if( fh ) fputs( ds, fh ); 

   } 

   bo.PostProcess(); 
   
   fclose( fh );   

   AddToComposite(OpenEquity, "~OpenEquity","I",
                  atcFlagEnableInPortfolio | atcFlagCompositeGroup |
atcFlagEnableInBacktest);
   AddToComposite(CurEquity,  "~OpenEquity","C",
                  atcFlagEnableInPortfolio | atcFlagCompositeGroup |
atcFlagEnableInBacktest);
   AddToComposite(EntryPosn,  "~OpenEquity","O",
                  atcFlagEnableInPortfolio | atcFlagCompositeGroup |
atcFlagEnableInBacktest);
   AddToComposite(TradeProfit,"~OpenEquity","L",
                  atcFlagEnableInPortfolio | atcFlagCompositeGroup |
atcFlagEnableInBacktest);

} 


// your trading system here 
SetOption("InitialEquity",50000);
SetOption("MaxOpenPositions",20);
SetOption("CommissionMode",0);
SetOption("UsePrevBarEquityForPosSizing",True);
SetOption("AllowPositionShrinking", False );


fast = Optimize("fast", 12, 5, 20, 1 ); 
slow = Optimize("slow", 26, 10, 25, 1 ); 
BSig =Cross(MACD(fast,slow),Signal(fast,slow)); 
SSig =Cross(Signal(fast,slow),MACD(fast,slow));
Buy  = ExRem(BSig,SSig);
Sell = ExRem(SSig,BSig);
SetPositionSize(10,spsPercentOfEquity);










------------------------ Yahoo! Groups Sponsor --------------------~--> 
Try Online Currency Trading with GFT. Free 50K Demo. Trade 
24 Hours. Commission-Free. 
http://us.click.yahoo.com/RvFikB/9M2KAA/U1CZAA/GHeqlB/TM
--------------------------------------------------------------------~-> 

Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For other support material please check also:
http://www.amibroker.com/support.html

 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/