Exemple #1
0
        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);
        }
Exemple #2
0
        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);
        }
Exemple #3
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);
        }
Exemple #4
0
        /*
         * 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);
        }
Exemple #5
0
        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);
        }