Simulate complex commission costs [Test]

Posted By: AndrewAMD

Simulate complex commission costs [Test] - 12/04/19 22:05

jcl,

In [Test] mode, can I simply subtract from Capital over time to emulate various costs/withdrawals? (The manual says that Balance and Equity are calculated based on Capital and trade profit - this suggests that Balance and Equity are strictly outputs in Test mode.)

Example applications:
* Assets with complicated commissions costs, especially IB assets. (Commissions set to zero in the asset list.)
* Monthly fees, such as data fees.
* Monthly balance withdrawals.

If not, what is the correct approach?

Thanks,
Andrew
Posted By: jcl

Re: Simulate complex commission costs [Test] - 12/09/19 13:25

Capital is the initial capital, so subtracting from it has probably no effect. Subtract from Balance.
Posted By: AndrewAMD

Re: Simulate complex commission costs [Test] - 12/09/19 13:52

Thanks!
Posted By: AndrewAMD

Re: Simulate complex commission costs [Test] - 01/22/20 02:21

For those interested, none of the above worked!

However, this below script did the job. The fee simulator is the incur_fee() function. Note that Zorro is being conservative in the simulation and assumes you lose at least one penny on a break-even trade.

The below script makes your money disappear. ( ͡° ͜ʖ ͡°)

Code
#include<default.c>

#define ASSET_FEE "DUMMY"
#define ALGO_FEE "FEE"

void incur_fee(var amt){
	if(amt<=0) return;
	if(Live) return;
	algo(ALGO_FEE);
	asset(ASSET_FEE);
	Commission = amt - 0.01;
	enterLong(1);
	exitLong();
}


void run(){
	if(!Test){
		print(TO_ANY,"\nOnly [Test] mode supported.");
		quit("#Quitting...");
		return;
	}
	
	if(Init){
		set(LOGFILE);
		Verbose = 3;
		Capital = 100000;
		BarPeriod = 1440;
		LookBack = 0;
		BarMode = BR_FLAT|BR_WEEKEND;
		StartDate = 2017;
		EndDate = 2019;
		printf("\nloading dummy asset list...");
		assetList("AssetsFix");
		printf("\nsetting up dummy asset...");
		assetAdd(ASSET_FEE,0.01,0.00,0,0,0.00,0.00,0,2,1,0.00,ASSET_FEE);
		algo(ALGO_FEE);
		asset(ASSET_FEE);
	}
	
	if(!Init && !is(LOOKBACK) && ((int)Equity)>0){
		printf("\nEquity: %0.2f",Equity);
		var fee = roundto(random(1000.0),50.0);
		printf("\nfee = %0.2f",fee);
		incur_fee(fee);
	}
	
	if(is(EXITRUN)){
		printf("\nFinal equity is: %0.2f",Equity);
	}
}


Attached picture img.PNG
Posted By: AndrewAMD

Re: Simulate complex commission costs [Test] - 01/22/20 13:05

Follow up: if you need to use Virtual Hedging, you will notice that the above method does not work. This is because if virtual trades are placed during run(), then pool trades are not placed until the end of run(). This means that if you enter long and exit long in the same run() call, no actual pool trade is placed.

Whereas if you place any trades outside of run(), pool trades are placed instantly. So the fix is to use call() to place the "fee" trades, like so:

Code
#include<default.c>

#define ASSET_FEE "DUMMY"
#define ALGO_FEE "FEE"

#define ASSET_TRADING "EUR/USD"
#define ALGO_TRADING "TRADING"

void incur_fee(int flags, var amt){
	if(amt<=0) return;
	if(Live) return;
	printf("#\nIncurring fee of %0.2f",amt);
	algo(ALGO_FEE);
	asset(ASSET_FEE);
	Commission = amt - 0.01;
	enterLong(1);
	exitLong();
}


void run(){
	if(!Test){
		print(TO_ANY,"\nOnly [Test] mode supported.");
		quit("#Quitting...");
		return;
	}
	
	if(Init){
		set(LOGFILE);
		Hedge = 5;
		Verbose = 7|DIAG;
		Capital = 100000;
		BarPeriod = 1440;
		LookBack = 10;
		BarMode = BR_FLAT|BR_WEEKEND;
		StartDate = 2017;
		EndDate = 2019;
		assetList("AssetsFix");
		
		printf("\nsetting up dummy asset...");
		assetAdd(ASSET_FEE,0.01,0.00,0,0,0.00,0.00,0,2,1,0.00,ASSET_FEE);
		algo(ALGO_FEE);
		asset(ASSET_FEE);
		algo(ALGO_TRADING);
		asset(ASSET_TRADING);
		
	}
	
	if(!Init && !is(LOOKBACK) && ((int)Equity)>0){
		algo(ALGO_TRADING);
		asset(ASSET_TRADING);
		if(Bar%5){
			if(Bar%2){
				enterLong(100);
			}
			else{
				exitLong();
				printf("#\nEquity: %0.2f",Equity);
				var fee = roundto(random(10.00),0.01);
				printf("#\nfee = %0.2f",fee);
				call(1,incur_fee,0,fee);
			}
		}
	}
	
	if(is(EXITRUN)){
		printf("#\nFinal equity is: %0.2f",Equity);
	}
}
Note: the above code requires the latest Zorro beta.
© 2024 lite-C Forums