Adding Exact Fee and Credit Calculations to Zorro

0 comments

Executive Summary

I describe a way to add exact fees and credits to any Zorro backtest, using the new enterTrade() function.  I will be needing this capability to detect the minimum capital requirements of my Don System by determining the exact Interactive Brokers commission fees incurred.

Motivation

Shortly after I published my success story, I received some technical questions. One individual saw that the system was designed for a $100k USD account size, and he asked what the actual minimum requirement was.  I do not know the answer myself, so I am determined to make a proper assessment.

I used to have a fee calculation solution, but it involved a weird hack of mine where I place a trade in a backtest and then I alter the TRADE struct, but it caused Zorro to become very unstable, so I abandoned this solution.

But it just so happens that the latest Zorro beta has a new feature that is officially supported!

Enter enterTrade()

I was pleased to find on the What’s New page that Zorro added the new function enterTrade().  A quick spin of the Simulate script revealed that you can create all kinds of trades in a backtest, even without having the entry and exit signals handy.

Based on the context from the Simulate script, the TRADE struct definition in the headers, and some testing, I came up with this prototype script:

void simulate_fee(var fee_amt){
	algo("Fees");
	fee_amt = roundto(fee_amt,0.01);
	
	Spread = Slippage = Commission = 0;
	if(fee_amt<0){
		algo("Credits");
		printf("\ncredit amount: $%0.2f",-fee_amt);
	}
	else if (fee_amt>0){
		algo("Fees");
		printf("\nfee amount: $%0.2f",fee_amt);
	}
	else{
		return;
	}
	TRADE T; memset(&T,0,sizeof(T));
	T.nLots = 1;
	T.nLotsTarget = 1;
	T.fEntryPrice = priceClose() + fee_amt;
	T.flags |= TR_OPEN;
	TRADE* p = enterTrade(&T);
	exitTrade(p);
	printf(", TradeProfit = $%0.2f",(var)p->fResult);
}

void run(void){
	if(!Test){
		print(TO_ANY,"\nOnly [Test] mode supported!");
	}
	BarPeriod = 1440;
	LookBack = 0;
	UnstablePeriod = 0;
	StartDate = 2019;
	EndDate = 2019;
	
	set(LOGFILE);
	set(NFA);
	Hedge = 0;
	assetList("AssetsIB");
	asset("DIS");
	algo("Fees");
	algo("Credits");
	
	if(Init)seed(59);
	if(Bar && !is(LOOKBACK)){
		simulate_fee(-random(1000.0)+500.);
	}
	
}

It’s simple. All it does is generate a random number from -500.00 to +500.00. For every positive number, it incurs a fee of that amount.  For every negative number, it incurs a negative fee (a credit).

For example, if the current priceClose() at the current bar is $100, and we want a $20 fee, we immediately enter a trade as if it entered at $120 with qty=1 stock, to take a loss of $20.

The opposite is true for the credit. In that case, to get a $20 credit, your trade must enter at $80 and exit at $100 for a profit of $20.

Of course, Spread, Slippage, and Commission have been set to zero in order for that to come out to the correct credit or fee amount.

Interestingly, it is okay to inject trades that open at a negative price, just for the sake of getting the desired credit amount.  For example, if I wanted a credit of $400, and the stock is trading at $100, I can enter a trade at -$300 and close at $100 for a profit of $400.

Here’s the Zorro log:

log view
There is a profitable trade closed for every credit and a losing trade closed for every fee.

Here’s a visual representation of the “trades”:

trade visual plot
Each green line is an individual credit trade, and each red line is an individual fee trade.

I will follow up when my analysis of the capital requirements are complete.  Look forward to it!

Comments locked due to spam.