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.
#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();
}
}