Group trades by parameters

Posted By: filip

Group trades by parameters - 10/01/19 19:15

Hello everyone, I am developing mean-reversion script on 4 FX pairs (strategy is always one currency long and one currency short). The script relies on 2 parameters and I would like to group trades by these parameters so that I can calculate information ratio for each of the parameter combinations. I am doing this using evaluate() and I found interesting behaviour:

Code
        for(all_algos)
        {
		int algocount = 0;
		
		for(all_trades)
		{
			algocount++;
		}
		
		print(TO_REPORT, "\n%d", algocount);
	}


Produces the correct total number of trades (multiple times). However, I would also except that this gives the same (only once):

Code
	int algocount = 0;
	for(all_algos)
	{
		for(all_trades)
		{
			if(TradeAlgo == Algo)
			{
			algocount++;
			}
		}
	}
	print(TO_REPORT, "\n%d", algocount);


The number produced is the total number of long trades only. Is this behaviour intended? How could I achieve the grouping of all trades?
Thanks a lot.
Posted By: AndrewAMD

Re: Group trades by parameters - 10/01/19 19:37

Post your entire script.
Posted By: filip

Re: Group trades by parameters - 10/01/19 20:25

The trading logic is from RobotWealth FXbootcamp, so I am not sure if I can post it here. I think this should be reproducible on any strategy that trades different assets and params. Basically I am doing this:

Code
trade_function(some params, param_1, param_2);
algo(strf("lb_%d_re_%d", param_1, param_2);


trade_function cycles through the pairs and selects the ones for trading orders enterShort(), enterLong().
Posted By: AndrewAMD

Re: Group trades by parameters - 10/02/19 13:03

Well, without looking at your full script:
* How do you even know that you are only counting the long trades? I see no use of TradeIsLong. Isolate, isolate, isolate.
* I suggest printing detailed information about every trade for debugging purposes.

https://zorro-project.com/manual/en/fortrades.htm
https://zorro-project.com/manual/en/trade.htm
Posted By: filip

Re: Group trades by parameters - 10/02/19 21:00

I did some experiments and found that this approach is not right. You can not simply nest the all_algos loop with the all_trades loop. (as these loops are not documented, they should not be used in other way than the official examples) Returning back to the original goal: Grouping trades by params can be for example achieved just by using all_trades and TradeVars. See the two examples, why the nesting approach is flawed:

Code
	for(all_algos)
	{
		for(all_trades)
		{
			print(TO_REPORT, "\n%s", Algo);
		}
	}
}


Does not produce output with multiple algo names. It is just stuck on one. Whereas this code does it:

Code
	for(all_algos)
	{
		print(TO_REPORT, "\n%s", Algo);
		for(all_trades)
		{
			continue;
		}
	}
Posted By: MatPed

Re: Group trades by parameters - 10/02/19 22:23

Well, you are swimming in dangerous waters... From the manual: "The above enumeration loops are macros defined in include\variables.h. Unlike a normal for loop, enumeration loops cannot be nested, and they must not be aborted by break or return statements. The break_trades macro can be used to abort trade loops. "

A better approach might be to write a new macro from scratch, not using the predefined ones in order to avoid unpredictable behaviors...
© 2024 lite-C Forums