//Model list price public PriceCandle(DateTimeUTC open_time, TimeScale time_scale, IList <PriceCandle> prices) { this.OpenTime = open_time; this.CloseTime = GetCloseTime(open_time, time_scale); this.TimeScale = TimeScale.Second1; this.OpenBid = prices[0].OpenBid; this.CloseBid = prices.Last().CloseBid; this.OpenAsk = prices[0].OpenAsk; this.CloseAsk = prices.Last().CloseAsk; this.HighBid = Math.Max(OpenBid, CloseBid); this.LowBid = Math.Min(OpenBid, CloseBid); this.HighAsk = Math.Max(OpenAsk, CloseAsk); this.LowAsk = Math.Min(OpenAsk, CloseAsk); // skip first and last for (int price_index = 0; price_index < prices.Count; price_index++) { this.HighBid = Math.Max(this.HighBid, prices[price_index].HighBid); this.LowBid = Math.Min(this.LowBid, prices[price_index].LowBid); this.HighAsk = Math.Max(this.HighAsk, prices[price_index].HighAsk); this.LowAsk = Math.Min(this.LowAsk, prices[price_index].LowAsk); } this.Volume = 0; this.RealVolume = 0; }
//TODO make unit tests private static List <Price> CreatePriceListClean(IReadOnlyList <Price> price_list_unclean) { //Build price List <Price> clean_price_list = new List <Price>(); DateTimeUTC time = price_list_unclean[0].Time; double bid = price_list_unclean[0].Bid; double ask = price_list_unclean[0].Ask; int price_index = 0; while (price_index < price_list_unclean.Count) { if (time < price_list_unclean[price_index].Time) { //Create new price clean_price_list.Add(new Price(time, bid, ask)); time = time.AddSeconds(1); } else { //Save and scroll ask = price_list_unclean[price_index].Ask; bid = price_list_unclean[price_index].Bid; price_index++; } } //TODO make last clean_price_list.Add(new Price(time, bid, ask)); return(clean_price_list); }
public MappingProfile() { // Current CreateMap <OpenWeathermapCurrent, Weather>(MemberList.Destination) .ForMember(x => x.DateTime, o => o.MapFrom(y => DateTimeUTC.FromSecondsSinceUnixEpoch(y.dt))) .ForMember(x => x.CloudDescription, o => o.MapFrom(y => y.weather[0].description)) .ForMember(x => x.FeelsLikeTemperature, o => o.MapFrom(y => y.main.feels_like)) .ForMember(x => x.Humidity, o => o.MapFrom(y => y.main.humidity)) .ForMember(x => x.MaximumTemperature, o => o.MapFrom(y => y.main.temp_max)) .ForMember(x => x.MinimumTemperature, o => o.MapFrom(y => y.main.temp_min)) .ForMember(x => x.Pressure, o => o.MapFrom(y => y.main.pressure)) .ForMember(x => x.Temperature, o => o.MapFrom(y => y.main.temp)) .ForMember(x => x.WindDirection, o => o.MapFrom(y => y.wind.deg)) .ForMember(x => x.Windspeed, o => o.MapFrom(y => y.wind.speed)) .ForMember(x => x.Icon, o => o.MapFrom(y => y.weather[0].icon)); // Forecast CreateMap <WeatherList, Weather>(MemberList.Destination) .ForMember(x => x.DateTime, o => o.MapFrom(y => DateTimeUTC.FromSecondsSinceUnixEpoch(y.dt))) .ForMember(x => x.CloudDescription, o => o.MapFrom(y => y.weather[0].description)) .ForMember(x => x.FeelsLikeTemperature, o => o.MapFrom(y => y.main.feels_like)) .ForMember(x => x.Humidity, o => o.MapFrom(y => y.main.humidity)) .ForMember(x => x.MaximumTemperature, o => o.MapFrom(y => y.main.temp_max)) .ForMember(x => x.MinimumTemperature, o => o.MapFrom(y => y.main.temp_min)) .ForMember(x => x.Pressure, o => o.MapFrom(y => y.main.pressure)) .ForMember(x => x.Temperature, o => o.MapFrom(y => y.main.temp)) .ForMember(x => x.WindDirection, o => o.MapFrom(y => y.wind.deg)) .ForMember(x => x.Windspeed, o => o.MapFrom(y => y.wind.speed)) .ForMember(x => x.Icon, o => o.MapFrom(y => y.weather[0].icon)); }
public static List <string> GetTradingCalendarHTMLLines(DateTimeUTC week_start) { if (week_start.DayOfWeek != DayOfWeek.Monday) { throw new Exception("Day is not a monday"); } string url = "http://www.forexfactory.com/calendar.php?week=" + IntToMonth(week_start.Month) + week_start.Day + "." + week_start.Year; Console.WriteLine(url); List <string> lines = new List <string>(); WebRequest web_request = WebRequest.Create(url); ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072; //This enables SSL using (Stream stream = web_request.GetResponse().GetResponseStream()) { StreamReader stream_reader = new StreamReader(stream); string line = stream_reader.ReadLine(); while (line != null) { lines.Add(line); line = stream_reader.ReadLine(); } } return(lines); }
public void UpdateForexFactory() { List <DateTimeUTC> query_date_list = new List <DateTimeUTC>(); if (LastUpdateDateTime < ToolsForexFactory.InitialDate) { LastUpdateDateTime = ToolsForexFactory.InitialDate; } DateTimeUTC current_time = DateTimeUTC.Now; query_date_list.Add(LastUpdateDateTime.GetDayEqualOrBefore(DayOfWeek.Monday)); while (query_date_list[query_date_list.Count - 1] <= current_time) { query_date_list.Add(query_date_list[query_date_list.Count - 1].AddDays(7)); } foreach (DateTimeUTC query_date in query_date_list) { List <TradingCalenderEvent> trading_calendar_event_list = ToolsForexFactory.GetTradingCalendarEvents(query_date); foreach (TradingCalenderEvent trading_calender_event in trading_calendar_event_list) { event_list.Add(trading_calender_event); } } }
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); }
private static void ParceEventRBA(DateTimeUTC week_start_day, string date_string, string event_html, List <ForexFactoryEvent> list) { int time_string_start = event_html.IndexOf("calendar__cell calendar__time time") + 36; int time_string_end = event_html.IndexOf("</td>", time_string_start); string time_string = event_html.Substring(time_string_start, time_string_end - time_string_start); if (time_string.Contains("upnext")) { time_string_start = event_html.IndexOf("span class=", time_string_start) + 20; time_string_end = event_html.IndexOf("</span>", time_string_start); time_string = event_html.Substring(time_string_start, time_string_end - time_string_start); } if (time_string.Equals("")) { time_string = list[list.Count - 1].TimeString; } int symbol_start = event_html.IndexOf("calendar__cell calendar__currency currency") + 44; int symbol_end = event_html.IndexOf("</td>", symbol_start); string symbol = event_html.Substring(symbol_start, symbol_end - symbol_start); if (symbol.Equals("")) { return; } int impact_start = event_html.IndexOf("calendar__impact-icon calendar__impact-icon--screen") + 67; int impact_end = event_html.IndexOf("</span>", impact_start) - 14; string impact = event_html.Substring(impact_start, impact_end - impact_start); int description_start = event_html.IndexOf("calendar__event-title") + 23; int description_end = event_html.IndexOf("</span>", description_start); string description = event_html.Substring(description_start, description_end - description_start); int actual_start = event_html.IndexOf("calendar__cell calendar__actual actual") + 40; int actual_end = event_html.IndexOf("</td>", actual_start); string actual = event_html.Substring(actual_start, actual_end - actual_start); int forecast_start = event_html.IndexOf("calendar__cell calendar__forecast forecast") + 44; int forecast_end = event_html.IndexOf("</td>", forecast_start); string forecast = event_html.Substring(forecast_start, forecast_end - forecast_start); int previous_start = event_html.IndexOf("calendar__cell calendar__previous previous") + 44; int previous_end = event_html.IndexOf("</td>", previous_start); string previous = event_html.Substring(previous_start, previous_end - previous_start); if (previous.Contains("revised")) { previous_start = event_html.IndexOf("revised", previous_start) + 37; previous_end = event_html.IndexOf("</span>", previous_start); previous = event_html.Substring(previous_start, previous_end - previous_start); } list.Add(new ForexFactoryEvent(week_start_day, date_string, time_string, symbol, impact, description, actual, forecast, previous)); }
public TradingCalenderEvent(DateTimeUTC event_time_utc, string symbol, string description, bool is_all_day, Dictionary <string, string> tags) { EventTimeUTC = event_time_utc; Symbol = symbol; Description = description; IsAllDay = is_all_day; Tags = tags; }
public static Price Read(BinaryReader reader) { DateTimeUTC time = reader.ReadDateTimeUTC(); double bid = reader.ReadDouble(); double ask = reader.ReadDouble(); return(new Price(time, bid, ask)); }
public void UpdateData() { //TODO update other? UpdateForexFactory(); LastUpdateDateTime = DateTimeUTC.Now; event_list.Sort(); SaveState(); }
private static IReadOnlyList <PriceCandle> CreateSecond1(DateTimeUTC open_time, DateTimeUTC final_time) { List <PriceCandle> candles = new List <PriceCandle>(); while (open_time < final_time) { candles.Add(new PriceCandle(open_time, 0, 0)); open_time = open_time.AddSeconds(1); } return(candles); }
public PriceSet GetPrices(TradingSymbol symbol, DateTimeUTC lower_inclusive, DateTimeUTC upper_exclusive) { List <PriceSet> month_price_set_list = new List <PriceSet>(); //Move through months DateTimeUTC initial_month = new DateTimeUTC(lower_inclusive.Year, lower_inclusive.Month, 1); int year = 0; int month = 0; PriceSet price_set = GetPrices(symbol, year, month); return(new PriceSet(month_price_set_list));// new PriceSet(List<PriceSet> month_price_set_list) }
private static IReadOnlyList <Price> CreatePrices(DateTimeUTC open_time, DateTimeUTC final_time) { List <Price> price = new List <Price>(); while (open_time <= final_time) { price.Add(new Price(open_time, 0, 0)); open_time = open_time.AddSeconds(1); } return(price); }
private IList <DateTimeUTC> CreateMonthRange(DateTimeUTC open, DateTimeUTC final_data_time) { open = new DateTimeUTC(open.Year, open.Month, 1); List <DateTimeUTC> months = new List <DateTimeUTC>(); while (open < final_data_time) { months.Add(open); open = open.AddMonths(1); } return(months); }
public void TestUTCFromOpenWeathermap() { DateTime result = DateTimeUTC.FromSecondsSinceUnixEpoch(1596358800); Assert.Equal(new DateTime(2020, 8, 2, 9, 0, 0), result); result = DateTimeUTC.FromSecondsSinceUnixEpoch(1596369600); Assert.Equal(new DateTime(2020, 8, 2, 12, 0, 0), result); result = DateTimeUTC.FromSecondsSinceUnixEpoch(1596380400); Assert.Equal(new DateTime(2020, 8, 2, 15, 0, 0), result); }
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)); } }
public DatabaseTradingCalender() { event_list = new List <TradingCalenderEvent>(); if (File.Exists(database_path)) { LoadState(); } else { LastUpdateDateTime = new DateTimeUTC(2000, 1, 1); //Before forex actory } }
public IList <TradingCalenderEvent> GetEvents(DateTimeUTC lower_inclusive, DateTimeUTC upper_exclusive, string symbol, string decription, Dictionary <string, string> required_tags) { List <TradingCalenderEvent> selected_event_list = new List <TradingCalenderEvent>(); foreach (TradingCalenderEvent callender_event in event_list) { if (CheckMatch(callender_event, lower_inclusive, upper_exclusive, symbol, decription, required_tags)) { selected_event_list.Add(callender_event); } } return(selected_event_list); }
public static string GetTradingCalendarHTMLLine(DateTimeUTC week_start) { List <string> lines = GetTradingCalendarHTMLLines(week_start); foreach (string line in lines) { if (line.Contains("calendar__row calendar__row--day-breaker")) { return(line); } } throw new Exception("html parce failure: table not found"); }
public ForexFactoryEvent(DateTimeUTC week_start_day, string date_string, string time_string, string symbol, string impact, string description, string actual, string forecast, string previous) { WeekStartDay = week_start_day; DateString = date_string; TimeString = time_string; Symbol = symbol; Impact = impact; Description = description; Actual = actual; Forecast = forecast; Previous = previous; QueuryTimeStamp = DateTimeUTC.Now; }
private static TradingOrder CloseOrder(TradingOrder trade_order, DateTimeUTC current_date_time, double current_bid, double current_ask) { switch (trade_order.OrderType) { case TradingOrderType.Long: return(trade_order.Close(current_date_time, current_bid)); case TradingOrderType.Short: return(trade_order.Close(current_date_time, current_ask)); default: throw new NotImplementedException(); } }
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]))); } }
public static List <ForexFactoryEvent> GetForexFactoryEvents(DateTimeUTC week_start) { List <string> day_html_list = GetTradingCalendarHTMLDays(week_start); List <ForexFactoryEvent> event_list = new List <ForexFactoryEvent>(); foreach (string day_html in day_html_list) { Tuple <string, List <string> > tuple = GetTradingCalendarEventsHTML(day_html); foreach (string event_html in tuple.Item2) { ParceEventRBA(week_start, tuple.Item1, event_html, event_list); } } return(event_list); }
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)); }
//Model Second1 public PriceCandle(DateTimeUTC date_time, double bid, double ask) { this.OpenTime = date_time; this.CloseTime = GetCloseTime(date_time, TimeScale.Second1); this.TimeScale = TimeScale.Second1; this.OpenBid = bid; this.HighBid = bid; this.LowBid = bid; this.CloseBid = bid; this.OpenAsk = ask; this.HighAsk = ask; this.LowAsk = ask; this.CloseAsk = ask; this.Volume = 0; this.RealVolume = 0; }
private bool CheckMatch(TradingCalenderEvent callender_event, DateTimeUTC lower_inclusive, DateTimeUTC upper_exclusive, string required_symbol, string required_decription, Dictionary <string, string> required_tags) { if ((callender_event.EventTimeUTC < lower_inclusive) || (upper_exclusive <= callender_event.EventTimeUTC)) { return(false); } if (!required_symbol.Equals("") && !required_symbol.Equals(callender_event.Symbol)) { return(false); } if (!required_decription.Equals("") && !(callender_event.Description.Contains(required_decription))) { return(false); } return(true); }
//Model 400 public PriceCandle(DateTimeUTC date_time, TimeScale time_scale, double open_bid, double high_bid, double low_bid, double close_bid, double volume, double spread) { this.OpenTime = date_time; this.CloseTime = GetCloseTime(date_time, time_scale); this.TimeScale = time_scale; this.OpenBid = open_bid; this.HighBid = high_bid; this.LowBid = low_bid; this.CloseBid = close_bid; this.OpenAsk = open_bid + spread; this.HighAsk = high_bid + spread; this.LowAsk = low_bid + spread; this.CloseAsk = close_bid + spread; this.Volume = (long)volume; this.RealVolume = 0; }
private static List <PriceCandle> ReadHST400(BinaryReader reader, long file_size, TimeScale time_scale, double spread) { // bars array(single-byte justification) . . . total 44 bytes long line_count = (file_size - 148) / 44; List <PriceCandle> data = new List <PriceCandle>(); for (int i = 0; i < line_count; i++) { reader.ReadBytes(4); DateTimeUTC open_date_time = ToolsTime.UnixTimeStampToDateTimeUTC(reader.ReadInt32()); double open = reader.ReadDouble(); // open price 8 bytes double low = reader.ReadDouble(); // lowest price 8 bytes double high = reader.ReadDouble(); // highest price 8 bytes double close = reader.ReadDouble(); // close price 8 bytes double volume = reader.ReadDouble(); // tick count 8 bytes data.Add(new PriceCandle(open_date_time, time_scale, open, high, low, close, volume, spread)); } return(data); }
public void DoExperiment() { List <PriceCandle> all_data = ToolsPrice.GetPriceCandles(); // new DateTime(2016, 9, 3) PlotLine2D line_plot_2d = new PlotLine2D(); for (int month_index = 1; month_index < 9; month_index++) { DateTimeUTC nfp_date = null;// ToolsTradingCalendarEvent.GetNFPDatetime(2016, month_index); DateTimeUTC nfp_date_begin = nfp_date.AddMinutes(-100); DateTimeUTC nfp_date_end = nfp_date.AddMinutes(100); List <double> nfp_time = new List <double>(); List <double> nfp_bid = new List <double>(); double candle_time = 0; double subtract = 0; foreach (PriceCandle candle in all_data) { if ((nfp_date_begin <= candle.OpenTime) && (candle.OpenTime < nfp_date_end)) { if (candle_time == 0) { subtract = candle.OpenBid; } nfp_time.Add(candle_time); nfp_time.Add(candle_time); nfp_time.Add(candle_time); nfp_time.Add(candle_time); nfp_bid.Add(candle.OpenBid); nfp_bid.Add(candle.LowBid); nfp_bid.Add(candle.HighBid); nfp_bid.Add(candle.CloseBid); candle_time++; } } line_plot_2d.AddSeries(nfp_time, nfp_bid, Color.Black); } ToolsPlotting.WriteToFile(ToolsTradingDataSet.GetPath() + "nfp.png", line_plot_2d, 800, 800); }
private static List <PriceCandle> ReadHST401(BinaryReader reader, long file_size, TimeScale time_scale) { //then the bars array(single-byte justification) . . . total 60 bytes long line_count = (file_size - 148) / 60; List <PriceCandle> data = new List <PriceCandle>(); for (int i = 0; i < line_count; i++) { DateTimeUTC open_date_time = ToolsTime.UnixTimeStampToDateTimeUTC(reader.ReadInt64()); // bar start time 8 bytes double open = reader.ReadDouble(); // open price 8 bytes double high = reader.ReadDouble(); // highest price 8 bytes double low = reader.ReadDouble(); // lowest price 8 bytes double close = reader.ReadDouble(); // close price 8 bytes long volume = reader.ReadInt64(); // tick count 8 bytes int spread = reader.ReadInt32();; // spread 4 bytes long real_volume = reader.ReadInt64(); // real volume 8 bytes data.Add(new PriceCandle(open_date_time, time_scale, open, high, low, close, volume, spread, real_volume)); } return(data); }