private static void displaySimResult(SimAccount ac, string title) { Console.WriteLine("pl=" + ac.performance_data.total_pl); Console.WriteLine("pl ratio=" + ac.performance_data.total_pl_ratio); 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()); Console.WriteLine("max_dd=" + ac.performance_data.max_dd); Console.WriteLine("max_pl=" + ac.performance_data.max_pl); Console.WriteLine("ave_holding_period=" + ac.holding_data.holding_period_list.Average()); //LineChart.DisplayLineChart2(ac.performance_data.total_capital_list, ac.log_data.close_log, ac.log_data.buy_points.Values.ToList(), ac.log_data.sell_points.Values.ToList(), title); var table_labels = new List <string>() { "PL Ratio", "Num Trade", "Win Rate", "Max DD", "Max PL", "Ave Holding Period", "Num Force Exit" }; var table_data = new List <string>() { Math.Round(ac.performance_data.total_pl_ratio, 4).ToString(), ac.performance_data.num_trade.ToString(), Math.Round(ac.performance_data.win_rate, 4).ToString(), Math.Round(ac.performance_data.max_dd, 4).ToString(), Math.Round(ac.performance_data.max_pl, 4).ToString(), Math.Round(ac.holding_data.holding_period_list.Average(), 1).ToString(), ac.performance_data.num_force_exit.ToString() }; LineChart.DisplayLineChart3(ac.performance_data.total_capital_list, ac.log_data.close_log, ac.performance_data.num_trade_list, table_labels, table_data, "from=" + ac.start_ind.ToString() + ", to=" + ac.end_ind.ToString()); System.Diagnostics.Process.Start(@"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", @"./line_chart.html"); }
/* * */ public SimAccount sim_entry_timing_ptlc(int from, int to, SimAccount ac, string side, double max_leverage, int entry_interval_miniute, int entry_num, double pt_ratio, double lc_ratio) { if (side == "buy" || side == "sell") { var strategy = new Strategy(); for (int i = from; i < to - 1; i++) { var max_size = max_leverage * ac.performance_data.total_capital / MarketData.Close[i]; //常にレバ2倍になるようにサイズを決める var actions = strategy.entryTimingPTLCStrategy(i, max_size, ac, side, entry_num, entry_interval_miniute, pt_ratio, lc_ratio); 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.Close[to]); ac.calc_sharp_ratio(); } else { Console.WriteLine("Invalid side! " + side); } return(ac); }
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); }
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); }
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); }
/* * */ public StrategyActionData entryTimingPTLCStrategy(int i, double max_amount, SimAccount ac, string side, int num_entry, int entry_interval, double pt_ratio, double lc_ratio) { var ad = new StrategyActionData(); var a = max_amount / (Convert.ToDouble(1 + num_entry) * Convert.ToDouble(num_entry) / 2.0); var entry_amount = (ac.holding_data.holding_entry_num + 1) * a; if (ac.holding_data.holding_entry_num >= num_entry - 1) { entry_amount = max_amount - ac.holding_data.holding_size; } var otype = "market"; //0:pt if (ac.holding_data.holding_side != "" && ac.performance_data.unrealized_pl_ratio >= pt_ratio) { ad.add_action("entry", ac.holding_data.holding_side == "buy" ? "sell" : "buy", otype, 0, ac.holding_data.holding_size, -1, "0. Profit Taking"); } //1:lc else if (ac.holding_data.holding_side != "" && ac.performance_data.unrealized_pl_ratio <= lc_ratio) { ad.add_action("entry", ac.holding_data.holding_side == "buy" ? "sell" : "buy", otype, 0, ac.holding_data.holding_size, -1, "1. Loss Cut"); } //2:Entry else if (ac.holding_data.holding_entry_num < num_entry && ac.holding_data.holding_size < max_amount && i - ac.holding_data.holding_i >= entry_interval) { ad.add_action("entry", side, otype, 0, entry_amount, -1, "2. Entry"); } //3:Holding max amount, no action else if (ac.holding_data.holding_size < max_amount) { } return(ad); }
public StrategyActionData GAStrategy(int nn_output, int amount, SimAccount ac) { var ad = new StrategyActionData(); var pred_side = nn_output == 1 ? "buy" : "sell"; if (nn_output == 0) { pred_side = "no"; } if (pred_side == "buy" && ac.holding_data.holding_side == "") { ad.add_action("entry", pred_side, "market", 0, amount, -1, "entry order"); } else if (pred_side == "buy" && ac.holding_data.holding_side == "sell") { ad.add_action("entry", pred_side, "market", 0, ac.holding_data.holding_size + amount, -1, "exit & entry order"); } else if (pred_side == "sell" && ac.holding_data.holding_side == "") { ad.add_action("entry", pred_side, "market", 0, amount, -1, "entry order"); } else if (pred_side == "sell" && ac.holding_data.holding_side == "buy") { ad.add_action("entry", pred_side, "market", 0, ac.holding_data.holding_size + amount, -1, "exit & entry order"); } return(ad); }
private static SimAccount doWinSim(int from, int to, int best_island_id, bool display_chart, double nn_threshold) { Console.WriteLine("Started Read Weight Win SIM"); var ga = new GA(0); var chromo = ga.readWeights(best_island_id, false); var sim = new Sim(); var ac = new SimAccount(); ac = sim.sim_win_ga_market(from, to, chromo, ac, nn_threshold); Console.WriteLine("pl=" + ac.performance_data.total_pl); Console.WriteLine("pl ratio=" + ac.performance_data.total_pl_ratio); 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 (display_chart) { LineChart.DisplayLineChart(ac.total_pl_list, "from=" + from.ToString() + ", to=" + to.ToString() + ", pl=" + ac.performance_data.total_pl.ToString() + ", num_trade=" + ac.performance_data.num_trade.ToString()); } return(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); }
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 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); }
//エントリータイミングをx分にy%以上変動した時などに予め限定してエントリーする戦略 public SimAccount sim_entry_timing_ptlc_price_change(int from, int to, SimAccount ac, double max_leverage, int entry_interval_miniute, int entry_num, double pt_ratio, double lc_ratio, int buy_price_change_minutes, double buy_price_change_ratio, int sell_price_change_minutes, double sell_price_change_ratio) { var strategy = new Strategy(); for (int i = from; i < to - 1; i++) { var max_size = max_leverage * ac.performance_data.total_capital / MarketData.Close[i]; //常にレバ2倍になるようにサイズを決める var actions = strategy.entryTimingPTLCPriceChangeStrategy(i, max_size, ac, entry_num, entry_interval_miniute, pt_ratio, lc_ratio, buy_price_change_minutes, buy_price_change_ratio, sell_price_change_minutes, sell_price_change_ratio); 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.Close[to]); ac.calc_sharp_ratio(); return(ac); }
public SimAccount sim_ga_market_limit(int from, int to, int max_amount, Gene2 chromo, string title, bool chart, double nn_threshold) { var sim = new Sim(); var ac = new SimAccount(); ac = sim.sim_ga_market_limit(from, to, max_amount, chromo, ac, nn_threshold, false); Console.WriteLine("pl=" + ac.performance_data.total_pl); Console.WriteLine("pl ratio=" + ac.performance_data.total_pl_ratio); 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); }
/* * buy / sellでエントリー or exitして、常にpositionを保有し続ける。 * orderは全てmarket order */ public StrategyActionData GAWinMarketStrategy(int i, int nn_output, SimAccount ac) { var ad = new StrategyActionData(); var output_action_list = new string[] { "no", "buy", "sell" }; var pred_side = output_action_list[nn_output]; var buy_entry_price = MarketData.Bid[i]; //- 0.5; var sell_entry_price = MarketData.Ask[i]; // + 0.5; var otype = "market"; var amount = 1; //1. New Entry if (pred_side != "no" && ac.holding_data.holding_side == "") { ad.add_action("entry", pred_side, otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, amount, -1, "1. New Entry"); } //2.Exit Order else if (pred_side != "no" && (ac.holding_data.holding_side != pred_side)) { ad.add_action("entry", "", otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, ac.holding_data.holding_size + amount, -1, "2. Eixt and Entry"); } return(ad); }
/* * 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); }
public StrategyActionData entryTimingPTLCPriceChangeStrategy(int i, double max_amount, SimAccount ac, int num_entry, int entry_interval, double pt_ratio, double lc_ratio, int buy_price_change_minutes, double buy_price_change_ratio, int sell_price_change_minutes, double sell_price_change_ratio) { var ad = new StrategyActionData(); var a = max_amount / (Convert.ToDouble(1 + num_entry) * Convert.ToDouble(num_entry) / 2.0); var entry_amount = (ac.holding_data.holding_entry_num + 1) * a; if (ac.holding_data.holding_entry_num >= num_entry - 1) { entry_amount = max_amount - ac.holding_data.holding_size; } var price_change_trigger = ""; if ((MarketData.Close[i] - MarketData.Close[i - buy_price_change_minutes]) / MarketData.Close[i - buy_price_change_minutes] <= buy_price_change_ratio) { price_change_trigger = "buy"; } else if ((MarketData.Close[i] - MarketData.Close[i - sell_price_change_minutes]) / MarketData.Close[i - sell_price_change_minutes] >= sell_price_change_ratio) { price_change_trigger = "sell"; } var otype = "market"; //0:pt if (ac.holding_data.holding_side != "" && ac.performance_data.unrealized_pl_ratio >= pt_ratio) { ad.add_action("entry", ac.holding_data.holding_side == "buy" ? "sell" : "buy", otype, 0, ac.holding_data.holding_size, -1, "0. Profit Taking"); } //1:lc else if (ac.holding_data.holding_side != "" && ac.performance_data.unrealized_pl_ratio <= lc_ratio) { ad.add_action("entry", ac.holding_data.holding_side == "buy" ? "sell" : "buy", otype, 0, ac.holding_data.holding_size, -1, "1. Loss Cut"); } //2:Entry else if (price_change_trigger != "" && ac.holding_data.holding_entry_num < num_entry && ac.holding_data.holding_size < max_amount && i - ac.holding_data.holding_i >= entry_interval) { ad.add_action("entry", price_change_trigger, otype, 0, entry_amount, -1, "2. Entry"); } //3:Holding max amount, no action else if (ac.holding_data.holding_size < max_amount) { } return(ad); }
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); }
//複数chromを使ったsimを行い、それらの結果の総合したパフォーマンスを表示する。 public SimAccount sim_ga_multi_chromo(int from, int to, int max_amount, List <Gene2> chromo, string title, bool chart, List <double> nn_threshold) { var ac_list = new List <SimAccount>(); for (int i = 0; i < chromo.Count; i++) { var sim = new Sim(); var ac = new SimAccount(); ac = sim.sim_ga_market_limit(from, to, max_amount, chromo[i], ac, nn_threshold[i], false); ac_list.Add(ac); Console.WriteLine("Chromo-" + i.ToString() + ":"); 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()); } //各chrom sim結果を平均する。 var ac_master = new SimAccount(); var buy_pl_sum = 0.0; var sell_pl_sum = 0.0; for (int i = 0; i < ac_list[0].total_pl_list.Count; i++) { ac_master.total_pl_list.Add(ac_list[0].total_pl_list[i]); ac_master.total_pl_ratio_list.Add(ac_list[0].total_pl_ratio_list[i]); } for (int i = 0; i < chromo.Count; i++) { ac_master.performance_data.num_trade += ac_list[i].performance_data.num_trade; ac_master.performance_data.num_buy += ac_list[i].performance_data.num_buy; ac_master.performance_data.num_sell += ac_list[i].performance_data.num_sell; ac_master.performance_data.num_win += ac_list[i].performance_data.num_win; ac_master.performance_data.total_pl += ac_list[i].performance_data.total_pl; ac_master.performance_data.total_pl_ratio += ac_list[i].performance_data.total_pl_ratio; buy_pl_sum += ac_list[i].performance_data.buy_pl_list.Sum(); sell_pl_sum += ac_list[i].performance_data.sell_pl_list.Sum(); for (int j = 0; j < ac_list[i].total_pl_list.Count; j++) { if (i > 0) { ac_master.total_pl_list[j] += ac_list[i].total_pl_list[j]; ac_master.total_pl_ratio_list[j] += ac_list[i].total_pl_ratio_list[j]; } } } for (int i = 0; i < ac_list[0].total_pl_list.Count; i++) { ac_master.total_pl_list[i] = ac_master.total_pl_list[i] / Convert.ToDouble(ac_list.Count); ac_master.total_pl_ratio_list[i] = ac_master.total_pl_ratio_list[i] / Convert.ToDouble(ac_list.Count); } Console.WriteLine(""); Console.WriteLine("Master Results:"); Console.WriteLine("pl=" + ac_master.performance_data.total_pl / Convert.ToDouble(ac_list.Count)); Console.WriteLine("num trade=" + ac_master.performance_data.num_trade / Convert.ToDouble(ac_list.Count)); Console.WriteLine("num market order=" + ac_master.performance_data.num_maker_order); if (ac_master.performance_data.num_trade > 0) { Console.WriteLine("win rate=" + ac_master.performance_data.num_win / ac_master.performance_data.num_trade); } else { Console.WriteLine("win rate=" + "0"); } Console.WriteLine("num_buy=" + ac_master.performance_data.num_buy); Console.WriteLine("num_sell=" + ac_master.performance_data.num_sell); Console.WriteLine("buy_pl=" + buy_pl_sum); Console.WriteLine("sell_pl=" + sell_pl_sum); if (chart) { LineChart.DisplayLineChart(ac_master.total_pl_ratio_list, title); } return(ac_master); }
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 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.Dt[to].ToString(), MarketData.Close[to]); ac.calc_sharp_ratio(); return(ac); }
public double[] generateNNInputDataLimit(SimAccount ac, int i, int[] index) { var input_data = new List <double>(); //Divergence_minmax_scale if (index[0] == 1) { foreach (var d in MarketData.Divergence_minmax_scale[i]) { input_data.Add(d); } } //vola_kyori_minmax_scale if (index[1] == 1) { foreach (var d in MarketData.Volakyori_minmax_scale[i]) { input_data.Add(d); } } //vol ma divergence minmax scale if (index[2] == 1) { foreach (var d in MarketData.Vol_ma_divergence_minmax_scale[i]) { input_data.Add(d); } } //buy sell vol ratio if (index[3] == 1) { foreach (var d in MarketData.Buysell_vol_ratio_minmax_scale[i]) { input_data.Add(d); } } //rsi if (index[4] == 1) { foreach (var d in MarketData.Rsi_scale[i]) { input_data.Add(d); } } //uwahige if (index[5] == 1) { foreach (var d in MarketData.Uwahige_scale[i]) { input_data.Add(d); } } //shitahige if (index[6] == 1) { foreach (var d in MarketData.Shitahige_scale[i]) { input_data.Add(d); } } //order side if (ac.order_data.order_side.Count > 0) { if (ac.order_data.getLastOrderSide() == "buy") { input_data.Add(1); input_data.Add(0); } else if (ac.order_data.getLastOrderSide() == "sell") { input_data.Add(0); input_data.Add(1); } else { Console.WriteLine("Unknown order side! " + ac.order_data.order_side[ac.order_data.order_serial_list[0]]); input_data.Add(0); input_data.Add(0); } } else { input_data.Add(0); input_data.Add(0); } //holding side if (ac.holding_data.holding_side == "buy") { input_data.Add(1); input_data.Add(0); } else if (ac.holding_data.holding_side == "sell") { input_data.Add(0); input_data.Add(1); } else { input_data.Add(0); input_data.Add(0); } //holding size //assumed max amount = 5 /* * var max_amount = 3; * for (int j = 0; j < max_amount; j++) * { * if (ac.holding_data.holding_size > j) * input_data.Add(1); * else * input_data.Add(0); * } */ //unrealized_pl = amount * (price - holding_price) //(price - holding_price) / holding_price <-目的式 //(unrealized_pl / amount) / holding_price //-20 - 20%の損益率を20unitで表現する。 var pl_ratio = ac.holding_data.holding_size > 0 ? 100.0 * (ac.performance_data.unrealized_pl / ac.holding_data.holding_size) / (ac.holding_data.holding_price) : 0; for (int j = 1; j < 21; j++) { if (pl_ratio >= -20 + (j * 2.0)) { input_data.Add(1); } else { input_data.Add(0); } } /* * //holding period * for (int j = 1; j < 21; j++) * { * if (ac.holding_data.holding_period >= j * 10) * input_data.Add(1); * else * input_data.Add(0); * } */ if (input_data.Contains(Double.NaN)) { Console.WriteLine("NNInputDataGenerator: Nan is included !"); } return(input_data.ToArray()); }
public double[] generateNNInputData(SimAccount ac, int i) { var input_data = new List <double>(); //Divergence_minmax_scale //foreach (var d in MarketData.Divergence_minmax_scale[i]) // input_data.Add(d); //vola_kyori //ac holding side if (ac.holding_data.holding_side == "buy") { input_data.Add(0); input_data.Add(1); input_data.Add(0); } else if (ac.holding_data.holding_side == "sell") { input_data.Add(0); input_data.Add(0); input_data.Add(1); } else { input_data.Add(1); input_data.Add(0); input_data.Add(0); } //ac pl, 損益率を2unitにわけて表現する if (ac.performance_data.unrealized_pl == 0) { input_data.Add(0); input_data.Add(0); } else if (ac.performance_data.unrealized_pl > 0) { //unrealized_pl = amount * (price - holding_price) //(price - holding_price) / holding_price <-目的式 //(unrealized_pl / amount) / holding_price input_data.Add((ac.performance_data.unrealized_pl / ac.holding_data.holding_size) / (ac.holding_data.holding_price)); input_data.Add(0); } else { input_data.Add(0); input_data.Add(-1.0 * (ac.performance_data.unrealized_pl / ac.holding_data.holding_size) / (ac.holding_data.holding_price)); } //holding period if (ac.holding_data.holding_period == 0) { input_data.Add(0); } else { input_data.Add(1.0 / ac.holding_data.holding_period); } //unrealized pl / holding period if (ac.holding_data.holding_period == 0) { input_data.Add(0); } else { input_data.Add(ac.performance_data.unrealized_pl / ac.holding_data.holding_period); } //unrealize pl change return(input_data.ToArray()); }
/* * NNでは最大出力ユニットを発火としているが、limit / marketのorder typeの別を決めるためにNN-ActivateUnitで最後のユニットが0.5以上・未満を判定してorder typeとして出力するように変更が必要。 */ //nn_output = "no", "buy", "sell", "cancel", "Market / Limit" public StrategyActionData GALimitMarketStrategy(int i, List <int> nn_output, int amount, int max_amount, SimAccount ac) { var ad = new StrategyActionData(); var output_action_list = new string[] { "no", "buy", "sell", "cancel" }; var pred_side = output_action_list[nn_output[0]]; var otype = nn_output[1] == 0 ? "market" : "limit"; var buy_entry_price = MarketData.Bid[i]; //- 0.5; var sell_entry_price = MarketData.Ask[i]; // + 0.5; var update_price_kijun = 10; //check invalid situation if (ac.holding_data.holding_size > max_amount) { Console.WriteLine("Sim Strategy: Holding size is larger than max_amount !"); } //1. No / Cancel if (pred_side == "no") { if (ac.order_data.getLastOrderSide() != "" && Math.Abs(ac.order_data.getLastOrderPrice() - (ac.order_data.getLastOrderSide() == "buy" ? buy_entry_price : sell_entry_price)) > update_price_kijun) { ad.add_action("update price", pred_side, otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, -1, ac.order_data.getLastSerialNum(), "1. No: update order price"); } } else if (pred_side == "cancel") { if (ac.order_data.getLastOrderSide() != "") { ad.add_action("cancel", "", "", 0, 0, ac.order_data.getLastSerialNum(), "1. Cancel: cancel all order"); } } else { //2. New Entry if (ac.holding_data.holding_side == "" && pred_side != ac.order_data.getLastOrderSide() && ac.order_data.getLastOrderSide() == "") { ad.add_action("entry", pred_side, otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, amount, -1, "2. New Entry"); } //3.Update Price else if (ac.order_data.getLastOrderSide() == pred_side && Math.Abs(ac.order_data.getLastOrderPrice() - (ac.order_data.getLastOrderSide() == "buy" ? buy_entry_price : sell_entry_price)) > update_price_kijun) { ad.add_action("update price", "", otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, -1, ac.order_data.getLastSerialNum(), "3. update order price"); } //4. Additional Entry (pred = holding sideで現在orderなく、holding sizeにamount加えてもmax_amount以下の時に追加注文) else if (ac.holding_data.holding_side == pred_side && ac.holding_data.holding_size + amount <= max_amount && ac.order_data.getLastOrderSide() == "") { ad.add_action("entry", pred_side, otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, amount, -1, "4. Additional Entry"); } //5. Exit (holding side != predでかつpred sideのorderがない時にexit orderを出す) else if ((ac.holding_data.holding_side != pred_side && ac.holding_data.holding_side != "") && (pred_side != ac.order_data.getLastOrderSide())) { //もし既存のadditional orderがあったらまずはそれをキャンセル) if (ac.order_data.getLastOrderSide() != "") { ad.add_action("cancel", "", "", 0, 0, ac.order_data.getLastSerialNum(), "5. cancel all order"); } ad.add_action("entry", pred_side, otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, ac.holding_data.holding_size, -1, "5. Exit Entry"); } //6. Opposite Order Cancel else if (pred_side != ac.order_data.getLastOrderSide() && ac.order_data.getLastOrderSide() != "") { ad.add_action("cancel", "", "", 0, 0, ac.order_data.getLastSerialNum(), "6. cancel all order"); if (ac.holding_data.holding_size + amount <= max_amount) { ad.add_action("entry", pred_side, otype, pred_side == "buy" ? buy_entry_price : sell_entry_price, amount, -1, "6. Opposite Entry"); } if (ac.holding_data.holding_side != "" && ac.holding_data.holding_side != pred_side) { Console.WriteLine("Strategy: Opposite holding exist while cancelling opposite order !"); } } else { //7. Others1 (既にmax amountのholdingがあり、pred side=holding sideで何もしなくて良い場合) if (ac.holding_data.holding_size >= max_amount && ac.holding_data.holding_side == pred_side) { } //8.Others2(holding side == pred sideで既にpred sideのorderが存在しており、その価格の更新が不要な場合) else if (ac.holding_data.holding_side == pred_side && ac.order_data.getLastOrderSide() == pred_side && Math.Abs(ac.order_data.getLastOrderPrice() - (ac.order_data.getLastOrderSide() == "buy" ? buy_entry_price : sell_entry_price)) <= update_price_kijun) { } //9. Others3 (holding side != predで既にexit orderが存在しており、update priceも不要な場合) else if (ac.holding_data.holding_side != pred_side && ac.order_data.getLastOrderSide() == pred_side && Math.Abs(ac.order_data.getLastOrderPrice() - (ac.order_data.getLastOrderSide() == "buy" ? buy_entry_price : sell_entry_price)) <= update_price_kijun) { } //10.Others4(holding side == pred sideでorderもない場合) else if (ac.holding_data.holding_side == pred_side && ac.order_data.getLastOrderSide() == "") { } else { Console.WriteLine("Strategy - Unknown Situation !"); } } } return(ad); }
public void startAnalysis3(List <Dictionary <string, double> > param_data, List <int> buy_price_change_minutes, List <double> buy_price_change_ratio, List <int> sell_price_change_minutes, List <double> sell_price_change_ratio) { var analytics_params = new List <double[]>(); var parameter_id = 0; var num_all_trials = param_data.Count * buy_price_change_minutes.Count * buy_price_change_ratio.Count * sell_price_change_minutes.Count * sell_price_change_ratio.Count; var holding_periods = new ConcurrentDictionary <int, double>(); var total_pl = new ConcurrentDictionary <int, double>(); var num_trade = new ConcurrentDictionary <int, int>(); var win_rate = new ConcurrentDictionary <int, double>(); var max_dd = new ConcurrentDictionary <int, double>(); var max_pl = new ConcurrentDictionary <int, double>(); var num_force_exit = new ConcurrentDictionary <int, int>(); for (int i = 0; i < param_data.Count; i++) { for (int j = 0; j < buy_price_change_minutes.Count; j++) { for (int k = 0; k < buy_price_change_ratio.Count; k++) { for (int l = 0; l < sell_price_change_minutes.Count; l++) { for (int m = 0; m < sell_price_change_ratio.Count; m++) { var ac = new SimAccount(); var sim = new Sim(); var entry_num = Convert.ToInt32(param_data[i]["entry_num"]); var entry_interval = Convert.ToInt32(param_data[i]["entry_interval"]); var leverage = param_data[i]["leverage"]; var pt = param_data[i]["pt"]; var lc = param_data[i]["lc"]; analytics_params.Add(new double[] { leverage, Convert.ToDouble(entry_num), Convert.ToDouble(entry_interval), pt, lc, Convert.ToDouble(buy_price_change_minutes[j]), buy_price_change_ratio [k], Convert.ToDouble(sell_price_change_minutes[l]), sell_price_change_ratio[m] }); ac = sim.sim_entry_timing_ptlc_price_change(1000, MarketData.Close.Count - 1, ac, leverage, entry_interval, entry_num, pt, lc, buy_price_change_minutes[j], buy_price_change_ratio[k], sell_price_change_minutes[l], sell_price_change_ratio[m]); holding_periods[parameter_id] = Math.Round(ac.holding_data.holding_period_list.Average(), 1); total_pl[parameter_id] = Math.Round(ac.performance_data.total_pl, 1); num_trade[parameter_id] = ac.performance_data.num_trade; win_rate[parameter_id] = ac.performance_data.win_rate; max_dd[parameter_id] = ac.performance_data.max_dd; max_pl[parameter_id] = ac.performance_data.max_pl; num_force_exit[parameter_id] = ac.performance_data.num_force_exit; var progress_ratio = Math.Round(Convert.ToDouble(parameter_id) / num_all_trials, 4); Console.WriteLine("************************************ (" + progress_ratio.ToString() + "%)" + parameter_id.ToString() + " / " + num_all_trials.ToString() + " ************************************"); Console.WriteLine("id=" + parameter_id.ToString() + ", leverage=" + leverage.ToString() + ", entry_num=" + entry_num.ToString() + ", interval=" + entry_interval.ToString() + ", pt ratio=" + pt.ToString() + ", lc ratio=" + lc.ToString() + ", buy change minute=" + buy_price_change_minutes[j].ToString() + ", buy change ratio=" + buy_price_change_ratio[k].ToString() + ", sell change minute=" + sell_price_change_minutes[l].ToString() + ", sell change ratio=" + sell_price_change_ratio[m].ToString()); Console.WriteLine("holding period=" + ac.holding_data.holding_period_list.Average().ToString() + ", num trade=" + ac.performance_data.num_trade + ", win rate=" + ac.performance_data.win_rate.ToString() + ", pl=" + ac.performance_data.total_pl.ToString() + ", max_dd=" + ac.performance_data.max_dd.ToString() + ", max pl=" + ac.performance_data.max_pl); parameter_id++; } } } } } Console.WriteLine("Writing results..."); using (StreamWriter sw = new StreamWriter("statistics analysis3" + ".csv", false, Encoding.UTF8)) { sw.WriteLine("ID,leverage,entry num,entry interval,pt,lc,buy change minutes,buy change ratio,sell change minutes,sell change ratio,holding period,pl,num trade,win rate,max dd,max pl,num_force_exit"); for (int i = 0; i < parameter_id; i++) { sw.WriteLine(i.ToString() + "," + analytics_params[i][0].ToString() + "," + analytics_params[i][1].ToString() + "," + analytics_params[i][2].ToString() + "," + analytics_params[i][3].ToString() + "," + analytics_params[i][4].ToString() + "," + analytics_params[i][5].ToString() + "," + analytics_params[i][6].ToString() + "," + analytics_params[i][7].ToString() + "," + analytics_params[i][8].ToString() + "," + holding_periods[i].ToString() + "," + holding_periods[i].ToString() + "," + total_pl[i].ToString() + "," + total_pl[i].ToString() + "," + num_trade[i].ToString() + "," + win_rate[i].ToString() + "," + max_dd[i].ToString() + "," + "," + max_pl[i].ToString() + "," + "," + num_force_exit[i].ToString()); } } Console.WriteLine("Completed Statistics Analysis3"); }
public static void Main(string[] args) { Console.WriteLine("# of CPU cores=" + System.Environment.ProcessorCount.ToString()); var key = ""; while (true) { Console.WriteLine("\"ga\" : island GA"); Console.WriteLine("\"sim\" : read sim"); Console.WriteLine("\"mul ga\" : multi strategy ga"); Console.WriteLine("\"mul sim\" : multi strategy sim"); Console.WriteLine("\"conti\" : do ga / sim continuously"); Console.WriteLine("\"win ga\" : do win ga"); Console.WriteLine("\"win sim\" : do win sim"); Console.WriteLine("\"sa1\" : statistics analysis1"); Console.WriteLine("\"sa2\" : statistics analysis2"); Console.WriteLine("\"sa3\" : statistics analysis3"); Console.WriteLine("\"ptlc sim\" : ptlc periodical entry sim"); Console.WriteLine("\"write\" : write MarketData"); Console.WriteLine("\"test\" : test"); key = Console.ReadLine(); if (key == "ga" || key == "sim" || key == "mul ga" || key == "mul sim" || key == "win ga" || key == "conti" || key == "win ga" || key == "win sim" || key == "write" || key == "test" || key == "sa1" || key == "sa2" || key == "sa3" || key == "ptlc sim") { break; } } Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); Console.WriteLine("started program."); List <int> terms = new List <int>(); for (int i = 10; i < 100; i = i + 10) { terms.Add(i); } MarketData.initializer(terms); var from = 1000; var to = MarketData.Close.Count - 1; int max_amount = 1; var index = new int[] { 0, 0, 0, 1, 1, 0, 0 }; double nn_threshold = 0.7; int best_island_id = 0; bool display_chart = true; var sim_type = 1; //0:limit, 1:market/limit if (key == "test") { var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold); LineChart.DisplayLineChart2(ac.total_pl_list, ac.log_data.close_log, ac.log_data.buy_points.Values.ToList(), ac.log_data.sell_points.Values.ToList(), "test"); System.Diagnostics.Process.Start(@"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", @"./line_chart.html"); } //read weight sim if (key == "sim") { var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold); LineChart.DisplayLineChart2(ac.total_pl_list, ac.log_data.close_log, ac.log_data.buy_points.Values.ToList(), ac.log_data.sell_points.Values.ToList(), "test"); System.Diagnostics.Process.Start(@"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome", @"./line_chart.html"); } //island ga else if (key == "ga") { int num_island = 1; int num_chromos = 4; int num_generations = 100; int banned_move_period = 5; var units = new int[] { 42, 90, 5 }; var mutation_rate = 0.7; var move_ratio = 0.5; best_island_id = doGA(from, to, max_amount, num_island, num_chromos, num_generations, banned_move_period, units, mutation_rate, move_ratio, index, display_chart, nn_threshold); doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold); doSim(to, MarketData.Close.Count - 1, max_amount, sim_type, best_island_id, display_chart, nn_threshold); } //multi strategy combination sim else if (key == "mul ga") { var index_list = new List <int[]> { new int[] { 1, 0, 0, 0, 0, 0, 0 }, new int[] { 0, 1, 0, 0, 0, 0, 0 }, new int[] { 0, 0, 1, 0, 0, 0, 0 } }; var units_list = new List <int[]> { new int[] { 34, 10, 5 }, new int[] { 34, 10, 5 }, new int[] { 34, 10, 5 } }; var best_pl_list = new List <List <double> >(); var best_ac_list = new List <SimAccount>(); int num_island = 2; int num_chromos = 4; int num_generations = 30; int banned_move_period = 3; var mutation_rate = 0.7; var move_ratio = 0.5; var id_list = new List <int>(); var nn_threshold_list = new List <double>(); for (int i = 0; i < index_list.Count; i++) { best_island_id = doGA(from, to, max_amount, num_island, num_chromos, num_generations, banned_move_period, units_list[i], mutation_rate, move_ratio, index_list[i], display_chart, nn_threshold); var ac = doSim(from, to, max_amount, sim_type, best_island_id, display_chart, nn_threshold); best_pl_list.Add(ac.total_pl_ratio_list); best_ac_list.Add(ac); if (File.Exists(@"./log_best_weight_ID-" + i.ToString() + ".csv")) { File.Delete(@"./log_best_weight_ID-" + i.ToString() + ".csv"); } File.Copy(@"./best_weight_ID-" + best_island_id.ToString() + ".csv", @"./log_best_weight_ID-" + i.ToString() + ".csv"); id_list.Add(i); nn_threshold_list.Add(nn_threshold); } doMultiSim(from, to, max_amount, id_list, true, nn_threshold_list); doMultiSim(to, MarketData.Close.Count - 1, max_amount, id_list, true, nn_threshold_list); } else if (key == "mul sim") { var num_best_chromo = 3; var id_list = new List <int>(); var nn_threshold_list = new List <double>(); for (int i = 0; i < num_best_chromo; i++) { id_list.Add(i); nn_threshold_list.Add(nn_threshold); } doMultiSim(from, to, max_amount, id_list, true, nn_threshold_list); } else if (key == "win ga") { int num_island = 2; int num_chromos = 4; int num_generations = 200; int banned_move_period = 5; int num_random_windows = 10; index = new int[] { 0, 0, 0, 1, 0, 0, 0 }; var units = new int[] { 10, 30, 3 }; var mutation_rate = 0.5; var move_ratio = 0.2; best_island_id = doWinGA(from, to, num_random_windows, num_island, num_chromos, num_generations, banned_move_period, units, mutation_rate, move_ratio, index, display_chart, nn_threshold); doWinSim(from, to, best_island_id, true, nn_threshold); doWinSim(to, MarketData.Close.Count - 1, best_island_id, true, nn_threshold); } else if (key == "win sim") { doWinSim(from, to, best_island_id, true, nn_threshold); } else if (key == "sa1") { var sa = new StatisticsAnalysis(); var data_entry_points = Enumerable.Range(0, MarketData.Close.Count).Where(i => i % 1000 == 0).ToList(); //var entry_num = Enumerable.Range(1, 100).Where(i => i % 5 == 0).ToList(); var entry_num = new List <int>() { 1, 5, 50, 100 }; //var entry_interval = Enumerable.Range(1, 100).Where(i => i % 5 == 0).ToList(); var entry_interval = new List <int>() { 1, 5, 50, 100 }; //var ptlc_ratio = Enumerable.Range(2, 100).ToList().ConvertAll(x => Math.Round(x * 0.1,1)).ToList(); var ptlc_ratio = new List <double>() { 0.2, 1, 5, 15 }; sa.startAnalysis1(data_entry_points, entry_num, entry_interval, entry_interval, ptlc_ratio); } else if (key == "sa2") { var sa = new StatisticsAnalysis(); /*var leverages = new List<double>() { 0.5,1,1.5,2,2.5,3,5}; * var entry_num = new List<int>() { 1, 5, 50, 100 }; * var entry_interval = new List<int>() { 1, 5, 50, 100 }; * var pt_ratio = new List<double>() { 0.05, 0.07, 0.1, 0.15, 0.2, 0.25 }; * var lc_ratio = new List<double>() { -0.05, -0.07, -0.1, -0.15, -0.2, -0.25 };*/ var leverages = new List <double>() { 0.5, 5 }; var entry_num = new List <int>() { 1, 5 }; var entry_interval = new List <int>() { 1, 5 }; var pt_ratio = new List <double>() { 0.05, 0.15 }; var lc_ratio = new List <double>() { -0.05, -0.15 }; sa.startAnalysis2(pt_ratio, lc_ratio, leverages, entry_interval, entry_num); } else if (key == "sa3") { var sa = new StatisticsAnalysis(); var buy_price_change_minutes = new List <int>() { 10, 30, 60, 90, 300, 600 }; var buy_price_change_ratio = new List <double>() { -0.01, -0.05, -0.07, -0.1, -0.15, -0.25 }; var sell_price_change_minutes = new List <int>() { 10, 30, 60, 90, 300, 600 }; var sell_price_change_ratio = new List <double>() { 0.01, 0.05, 0.07, 0.1, 0.15, 0.25 }; var param_data = new List <Dictionary <string, double> >(); var param1 = new Dictionary <string, double>() { { "leverage", 3.0 }, { "entry_num", 100 }, { "entry_interval", 5 }, { "pt", 0.25 }, { "lc", -0.2 } }; var param2 = new Dictionary <string, double>() { { "leverage", 2.0 }, { "entry_num", 50 }, { "entry_interval", 5 }, { "pt", 0.07 }, { "lc", -0.2 } }; var param3 = new Dictionary <string, double>() { { "leverage", 2.0 }, { "entry_num", 5 }, { "entry_interval", 5 }, { "pt", 0.15 }, { "lc", -0.2 } }; param_data.Add(param1); param_data.Add(param2); param_data.Add(param3); sa.startAnalysis3(param_data, buy_price_change_minutes, buy_price_change_ratio, sell_price_change_minutes, sell_price_change_ratio); } else if (key == "conti") { int num_island = 2; int num_chromos = 4; int num_generations = 20; int banned_move_period = 2; var units = new int[] { 67, 5, 5, 5, 5 }; var mutation_rate = 0.5; var move_ratio = 0.2; var sim_period = 5000; var ga_period = 10000; var conti_from = from; var ac_list = new List <SimAccount>(); var all_pl_list = new List <double>(); all_pl_list.Add(0); var all_num_trade = 0; while (to > sim_period + ga_period + conti_from) { best_island_id = doGA(conti_from, conti_from + ga_period, max_amount, num_island, num_chromos, num_generations, banned_move_period, units, mutation_rate, move_ratio, index, display_chart, nn_threshold); ac_list.Add(doSim(conti_from + ga_period, conti_from + ga_period + sim_period, max_amount, sim_type, best_island_id, true, nn_threshold)); foreach (var p in ac_list.Last().total_pl_list) { all_pl_list.Add(all_pl_list.Last() + p); } all_num_trade += ac_list.Last().performance_data.num_trade; conti_from += sim_period; } Console.WriteLine("Total pl =" + all_pl_list.Last().ToString() + ", num trade=" + all_num_trade.ToString()); LineChart.DisplayLineChart(all_pl_list, "from=" + (from + ga_period).ToString() + ", to=" + (conti_from + ga_period + sim_period).ToString() + ", Total pl =" + all_pl_list.Last().ToString() + ", num trade=" + all_num_trade.ToString()); } else if (key == "ptlc sim") { var pt = 0.07; var lc = -0.2; var leverage = 2; var side = "buy"; var entry_interval_minutes = 5; var entry_num = 50; var ac = new SimAccount(); var sim = new Sim(); ac = sim.sim_entry_timing_ptlc(from, to, ac, side, leverage, entry_interval_minutes, entry_num, pt, lc); displaySimResult(ac, "ptlc sim from:" + from.ToString() + " to:" + to.ToString() + ", pl=" + ac.performance_data.total_pl.ToString() + ", num=" + ac.performance_data.num_trade.ToString()); } else if (key == "write") { MarketData.writeData(); } stopWatch.Stop(); Console.WriteLine("Completed all processes."); Console.WriteLine("Time Elapsed (sec)=" + stopWatch.Elapsed.TotalSeconds.ToString()); }
/*常にlimit entry、num order = 1以下。 * 1. pred_side == order_sideのときは、max_amountに達するまでamountを追加してupdate price * 2. pred_side != order_sideのときは、cancel all orders, pred_sideにamountのlimit orderを出す。 * 3. pred_side == holding_sideのときは、max_amountに達するまでamountのlimit orderを出す。 * 4. pred_side != holding_sideのときは、amount + holding_sizeのlimit orderを出す * */ public StrategyActionData GALimitStrategy(int i, int nn_output, int amount, int max_amount, SimAccount ac) { var ad = new StrategyActionData(); var output_action_list = new string[] { "no", "buy", "sell", "cancel" }; var pred_side = output_action_list[nn_output]; if (pred_side == "no") { } else if (pred_side == "cancel") { if (ac.order_data.getLastSerialNum() > 0) { ad.add_action("cancel", "", "", 0, 0, ac.order_data.order_serial_list.Last(), "cancel all order"); } } else { if (pred_side == ac.order_data.getLastOrderSide()) //1. { if (ac.holding_data.holding_size + ac.order_data.getLastOrderSize() < max_amount) { ad.add_action("update amount", pred_side, "limit", 0, ac.order_data.getLastOrderSize() + amount, ac.order_data.order_serial_list.Last(), "update order amount"); } //if ((ac.order_data.getLastOrderSide() == "buy" && MarketData.Close[i] > ac.order_data.getLastOrderPrice()) || (ac.order_data.getLastOrderSide() == "sell" && MarketData.Close[i] < ac.order_data.getLastOrderPrice())) if (ac.order_data.getLastOrderPrice() != MarketData.Close[i]) { ad.add_action("update price", pred_side, "limit", MarketData.Close[i], ac.order_data.getLastOrderSize(), ac.order_data.order_serial_list.Last(), "update order price"); } } else if (pred_side != ac.order_data.getLastOrderSide()) //2. { if (ac.order_data.getLastOrderSide() != "") { ad.add_action("cancel", "", "", 0, 0, ac.order_data.order_serial_list.Last(), "cancel all order"); } if ((pred_side == ac.holding_data.holding_side && ac.holding_data.holding_size + amount > max_amount) == false) { ad.add_action("entry", pred_side, "limit", MarketData.Close[i], amount, -1, "entry order"); } } else if (pred_side == ac.holding_data.holding_side && ac.holding_data.holding_size + ac.order_data.getLastOrderSize() < max_amount) //3. { ad.add_action("entry", pred_side, "limit", MarketData.Close[i], amount, -1, "entry order"); } else if (pred_side != ac.holding_data.holding_side && ac.order_data.getLastOrderSide() != pred_side) //4. { ad.add_action("entry", pred_side, "limit", MarketData.Close[i], Math.Min(ac.holding_data.holding_size + amount, ac.holding_data.holding_size + max_amount), -1, "entry order"); } } return(ad); }
public void startAnalysis2(List <double> pt_list, List <double> lc_list, List <double> leverage_list, List <int> entry_interval_minutes, List <int> entry_num_list) { var analytics_params = new List <double[]>(); var parameter_id = 0; var num_all_trials = pt_list.Count * lc_list.Count * leverage_list.Count * entry_interval_minutes.Count * entry_num_list.Count; var buy_holding_periods = new ConcurrentDictionary <int, double>(); var sell_holding_periods = new ConcurrentDictionary <int, double>(); var buy_total_pl = new ConcurrentDictionary <int, double>(); var sell_total_pl = new ConcurrentDictionary <int, double>(); var buy_num_trade = new ConcurrentDictionary <int, int>(); var sell_num_trade = new ConcurrentDictionary <int, int>(); var buy_win_rate = new ConcurrentDictionary <int, double>(); var sell_win_rate = new ConcurrentDictionary <int, double>(); var buy_max_dd = new ConcurrentDictionary <int, double>(); var sell_max_dd = new ConcurrentDictionary <int, double>(); var buy_max_pl = new ConcurrentDictionary <int, double>(); var sell_max_pl = new ConcurrentDictionary <int, double>(); var buy_num_force_exit = new ConcurrentDictionary <int, int>(); var sell_num_force_exit = new ConcurrentDictionary <int, int>(); for (int i = 0; i < pt_list.Count; i++) { for (int j = 0; j < lc_list.Count; j++) { for (int k = 0; k < leverage_list.Count; k++) { for (int l = 0; l < entry_interval_minutes.Count; l++) { for (int m = 0; m < entry_num_list.Count; m++) { var ac_buy = new SimAccount(); var ac_sell = new SimAccount(); var sim = new Sim(); analytics_params.Add(new double[] { leverage_list[k], Convert.ToDouble(entry_interval_minutes[l]), Convert.ToDouble(entry_num_list[m]), pt_list[i], lc_list[j] }); ac_buy = sim.sim_entry_timing_ptlc(1000, MarketData.Close.Count - 1, ac_buy, "buy", leverage_list[k], entry_interval_minutes[l], entry_num_list[m], pt_list[i], lc_list[j]); ac_sell = sim.sim_entry_timing_ptlc(1000, MarketData.Close.Count - 1, ac_sell, "sell", leverage_list[k], entry_interval_minutes[l], entry_num_list[m], pt_list[i], lc_list[j]); buy_holding_periods[parameter_id] = Math.Round(ac_buy.holding_data.holding_period_list.Average(), 1); sell_holding_periods[parameter_id] = Math.Round(ac_sell.holding_data.holding_period_list.Average(), 1); buy_total_pl[parameter_id] = Math.Round(ac_buy.performance_data.total_pl, 1); sell_total_pl[parameter_id] = Math.Round(ac_sell.performance_data.total_pl, 1); buy_num_trade[parameter_id] = ac_buy.performance_data.num_trade; sell_num_trade[parameter_id] = ac_sell.performance_data.num_trade; buy_win_rate[parameter_id] = ac_buy.performance_data.win_rate; sell_win_rate[parameter_id] = ac_sell.performance_data.win_rate; buy_max_dd[parameter_id] = ac_buy.performance_data.max_dd; sell_max_dd[parameter_id] = ac_sell.performance_data.max_dd; buy_max_pl[parameter_id] = ac_buy.performance_data.max_pl; sell_max_pl[parameter_id] = ac_sell.performance_data.max_pl; buy_num_force_exit[parameter_id] = ac_buy.performance_data.num_force_exit; sell_num_force_exit[parameter_id] = ac_sell.performance_data.num_force_exit; var progress_ratio = Math.Round(Convert.ToDouble(parameter_id) / num_all_trials, 4); Console.WriteLine("************************************ (" + progress_ratio.ToString() + "%)" + parameter_id.ToString() + " / " + num_all_trials.ToString() + " ************************************"); Console.WriteLine("id=" + parameter_id.ToString() + ", leverage=" + leverage_list[k].ToString() + ", entry_num=" + entry_num_list[m].ToString() + ", interval=" + entry_interval_minutes[l].ToString() + ", pt ratio=" + pt_list[i].ToString() + ", lc ratio=" + lc_list[j].ToString()); Console.WriteLine("BUY: " + "holding period=" + ac_buy.holding_data.holding_period_list.Average().ToString() + ", num trade=" + ac_buy.performance_data.num_trade + ", win rate=" + ac_buy.performance_data.win_rate.ToString() + ", pl=" + ac_buy.performance_data.total_pl.ToString() + ", max_dd=" + ac_buy.performance_data.max_dd.ToString() + ", max pl=" + ac_buy.performance_data.max_pl); Console.WriteLine("SEL: " + "holding period=" + ac_sell.holding_data.holding_period_list.Average().ToString() + ", num trade=" + ac_sell.performance_data.num_trade + ", win rate=" + ac_sell.performance_data.win_rate.ToString() + ", pl=" + ac_sell.performance_data.total_pl.ToString() + ", max_dd=" + ac_sell.performance_data.max_dd.ToString() + ", max pl=" + ac_sell.performance_data.max_pl); parameter_id++; } } } } } Console.WriteLine("Writing results..."); using (StreamWriter sw = new StreamWriter("statistics analysis2" + ".csv", false, Encoding.UTF8)) { sw.WriteLine("ID,leverage,entry num,entry interval,pt,lc,buy holding period,sell holding period,buy pl,sell pl,buy num trade,sell num trade,buy win rate,sell win rate,buy max dd,sell max dd,buy max pl,sell max pl,buy_num_force_exit,sell_num_force_exit"); for (int i = 0; i < parameter_id; i++) { sw.WriteLine(i.ToString() + "," + analytics_params[i][0].ToString() + "," + analytics_params[i][2].ToString() + "," + analytics_params[i][1].ToString() + "," + analytics_params[i][3].ToString() + "," + analytics_params[i][4].ToString() + "," + buy_holding_periods[i].ToString() + "," + sell_holding_periods[i].ToString() + "," + buy_total_pl[i].ToString() + "," + sell_total_pl[i].ToString() + "," + buy_num_trade[i].ToString() + "," + sell_num_trade[i].ToString() + "," + buy_win_rate[i].ToString() + "," + sell_win_rate[i].ToString() + "," + buy_max_dd[i].ToString() + "," + sell_max_dd[i].ToString() + "," + buy_max_pl[i].ToString() + "," + sell_max_pl[i].ToString() + "," + buy_num_force_exit[i].ToString() + "," + sell_num_force_exit[i].ToString()); } } Console.WriteLine("Completed Statistics Analysis2"); }