Gamestudio Links
Zorro Links
Newest Posts
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
M1 Oversampling
by 11honza11. 04/20/24 20:57
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 177 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 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,982
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,982
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