Add Funds or Withdraw Funds - Simulation in Backtest

Hi,
the way I trade my stuff is like this:
Get EOD quotes, import them in AB, run backtest in AA to get my signals (rollover, stop adjustments, new trades, close trades, etc), then send orders to my broker manually.

Now if you’re trading a system for a couple of years, sooner or later there will come a point in time when you need to add or withdraw funds.

Say:
SetOption(“InitialEquity”,100000);//
but after a while:

2017/11/02: deposit 100k$

I’m wondering if someone has implemented something similar already and how it’s done. During a custom backtest Equity has read only property, same goes for the EquityArray.
After adding funds or withdrawing them, of course a re-balance of open position needs to be performed, but this is relatively straight forward in CBT.

Any suggestion on how to approach the add/withdraw funds during a backtest?

Thanks,
Dio

See “Pyramiding (scaling in/out) and mutliple currencies in the portfolio backtester” in help file.

As I say, re-balance is pretty straight forward, but withdrawing and adding funds seems more tricky.
Why do you suggest multiple currencies? It has nothing to do with it so it seems. Did I overlook something?

Do you have some snippets for ADDING FUNDS / WITHDRAWING FUNDS as opposed to scaling IN/OUT?
I read everything you suggest already.

One way I see it currently is to play with:

https://www.amibroker.com/guide/a_custombacktest.html

Properties:
double Cash
available funds (cash) in your portfolio

It should have write property (?), and after adding cash, perform a re-balance.

Taking funds out seem more tricky. The way I currently imagine it is to scale out of all trades (proportionally at a given date) to make cash available, then take out the cash by overwriting bo.cash…

Anyone tried something similar?

Buy = Sell = Short = Cover = 0; 

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) 
{
    bo = GetBacktesterObject();	//  Get backtester object
    bo.PreProcess();	//  Do pre-processing (always required)

    for (i = 0; i < BarCount; i++)	//  Loop through all bars
    {
        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
        bo.Cash *= 1.01; 
        
    }	  
      
    bo.PostProcess();	//  Do post-processing (always required)
}

image

3 Likes

Sweet!

So it works with bo.cash. Let me ride on this example and try it out!

Thank you very much aron.

Dio

adding funds at a specific date, this would be a start:

Buy = Sell = Short = Cover = 0; 

SetCustomBacktestProc("");

if (Status("action") == actionPortfolio) 
{
    bo = GetBacktesterObject();	//  Get backtester object
   
    bo.PreProcess();	//  Do pre-processing (always required)
	
    for (i = 0; i < BarCount; i++)	//  Loop through all bars
    {
        bo.ProcessTradeSignals( i );	//  Process trades at bar (always required)
        addfunds= DateNum()==1000301;//
        if(addfunds[i])
			{
				bo.Cash = bo.Cash + 15000; 
			}
 
        
    }	  
      
    bo.PostProcess();	//  Do post-processing (always required)
}


image

is there a way to prevent the performance stats from updating? Because right now it increases the profit, CAR, etc. even though no trade was ever entered.

image

1 Like