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

[amibroker] Trade-by-trade basis of r-multiple calculation.



PureBytes Links

Trading Reference Links

Hi again,

I know this topic has been discussed a number of times. However, I
never found out the solution to it, at least the one I'm looking for.
It'd be good if I get some hints about doing this.

/* First we need to enable custom backtest procedure and 
** tell AmiBroker to use current formula 
*/ 

SetCustomBacktestProc(""); 

MaxLossPercentStop = 1; // 10% max. loss stop 

/* Now custom-backtest procedure follows */ 
if( Status("action") == actionPortfolio ) 
{ 
    bo = GetBacktesterObject(); 

    bo.Backtest(1); // run default backtest procedure 

   SumProfitPerRisk = 0; 
   NumTrades = 0; 

   // iterate through closed trades first 
   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() ) 
   { 
      // risk is calculated as the maximum value we can loose per trade 
      // in this example we are using  max. loss stop 
      // it means we can not lose more than (MaxLoss%) of invested amount 
      // hence ris 

       Risk = ( MaxLossPercentStop / 100 ) * trade.GetEntryValue();
/* Risk = (Entry Price - Stop Loss Price)  e.g. (173.20 - 163.40) */

       RMultiple = trade.GetProfit()/Risk;

       trade.AddCustomMetric("Initial risk $", Risk  );
       trade.AddCustomMetric("R-Multiple", RMultiple  );

       SumProfitPerRisk = SumProfitPerRisk + RMultiple;
       NumTrades++; 
   } 

    expectancy3 = SumProfitPerRisk / NumTrades; 

    bo.AddCustomMetric( "Expectancy (per risk)", expectancy3 ); 

    bo.ListTrades(); 

} 






/*
----------------------------------------------------------------------------
** Trade options
*/
SetOption("MaxOpenPositions", 1 );
SetTradeDelays( 1, 1, 1, 1 );        // settradedelays( buydelay,
selldelay, shortdelay, coverdelay )      


/*
----------------------------------------------------------------------------
** Calculate stops
*/ 

InitialStop = 0.5 * ATR(20);
TrailStopAmount = 1 * ATR(20);
ApplyStop( stopTypeLoss, stopModePoint, InitialStop, 0 );
ApplyStop( stopTypeTrailing, stopModePoint, TrailStopAmount, 0, True );
/* N-bar stop */ 
ApplyStop( stopTypeNBar, stopModeBars, 15 );


Buy = Ref(Cross(BBandBot(Close,15,2),Close),-1) AND (Close >
Ref(Close,-1)) AND (Close>Open);
Sell = 0; // selling only by stop



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 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
 
Yahoo! Groups Links

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

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> 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/