public int FindIndexOfDataRequest(string symbol, StBarInterval interval, int count)
        {
            //int index = DataRequests.FindIndex(req => (req.Symbol == symbol) && (req.Interval == interval) && (req.TotalCount == Math.Abs(count)));
            int index = DataRequests.FindIndex(req => req.Symbol == symbol);

            return(index);
        }
Beispiel #2
0
        public static int BarLenCast(StBarInterval interval)
        {
            switch (interval)
            {
            case StBarInterval.StBarInterval_1Min:
                return(1);

            case StBarInterval.StBarInterval_5Min:
                return(5);

            case StBarInterval.StBarInterval_10Min:
                return(10);

            case StBarInterval.StBarInterval_15Min:
                return(15);

            case StBarInterval.StBarInterval_30Min:
                return(30);

            case StBarInterval.StBarInterval_60Min:
                return(60);

            default:
                throw new SmartException(ExceptionImportanceLevel.MEDIUM, "BarLenCast", "Server", "StBarInterval not defined!");
            }
        }
Beispiel #3
0
        public List <Candle> GetSmartComCandleHistory(string security, TimeSpan timeSpan, int count)
        {
            if (timeSpan.TotalMinutes > 60 ||
                timeSpan.TotalMinutes < 1)
            {
                return(null);
            }

            try
            {
                StBarInterval tf = StBarInterval.StBarInterval_Quarter;

                if (Convert.ToInt32(timeSpan.TotalMinutes) == 1)
                {
                    tf = StBarInterval.StBarInterval_1Min;
                }
                else if (Convert.ToInt32(timeSpan.TotalMinutes) == 5)
                {
                    tf = StBarInterval.StBarInterval_5Min;
                }
                else if (Convert.ToInt32(timeSpan.TotalMinutes) == 10)
                {
                    tf = StBarInterval.StBarInterval_10Min;
                }
                else if (Convert.ToInt32(timeSpan.TotalMinutes) == 15)
                {
                    tf = StBarInterval.StBarInterval_15Min;
                }
                else if (Convert.ToInt32(timeSpan.TotalMinutes) == 30)
                {
                    tf = StBarInterval.StBarInterval_30Min;
                }
                else if (Convert.ToInt32(timeSpan.TotalMinutes) == 60)
                {
                    tf = StBarInterval.StBarInterval_60Min;
                }

                if (tf == StBarInterval.StBarInterval_Quarter)
                {
                    return(null);
                }

                _candles = null;

                while (_candles == null)
                {
                    lock (_smartComServerLocker)
                    {
                        SmartServer.GetBars(security, tf, DateTime.Now.AddHours(6), count);
                    }
                }

                return(_candles);
            }
            catch
            {
                return(null);
            }
        }
 public DataRequest(string symbol, StBarInterval interval, System.DateTime since, int count)
 {
     TotalCount = Math.Abs(count);
     Interval   = interval;
     SinceTime  = since;
     Symbol     = symbol;
     Bars       = new List <Bar>();
 }
 public void InitDataRequest(string symbol, StBarInterval interval, System.DateTime since, int count)
 {
     if (DataRequestExists(symbol, interval, since, count))
     {
         throw new SmartException(ExceptionImportanceLevel.HIGH, "InitRequest", "DataCollector", "Already exists!");
     }
     DataRequests.Add(new DataRequest(symbol, interval, since, count));
 }
Beispiel #6
0
 void SmartComServer_AddBar(int row, int nrows, string symbol, StBarInterval interval, System.DateTime datetime,
                            double open, double high, double low, double close, double volume, double open_int)
 {
     //string message = row + ":" + nrows + ":" + symbol+":"+interval+":"+datetime+":"+open+":"+high+":"+low+":"+close+":"+volume+":"+open_int;
     //Form.WriteToStatus(message);
     Collector.AddBar(row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int);
     //WriteToLog(message);
 }
 private void Server_AddBar(int row, int nrows, string symbol, StBarInterval interval, DateTime datetime, double open, double high, double low, double close, double volume, double open_int)
 {
     data.Add(new Bar(datetime, open, high, low, close, (long)volume));
     if (row >= nrows - 1)
     {
         dataSemaphore.Release();
     }
 }
Beispiel #8
0
        private void stServer_AddBar(int row, int nrows, string symbol, StBarInterval interval, DateTime datetime, double open, double high, double low, double close, double volume, double open_int)
        {
            Bar item = new Bar(symbol, BarIntervalFactory.Make(interval), datetime, open, high, low, close, volume);

            this.logger.Log(String.Format("{0:dd/MM/yyyy H:mm:ss.fff}, {1}, получен Bar {2}",
                                          BrokerDateTime.Make(DateTime.Now),
                                          this.GetType().Name,
                                          item.ToString()));

            this.tradingData.Get <ObservableCollection <Bar> >().Add(item);
        }
Beispiel #9
0
 public void BarsCollectedHandler(string symbol, StBarInterval interval, List <Bar> bars)
 {
     InformUser(symbol + " - Bars Collected");
     bars.RemoveAt(bars.Count - 1);
     foreach (Bar bar in bars)
     {
         dbWriter.InsertOrderBook(bar.StartTime, symbol, interval, bar.Open, bar.High, bar.Low, bar.Close, (int)bar.Volume);
         Console.Write(symbol + " - " + bar.ToString());
     }
     Console.WriteLine("");
 }
Beispiel #10
0
        public static int Make(StBarInterval interval)
        {
            switch (interval)
            {
            case StBarInterval.StBarInterval_1Min:
                return(60);

            case StBarInterval.StBarInterval_5Min:
                return(300);

            case StBarInterval.StBarInterval_10Min:
                return(600);

            case StBarInterval.StBarInterval_15Min:
                return(900);

            case StBarInterval.StBarInterval_30Min:
                return(1800);

            case StBarInterval.StBarInterval_60Min:
                return(3600);

            case StBarInterval.StBarInterval_2Hour:
                return(7200);

            case StBarInterval.StBarInterval_4Hour:
                return(14400);

            case StBarInterval.StBarInterval_Day:
                return(86400);

            case StBarInterval.StBarInterval_Week:
                return(604800);

            case StBarInterval.StBarInterval_Month:
                return(2592000);

            case StBarInterval.StBarInterval_Quarter:
                return(7776000);

            case StBarInterval.StBarInterval_Year:
                return(31536000);

            case StBarInterval.StBarInterval_Tick:
                return(0);

            default:
                return(0);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Метод установки даты отсчета
        /// </summary>
        /// <param name="interval"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public static DateTime SetDateTime(StBarInterval interval, int n)
        {
            DateTime setdate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, 0);

            switch (interval)
            {
            case StBarInterval.StBarInterval_1Min:
                setdate = setdate.AddMinutes(-n);
                break;

            case StBarInterval.StBarInterval_5Min:
                setdate = setdate.AddMinutes(-n * 5);
                break;

            case StBarInterval.StBarInterval_10Min:
                setdate = setdate.AddMinutes(-n * 10);
                break;

            case StBarInterval.StBarInterval_15Min:
                setdate = setdate.AddMinutes(-n * 15);
                break;

            case StBarInterval.StBarInterval_30Min:
                setdate = setdate.AddMinutes(-n * 30);
                break;

            case StBarInterval.StBarInterval_60Min:
                setdate = setdate.AddMinutes(-n * 60);
                break;

            case StBarInterval.StBarInterval_2Hour:
                setdate = setdate.AddHours(-n * 2);
                break;

            case StBarInterval.StBarInterval_4Hour:
                setdate = setdate.AddHours(-n * 4);
                break;

            case StBarInterval.StBarInterval_Day:
                setdate = setdate.AddDays(-n);
                break;

            case StBarInterval.StBarInterval_Week:
                setdate = setdate.AddDays(-n * 7);
                break;
            }
            return(setdate);
        }
Beispiel #12
0
        /// <summary>
        /// incoming from system candles
        /// входящие из системы свечи
        /// </summary>
        private void SmartServerOnAddBar(int row, int nrows, string symbol, StBarInterval interval, DateTime datetime,
                                         double open, double high, double low, double close, double volume, double openInt)
        {
            Candle candle = new Candle();

            candle.Volume = Convert.ToInt32(volume);
            candle.Open   = Convert.ToDecimal(open);
            candle.High   = Convert.ToDecimal(high);
            candle.Low    = Convert.ToDecimal(low);
            candle.Close  = Convert.ToDecimal(close);

            if (interval == StBarInterval.StBarInterval_1Min)
            {
                candle.TimeStart = datetime.AddMinutes(-1.0);
            }
            else if (interval == StBarInterval.StBarInterval_5Min)
            {
                candle.TimeStart = datetime.AddMinutes(-5.0);
            }
            else if (interval == StBarInterval.StBarInterval_10Min)
            {
                candle.TimeStart = datetime.AddMinutes(-10.0);
            }
            else if (interval == StBarInterval.StBarInterval_15Min)
            {
                candle.TimeStart = datetime.AddMinutes(-15.0);
            }
            else if (interval == StBarInterval.StBarInterval_30Min)
            {
                candle.TimeStart = datetime.AddMinutes(-30.0);
            }
            else if (interval == StBarInterval.StBarInterval_60Min)
            {
                candle.TimeStart = datetime.AddMinutes(-60.0);
            }
            else
            {
                candle.TimeStart = datetime;
            }

            if (_candles == null || row == 0)
            {
                _candles = new List <Candle>();
            }

            _candles.Add(candle);
        }
Beispiel #13
0
        public void GetBars(string symbol, StBarInterval interval, System.DateTime since, int count)
        {
            Collector.InitDataRequest(symbol, interval, since, count);
            try
            {
                SmartComServer.GetBars(symbol, interval, since, count);
            } catch (Exception e)
            {
                DisconnectStockServer();
                ConnectStockServer();
                System.Threading.Thread.Sleep(7000);
                EmailSender.SendEmail("Server Disconnected", "In GetBars: Server was disconnected. Tryied to reconect.\r\n" + e.Message);
                SmartComServer.GetBars(symbol, interval, since, count);
            }

            //System.Threading.Thread.Sleep(500);
        }
Beispiel #14
0
 public void GetBars(string symbol, StBarInterval interval, int count)
 {
     try
     {
         if (count <= 0)
         {
             throw new ListenerException(ExceptionImportanceLevel.MEDIUM, "GetBars", "Listener", "No other choice for count: " + count);
         }
         DateTime since = DateTime.Now;
         Collector.InitDataRequest(symbol, interval, since, count);
         InformUser(symbol + " - Geting Bars ...");
         SmartComServer.GetBars(symbol, interval, since, count);
     } catch (SmartException e)
     {
         //dbWriter.InsertGeneral("Failed", "Listener");
         dbWriter.InsertSts(Stocks.ServerTime.GetRealTime(), "Listener", "Failed");
         Environment.Exit(0);
     }
 }
        private void handleMessage(string msg, NetMQSocket sock)
        {
            JObject       req          = JObject.Parse(msg);
            string        ticker       = (string)req["ticker"];
            string        fromRaw      = (string)req["from"];
            string        toRaw        = (string)req["to"];
            string        timeframeRaw = (string)req["timeframe"];
            DateTime      firstTime    = parseDate(fromRaw);
            DateTime      lastTime     = parseDate(toRaw);
            StBarInterval timeframe    = parseSmartComTimeFrame(timeframeRaw);
            int           countBars    = calculateCountBars(firstTime, lastTime, timeframe);

            data.Clear();

            server.GetBars(ticker, timeframe, lastTime, countBars);

            dataSemaphore.Wait();

            sendResponse(data, sock);
        }
        public void AddBar(int row, int nrows, string symbol, StBarInterval interval, System.DateTime datetime,
                           double open, double high, double low, double close, double volume, double open_int)
        {
            int index = FindIndexOfDataRequest(symbol, interval, nrows);

            if (index == -1)
            {
                string message = "Can't find: symbol = " + symbol + ", interval = " + interval + ", nrows = " + nrows;
                throw new SmartException(ExceptionImportanceLevel.LOW, "AddBar", "DataCollector", message);
            }
            Bar bar = new Bar(open, close, low, high, volume, datetime);

            DataRequests[index].AddBar(bar);
            if (Math.Abs(row) == DataRequests[index].TotalCount - 1)
            {
                DataRequests[index].UserSort();
                //Server server = Server.GetInstance();
                //server.NotifyBarsCollected(DataRequests[index].Bars, symbol);
                BarsCollected(DataRequests[index].Symbol, DataRequests[index].Interval, DataRequests[index].Bars);
                DataRequests.RemoveAt(index);
            }
        }
        private int secondsInInterval(StBarInterval interval)
        {
            switch (interval)
            {
            case StBarInterval.StBarInterval_1Min:
                return(60);

            case StBarInterval.StBarInterval_5Min:
                return(60 * 5);

            case StBarInterval.StBarInterval_10Min:
                return(60 * 10);

            case StBarInterval.StBarInterval_15Min:
                return(60 * 15);

            case StBarInterval.StBarInterval_30Min:
                return(60 * 30);

            case StBarInterval.StBarInterval_60Min:
                return(60 * 60);

            case StBarInterval.StBarInterval_2Hour:
                return(60 * 60 * 2);

            case StBarInterval.StBarInterval_4Hour:
                return(60 * 60 * 4);

            case StBarInterval.StBarInterval_Day:
                return(60 * 60 * 24);

            case StBarInterval.StBarInterval_Month:
                return(60 * 60 * 24 * 30);

            default:
                throw new ArgumentException("Not supported: " + interval.ToString());
            }
        }
Beispiel #18
0
 /// <summary>
 /// Метод добавления бара в список баров
 /// </summary>
 /// <param name="row"></param>
 /// <param name="nrows"></param>
 /// <param name="symbol"></param>
 /// <param name="interval"></param>
 /// <param name="date"></param>
 /// <param name="open"></param>
 /// <param name="high"></param>
 /// <param name="low"></param>
 /// <param name="close"></param>
 /// <param name="volume"></param>
 /// <param name="open_int"></param>
 private void AddBars(int row, int nrows, string symbol, StBarInterval interval, DateTime date, double open, double high, double low, double close, double volume, double open_int)
 {
     BarsList.Add(new Collections.Bar(date, open, high, low, close));
     //
     //ниже представлена инстуркция, регулирующая масштаб графика
     //
     if (InvokeRequired)
     {
         Invoke(new MethodInvoker(delegate
         {
             chart1.Series[0].Points.AddXY(date, high, low, open, close);
             if (BarsList.Count > nBars)
             {
                 chart1.ChartAreas[0].AxisY.Minimum = chart1.Series[0].Points.Where(p => p.YValues[1] > 0).Min(p => p.YValues[1]) - (Math.Abs(chart1.Series[0].Points.Where(p => p.YValues[0] > 0).Min(p => p.YValues[0]) - chart1.Series[0].Points.Where(p => p.YValues[1] > 0).Min(p => p.YValues[1])));
             }
             if (BarsList.Count > sma)
             {
                 chart1.Series[2].Points.AddXY(BarsList.Last().Date, ((BarsList.GetRange(BarsList.Count() - sma, sma).Select(p => p.Close).Sum()) / sma));
             }
         }));
     }
     Strategy1(BarsList, TradesList);
 }
 private void OnAddBar(int row, int rowsCount, string symbol, StBarInterval interval, DateTime datetime,
                       double open, double high, double low, double close, double volume, double openInt)
 {
     ProduceMessage(
         TopicName.AddBar,
         JsonSerializer.Serialize(
             new AddBar
     {
         Row        = row,
         RowsCount  = rowsCount,
         Symbol     = symbol,
         Interval   = interval,
         DateTime   = datetime,
         OpenPrice  = open,
         HighPrice  = high,
         LowPrice   = low,
         ClosePrice = close,
         Volume     = volume,
         Opened     = openInt
     }
             )
         );
 }
Beispiel #20
0
        /// <summary>
        /// Метод инициалезирует поле ComboBox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox1_Change(object sender, EventArgs e)
        {
            switch (comboBox1.Text)
            {
            case "1 мин.":
            {
                interval = StBarInterval.StBarInterval_1Min;
                break;
            }

            case "5 мин.":
            {
                interval = StBarInterval.StBarInterval_5Min;
                break;
            }

            case "10 мин.":
            {
                interval = StBarInterval.StBarInterval_10Min;
                break;
            }

            case "15 мин.":
            {
                interval = StBarInterval.StBarInterval_15Min;
                break;
            }

            case "30 мин.":
            {
                interval = StBarInterval.StBarInterval_30Min;
                break;
            }

            case "60 мин.":
            {
                interval = StBarInterval.StBarInterval_60Min;
                break;
            }

            case "2 часа":
            {
                interval = StBarInterval.StBarInterval_2Hour;
                break;
            }

            case "4 часа":
            {
                interval = StBarInterval.StBarInterval_4Hour;
                break;
            }

            case "1 день":
            {
                interval = StBarInterval.StBarInterval_Day;
                break;
            }

            case "1 неделя":
            {
                interval = StBarInterval.StBarInterval_Week;
                break;
            }
            }
        }
 private int calculateCountBars(DateTime start, DateTime end, StBarInterval timeframe)
 {
     return((int)end.Subtract(start).TotalSeconds / secondsInInterval(timeframe));
 }
Beispiel #22
0
        private void OnAddBar(int row, int nrows, string symbol, StBarInterval smartInterval, DateTime datetime, double open, double high, double low, double close, double volume, double openInt)
        {
            SmartBarInterval interval;

            switch (smartInterval)
            {
            case StBarInterval.StBarInterval_Tick:
                interval = SmartBarInterval.Tick;
                break;

            case StBarInterval.StBarInterval_1Min:
                interval = SmartBarInterval.Min1;
                break;

            case StBarInterval.StBarInterval_5Min:
                interval = SmartBarInterval.Min5;
                break;

            case StBarInterval.StBarInterval_10Min:
                interval = SmartBarInterval.Min10;
                break;

            case StBarInterval.StBarInterval_15Min:
                interval = SmartBarInterval.Min15;
                break;

            case StBarInterval.StBarInterval_30Min:
                interval = SmartBarInterval.Min30;
                break;

            case StBarInterval.StBarInterval_60Min:
                interval = SmartBarInterval.Min60;
                break;

            case StBarInterval.StBarInterval_2Hour:
                interval = SmartBarInterval.Hour2;
                break;

            case StBarInterval.StBarInterval_4Hour:
                interval = SmartBarInterval.Hour4;
                break;

            case StBarInterval.StBarInterval_Day:
                interval = SmartBarInterval.Day;
                break;

            case StBarInterval.StBarInterval_Week:
                interval = SmartBarInterval.Week;
                break;

            case StBarInterval.StBarInterval_Month:
                interval = SmartBarInterval.Month;
                break;

            case StBarInterval.StBarInterval_Quarter:
                interval = SmartBarInterval.Quarter;
                break;

            case StBarInterval.StBarInterval_Year:
                interval = SmartBarInterval.Year;
                break;

            default:
                throw new ArgumentOutOfRangeException("smartInterval");
            }

            OnAddBar(row, nrows, symbol, interval, datetime, open, high, low, close, volume, openInt);
        }
Beispiel #23
0
 public void GetBars(string symbol, StBarInterval interval, DateTime since, int count)
 {
     this.barsReceived = count;
 }
Beispiel #24
0
 public void EmulateBarArrival(string symbol, StBarInterval interval, DateTime datetime, double open, double high, double low, double close, double volume, double open_int)
 {
     this.AddBar(0, 1, symbol, interval, datetime, open, high, low, close, volume, open_int);
 }
Beispiel #25
0
 void SmartComServer_AddBar(int row, int nrows, string symbol, StBarInterval interval, System.DateTime datetime,
                            double open, double high, double low, double close, double volume, double open_int)
 {
     Collector.AddBar(row, nrows, symbol, interval, datetime, open, high, low, close, volume, open_int);
 }
 public bool DataRequestExists(string symbol, StBarInterval interval, System.DateTime since, int count)
 {
     return(FindIndexOfDataRequest(symbol, interval, count) != -1);
 }