Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (Edgar_Herrera, VoroneTZ, Akow), 973 guests, and 4 spiders.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
executing Run() function multiple times for Monte Carlo #483476
06/04/21 08:49
06/04/21 08:49
Joined: Jun 2021
Posts: 2
F
froglog Offline OP
Guest
froglog  Offline OP
Guest
F

Joined: Jun 2021
Posts: 2
Hello.

I try get distribution data using Monte Carlo simulation for a strategy.

Given fixed parameters for a signal, I try to randomize entry conditions.
After each run I try to save the resulted statistics into a file, so that later I can obtain the distribution of performance metrics and performance metrics variance.
For example, given fixed parameters for 2 moving averages 20 and 200 periods and given crossover signal, I try to randomize the entries so that in some cases strategy enter the signal and in some cases, it skips.
After each run, the performance metrics should be saved into a file. And then using aggregated values the distribution and variance of performance metrics can be made.

Currently, I tried to implement the looping mechanism without success. It executes run() function loop only once.
I also tried without success to apply to optimize() from 1 to 100 to a random iteration variable and use it as a seed.

Is it possible to call run() function multiple times with provided iter number as a seed value?

Code below displays the current implementation solution.

Code
#include <default.c>

int iter_no;
int iter_array[300];
int i;
for (i=0; i<300; i++) 
  iter_array[i] = i;

var evaluate()  // objective()
{ 
	if(NumWinTotal < 30 || NumLossTotal < 30) { // not enough trades
		StepNext = 0;	// abort optimization
		return 0; 
	}

string FileName = "Log\\my_stats_file.txt";

float ProfitFactor = WinTotal/max(1,LossTotal);
string values = strf("%i,%i,%i,%i,%.4f\n", iter_no, WinTotal, LossTotal, ProfitFactor);

lock();
if (file_length(FileName) == 0.0){
string header = strf("IterNo, WinTotal, LossTotal, ProfitFactor\n");
file_append(FileName,header,0);
}

file_append(FileName, values, 0);

unlock();

return WinTotal/max(1,LossTotal);
}

function run()
{

		set(PARAMETERS); 

		BarPeriod = 60;
		LookBack = 300;
		
		var SMAShortPeriod = 20;
		var SMALongPeriod = 200;
		
		vars Close = series(priceClose());
		vars SMAShort = series(SMA(Close, SMAShortPeriod));
		vars SMALong = series(SMA(Close, SMALongPeriod));
		
         while(loop(iter_array)){
		iter_no = Loop1;
		seed(iter_no);
		var random_val = random();
  
		if(crossOver(SMAShort, SMALong) and (random_val > 0.0)) enterLong();
		if(crossOver(SMAShort, SMALong) and (random_val > 0.0)) enterShort();
		
		}

}

Last edited by froglog; 06/04/21 09:25.
Re: executing Run() function multiple times for Monte Carlo [Re: froglog] #483478
06/04/21 09:42
06/04/21 09:42
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
There are 2 ways for getting montecarlo statistics: Either with NumTotalCycles with export in evaluate(), or with a brute force training run with export in objective().

Re: executing Run() function multiple times for Monte Carlo [Re: jcl] #483481
06/04/21 10:15
06/04/21 10:15
Joined: Jun 2021
Posts: 2
F
froglog Offline OP
Guest
froglog  Offline OP
Guest
F

Joined: Jun 2021
Posts: 2
Thank you, it works.
I was able to get statistics data.

I did using brute force method.
And setting seed vaue optimize.
Code
...
TrainMode = BRUTE;

iter_no = optimize(1, 0, 100, 1, 0);
if(is(INITRUN)) 
	seed(iter_no);
		
var random_val = random();

...


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1