наконец-то смог вытащить какой-то рабочий код, я не знаю, почему он работает, но он работает вроде кода. по крайней мере он получает то, что я хочу
sma_day, 5,7,9
sma_day10, 10,15
sma_day50, 50,55,60
sma_vol5, 5,7, 9
sma_vol10, 10,15,20
sma_vol20, 20,25,30
psyvalue 30,35,40,45,50
Это должно дать все 87480 комбинаций
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Diagnostics;
using System.Data.SqlClient;
using System.Collections;
namespace optimization
{
class indiObject
{
public string indicName { get; set; }
public int numbegin { get; set; }
public int numend { get; set; }
public int gap { get; set; }
}
class IndiObjects : IEnumerable
{
ArrayList mylist = new ArrayList();
public indiObject this[int index]
{
get { return (indiObject)mylist[index]; }
set { mylist.Insert(index, value); }
}
IEnumerator IEnumerable.GetEnumerator()
{
return mylist.GetEnumerator();
}
}
class Program
{
public static List<Task> tasks;
private const string pythonpath = @"C:\Python\Scripts\python.exe";
private const string scriptpath = @"C:\psy-Optimizer.py";
private const string connectionstr = @"SERVER =localhost;DATABASE=Stock;Trusted_Connection=True";
public delegate void strategy(Dictionary<string, string> dict);
public static int processcount = 0;
public static Task[] tasksArray;
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
tasks = new List<Task>();
int countC = 0;
const int k = 3;
IndiObjects myObjects = new IndiObjects();
myObjects[0] = new indiObject() { indicName = "sma_day5", numbegin = 5, numend = 9, gap = 2 };
myObjects[1] = new indiObject() { indicName = "sma_day20", numbegin = 20, numend = 30, gap = 5 };
myObjects[2] = new indiObject() { indicName = "sma_day50", numbegin = 50, numend = 60, gap = 5 };
IndiObjects myObjects2 = new IndiObjects();
myObjects2[0] = new indiObject() { indicName = "sma_vol5", numbegin = 5, numend = 9, gap = 2 };
myObjects2[1] = new indiObject() { indicName = "sma_vol10", numbegin = 10, numend = 19, gap = 5 };
myObjects2[2] = new indiObject() { indicName = "sma_vol20", numbegin = 20, numend = 30, gap = 5 };
var var1 = new[] { myObjects[0].indicName, myObjects[1].indicName, myObjects[2].indicName };
var var2 = new[] { myObjects2[0].indicName, myObjects2[1].indicName, myObjects2[2].indicName };
foreach (IEnumerable<string> i in GetPermutations(var1, k))
{
int[,] loopcount0 = new int[3, 3];
int count0 = 0;
foreach (string s in i)
{
//Console.WriteLine(s);
switch (s)
{
case "sma_day5":
loopcount0[count0, 0] = myObjects[0].numbegin;
loopcount0[count0, 1] = myObjects[0].numend;
loopcount0[count0, 2] = myObjects[0].gap;
break;
case "sma_day20":
loopcount0[count0, 0] = myObjects[1].numbegin;
loopcount0[count0, 1] = myObjects[1].numend;
loopcount0[count0, 2] = myObjects[1].gap;
break;
case "sma_day50":
loopcount0[count0, 0] = myObjects[2].numbegin;
loopcount0[count0, 1] = myObjects[2].numend;
loopcount0[count0, 2] = myObjects[2].gap;
break;
}
count0++;
}
for (int f1 = loopcount0[0, 0]; f1 <= loopcount0[0, 1]; f1 += loopcount0[0, 2])
for (int f2 = loopcount0[1, 0]; f2 <= loopcount0[1, 1]; f2 += loopcount0[1, 2])
for (int f3 = loopcount0[2, 0]; f3 <= loopcount0[2, 1]; f3 += loopcount0[2, 2])
foreach (IEnumerable<string> i2 in GetPermutations(var2, k))
{
int[,] loopcount = new int [3,3];
int count = 0;
//Console.WriteLine("sma_day5 " + f1 + "sma_day20" + f2 + "sma_day50" + f3);
foreach (string s in i2)
{
//Console.WriteLine(s);
switch (s)
{
case "sma_vol5":
loopcount[count, 0] = myObjects2[0].numbegin;
loopcount[count, 1] = myObjects2[0].numend;
loopcount[count, 2] = myObjects2[0].gap;
break;
case "sma_vol10":
loopcount[count, 0] = myObjects2[1].numbegin;
loopcount[count, 1] = myObjects2[1].numend;
loopcount[count, 2] = myObjects2[1].gap;
break;
case "sma_vol20":
loopcount[count, 0] = myObjects2[2].numbegin;
loopcount[count, 1] = myObjects2[2].numend;
loopcount[count, 2] = myObjects2[2].gap;
break;
}
count++;
}
for (int f4 = loopcount[0, 0]; f4 <= loopcount[0,1]; f4 += loopcount[0, 2])
for (int f5 = loopcount[1, 0]; f5 <= loopcount[1, 1]; f5 += loopcount[1, 2])
for (int f6 = loopcount[2, 0]; f6 <= loopcount[2, 1]; f6 += loopcount[2, 2])
for (int psyvalue = 30; psyvalue <= 50; psyvalue += 5)
for (int atrdist = 5; atrdist <= 5; atrdist++)
for (int day_atrdist = 5; day_atrdist <= 5; day_atrdist++)
{
try
{
dict.Clear();
dict.Add("--minutefromdate", "2017-01-01");
dict.Add("--dayfromdate", "2016-01-01");
dict.Add("--todate", "2022-01-01");
dict.Add("--sma_day5", f1.ToString());
dict.Add("--sma_day20", f2.ToString());
dict.Add("--sma_day50", f3.ToString());
dict.Add("--lfi", "11");
dict.Add("--sma_vol5", f4.ToString());
dict.Add("--sma_vol10", f5.ToString());
dict.Add("--sma_vol20", f6.ToString());
dict.Add("--day_atrdist", "5");
dict.Add("--atrdist", "5");
dict.Add("--psyvalue", psyvalue.ToString());
if (countC >= 72893)
{
tasks.Add(Task.Factory.StartNew(() => RunBacktrader(dict), CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default));
}
// Console.WriteLine("Process ID " + countC++);
/*
string str="";
foreach (KeyValuePair<string, string> kvp in dict)
{
str += kvp.Key + "=" + kvp.Value ;
}
*/
Console.WriteLine("Process ID " + countC++);
tasksArray = tasks.Where(t => t != null).ToArray();
while (tasksArray.Length >= 10)
{
var completedTask = Task.WhenAny(tasks).Result;
tasks.Remove(completedTask);
Console.WriteLine("A task has been completed with result {0}.", completedTask.Id);
}
}
catch (Exception ex)
{
RecordLog(ex.Message);
}
}
}
//Console.WriteLine(string.Join(" ", i));
}
Console.Read();
}
private static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
{
if (length == 1) return list.Select(t => new T[] { t });
return GetPermutations(list, length - 1)
.SelectMany(t => list.Where(o => !t.Contains(o)),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
private static void RunBacktrader(Dictionary<string, string> dict)
{
try
{
string paras = "";
foreach (KeyValuePair<string, string> pp in dict)
{
paras += pp.Key + "=" + pp.Value + " ";
}
Console.WriteLine("Tasks count " + processcount++);
string scriptName = scriptpath + " " + paras;
Process p = new Process();
p.StartInfo = new ProcessStartInfo(@pythonpath, @scriptName)
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
catch (Exception ex)
{
RecordLog("5 " + ex.Message);
}
}
private static void RecordLog(string message)
{
try
{
using (SqlConnection connection = new SqlConnection(connectionstr))
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.Parameters.Add(new SqlParameter("message", message));
connection.Open();
sqlcmd.Connection = connection;
sqlcmd.CommandText = "INSERT INTO [dbo].[optmizationLog] (ExecTime, Steps) VALUES (GETDATE(), CAST(@message as varchar(512)) )";
sqlcmd.ExecuteNonQuery();
connection.Close();
}
}
catch (Exception ex)
{
Console.Write("duplicate");
}
}
}
}