private List <Rate> DecodeRates(string pair, string packet, string dateTimeFormat = "M/d/yyyy h:mm:ss tt") { List <Rate> Rates = new List <Rate>(); string[] DelimitedData = Asmodat.Abbreviate.String.ToList(packet, "$"); if (DelimitedData == null) { return(Rates); } //Decimals must be backchecked with number of decimals in order to set correct value; int decimals = ForexConfiguration.GetDecimals(pair); foreach (string data in DelimitedData) { if (System.String.IsNullOrEmpty(data)) { continue; } Rate rate = this.ToRateChartData(pair, data, decimals); if (rate == null) { continue; } Rates.Add(rate); } return(Rates); }
/// <summary> /// @"Token\Pair\BID\OFFER\HIGH\LOW\STATUS\NOTATION\DECIMALS\CLOSINGBID", @"\"); /// </summary> /// <param name="data"></param> /// <param name="dateTimeFormat"></param> /// <returns></returns> public Rate ToRateOnConnect(string data, ref TickTime origin) { if (System.String.IsNullOrEmpty(data) || data.Length < 10) { return(null); } //Ignore token, might be corrupted string[] properties = Asmodat.Abbreviate.String.ToList(data, "\\"); if (properties.Length != 10) { return(null); } string status = properties[6]; string notation = properties[7]; if (status != "D" && status != "R") { return(null); } if (notation != "E" && notation != "A") { return(null); } Rate rate = new Rate(); try { rate.Pair = properties[1]; rate.DECIMALS = ForexConfiguration.GetDecimals(rate.Pair); rate.BID = Doubles.Parse(properties[2], rate.DECIMALS); rate.OFFER = Doubles.Parse(properties[3], rate.DECIMALS); rate.HIGH = Doubles.Parse(properties[4], rate.DECIMALS); rate.LOW = Doubles.Parse(properties[5], rate.DECIMALS); rate.STATUS = status; rate.NOTATION = notation; rate.CLOSINGBID = Doubles.Parse(properties[8], rate.DECIMALS); rate.ChartData.TickTime = (origin += 1); //backtest double pchange = RateInfo.ChangePercentage(rate.BID, rate.OFFER, rate.HIGH, rate.LOW); if (pchange < 25) { return(null); } } catch (Exception e) { Exceptions.Add(e); return(null); } return(rate); }
public void Update(string pair, ServiceConfiguration.TimeFrame frame) { List<DateTime> frames = this.GetMissingDates(pair, frame); if (frames.Count <= 1) return; //frames.Reverse(); double dTimePeriod = ServiceConfiguration.ToMinutes(frame); //Decimals must be backchecked with number of decimals in order to set correct value; int decimals = ForexConfiguration.GetDecimals(pair); string data = ""; for (int i = 0; i < frames.Count - 1; i++) { DateTime DateStart = frames[i + 1]; DateTime DateEnd = frames[i]; Thread.Sleep(1); //Wait not to spam the server with requests string packet = this.GetChartData(ForexAuthentication.Token, pair, frame, DateStart, DateEnd, true); //fresh data cames first if (!System.String.IsNullOrEmpty(packet)) data = data + "$" + packet; } List<Rate> Rates; if (!System.String.IsNullOrEmpty(data)) Rates = this.DecodeRates(pair, data); //Data is sorted by threaded dictionary else Rates = new List<Rate>(); foreach (Rate rate in Rates) { if (ForexArchive.Data[pair][frame].ContainsKey(rate.DateTime)) continue; ForexArchive.Data[pair][frame].Add(rate.DateTime, rate); ++ForexArchive.Updates; } }
/// <summary> /// Converts string data into Rate property /// Example rate: "EUR/USD\\1.17684\\1.17701\\D\\1.18485\\1.17619\\5\\A\\1.18385\\EUR/USD\\EUR/USD$" /// Example data: "Pair\BID\OFFER\STATUS\HIGH\LOW\DECIMALS\NOTATION\CLOSINGBID\CONTRACTPAIR\COUNTERPAIR$" /// @"Pair\BID\OFFER\STATUS\HIGH\LOW\DECIMALS\NOTATION\CLOSINGBID\CONTRACTPAIR\COUNTERPAIR\DateTime\Token\OPEN\CLOSE", @"\"); /// </summary> /// <param name="getRateString">Sting formattes as: Pair\BID\OFFER\STATUS\HIGH\LOW\DECIMALS\NOTATION\CLOSINGBID\CONTRACTPAIR\COUNTERPAIR$ without $ char</param> /// <returns>Rate based on string input.</returns> private Rate ToRate(string data, ref TickTime origin) { if (System.String.IsNullOrEmpty(data) || data.Length < 11) { return(null); } data = data.Replace("$", ""); string[] properties = Asmodat.Abbreviate.String.ToList(data, "\\"); if (properties.Length != 11) { return(null); } string status = properties[3]; string notation = properties[7]; if (status != "D" && status != "R") { return(null); } if (notation != "E" && notation != "A") { return(null); } Rate rate = new Rate(); try { rate.Pair = properties[0]; rate.DECIMALS = int.Parse(properties[6]); if (rate.DECIMALS != ForexConfiguration.GetDecimals(rate.Pair)) { return(null); } rate.BID = Doubles.Parse(properties[1], rate.DECIMALS); rate.OFFER = Doubles.Parse(properties[2], rate.DECIMALS); rate.STATUS = status; rate.NOTATION = notation; //rate.HIGH = Doubles.Parse(properties[4], rate.DECIMALS); //rate.LOW = Doubles.Parse(properties[5], rate.DECIMALS); //rate.CLOSINGBID = Doubles.Parse(properties[8], rate.DECIMALS); //rate.CONTRACTPAIR = properties[9]; //rate.COUNTERPAIR = properties[10]; rate.ChartData.TickTime = (origin += 1); //backtest double pchange = RateInfo.ChangePercentage(rate.BID, rate.OFFER); if (pchange < 25) { return(null); } } catch (Exception e) { Exceptions.Add(e); return(null); } return(rate); }
/// <summary> /// @"Token\BID\OFFER\\\\\" /// "R27\\89.514\\89.549\\D\\\\\\02/19/2015 09:25:55\\" /// TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")); /// </summary> /// <param name="data"></param> /// <param name="dateTimeFormat"></param> /// <returns></returns> public Rate ToRateOnChange(string data, ref TickTime origin) { if (System.String.IsNullOrEmpty(data) || data.Length < 7) { return(null); } char cMessageType = data[0]; if (!Char.IsLetter(cMessageType)) { return(null); //This is not RateChange frame } data = data.Substring(1, data.Length - 1); //Remove Message Type Rate string[] properties = Asmodat.Abbreviate.String.ToList(data, "\\"); if (properties.Length != 7) { return(null); } string status = properties[3]; if (status != "D" && status != "R") { return(null); } Rate rate = new Rate(); try { rate.Token = int.Parse(properties[0]); rate.Pair = ForexConfiguration.OrderPair[rate.Token]; rate.DECIMALS = ForexConfiguration.GetDecimals(rate.Pair); rate.BID = Doubles.Parse(properties[1], rate.DECIMALS); rate.OFFER = Doubles.Parse(properties[2], rate.DECIMALS); rate.STATUS = status; rate.ChartData.TickTime = (origin += 1); //backtest double pchange = RateInfo.ChangePercentage(rate.BID, rate.OFFER); if (pchange < 50) { return(null); } } catch (Exception e) { Exceptions.Add(e); return(null); } return(rate); }
/// <summary> /// This method tries to instantly execute passed request /// </summary> /// <param name="DRequest">DealRequest containing information how to communicate with server.</param> public void DealInstantExecute(DealRequest DRequest) { if (!IsReady) { return; } #region Liquidate All if (DRequest.LiquidateAll) { BlotterOfDealResponse blotter = CEDTS_TradingService.LiquidateAll(ForexAuthentication.Token); if (ServiceTrading.BlotterSuccess(blotter)) { DataDealRequest[DRequest.CreationTime].Executed = true; this.Save(DRequest); } AccountLogs.IsValid = false; return; } #endregion string product = DRequest.Product; if (!ForexRates.Data.ContainsKey(product)) { return; } #region Close Position if (DRequest.ClosePosition) { DealResponse DResponse = CEDTS_TradingService.ClosePosition(ForexAuthentication.Token, product); if (DResponse.success) { DataDealRequest[DRequest.CreationTime].Executed = true; this.Save(DRequest); } AccountLogs.IsValid = false; return; } #endregion int iDecimals = ForexConfiguration.GetDecimals(product);// ForexRates.Data[product].DECIMALS; string sRate = DRequest.Buy ? Doubles.ToString(DRequest.ASK, null, iDecimals, 0, double.MaxValue, '.') : Doubles.ToString(DRequest.BID, null, iDecimals, 0, double.MaxValue, '.'); string amount = DRequest.Amount.ToString(); #region Close if (DRequest.Close) { //double profit = this.GetLiveProfit(DRequest.ConfirmationNumber); DealResponse DResponse = CEDTS_TradingService.InstantExecution( ForexAuthentication.Token, product, DRequest.BuySell, amount, sRate, (int)DRequest.Tolerance); if (DResponse.success) { //Account.ClosedBalance += profit; DataDealRequest[DRequest.CreationTime].Executed = true; this.Save(DRequest); } AccountLogs.IsValid = false; return; } #endregion #region Buy Sell if (DRequest.Buy || DRequest.Sell) { DealResponse DResponse = CEDTS_TradingService.InstantExecution( ForexAuthentication.Token, product, DRequest.BuySell, amount, sRate, (int)DRequest.Tolerance); if (DResponse.success) { DataDealRequest[DRequest.CreationTime].Executed = true; this.Save(DRequest); } AccountLogs.IsValid = false; return; } #endregion throw new Exception("DealInstantExecute Undefined request !"); }