Exemplo n.º 1
0
        public void LimitCloseLongProfitTest()
        {
            double       initial_cash        = 1000;
            double       commission_per_unit = 0.01;
            List <Price> prices = new List <Price>();

            prices.Add(new Price(new DateTimeUTC(2000, 0, 0, 0, 0, 0), 1.0, 1.1));
            prices.Add(new Price(new DateTimeUTC(2000, 0, 0, 0, 0, 1), 1.4, 1.5));
            prices.Add(new Price(new DateTimeUTC(2000, 0, 0, 0, 0, 2), 2.0, 2.1));
            PriceSet price_set           = new PriceSet(ToolsPrice.DefaultSymbolGBPUSD, prices.AsReadOnly());
            MarketModelSimulation market = new MarketModelSimulation(initial_cash, commission_per_unit, price_set);

            market.ProcessOrderCommand(new TradingOrderCommand(TradingOrderType.Long, 1.0, 1.0, 0.0, 0.9, 1.6));

            market.StepSecond();
            List <int> open_orders_0 = new List <int>(market.OpenOrders.Keys);

            Assert.AreEqual(1, open_orders_0.Count);
            market.StepSecond();
            List <int> open_orders_1 = new List <int>(market.OpenOrders.Keys);

            Assert.AreEqual(0, open_orders_1.Count);

            Assert.AreEqual(1000.49, market.Cash);
        }
        public void TestCreateSmall()
        {
            //Prices 1.0  0.9, 1.1, 1.2
            List <Price> prices = new List <Price>();

            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 0), 1.0, 1.1));
            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 1), 0.9, 1.0));
            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 2), 1.1, 1.2));
            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 3), 1.2, 1.3));
            PriceSet price_set = new PriceSet(ToolsPrice.DefaultSymbolGBPUSD, prices.AsReadOnly());

            List <IIndicator> indicators_features = new List <IIndicator>();

            indicators_features.Add(new IndicatorRunningAverage(0));
            indicators_features.Add(new IndicatorRunningAverage(1));
            List <IIndicator> indicators_labels = new List <IIndicator>();

            indicators_labels.Add(new IndicatorMagicProfit(0));
            indicators_labels.Add(new IndicatorMagicProfit(1));
            MarketModelSimulation     market  = new MarketModelSimulation(1000, price_set);
            IDataSet <double, double> dataset = ToolsTradingDataSet.CreateDataSet(market, new IndicatorFusion(indicators_features), new IndicatorFusion(indicators_labels));

            Assert.AreEqual(0.90, dataset.FeatureData[0][0], 0.0000001);
            Assert.AreEqual(0.95, dataset.FeatureData[0][1], 0.0000001);
            Assert.AreEqual(1.10, dataset.FeatureData[1][0], 0.0000001);
            Assert.AreEqual(1.00, dataset.FeatureData[1][1], 0.0000001);
            Assert.AreEqual(-0.1, dataset.LabelData[0][0], 0.0000001);
            Assert.AreEqual(-0.1, dataset.LabelData[0][1], 0.0000001);
            Assert.AreEqual(0.10, dataset.LabelData[0][2], 0.0000001);
            Assert.AreEqual(-0.1, dataset.LabelData[0][3], 0.0000001);
            Assert.AreEqual(-0.1, dataset.LabelData[1][0], 0.0000001);
            Assert.AreEqual(-0.1, dataset.LabelData[1][1], 0.0000001);
            Assert.AreEqual(0.0, dataset.LabelData[1][2], 0.0000001);
            Assert.AreEqual(0.0, dataset.LabelData[1][3], 0.0000001);
        }
Exemplo n.º 3
0
        private static void AddIndicators(MarketModelSimulation market, IIndicator feature_indicator, IIndicator label_indicators, IList <double[]> feature_data, IList <bool[]> missing_data, IList <double[]> label_data)
        {
            Tuple <double[], bool> tuple_features = feature_indicator.Compute(market);
            Tuple <double[], bool> tuple_labels   = label_indicators.Compute(market);

            //Only add valid entries?
            if (tuple_features.Item2 && tuple_labels.Item2)
            {
                feature_data.Add(tuple_features.Item1);
                missing_data.Add(ToolsCollection.CreateArray(feature_indicator.SubIndicatorCount, false));
                label_data.Add(tuple_labels.Item1);
            }
        }
Exemplo n.º 4
0
        public void CloseOrderNoLimitTest()
        {
            double spread = 0.1;
            Dictionary <int, TradingOrder> OpenOrders = new Dictionary <int, TradingOrder>();
            PriceCandle candle = new PriceCandle(DateTimeUTC.Now, TimeScale.Second1, 1, 1.2, 0.8, 1, spread);


            TradingOrder order_0 = new TradingOrder(TradingOrderType.Long, 0, DateTimeUTC.Now, 1, 1, 0.01);

            OpenOrders.Add(order_0.OrderTicket, order_0);

            List <TradingOrder> orders = MarketModelSimulation.CheckOrderLimits(OpenOrders, candle);

            Assert.AreEqual(orders.Count, 0);
        }
Exemplo n.º 5
0
        public static DataSet <double, double> CreateDataSet(MarketModelSimulation market, IIndicator feature_indicator, IIndicator label_indicator)
        {
            IList <double[]> feature_data = new List <double[]>();
            IList <bool[]>   missing_data = new List <bool[]>();
            IList <double[]> label_data   = new List <double[]>();

            AddIndicators(market, feature_indicator, label_indicator, feature_data, missing_data, label_data);
            while (market.Second1.FutureCount != 0)
            {
                market.StepSecond();
                AddIndicators(market, feature_indicator, label_indicator, feature_data, missing_data, label_data);
            }

            IDataContext data_context_labeled = new DataContext(DataLevel.INTERVAL, feature_indicator.SubIndicatorNames, DataLevel.INTERVAL, label_indicator.SubIndicatorNames);

            return(new DataSet <double, double>(data_context_labeled, feature_data, missing_data, label_data));
        }
        public void TestCreateMedium()
        {
            PriceSet          price_set           = new PriceSet(ToolsPrice.DefaultSymbolGBPUSD, ToolsCollection.Select(ToolsPrice.GetPriceList(ToolsPrice.DefaultSymbolGBPUSD), 0, 40));
            List <IIndicator> indicators_features = new List <IIndicator>();

            indicators_features.Add(new IndicatorRunningAverage(0));
            indicators_features.Add(new IndicatorRunningAverage(1));
            List <IIndicator> indicators_labels = new List <IIndicator>();

            indicators_labels.Add(new IndicatorMagicProfit(0));
            indicators_labels.Add(new IndicatorMagicProfit(1));
            MarketModelSimulation     market  = new MarketModelSimulation(1000, price_set);
            IDataSet <double, double> dataset = ToolsTradingDataSet.CreateDataSet(market, new IndicatorFusion(indicators_features), new IndicatorFusion(indicators_labels));

            Assert.AreEqual(39, dataset.InstanceCount);
            Assert.AreEqual(2, dataset.FeatureCount);
            Assert.AreEqual(4, dataset.LabelCount);
        }
Exemplo n.º 7
0
        public DateTime GetNextDateTime(DateTime current, MarketModelSimulation data_set)
        {
            DateTime previous = current;

            if (previous.Month == 1) //december
            {
                previous = new DateTime(current.Year - 1, 12, 1);
            }
            else
            {
                previous = new DateTime(current.Year, current.Month - 1, 1);
            }
            while (previous.DayOfWeek != this.day_of_week)
            {
                previous = previous.AddDays(1);
            }
            return(previous);
        }
Exemplo n.º 8
0
        public static void AddIndicatorResult(PlotLine2D line_plot_2d, PriceSet price_set, IIndicator indicator, IList <Color> color_list, IList <int> selected_subindicators)
        {
            MarketModelSimulation market = new MarketModelSimulation(10000, price_set);

            double[] time = new double[price_set.Prices.Count];
            for (int price_index = 0; price_index < price_set.Prices.Count; price_index++)
            {
                time[price_index] = price_set.Prices[price_index].Time.Ticks;
            }
            Tuple <double[, ], bool[]> tuple      = indicator.ComputeAll(market, price_set.Second1.Count);
            List <IList <int> >        selections = CreateSections(tuple.Item2);

            for (int index = 0; index < selected_subindicators.Count; index++)
            {
                double[] signal = tuple.Item1.Select1DIndex1(selected_subindicators[index]);
                AddSignal(line_plot_2d, time, signal, selections, color_list[index]);
            }
        }
Exemplo n.º 9
0
        public void TestStep()
        {
            List <Price> prices = new List <Price>();

            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 0), 1.0, 1.1));
            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 1), 0.9, 1.0));
            prices.Add(new Price(new DateTimeUTC(2000, 1, 1, 0, 0, 2), 1.1, 1.2));
            PriceSet price_set           = new PriceSet(ToolsPrice.DefaultSymbolGBPUSD, prices.AsReadOnly());
            MarketModelSimulation market = new MarketModelSimulation(1000, price_set);

            Assert.AreEqual(1.0, market.CurrentBid, 0.0000001);
            Assert.AreEqual(1.1, market.CurrentAsk, 0.0000001);
            market.StepSecond();
            Assert.AreEqual(0.9, market.CurrentBid, 0.0000001);
            Assert.AreEqual(1.0, market.CurrentAsk, 0.0000001);
            market.StepSecond();
            Assert.AreEqual(1.1, market.CurrentBid, 0.0000001);
            Assert.AreEqual(1.2, market.CurrentAsk, 0.0000001);
        }
Exemplo n.º 10
0
        public void ManualCloseShortTest()
        {
            double       initial_cash        = 1000;
            double       commission_per_unit = 0.01;
            List <Price> prices = new List <Price>();

            prices.Add(new Price(new DateTimeUTC(2000, 0, 0, 0, 0, 0), 1.0, 1.1));
            prices.Add(new Price(new DateTimeUTC(2000, 0, 0, 0, 0, 1), 2.0, 2.1));
            PriceSet price_set = new PriceSet(ToolsPrice.DefaultSymbolGBPUSD, prices.AsReadOnly());

            MarketModelSimulation market = new MarketModelSimulation(initial_cash, commission_per_unit, price_set);

            market.ProcessOrderCommand(new TradingOrderCommand(TradingOrderType.Short, 1.0, 1.0, 0.0));
            List <int> open_orders = new List <int>(market.OpenOrders.Keys);

            foreach (int open_order in open_orders)
            {
                market.CloseOrder(open_order);
            }
            Assert.AreEqual(998.89, market.Cash);
        }
Exemplo n.º 11
0
        public void DoExperiment()
        {
            PriceSet          price_set  = ToolsPrice.GetPriceSet(ToolsPrice.DefaultSymbolGBPUSD);
            List <IIndicator> indicators = new List <IIndicator>();

            indicators.Add(new IndicatorRunningAverage(4));
            indicators.Add(new IndicatorRunningAverage(6));
            indicators.Add(new IndicatorRunningAverage(8));
            indicators.Add(new IndicatorRunningAverage(10));
            indicators.Add(new IndicatorRunningAverage(12));
            IIndicator feature_indicator = new IndicatorFusion(indicators);
            IIndicator label_indicator   = new IndicatorMagicProfit(60);



            MarketModelSimulation                market   = new MarketModelSimulation(1000, price_set);
            DataSet <double, double>             dataset  = ToolsTradingDataSet.CreateDataSet(market, feature_indicator, label_indicator);
            ITemplateModelLabel <double, double> template = null;//new TemplateModelLibSVMCSVC();
            IModelLabel <double, double>         model    = template.GenerateModel(dataset);

            //TODO change everyting into templates
            List <IIndicator> indicators_2 = new List <IIndicator>();

            indicators_2.Add(new IndicatorRunningAverage(4));
            indicators_2.Add(new IndicatorRunningAverage(6));
            indicators_2.Add(new IndicatorRunningAverage(8));
            indicators_2.Add(new IndicatorRunningAverage(10));
            indicators_2.Add(new IndicatorRunningAverage(12));
            IIndicator feature_indicator_2 = new IndicatorFusion(indicators_2);

            //Build actual incator

            IIndicator    indicator_0 = new IndicatorMagicProfit(60);
            IIndicator    indicator_1 = new IndicatorMachineLearning(feature_indicator_2, model, "Profit_long_ml");
            IList <Color> color_list  = new Color[] { Color.Black };
            IList <int>   index_list  = new int[] { 0 };

            ToolsTradingPlotting.PlotIndicatorResult(ToolsTradingDataSet.GetPath() + "indicator_0.png", price_set, indicator_0, color_list, index_list, false);
            ToolsTradingPlotting.PlotIndicatorResult(ToolsTradingDataSet.GetPath() + "indicator_1.png", price_set, indicator_1, color_list, index_list, false);
        }
Exemplo n.º 12
0
        public void CloseOrderLimit()
        {
            double spread = 0.05;
            Dictionary <int, TradingOrder> OpenOrders0 = new Dictionary <int, TradingOrder>();
            Dictionary <int, TradingOrder> OpenOrders1 = new Dictionary <int, TradingOrder>();
            Dictionary <int, TradingOrder> OpenOrders2 = new Dictionary <int, TradingOrder>();
            Dictionary <int, TradingOrder> OpenOrders3 = new Dictionary <int, TradingOrder>();
            Dictionary <int, TradingOrder> OpenOrders4 = new Dictionary <int, TradingOrder>();
            Dictionary <int, TradingOrder> OpenOrders5 = new Dictionary <int, TradingOrder>();
            PriceCandle candle = new PriceCandle(DateTimeUTC.Now, TimeScale.Second1, 1, 1.2, 0.8, 1, spread);


            TradingOrder order_0 = new TradingOrder(TradingOrderType.Long, 0, DateTimeUTC.Now, 1, 1, 0.01, true, 0.7, 1.1);

            OpenOrders0.Add(order_0.OrderTicket, order_0);
            TradingOrder order_1 = new TradingOrder(TradingOrderType.Long, 0, DateTimeUTC.Now, 1, 1, 0.01, true, 0.9, 1.3);

            OpenOrders1.Add(order_0.OrderTicket, order_1);
            TradingOrder order_2 = new TradingOrder(TradingOrderType.Short, 0, DateTimeUTC.Now, 1, 1, 0.01, true, 1.1, 0.7);

            OpenOrders2.Add(order_0.OrderTicket, order_2);
            TradingOrder order_3 = new TradingOrder(TradingOrderType.Short, 0, DateTimeUTC.Now, 1, 1, 0.01, true, 1.3, 0.9);

            OpenOrders3.Add(order_0.OrderTicket, order_3);
            TradingOrder order_4 = new TradingOrder(TradingOrderType.Long, 0, DateTimeUTC.Now, 1, 1, 0.01, true, 0.7, 1.3);

            OpenOrders4.Add(order_0.OrderTicket, order_4);
            TradingOrder order_5 = new TradingOrder(TradingOrderType.Short, 0, DateTimeUTC.Now, 1, 1, 0.01, true, 1.3, 0.7);

            OpenOrders5.Add(order_0.OrderTicket, order_5);

            Assert.AreEqual(1, MarketModelSimulation.CheckOrderLimits(OpenOrders0, candle).Count);
            Assert.AreEqual(1, MarketModelSimulation.CheckOrderLimits(OpenOrders1, candle).Count);
            Assert.AreEqual(1, MarketModelSimulation.CheckOrderLimits(OpenOrders2, candle).Count);
            Assert.AreEqual(1, MarketModelSimulation.CheckOrderLimits(OpenOrders3, candle).Count);
            Assert.AreEqual(0, MarketModelSimulation.CheckOrderLimits(OpenOrders4, candle).Count);
            Assert.AreEqual(0, MarketModelSimulation.CheckOrderLimits(OpenOrders5, candle).Count);
        }
        public void DoExperiment()
        {
            ToolsPrice.ComposeBinary(ToolsPrice.DefaultSymbolGBPUSD);
            PriceSet price_set = ToolsPrice.GetPriceSet(ToolsPrice.DefaultSymbolGBPUSD).SubSet(new DateTimeUTC(2016, 10, 17), new DateTimeUTC(2016, 10, 20));


            IIndicator            indicator_feature = new IndicatorSuperBollinger();
            IIndicator            indicator_label   = new IndicatorMagicProfit(60);
            MarketModelSimulation market0           = new MarketModelSimulation(10000, price_set);
            MarketModelSimulation market1           = new MarketModelSimulation(10000, price_set);

            double[] time = new double[price_set.Prices.Count];
            for (int price_index = 0; price_index < price_set.Prices.Count; price_index++)
            {
                time[price_index] = price_set.Prices[price_index].Time.Ticks;
            }

            Tuple <double[, ], bool[]> tuple0 = indicator_feature.ComputeAll(market0, price_set.Second1.Count);
            Tuple <double[, ], bool[]> tuple1 = indicator_label.ComputeAll(market1, price_set.Second1.Count);

            List <string[]> list_string = new List <string[]>();

            for (int index_0 = 0; index_0 < tuple0.Item2.Length; index_0++)
            {
                if (tuple0.Item2[index_0] && tuple1.Item2[index_0])
                {
                    double[] array_double = ToolsCollection.Append(tuple0.Item1.Select1DIndex0(index_0), tuple1.Item1.Select1DIndex0(index_0));
                    string[] array_string = new string[array_double.Length];
                    for (int index_1 = 0; index_1 < array_double.Length; index_1++)
                    {
                        array_string[index_1] = array_double[index_1].ToString(CultureInfo.InvariantCulture);
                    }
                    list_string.Add(array_string);
                }
            }

            ToolsIOCSV.WriteCSVFile(ToolsTradingDataSet.GetPath() + "data.csv", ToolsCollection.ConvertToArray2D(list_string));
        }
Exemplo n.º 14
0
 public static DataSet <double, double> CreateDataSet(MarketModelSimulation market, IIndicator indicator_features)
 {
     return(CreateDataSet(market, indicator_features, new IndicatorNull()));
 }