Portfolio Backtester: Closing positions based on Acc Equity  [SOLVED]

Questions about MultiCharts .NET and user contributed studies.
robbob
Posts: 46
Joined: 26 Jan 2013
Has thanked: 2 times
Been thanked: 3 times

Portfolio Backtester: Closing positions based on Acc Equity

Postby robbob » 04 May 2013

Hi,

I'm attempting to implement an exit rule that will close a position if the current account equity has dropped by 3 percent from the previous day. Basically, if the threshold is hit, I would like the strategy to exit all positions. I am using the portfolio backtester on intraday futures data.

What I am seeing is that one symbol will exit, but other positions that are open that day will not exit.
For example, the threshold was hit on 7/26/07, which the strategy was holding positions in NQ, GBP, and EUR. The portfolio backtester exited the NQ position when the account equity theshhold was hit, but the GBP and EUR positions did not exit that day, and were closed out a few days later due to other exit rules being hit.

The calcBar() method for this rule appears below:

Code: Select all

protected override void CalcBar(){

int today = Bars.TimeValue.Day;
if( yesterdayDate == -1 ) {
yesterdayDate = today;
yesterdayEquity = currentEquity;
return;
}

if( today != yesterdayDate ) {
yesterdayEquity = currentEquity;
yesterdayDate = today;
}

stopAmount = (100.0-m_switchAmount)/100.0;

currentEquity = InitialCapital + StrategyInfo.ClosedEquity + StrategyInfo.OpenEquity;

double thresholdAmount = (yesterdayEquity * stopAmount);
Output.WriteLine( "Switch Date:" + Bars.TimeValue + " today Date: " + today + " yesterday Date: " + yesterdayDate + " currentEquity: " + currentEquity + " yesterdayEquity: " + yesterdayEquity + " switch value: " + thresholdAmount );
if( currentEquity < thresholdAmount ) {
Output.WriteLine( "### Kill switch hit" );
m_Order.Send( );
}
}

Please let me know what other info you need to help debug what may be going on.

thanks,
-Rob

User avatar
Henry MultiСharts
Posts: 9165
Joined: 25 Aug 2011
Has thanked: 1264 times
Been thanked: 2957 times

Re: Portfolio Backtester: Closing positions based on Acc Equ  [SOLVED]

Postby Henry MultiСharts » 06 May 2013

Hello Rob,

The Equity calculation is incorrect. In your strategy equity is calculated for a particular strategy only, while you need to calculate it for the entire portfolio:

Code: Select all

currentEquity = InitialCapital + Portfolio.NetProfit + Portfolio.OpenPositionProfit;

robbob
Posts: 46
Joined: 26 Jan 2013
Has thanked: 2 times
Been thanked: 3 times

Re: Portfolio Backtester: Closing positions based on Acc Equ

Postby robbob » 15 May 2013

That took care of the problem.

Thanks!


Return to “MultiCharts .NET”