public void SaveState()
        {
            List <string[]> array_list = new List <string[]>();

            array_list.Add(new string[]
            {
                LastUpdateDateTime.ToString(),
                "",
                "",
                "",
                ""
            });

            foreach (TradingCalenderEvent trading_calender_event in event_list)
            {
                array_list.Add(new string[]
                {
                    trading_calender_event.EventTimeUTC.ToString(),
                    trading_calender_event.Symbol,
                    trading_calender_event.Description,
                    trading_calender_event.IsAllDay.ToString(),
                    ToolsString.DictionaryToString(trading_calender_event.Tags)
                });
            }


            ToolsIOCSV.WriteCSVFile(database_path, ToolsCollection.ConvertToArray2D(array_list));
        }
예제 #2
0
        public void DoExperiment()
        {
            DateTimeUTC week_start = new DateTimeUTC(2007, 1, 1);
            List <ForexFactoryEvent> all_event_list = new List <ForexFactoryEvent>();

            while (week_start < DateTimeUTC.Now)
            {
                Console.WriteLine(week_start);
                all_event_list.AddRange(ToolsForexFactory.GetForexFactoryEvents(week_start));
                week_start = week_start.AddDays(7);
                Thread.Sleep(1000);
            }
            string[,] table = new string[all_event_list.Count, 9];
            for (int index = 0; index < all_event_list.Count; index++)
            {
                table[index, 0] = all_event_list[index].WeekStartDay.ToString();
                table[index, 1] = all_event_list[index].DateString;
                table[index, 2] = all_event_list[index].TimeString;
                table[index, 3] = all_event_list[index].Symbol;
                table[index, 4] = all_event_list[index].Impact;
                table[index, 5] = all_event_list[index].Description;
                table[index, 6] = all_event_list[index].Actual;
                table[index, 7] = all_event_list[index].Forecast;
                table[index, 8] = all_event_list[index].Previous;
            }
            ToolsIOCSV.WriteCSVFile(@"D:\GoogleDrive\TestData\Trading\events.csv", table, Delimiter.SemiColon);
        }
 public void DoExperiment()
 {
     string [,] event_table = ToolsIOCSV.ReadCSVFile(@"D:\GoogleDrive\TestData\Trading\events.csv", Delimiter.SemiColon);
     //Load events
     //ToolsTradingCalendarEvent.GetTradingCalendarEvents(new string[] { "EUR", "USD" }, start_date, end_date);
     //Load candles
     ToolsPrice.GetPriceCandles("EUR_USD", TimeScale.Minute1, start_date, end_date, 0.07);
     //
 }
예제 #4
0
 public static void AddFileToPriceList(List <Price> price_list, string file)
 {
     string[,] table = ToolsIOCSV.ReadCSVFile(file);
     for (int index_0 = 0; index_0 < table.GetLength(0); index_0++)
     {
         //TODO duplicate date_times can exist both on the server and on the client
         DateTimeUTC date_time = ToolsTime.UnixTimeStampToDateTimeUTC(int.Parse(table[index_0, 1]));
         double      bid       = double.Parse(table[index_0, 2], CultureInfo.InvariantCulture);
         double      ask       = double.Parse(table[index_0, 3], CultureInfo.InvariantCulture);
         price_list.Add(new Price(date_time, bid, ask));
     }
 }
예제 #5
0
        public void DoExperiment()
        {
            double commission = 7;
            double spread     = 5;
            int    lookAhead  = 60;

            //Load data
            string[,] table = ToolsIOCSV.ReadCSVFile(@"D:\GoogleDrive\TestData\Trading\eurusddata.csv", Delimiter.Comma);

            //Convert data to values
            double[] ask = new double[table.GetLength(0)];
            double[] bid = new double[table.GetLength(0)];
            for (int i = 0; i < table.GetLength(0); i++)
            {
                bid[i] = 100000 * double.Parse(table[i, 2].Replace(".", ","));
                ask[i] = bid[i] + spread;
            }
            double mean1 = ToolsMathStatistics.Mean(ask);
            double mean2 = ToolsMathStatistics.Mean(bid);

            double[] buyCurve       = new double[table.GetLength(0) - lookAhead];
            double[] sellCurve      = new double[table.GetLength(0) - lookAhead];
            string[] buyCurveString = new string[buyCurve.Length];

            for (int i = 0; i < buyCurve.Length; i++)
            {
                double max = double.MinValue;
                double min = double.MaxValue;

                for (int j = 1; j < lookAhead; j++)
                {
                    max = Math.Max(max, bid[i + j]);
                    min = Math.Min(min, ask[i + j]);
                }

                buyCurve[i]       = max - ask[i] - commission;
                buyCurveString[i] = bid[i] + ";" + buyCurve[i];
                sellCurve[i]      = bid[i] - min - commission;
            }

            double mean3 = ToolsMathStatistics.Mean(buyCurve);
            double mean4 = ToolsMathStatistics.Mean(sellCurve);

            File.AppendAllLines(@"D:\GoogleDrive\TestData\Trading\buyCurveOut.csv", buyCurveString);

            /*
             * Load data
             * for each time point find optimal buy order
             * create line of optimal buy orders
             */
        }
예제 #6
0
 static void Old0(string[] args)
 {
     // sample are 30 per second.
     string [,] table = ToolsIOCSV.ReadCSVFile(@"D:\Dropbox\TestData\ML6\data_0.csv", Delimiter.Comma, Delimiter.None);
     string [] row    = table.Select1DIndex0(1);
     double[]  signal = new double[row.Length];
     for (int element_index = 0; element_index < row.Length; element_index++)
     {
         signal[element_index] = double.Parse(row[element_index].Replace('.', ','), NumberStyles.Any);
     }
     double[] speed = RunRoy1(signal, 128);
     ToolsPlotting.PlotLinesXY(@"D:\Dropbox\TestData\ML6\data_0.png", speed);
     Console.ReadLine();
 }
 public void LoadState()
 {
     string [,] table   = ToolsIOCSV.ReadCSVFile(database_path);
     LastUpdateDateTime = DateTimeUTC.Parse(table[0, 0]);
     for (int index_0 = 1; index_0 < table.GetLength(0); index_0++)
     {
         event_list.Add(new TradingCalenderEvent(
                            DateTimeUTC.Parse(table[index_0, 0]),
                            table[index_0, 1],
                            table[index_0, 2],
                            bool.Parse(table[index_0, 3]),
                            ToolsString.StringToDictionary(table[index_0, 4])));
     }
 }
예제 #8
0
        public void DoExperiment()
        {
            double commission = 7;

            //Load data
            string [,] table = ToolsIOCSV.ReadCSVFile(@"D:\GoogleDrive\TestData\Trading\spreads.csv", Delimiter.SemiColon);

            //Convert data to values
            double[] ask = new double [table.GetLength(0)];
            double[] bid = new double [table.GetLength(0)];
            for (int i = 0; i < table.GetLength(0); i++)
            {
                ask[i] = 10000 * double.Parse(table[i, 3].Replace(".", ","));
                bid[i] = 10000 * double.Parse(table[i, 2].Replace(".", ","));
            }
            double mean1 = ToolsMathStatistics.Mean(ask);
            double mean2 = ToolsMathStatistics.Mean(bid);

            double[] buyCurve  = new double[table.GetLength(0) - 3600];
            double[] sellCurve = new double[table.GetLength(0) - 3600];

            for (int i = 0; i < buyCurve.Length; i++)
            {
                double max = double.MinValue;
                double min = double.MaxValue;

                for (int j = 1; j < 3600; j++)
                {
                    max = Math.Max(max, bid[i + j]);
                    min = Math.Min(min, ask[i + j]);
                }

                buyCurve[i]  = max - ask[i] - commission;
                sellCurve[i] = bid[i] - min - commission;
            }

            double mean3 = ToolsMathStatistics.Mean(buyCurve);
            double mean4 = ToolsMathStatistics.Mean(sellCurve);

            /*
             * Load data
             * for each time point find optimal buy order
             * create line of optimal buy orders
             */
        }
예제 #9
0
        private PriceSet ImportSecondData(string file_path)
        {
            string        symbol         = Path.GetFileName(file_path).Substring(0, 6);
            TradingSymbol trading_symbol = new TradingSymbol("TradersWay", "MT4.VAR.DEMO", symbol, "The one we mine on the VM");
            List <Price>  price_list     = new List <Price>();

            string[,] table = ToolsIOCSV.ReadCSVFile(file_path);
            for (int index_0 = 0; index_0 < table.GetLength(0); index_0++)
            {
                //TODO duplicate date_times can exist both on the server and on the client
                DateTimeUTC date_time = ToolsTime.UnixTimeStampToDateTimeUTC(int.Parse(table[index_0, 0]));
                double      bid       = double.Parse(table[index_0, 2], CultureInfo.InvariantCulture);
                double      ask       = double.Parse(table[index_0, 3], CultureInfo.InvariantCulture);
                price_list.Add(new Price(date_time, bid, ask));
            }

            return(new PriceSet(trading_symbol, price_list));
        }
예제 #10
0
        public void DoExperiment()
        {
            IReadOnlyList <PriceCandle> candles = ToolsPrice.GetPriceCandles(ToolsPrice.DefaultSymbolUSDEUR, TimeScale.Second1);
            DictionaryCount <int>       dic     = new DictionaryCount <int>();

            foreach (PriceCandle candle in candles)
            {
                dic.Increment((int)Math.Round((candle.OpenAsk - candle.OpenBid) / TradingConstants.POINT));
            }
            List <int> keys = new List <int>(dic.Keys);

            keys.Sort();

            string[,] spread_table = new string[keys.Count, 2];
            for (int i = 0; i < keys.Count; i++)
            {
                Console.WriteLine(keys[i] + " " + dic[keys[i]]);
                spread_table[i, 0] = keys[i].ToString(CultureInfo.InvariantCulture);
                spread_table[i, 1] = dic[keys[i]].ToString(CultureInfo.InvariantCulture);
            }
            ToolsIOCSV.WriteCSVFile(@"D:\GoogleDrive\TestData\Trading\Spreads.csv", spread_table);
        }
예제 #11
0
        private static Dictionary <string, List <Tuple <GeoLocationDecimalDegree, DateTime> > > ReadData(string file_path)
        {
            string [,] table = ToolsIOCSV.ReadCSVFile(file_path, Delimiter.Comma);
            //for (int i = 0; i < table.GetLength(1); i++)
            //{
            //    System.Console.WriteLine(i);
            //    System.Console.WriteLine(table[0, i]);
            //}
            Dictionary <string, List <Tuple <GeoLocationDecimalDegree, DateTime> > > data = new Dictionary <string, List <Tuple <GeoLocationDecimalDegree, DateTime> > >();

            for (int i = 1; i < table.GetLength(0); i++)
            {
                string   ship_id             = table[i, 14];
                double   latitutde           = Double.Parse(table[i, 5].Replace(".", ","));
                double   longitude           = Double.Parse(table[i, 4].Replace(".", ","));
                string[] date_time_utc_split = table[i, 11].Split(" ".ToCharArray());
                string[] date_utc_split      = date_time_utc_split[0].Split("/".ToCharArray());
                DateTime time      = DateTime.Parse(date_time_utc_split[1]);
                DateTime date      = new DateTime(Int32.Parse(date_utc_split[2]), Int32.Parse(date_utc_split[0]), Int32.Parse(date_utc_split[1]));
                DateTime data_time = new DateTime(date.Year, date.Month, date.Day, time.Hour, time.Minute, time.Second, DateTimeKind.Utc);
                System.Console.WriteLine(ship_id);
                System.Console.WriteLine(latitutde);
                System.Console.WriteLine(longitude);
                System.Console.WriteLine(data_time);
                if (!data.ContainsKey(ship_id))
                {
                    data.Add(ship_id, new List <Tuple <GeoLocationDecimalDegree, DateTime> >());
                }
                data[ship_id].Add(new Tuple <GeoLocationDecimalDegree, DateTime>(new GeoLocationDecimalDegree((AngleDegree)latitutde, (AngleDegree)longitude), data_time));
            }
            List <string> ship_ids = new List <string>(data.Keys);

            foreach (string ship_id in ship_ids)
            {
                data[ship_id] = new List <Tuple <GeoLocationDecimalDegree, DateTime> >(data[ship_id].OrderBy(tuple => tuple.Item2));
            }

            return(data);
        }
        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));
        }
예제 #13
0
        //Layout: open, high, low, close, year, month, day_of_month, day of week, minute_of_day,
        public static void ExportDataSet(IList <PriceCandle> prices, DateTimeUTC first_day, DateTimeUTC last_day, string file_path)
        {
            IList <string[]> lines = new List <string[]>();

            foreach (PriceCandle price_candle in prices)
            {
                if ((first_day.Date <= price_candle.OpenTime.Date) && (price_candle.OpenTime.Date <= last_day.Date))
                {
                    string[] line = new string[9];
                    line[0] = price_candle.OpenBid.ToString(CultureInfo.InvariantCulture);
                    line[1] = price_candle.HighBid.ToString(CultureInfo.InvariantCulture);
                    line[2] = price_candle.LowBid.ToString(CultureInfo.InvariantCulture);
                    line[3] = price_candle.CloseBid.ToString(CultureInfo.InvariantCulture);
                    line[4] = price_candle.OpenTime.Year.ToString(CultureInfo.InvariantCulture);
                    line[5] = price_candle.OpenTime.Month.ToString(CultureInfo.InvariantCulture);
                    line[6] = price_candle.OpenTime.Day.ToString(CultureInfo.InvariantCulture);
                    line[7] = ((int)price_candle.OpenTime.DayOfWeek).ToString();
                    line[8] = ((price_candle.OpenTime.Hour * 60) + price_candle.OpenTime.Minute).ToString();
                    lines.Add(line);
                }
            }
            ToolsIOCSV.WriteCSVFile(file_path, ToolsCollection.ConvertToArray2D(lines));
        }
예제 #14
0
        private void ParseData()
        {
            try
            {
                this.DataSetNominal = new DataSet <int, int>(ToolsIOCSV.ReadCSVLines(lines, this.FieldDelimiter, this.StringDelimiter)).PromoteFeatureToLabel(0);
            }
            catch (Exception)
            {
                this.DataSetNominal = new DataSet <int, int>(ToolsIOCSV.ReadCSVLines(lines, this.FieldDelimiter, this.StringDelimiter)).PromoteFeatureToLabel(0);
            }
            this.FeatureList.Clear();

            IList <VariableDescriptor> descriptors = this.DataSetNominal.DataContext.FeatureDescriptors;

            foreach (VariableDescriptor descriptor in descriptors)
            {
                FeatureList.Add(new Tuple <string, string>(descriptor.Name, descriptor.DataLevel.ToString()));
            }

            if (0 < descriptors.Count)
            {
                this.SelectedFeature = new ModelFeatureNominal(descriptors[0]);
            }
        }
예제 #15
0
 private static IDataSet <int> ReadCSVNominal(string file_path)
 {
     return(new DataSet <int, int>(ToolsIOCSV.ReadCSVFile(file_path, Delimiter.Comma, Delimiter.None)));
 }
예제 #16
0
        private void ExecuteParseAll()
        {
            if (PageFileSelected == null)
            {
                return;
            }
            bitmapSource = new Bitmap(PageFileSelected);
            if (bitmapSource == null)
            {
                return;
            }
            if ((bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) ||
                (bitmapSource.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb))
            {
                bitmapSource = ToolsConvolution.ConvertTo24(bitmapSource);
            }


            //Rotate
            var bitmapRot             = new RotateBilinear(PageLayout.Rotation.Value, true).Apply(bitmapSource);
            var linesH                = ToolsFindLine.FindLinesH(bitmapRot.Width, bitmapRot.Height, PageLayout.LineSize.Value, PageLayout.LineOffset.Value, PageLayout.LineCount.Value);
            var linesV                = ToolsFindLine.FindLinesV(bitmapRot.Width, bitmapRot.Height, PageLayout.ColSize.Value, PageLayout.ColOffset.Value, PageLayout.ColCount.Value);
            List <RenderLine> rl_list = new List <RenderLine>();

            rl_list.AddRange(linesH);
            rl_list.AddRange(linesV);

            //Convert to gray
            Bitmap bitmapTemp = new Grayscale(1, 0, 0).Apply(bitmapRot);

            bitmapTemp = new Erosion().Apply(bitmapTemp);
            bitmapTemp = new Threshold(160).Apply(bitmapTemp);
            bitmapTemp = new Invert().Apply(bitmapTemp);


            //Tuple<List<ConnectedComponent>, double, double>[] componentSectionList = new Tuple<List<ConnectedComponent>, double, double>[1];
            //Bitmap[] BitmapArray = new Bitmap[1];
            Tuple <List <ConnectedComponent>, double, double>[,] componentTable = new Tuple <List <ConnectedComponent>, double, double> [PageLayout.LineCount.Value, PageLayout.ColCount.Value];

            for (int lineIndex = 0; lineIndex < PageLayout.LineCount.Value; lineIndex++)
            {
                for (int colIndex = 0; colIndex < PageLayout.ColCount.Value; colIndex++)
                {
                    //TODO add margin?
                    int    x      = (int)((PageLayout.ColSize.Value * colIndex) + PageLayout.ColOffset.Value); // - PageLayout.Margin.Value;
                    int    y      = (int)((PageLayout.LineSize.Value * lineIndex) + PageLayout.LineOffset.Value) - PageLayout.Margin.Value;
                    int    w      = (int)PageLayout.ColSize.Value;                                             // + (PageLayout.Margin.Value * 2);
                    int    h      = (int)PageLayout.LineSize.Value + (PageLayout.Margin.Value * 2);
                    Bitmap bitmap = new Crop(new Rectangle(x, y, w, h)).Apply(bitmapTemp);
                    List <ConnectedComponent> lineComponentList = new List <ConnectedComponent>();
                    lineComponentList = GetTokens(bitmap);
                    componentTable[lineIndex, colIndex] = new Tuple <List <ConnectedComponent>, double, double>(lineComponentList, x, y);
                }
            }

            //ImageParse = RenderBitmap(bitmapRot, rl_list, componentTable);
            ImageLayout = RenderBitmap(bitmapRot, rl_list, componentTable);
            //Create output
            string[,] string_table = new string[PageLayout.LineCount.Value, PageLayout.ColCount.Value];
            for (int i = 0; i < PageLayout.LineCount.Value; i++)
            {
                for (int j = 0; j < PageLayout.ColCount.Value; j++)
                {
                    string_table[i, j] = TokensToString(componentTable[i, j].Item1, 3, " ");
                }
            }
            string csvFileSelected = LayoutFileSelected.Replace(".png", ".csv");

            ToolsIOCSV.WriteCSVFile(csvFileSelected, string_table, KozzionCore.IO.CSV.Delimiter.SemiColon);
        }
예제 #17
0
        private static void CreateEventCSV()
        {
            // read CSV
            string[,] source = ToolsIOCSV.ReadCSVFile(ToolsTradingDataSet.GetPath() + "events.csv", Delimiter.SemiColon);

            //Allocate new array
            List <string[]> result_list = new List <string[]>();

            string[] exclusion = new string[] { "day", "Day", "Data" };
            //For each other column
            for (int index_0 = 0; index_0 < source.GetLength(0); index_0++)
            {
                if (!ExtensionsString.ContainsAny(source[index_0, 2], exclusion))
                {
                    string[] result = new string[3]; // Date symbol impact


                    //Find dat
                    DateTime week_start = DateTime.Parse(source[index_0, 0]);
                    int      year       = week_start.Year;
                    int      month      = ToolsForexFactory.MonthToInt(source[index_0, 1].Split(' ')[0]);
                    int      day        = int.Parse(source[index_0, 1].Split(' ')[1]);

                    if ((week_start.Month == 12) && (month == 1))
                    {
                        year++;
                    }

                    int hour   = int.Parse(source[index_0, 2].Split(':')[0]);
                    int minute = int.Parse(source[index_0, 2].Split(':')[1].Substring(0, 2));

                    if (source[index_0, 2].Contains("pm"))
                    {
                        hour = hour + 12;
                    }
                    if (hour == 24)
                    {
                        hour = 0;
                        DateTime date_time = new DateTime(year, month, day, hour, minute, 0);
                        date_time = date_time.AddDays(1);
                        result[0] = ToolsTime.DateTimeToUnixTimestampInt32(date_time).ToString();
                    }
                    else
                    {
                        DateTime date_time = new DateTime(year, month, day, hour, minute, 0);
                        result[0] = ToolsTime.DateTimeToUnixTimestampInt32(date_time).ToString();
                    }

                    result[1] = source[index_0, 3];
                    switch (source[index_0, 4])
                    {
                    case "Non-Economic":
                        result[2] = "0";     // Impacs
                        break;

                    case "Low Impact Expected":
                        result[2] = "1";     // Impacs
                        break;

                    case "Medium Impact Expected":
                        result[2] = "2";     // Impacs
                        break;

                    case "High Impact Expected":
                        result[2] = "3";     // Impacs
                        break;

                    default:
                        throw new Exception("Unknown impact");
                    }
                    result_list.Add(result);
                }
            }
            ToolsIOCSV.WriteCSVFile(ToolsTradingDataSet.GetPath() + "events_small.csv", ToolsCollection.ConvertToArray2D(result_list));
        }