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

Re: [amibroker] Expectancy



PureBytes Links

Trading Reference Links

Hello,
 
If you want to use Risk = 1% of portfolio-level equity at the trade-entry time then appropriate code would look like this:
 
SetCustomBacktestProc("");
MaxLossPointStop =
2*ATR(10); // dynamic volatility stoploss, 1st modification


function
FindEquityAtDateTime( eq, dt, Value )
{
   found = -
1
;
  
for( i = 0; i < BarCount AND found == -1
; i++ )
   {
      
if
( dt[ i ] == Value ) found = i;
   }
  
return IIf( found != -1, eq[ found  - 1 ], Null
);
}
if( Status("action"
) == actionPortfolio )
{
    bo =
GetBacktesterObject
();
    bo.Backtest(
1); // run default backtest procedure

    SumProfitPerRisk =
0;
    NumTrades =
0
;
    dt =
DateTime
();
    eq =
Foreign("~~~EQUITY", "C"
);

  
for
( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
   {
      EquityAtEntry = FindEquityAtDateTime( eq, dt, trade.EntryDateTime );

      Risk =
0.01 * EquityAtEntry ; //risk is defined as a constant 1% of current equity. 

      RiskAsPecentOfCurrentEquity =
100
* Risk / EquityAtEntry;
      RMultiple = trade.GetProfit()/Risk;
      trade.AddCustomMetric(
"Initial risk $"
, Risk  );        
      trade.AddCustomMetric(
"Equity at entry"
, EquityAtEntry );
      trade.AddCustomMetric(
"Risk as % of Eq."
, RiskAsPecentOfCurrentEquity );

      trade.AddCustomMetric(
"R-Multiple"
, RMultiple  );
      SumProfitPerRisk = SumProfitPerRisk + RMultiple;
      NumTrades++;
   }
    Expectancy3 = SumProfitPerRisk / NumTrades;
    bo.AddCustomMetric(
"Expectancy (per risk)"
, Expectancy3 );
    bo.ListTrades();
}
// your trading system here


ApplyStop( stopTypeLoss, stopModePoint, MaxLossPointStop );

Best regards,
Tomasz Janeczko
amibroker.com
----- Original Message -----
From: Al Venosa
Sent: Tuesday, January 25, 2005 2:42 PM
Subject: Re: [amibroker] Expectancy

Thanks for the clarification, TJ. Yes, you are right. I did miss the ApplyStop function. I'm more interested, however, in dynamic, volatility-based max stoplosses and calculating risk as a function of equity, not as a function of the dollar amount invested per trade. I took a crack at re-writing your Expectancy3 example using a 2*ATR(10) stoploss rather than a percentage stoploss, using a constant defined risk as 1% of current equity. Now, don't shoot me, but is the code below correct? I only changed 2 lines (bold font) plus the ApplyStop at the end.

SetCustomBacktestProc("");
MaxLossPointStop =
2*ATR(10); // dynamic volatility stoploss


function FindEquityAtDateTime( eq, dt, Value )
{
   found = -
1
;
   
for( i = 0; i < BarCount AND found == -1
; i++ )
   {
      
if
( dt[ i ] == Value ) found = i;
   }
  
return IIf( found != -1, eq[ found  - 1 ], Null
);
}
if( Status("action") == actionPortfolio )
{
    bo =
GetBacktesterObject
();
    bo.Backtest(
1); // run default backtest procedure

    SumProfitPerRisk =
0;
    NumTrades =
0

<>    dt = DateTime(); 
    eq = Foreign("~~~EQUITY", "C" );

   for( trade = bo.GetFirstTrade(); trade; trade = bo.GetNextTrade() )
   {
      Risk = 0.01 * eq; //risk is defined as a constant 1% of current equity
      EquityAtEntry = FindEquityAtDateTime( eq, dt, trade.EntryDateTime ); 
<>      RiskAsPecentOfCurrentEquity = 100 * Risk / EquityAtEntry;
      RMultiple = trade.GetProfit()/Risk; 
      trade.AddCustomMetric(
"Initial risk $"
, Risk  );         
     
trade.AddCustomMetric("Equity at entry"
, EquityAtEntry ); 
     
trade.AddCustomMetric("Risk as % of Eq.", RiskAsPecentOfCurrentEquity ); 
      trade.AddCustomMetric("R-Multiple", RMultiple  );
      SumProfitPerRisk = SumProfitPerRisk + RMultiple; 
      NumTrades++;
   }
    Expectancy3 = SumProfitPerRisk / NumTrades;
    bo.AddCustomMetric(
"Expectancy (per risk)"
, Expectancy3 );
    bo.ListTrades();
}
// your trading system here


ApplyStop( stopTypeLoss, stopModePoint, MaxLossPointStop );


Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html




Check AmiBroker web page at:
http://www.amibroker.com/

Check group FAQ at: http://groups.yahoo.com/group/amibroker/files/groupfaq.html




Yahoo! Groups Links