#!C:\Perl\bin\perl.exe
use strict;
use Win32::IEAutomation;
use List::MoreUtils::PP;
use Config::IniFiles;
use Math::Round qw(nearest);
# set vars
my $template; 
my %volatility;
my $week = $ARGV[0] or die($!);
#my $week = 4;
my $cfg = Config::IniFiles->new( -file => "config.ini" ) or die($!);
my $pairs = $cfg->{mysects};
open(FIL,"template.set") or die($!);
while(<FIL>){$template .= $_}
#get volatility
my $ie = Win32::IEAutomation->new(visible => 0, maximize => 0, warnings => 0);
$ie->gotoURL('https://www.mataf.net/ru/tools/02-01-volatility', 10);
#print $ie->Content();exit;
$ie->getTextBox('name:', "nbweek")->SetValue($week);
$ie->getButton('class:', "btn btn-primary")->Click(1);
sleep(10);
my $table = $ie->getTable("id:", "volTable");
my @rows = $table->rows;
shift @rows;
for(@rows){
	my $pair = $_->cells(1)->cellText;
	my $volatility = $_->cells(3)->cellText;
	$volatility{$pair} = $volatility;	
}
$ie->closeIE;
#make sets
for(@$pairs){
	my $pair = $_;
	my $tpl = $template;
	my $MagicNumber_1 = $cfg->val( $pair, 'MagicNumber_1' ); 
	my $MagicNumber_2 = $cfg->val( $pair, 'MagicNumber_2' );
	my $MagicNumber_3 = $cfg->val( $pair, 'MagicNumber_3' );
	my $TakeProfit = $cfg->val( $pair, 'TakeProfit' );
	my $MaxTrades = $cfg->val( $pair, 'MaxTrades' ); 
	my $PipStarter = $volatility{$pair}/$MaxTrades;
	$PipStarter += $TakeProfit;
	$PipStarter = nearest(.00000001, $PipStarter);	
	$tpl =~ s/TakeProfit.*/TakeProfit=$TakeProfit/;
	$tpl =~ s/PipStarter.*/PipStarter=$PipStarter/;
	$tpl =~ s/MaxTrades(_\d).*/MaxTrades$1=$MaxTrades/g;
	$tpl =~ s/MagicNumber_1.*/MagicNumber_1=$MagicNumber_1/;
	$tpl =~ s/MagicNumber_2.*/MagicNumber_2=$MagicNumber_2/;
	$tpl =~ s/MagicNumber_3.*/MagicNumber_3=$MagicNumber_3/;
	$pair =~ s/\///;    
	open(my $fh, '>', "${pair}.set") or die($!);
	print $fh $tpl;
	close $fh;
}