public SimAccount sim_win_ga_market(int from, int to, Gene2 chromo, SimAccount ac, double nn_threshold) { var nn = new NN(); var strategy = new Strategy(); var nn_input_data_generator = new NNInputDataGenerator(); for (int i = from; i < to - 1; i++) { var nn_inputs = nn_input_data_generator.generateNNWinGA(i, chromo.num_index); var nn_outputs = nn.calcNN(nn_inputs, chromo, 0); var pred = nn.getActivatedUnitOnlyBuySell(nn_outputs, nn_threshold); var actions = strategy.GAWinMarketStrategy(i, pred, ac); for (int j = 0; j < actions.action.Count; j++) { if (actions.action[j] == "entry") { ac.entry_order(actions.order_type[j], actions.order_side[j], actions.order_size[j], actions.order_price[j], i, MarketData.Dt[i].ToString(), actions.order_message[j]); } } ac.move_to_next(i + 1, MarketData.Dt[i + 1].ToString(), MarketData.Open[i + 1], MarketData.High[i + 1], MarketData.Low[i + 1], MarketData.Close[i + 1]); } ac.last_day(to, MarketData.Dt[to].ToString(), MarketData.Close[to]); ac.calc_sharp_ratio(); return(ac); }
public SimAccount sim_ga(int from, int to, Gene2 chromo, SimAccount ac) { var nn = new NN(); var strategy = new Strategy(); int amount = 1; var nn_input_data_generator = new NNInputDataGenerator(); for (int i = from; i < to - 1; i++) { var nn_inputs = nn_input_data_generator.generateNNInputData(ac, i); var nn_outputs = nn.calcNN(nn_inputs, chromo, 0); var pred = nn.getActivatedUnit(nn_outputs); var actions = strategy.GAStrategy(pred, amount, ac); for (int j = 0; j < actions.action.Count; j++) { if (actions.action[j] == "entry") { ac.entry_order(actions.order_type[j], actions.order_side[j], actions.order_size[j], actions.order_price[j], i, MarketData.Dt[i].ToString(), actions.order_message[j]); } } ac.move_to_next(i + 1, MarketData.Dt[i + 1].ToString(), MarketData.Open[i + 1], MarketData.High[i + 1], MarketData.Low[i + 1], MarketData.Close[i + 1]); } ac.last_day(to, MarketData.Dt[to].ToString(), MarketData.Close[to]); ac.calc_sharp_ratio(); return(ac); }
//reset chromos with random weigths except public void resetChromos() { for (int i = 0; i < chromos.Length; i++) { chromos[i] = new Gene2(chromos[i].num_units, chromos[i].num_index); } }
private void generate_chromos(int num_chrom, int[] num_units_layer, int[] index) { chromos = new Gene2[num_chrom]; for (int i = 0; i < num_chrom; i++) { chromos[i] = new Gene2(num_units_layer, index); } }
private void crossover(List <int> selected, double cross_over_ratio) { var rnd = new Random(DateTime.Now.Millisecond); var new_chromos = new Gene2[chromos.Count()]; //deep copy chromos for (int i = 0; i < new_chromos.Length; i++) { new_chromos[i] = new Gene2(chromos[0].num_units, chromos[0].num_index); } for (int i = 0; i < new_chromos.Length; i++) { for (int j = 0; j < chromos[i].bias_gene.Count; j++) { for (int k = 0; k < chromos[i].bias_gene[j].Length; k++) { new_chromos[i].bias_gene[j][k] = chromos[i].bias_gene[j][k]; } } for (int j = 0; j < chromos[i].weight_gene.Count; j++) { for (int k = 0; k < chromos[i].weight_gene[j].Count; k++) { for (int l = 0; l < chromos[i].weight_gene[j][k].Length; l++) { new_chromos[i].weight_gene[j][k][l] = chromos[i].weight_gene[j][k][l]; } } } } for (int i = 0; i < chromos.Count(); i++) { if (i != best_chromo) { //bias1/2, weight1/2からそれぞれからランダムにratio %のweightを選択して交配 for (int j = 0; j < chromos[i].weight_gene.Count; j++) { for (int k = 0; k < chromos[i].weight_gene[j].Count; k++) { if (rnd.NextDouble() > (1 - cross_over_ratio)) { new_chromos[i].weight_gene[j][k] = chromos[selected[i]].weight_gene[j][k]; new_chromos[i].bias_gene[j] = chromos[selected[i]].bias_gene[j]; } else { new_chromos[i].weight_gene[j][k] = chromos[i].weight_gene[j][k]; new_chromos[i].bias_gene[j] = chromos[i].bias_gene[j]; } } } } } chromos = new Gene2[chromos.Count()]; new_chromos.CopyTo(chromos, 0); }
public SimAccount sim_ga_market_limit(int from, int to, int max_amount, Gene2 chromo, SimAccount ac, double nn_threshold, bool stop_no_trade) { var nn = new NN(); var strategy = new Strategy(); int amount = 1; var nn_input_data_generator = new NNInputDataGenerator(); var zero_trade_check_point = 0.1; //to - fromの間のx%進捗した時にnum trade=0だったらsimを打ち切る。 for (int i = from; i < to - 1; i++) { //check num trade=0 discontinue sim if (stop_no_trade && i - from > ((to - from) * zero_trade_check_point) && ac.performance_data.num_trade == 0) { //Console.WriteLine("Stopped sim due to zero trade."); break; } var nn_inputs = nn_input_data_generator.generateNNInputDataLimit(ac, i, chromo.num_index); var nn_outputs = nn.calcNN(nn_inputs, chromo, 0); //var pred = nn.getActivatedUnitLimitMarket(nn_outputs, nn_threshold); var pred = nn.getActivatedUnitLimitMarket2(nn_outputs, nn_threshold); var actions = strategy.GALimitMarketStrategy(i, pred, amount, max_amount, ac); //check invalid ac situation if (ac.order_data.order_side.Count > 1) { Console.WriteLine("Sim: # of order is more than 1 !"); } for (int j = 0; j < actions.action.Count; j++) { if (actions.action[j] == "entry") { ac.entry_order(actions.order_type[j], actions.order_side[j], actions.order_size[j], actions.order_price[j], i, MarketData.Dt[i].ToString(), actions.order_message[j]); } else if (actions.action[j] == "cancel") { ac.cancel_all_order(i, MarketData.Dt[i].ToString()); } else if (actions.action[j] == "update amount") { ac.update_order_amount(actions.order_size[j], actions.order_serial_num[j], i, MarketData.Dt[i].ToString()); } else if (actions.action[j] == "update price") { ac.update_order_price(actions.order_price[j], actions.order_serial_num[j], i, MarketData.Dt[i].ToString()); } else { Console.WriteLine("Sim: Unknown strategy action !"); } } ac.move_to_next(i + 1, MarketData.Dt[i + 1].ToString(), MarketData.Open[i + 1], MarketData.High[i + 1], MarketData.Low[i + 1], MarketData.Close[i + 1]); } ac.last_day(to, MarketData.Close[to]); ac.calc_sharp_ratio(); return(ac); }
/* * Window内の右端からbuy or sellの有無を確認して、buy / sellがあれば記録する。 * Buy / selが記録された状態で反対のbuy / sellがあれば値幅として記録。最後に残ったbuy / sellは無視。(最後のプライスで値幅を考慮すると結局全てbuy出すようになる気がする) */ public SimAccount sim_win_market(int from, int to, List <int[]> sim_windows, Gene2 chromo, SimAccount ac, double nn_threshold) { var nn = new NN(); var nn_input_data_generator = new NNInputDataGenerator(); var pred_list = new List <int>(); for (int i = from; i < to + 1; i++) { var nn_inputs = nn_input_data_generator.generateNNWinGA(i, chromo.num_index); var nn_outputs = nn.calcNN(nn_inputs, chromo, 0); pred_list.Add(nn.getActivatedUnitOnlyBuySell(nn_outputs, nn_threshold)); } double maker_fee = -0.00025; int num_trade = 0; double total_nehaba = 0; for (int i = 0; i < sim_windows.Count; i++) { var buy_price = new List <double>(); var sell_price = new List <double>(); for (int j = sim_windows[i][0]; j <= sim_windows[i][1]; j++) { if (pred_list[j - from] == 1 && sell_price.Count == 0) { buy_price.Add(MarketData.Bid[j] * (1 + maker_fee)); } else if (pred_list[j - from] == 2 && buy_price.Count == 0) { sell_price.Add(MarketData.Ask[j] * (1 - maker_fee)); } else if (pred_list[j - from] == 1 && sell_price.Count > 0) //exit sell position { var pl = (sell_price[0] - MarketData.Bid[j] * (1 + maker_fee)); ac.performance_data.total_pl += pl; ac.performance_data.sell_pl_list.Add(pl); ac.performance_data.realized_pl_list.Add(pl); ac.performance_data.num_trade++; total_nehaba += pl; num_trade++; sell_price.RemoveAt(0); } else if (pred_list[j - from] == 2 && buy_price.Count > 0) //exit buy position { var pl = (MarketData.Ask[j] * (1 - maker_fee) - buy_price[0]); ac.performance_data.total_pl += pl; ac.performance_data.buy_pl_list.Add(pl); ac.performance_data.realized_pl_list.Add(pl); ac.performance_data.num_trade++; total_nehaba += pl; num_trade++; buy_price.RemoveAt(0); } } } return(ac); }
private static SimAccount doMultiSim(int from, int to, int max_amount, List <int> best_chrom_log_id, bool display_chart, List <double> nn_threshold) { Console.WriteLine("Started Multi SIM"); var chromos = new Gene2[best_chrom_log_id.Count]; var ga = new GA(0); for (int i = 0; i < best_chrom_log_id.Count; i++) { chromos[i] = ga.readWeights(best_chrom_log_id[i], true); } var title = "Combined PL Ratio - " + from.ToString() + " - " + to.ToString() + ", dt:" + MarketData.Dt[from].ToString() + " - " + MarketData.Dt[to - 1]; return(ga.sim_ga_multi_chromo(from, to, max_amount, chromos.ToList(), title, display_chart, nn_threshold)); }
public SimAccount sim_ga_limit(int from, int to, int max_amount, Gene2 chromo, SimAccount ac) { var nn = new NN(); var strategy = new Strategy(); int amount = 1; var nn_input_data_generator = new NNInputDataGenerator(); for (int i = from; i < to - 1; i++) { var nn_inputs = nn_input_data_generator.generateNNInputDataLimit(ac, i, chromo.num_index); var nn_outputs = nn.calcNN(nn_inputs, chromo, 0); var pred = nn.getActivatedUnit(nn_outputs); var actions = strategy.GALimitStrategy2(i, pred, amount, max_amount, ac); //check invalid ac situation if (ac.order_data.order_side.Count > 1) { Console.WriteLine("Sim: # of order is more than 1 !"); } for (int j = 0; j < actions.action.Count; j++) { if (actions.action[j] == "entry") { ac.entry_order(actions.order_type[j], actions.order_side[j], actions.order_size[j], actions.order_price[j], i, MarketData.Dt[i].ToString(), actions.order_message[j]); } else if (actions.action[j] == "cancel") { ac.cancel_all_order(i, MarketData.Dt[i].ToString()); } else if (actions.action[j] == "update amount") { ac.update_order_amount(actions.order_size[j], actions.order_serial_num[j], i, MarketData.Dt[i].ToString()); } else if (actions.action[j] == "update price") { ac.update_order_price(actions.order_price[j], actions.order_serial_num[j], i, MarketData.Dt[i].ToString()); } else { Console.WriteLine("Sim: Unknown strategy action !"); } } ac.move_to_next(i + 1, MarketData.Dt[i + 1].ToString(), MarketData.Open[i + 1], MarketData.High[i + 1], MarketData.Low[i + 1], MarketData.Close[i + 1]); } ac.last_day(to, MarketData.Dt[to].ToString(), MarketData.Close[to]); ac.calc_sharp_ratio(); return(ac); }
private double[] calcWeights(double[] input_vals, Gene2 chromo, int layer_key, int activation) { var res = new double[chromo.weight_gene[layer_key].Count]; for (int i = 0; i < chromo.weight_gene[layer_key].Count; i++) //for units { var sum_v = 0.0; for (int j = 0; j < input_vals.Length; j++) //for weight { sum_v += input_vals[j] * chromo.weight_gene[layer_key][i][j]; //weight_gene[layer][input unit][output unit] } sum_v += chromo.bias_gene[layer_key][i]; res[i] = (activation == 0 ? sigmoid(sum_v) : tanh(sum_v)); } return(res); }
public SimAccount sim_ga(int from, int to, Gene2 chromo, string title) { var sim = new Sim(); var ac = new SimAccount(); ac = sim.sim_ga(from, to, chromo, ac); Console.WriteLine("pl=" + ac.performance_data.total_pl); Console.WriteLine("num trade=" + ac.performance_data.num_trade); Console.WriteLine("num market order=" + ac.performance_data.num_maker_order); Console.WriteLine("win rate=" + ac.performance_data.win_rate); Console.WriteLine("sharp_ratio=" + ac.performance_data.sharp_ratio); Console.WriteLine("num_buy=" + ac.performance_data.num_buy); Console.WriteLine("num_sell=" + ac.performance_data.num_sell); Console.WriteLine("buy_pl=" + ac.performance_data.buy_pl_list.Sum()); Console.WriteLine("sell_pl=" + ac.performance_data.sell_pl_list.Sum()); LineChart.DisplayLineChart(ac.total_pl_list, title); return(ac); }
public SimAccount sim_ga_limit_conti(int from, int to, int max_amount, Gene2 chromo, string title, SimAccount ac, bool chart) { var sim = new Sim(); ac = sim.sim_ga_limit(from, to, max_amount, chromo, ac); Console.WriteLine("pl=" + ac.performance_data.total_pl); Console.WriteLine("num trade=" + ac.performance_data.num_trade); Console.WriteLine("num market order=" + ac.performance_data.num_maker_order); Console.WriteLine("win rate=" + ac.performance_data.win_rate); Console.WriteLine("sharp_ratio=" + ac.performance_data.sharp_ratio); Console.WriteLine("num_buy=" + ac.performance_data.num_buy); Console.WriteLine("num_sell=" + ac.performance_data.num_sell); Console.WriteLine("buy_pl=" + ac.performance_data.buy_pl_list.Sum()); Console.WriteLine("sell_pl=" + ac.performance_data.sell_pl_list.Sum()); if (chart) { LineChart.DisplayLineChart(ac.total_pl_list, title); } return(ac); }
public double[] calcNN(double[] input_vals, Gene2 chromo, int activation) { if (input_vals.Contains(Double.NaN)) { Console.WriteLine("NN-calcNN: nan in included in input_vals !"); } if (input_vals.Length != chromo.num_units[0]) { Console.WriteLine("NN-calcNN: # of input_data is not matched with # of first layer units !"); Console.WriteLine("input_vals=" + input_vals.Length.ToString() + ", units=" + chromo.num_units[0].ToString()); } //input layer var inputs = calcWeights(input_vals, chromo, 0, activation); //middle layers for (int i = 1; i < chromo.weight_gene.Count - 1; i++) //do calc for each layers { var outputs = calcWeights(inputs, chromo, i, activation); inputs = outputs; } return(calcWeights(inputs, chromo, chromo.weight_gene.Count - 1, 0)); }
private (double, SimAccount) evaluationSimWin(int from, int to, List <int[]> sim_windows, int chro_id, Gene2 chro, double nn_threshold) { var ac = new SimAccount(); var sim_win = new WinSim(); ac = sim_win.sim_win_market(from, to, sim_windows, chro, ac, nn_threshold); var eva = ac.performance_data.num_trade > 0 ? ac.performance_data.total_pl / Math.Sqrt(Convert.ToDouble(ac.performance_data.num_trade)) : 0; return(eva, ac); }
public Gene2 readWeights(int island_id, bool multi_sim) { var file_name = ""; if (multi_sim) { file_name = @"./log_best_weight_ID-" + island_id.ToString() + ".csv"; } else { file_name = @"./best_weight_ID-" + island_id.ToString() + ".csv"; } using (StreamReader sr = new StreamReader(file_name, Encoding.UTF8, false)) { var data = new List <string>(); var units = new List <int>(); var index = new List <int>(); var bias = new List <double[]>(); var weights = new List <Dictionary <int, double[]> >(); var layer_id_list = new List <int>(); while (true) { var line = sr.ReadLine(); data.Add(line); if (line == null) { break; } else { if (line.Contains("units")) { var ele = line.Split(',').ToList(); units = ele.GetRange(1, ele.Count - 1).Select(int.Parse).ToList(); } else if (line.Contains("index")) { var ele = line.Split(',').ToList(); index = ele.GetRange(1, ele.Count - 1).Select(int.Parse).ToList(); } else if (line.Contains("bias")) { var ele = line.Split(',').ToList(); var ele_range = ele.GetRange(1, ele.Count - 1).Select(double.Parse).ToList(); bias.Add(ele_range.ToArray()); } else if (line.Contains("weight")) //weight:0:0,-0.369,0.9373 -> weight:layer:unit { var ele = line.Split(',').ToList(); var ele_range = ele.GetRange(1, ele.Count - 1).Select(double.Parse).ToArray(); var layer_id = Convert.ToInt32(ele[0].Split(':')[1]); var unit_id = Convert.ToInt32(ele[0].Split(':')[2]); var dic = new Dictionary <int, double[]>(); dic[unit_id] = ele_range; if (layer_id_list.Contains(layer_id)) { weights[layer_id][unit_id] = ele_range; } else { weights.Add(dic); } layer_id_list.Add(layer_id); } } } data.RemoveAt(data.Count - 1); //remove null var chrom = new Gene2(units.ToArray(), index.ToArray()); chrom.bias_gene = bias; chrom.weight_gene = weights; chrom.num_units = units.ToArray(); chrom.num_index = index.ToArray(); return(chrom); } }
/*各islandにおいて、ランダムに選択したislandからランダムに選択した染色体を交換する * ->best chromo以外を選択するようにする*/ private void moveBetweenIsland(double move_ratio) { if (gas.Count > 1) { for (int i = 0; i < gas.Count; i++) { var num_move = Convert.ToInt32(gas[i].chromos.Length * move_ratio); for (int j = 0; j < num_move; j++) { var island_list = Enumerable.Range(0, gas.Count).ToList(); island_list.RemoveAt(island_list.IndexOf(i)); var selected_island = island_list[RandomSeed.rnd.Next(0, island_list.Count)]; var target_chrom_list = Enumerable.Range(0, gas[selected_island].chromos.Length).ToList(); target_chrom_list.RemoveAt(target_chrom_list.IndexOf(gas[selected_island].best_chromo)); var selected_target_chromo = target_chrom_list[RandomSeed.rnd.Next(0, target_chrom_list.Count)]; var selected_id = RandomSeed.rnd.Next(0, gas[i].chromos.Length); while (selected_id == gas[i].best_chromo) { selected_id = RandomSeed.rnd.Next(0, gas[i].chromos.Length); } //exchange chromo //copy targe chromo to tmp chromo var tmp_chrom = new Gene2(gas[selected_island].chromos[selected_target_chromo].num_units, gas[selected_island].chromos[selected_target_chromo].num_index); for (int k = 0; k < gas[selected_island].chromos[selected_target_chromo].bias_gene.Count; k++) //for layers { for (int l = 0; l < gas[selected_island].chromos[selected_target_chromo].bias_gene[k].Length; l++) //for weights { tmp_chrom.bias_gene[k][l] = gas[selected_island].chromos[selected_target_chromo].bias_gene[k][l]; } } for (int k = 0; k < gas[selected_island].chromos[selected_target_chromo].weight_gene.Count; k++) //for layers { for (int l = 0; l < gas[selected_island].chromos[selected_target_chromo].weight_gene[k].Count; l++) //for units { for (int m = 0; m < gas[selected_island].chromos[selected_target_chromo].weight_gene[k][l].Length; m++) //for weights { tmp_chrom.weight_gene[k][l][m] = gas[selected_island].chromos[selected_target_chromo].weight_gene[k][l][m]; } } } //copy from selected chromo to target chromo for (int k = 0; k < gas[selected_island].chromos[selected_target_chromo].bias_gene.Count; k++) //for layers { for (int l = 0; l < gas[selected_island].chromos[selected_target_chromo].bias_gene[k].Length; l++) //for weights { gas[selected_island].chromos[selected_target_chromo].bias_gene[k] = gas[i].chromos[selected_id].bias_gene[k]; } } for (int k = 0; k < gas[selected_island].chromos[selected_target_chromo].weight_gene.Count; k++) //for layers { for (int l = 0; l < gas[selected_island].chromos[selected_target_chromo].weight_gene[k].Count; l++) //for units { for (int m = 0; m < gas[selected_island].chromos[selected_target_chromo].weight_gene[k][l].Length; m++) //for weights { gas[selected_island].chromos[selected_target_chromo].weight_gene[k][l][m] = gas[i].chromos[selected_id].weight_gene[k][l][m]; } } } //copy from target chromo to selected chromo for (int k = 0; k < gas[selected_island].chromos[selected_target_chromo].bias_gene.Count; k++) //for layers { for (int l = 0; l < gas[selected_island].chromos[selected_target_chromo].bias_gene[k].Length; l++) //for weights { gas[i].chromos[selected_id].bias_gene[k][l] = tmp_chrom.bias_gene[k][l]; } } for (int k = 0; k < gas[selected_island].chromos[selected_target_chromo].weight_gene.Count; k++) //for layers { for (int l = 0; l < gas[selected_island].chromos[selected_target_chromo].weight_gene[k].Count; l++) //for units { for (int m = 0; m < gas[selected_island].chromos[selected_target_chromo].weight_gene[k][l].Length; m++) //for weights { gas[i].chromos[selected_id].weight_gene[k][l][m] = tmp_chrom.weight_gene[k][l][m]; } } } } } } }
private (double, SimAccount) evaluation(int from, int to, int max_amount, int chro_id, Gene2 chro, int sim_type, double nn_threshold) { var ac = new SimAccount(); var sim = new Sim(); //ac = sim.sim_ga(from, to, chro, ac); if (sim_type == 0) { ac = sim.sim_ga_limit(from, to, max_amount, chro, ac); } else if (sim_type == 1) { ac = sim.sim_ga_market_limit(from, to, max_amount, chro, ac, nn_threshold, true); } else { Console.WriteLine("GA-evaluation: Invalid Sim Type!"); } var sm = calcSquareError(ac.total_pl_ratio_list, ac.performance_data.num_trade); //var eva = ac.performance_data.total_pl * Math.Sqrt(ac.performance_data.num_buy * ac.performance_data.num_sell) / sm; var eva = ac.performance_data.sharp_ratio * Math.Sqrt(Math.Sqrt(ac.performance_data.num_buy * ac.performance_data.num_sell)); if (ac.performance_data.buy_pl_list.Sum() <= 0 || ac.performance_data.sell_pl_list.Sum() <= 0) { eva = 0; } if (eva.ToString().Contains("N")) { eva = 0; } return(eva, ac); }
private (double, SimAccount) evaluationSimWin(int from, int to, List <int[]> sim_windows, int chro_id, Gene2 chro, double nn_threshold) { var ac = new SimAccount(); var sim_win = new WinSim(); ac = sim_win.sim_win_market(from, to, sim_windows, chro, ac, nn_threshold); return(ac.performance_data.total_pl, ac); }