Example #1
0
 private void SetupGrid()
 {
     var blank = new TradeSignalUpdate(0, "");
     // PerformerStat
     grid.Columns.Add(new FastColumn(blank.Property(p => p.CategoryName), Localizer.GetString("TitleSignal"))
         {
             ColumnWidth = 160,
             SortOrder = FastColumnSort.Descending,
             IsHyperlinkStyleColumn = true,
             HyperlinkActiveCursor = Cursors.Hand,
             ColorHyperlinkTextActive = Color.Blue,
             HyperlinkFontActive = new Font(Font, FontStyle.Bold)
         });
     grid.Columns.Add(new FastColumn(blank.Property(p => p.Ticker), Localizer.GetString("TitleInstrument"))
         {
             ColumnWidth = 100
         });
     grid.Columns.Add(new FastColumn(blank.Property(p => p.TimeframeFriendlyName), Localizer.GetString("TitleTimeframe"))
         {
             ColumnWidth = 100
         });
     grid.Columns.Add(new FastColumn(blank.Property(p => p.TimeUpdated), Localizer.GetString("TitleUpdateTime"))
         {
             ColumnMinWidth = 80
         });
     grid.Columns.Add(new FastColumn(blank.Property(p => p.ObjectCount), Localizer.GetString("TitleObjectCountShort"))
         {
             ColumnWidth = 90
         });
     grid.ColorAltCellBackground = Color.White;
     grid.UserHitCell += GridUserHitCell;
     grid.MultiSelectEnabled = true;
     grid.CalcSetTableMinWidth();
 }
        private bool LoadTradeSignalXml(TradeSignalUpdate signal)
        {
            // указать в параметрах запроса параметры сигнала
            var reqParams = new Dictionary<string, string>
                                {
                                    { TradeSignalXml.ReqPtrGetForecast, "1" },
                                    { TradeSignalXml.ReqPtrForecastCategory, signal.ServiceId.ToString() },
                                    { TradeSignalXml.ReqPtrForecastTicker, signal.Ticker },
                                    { TradeSignalXml.ReqPtrForecastTimeframe, signal.Timeframe.ToString(TradeSignalXml.TimeframeSeparator) }
                                };
            var queryString = string.Join("&", reqParams.Select(p => string.Format("{0}={1}", p.Key, p.Value)));

            WebRequest serverRequest;
            try
            {
                serverRequest = WebRequest.Create(url + "?" + queryString);
                serverRequest.Timeout = ServerRequestTimeooutMils;
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("LoadTradeSignalXml: error creating request to {0}: {1}", url, ex);
                return false;
            }
            if (!string.IsNullOrEmpty(username))
                serverRequest.Credentials = new NetworkCredential(username, pass);

            WebResponse serverResponse;
            try
            {
                serverResponse = serverRequest.GetResponse();
            }
            catch (Exception ex)
            {
                logNoFlood.LogMessageFormatCheckFlood(LogEntryType.Error, LogServerRequestXmlError, 1000 * 60 * 5,
                    "LoadTradeSignalXml: error getting response from {0}: {1}", url, ex);
                return false;
            }
            if (serverResponse == null)
            {
                logNoFlood.LogMessageFormatCheckFlood(LogEntryType.Error, LogServerResponseXmlNil, 1000 * 60 * 5,
                    "LoadTradeSignalXml: response from {0} is null", url);
                return false;
            }

            try
            {
                // прочитать и сохранить ответ - XML
                using (var s = serverResponse.GetResponseStream())
                {
                    if (s == null) return false;

                    using (var sr = new StreamReader(s, TradeSignalXml.DefaultEncoding))
                    {
                        var signalXml = sr.ReadToEnd();
                        if (string.IsNullOrEmpty(signalXml)) return false;
                        if (signalXml.StartsWith(TradeSignalXml.ResponseErrorPreffix))
                        {
                            Logger.ErrorFormat("LoadTradeSignalXml({0}, {1}, {2}): response error ({3})",
                                signal.ServiceId, signal.Ticker, signal.Timeframe,
                                signalXml.Substring(TradeSignalXml.ResponseErrorPreffix.Length));
                            return false;
                        }
                        if (signalXml == TradeSignalXml.ResponseForecastIsEmpty)
                        {
                            Logger.InfoFormat("LoadTradeSignalXml({0}, {1}, {2}): response is empty",
                                signal.ServiceId, signal.Ticker, signal.Timeframe);
                            return false;
                        }

                        // таки сохранить XML
                        TradeSignalFileStorage.Instance.SaveTradeSignal(signal.ServiceId,
                            signal.Ticker, signal.Timeframe.ToString(), signalXml);
                        return true;
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat("LoadTradeSignalXml error", ex);
                return false;
            }
            finally
            {
                serverResponse.Close();
            }
        }
        /// <summary>
        /// обработать строку вида
        /// 1_EURUSD_0;#240_20.12.2012 11:04:42        
        /// </summary>        
        private static bool ProcessUpdateRequestLine(string line, out TradeSignalUpdate signalUpdate)
        {
            signalUpdate = null;
            var parts = line.Split(new[] { '_' }, StringSplitOptions.RemoveEmptyEntries);
            if (parts.Length != 4) return false;
            // категория ТС
            var catId = parts[0].ToIntSafe();
            if (!catId.HasValue) return false;
            // тикер
            var ticker = parts[1];
            if (!DalSpot.Instance.GetTickerNames().Contains(ticker)) return false;
            // таймфрейм
            var timeframeStr = parts[2];
            var barSets = BarSettings.TryParseString(timeframeStr);
            if (barSets == null) return false;
            // время обновления
            var timeUpdated = parts[3].ToDateTimeUniformSafe();
            if (!timeUpdated.HasValue) return false;

            // проверить актуальность обновления
            var localUpdateInfo = TradeSignalFileStorage.Instance.FindTradeSignal(catId.Value, ticker,
                barSets.ToString());
            if (localUpdateInfo != null && localUpdateInfo.TimeUpdated == timeUpdated.Value)
                return true; // такое обновление уже имело место
            signalUpdate = new TradeSignalUpdate(catId.Value, ticker, barSets, timeUpdated.Value);
            return true;
        }