Ejemplo n.º 1
0
        public static IDictionary<string, IList<Candle>> BuildCandles(IDictionary<string, IDictionary<DateTime, decimal>> pHistoryData, int intervalMinutes)
        {
            Dictionary<string, IList<Candle>> _candles = new Dictionary<string, IList<Candle>>();

            foreach (string rateName in pHistoryData.Keys)
            {
                IDictionary<DateTime, decimal> historyData = pHistoryData[rateName];
                List<Candle> candles = new List<Candle>();
                _candles.Add(rateName, candles);
                if (historyData.Count() == 0)
                    continue;
                DateTime minDate = historyData.Keys.Min();
                DateTime lastDate = historyData.Keys.Max();
                int firstIntervalStartMinute = ((minDate.Minute / 5) + 1) * 5;
                int minutesDiff = firstIntervalStartMinute - minDate.Minute;
                minDate = minDate.AddMinutes(minutesDiff);
                DateTime firstIntervalStartDate = new DateTime(minDate.Year, minDate.Month, minDate.Day, minDate.Hour, minDate.Minute, 0);
                DateTime currentTime = firstIntervalStartDate;

                while (currentTime < lastDate)
                {
                    DateTime nextTime = currentTime.AddMinutes(intervalMinutes);
                    Candle candle = new Candle();
                    candle.OpenTime = currentTime;

                    DateTime? openTime = null;
                    if (historyData.Keys.Where(item => item >= currentTime && item < nextTime).Count() > 0)
                        openTime = historyData.Keys.Where(item => item >= currentTime && item < nextTime).Min();
                    if (!openTime.HasValue)
                    {
                        currentTime = currentTime.AddMinutes(intervalMinutes);
                        continue;
                    }

                    DateTime? closeTime = null;
                    if (historyData.Keys.Where(item => item >= currentTime && item < nextTime).Count() > 0)
                        closeTime = historyData.Keys.Where(item => item >= currentTime && item < nextTime).Max();
                    if (!closeTime.HasValue)
                    {
                        currentTime = currentTime.AddMinutes(intervalMinutes);
                        continue;
                    }
                    candle.RateName = rateName;
                    candle.OpenPrice = historyData[openTime.Value];
                    candle.ClosePrice = historyData[closeTime.Value];
                    candle.HighPrice = historyData.Where(item => item.Key >= currentTime && item.Key < nextTime).Max(item => item.Value);
                    candle.LowPrice = historyData.Where(item => item.Key >= currentTime && item.Key < nextTime).Min(item => item.Value);
                    candles.Add(candle);
                    currentTime = currentTime.AddMinutes(intervalMinutes);
                }
                Candle lastCandle = candles.OrderBy(item => item.OpenTime).LastOrDefault();
                if (lastCandle != null)
                {

                    foreach (KeyValuePair<DateTime, decimal> pair in historyData.Where(item => item.Key >= lastCandle.OpenTime && item.Key < lastCandle.OpenTime.AddMinutes(intervalMinutes)))
                        lastCandle.AddNewData(pair.Key, pair.Value);
                }
            }
            return _candles;
        }
Ejemplo n.º 2
0
        private void DisplayCandle(Candle candle)
        {
            if (candle == null)
                return;
            if (GetSelectedRates()[0] != candle.RateName)
                return;
            DataPoint point = new DataPoint(candle.OpenTime.ToOADate(), new double[] { (double)candle.HighPrice, (double)candle.LowPrice, (double)candle.OpenPrice, (double)candle.ClosePrice });

            point.Color = Color.Black;
            DataPoint existingPoint=chart1.Series[0].Points.Where(item => item.XValue == candle.OpenTime.ToOADate()).FirstOrDefault();
            if (existingPoint != null)
            {
                chart1.Series[0].Points.Remove(existingPoint);
            }

            chart1.Series[0].Points.Add(point);
            chart1.ChartAreas[0].AxisX.Maximum = candle.OpenTime.AddMinutes(10).ToOADate();
            chart1.ChartAreas[0].AxisX.Minimum = candle.OpenTime.AddHours(-3).ToOADate();
            chart1.ChartAreas[0].AxisY2.Maximum = (double)candle.HighPrice + 0.0050; //by the values found on the loop, not good i want it to be automated when u scroll
            chart1.ChartAreas[0].AxisY2.Minimum = (double)candle.LowPrice - 0.0050;
        }
Ejemplo n.º 3
0
 //send events
 private void SendNewRatesEvent(RateRecord rec, Candle candle)
 {
     if (NewRatesEvent != null)
         _asyncOperation.Post(delegate {NewRatesEvent(rec,candle); }, null);
 }
Ejemplo n.º 4
0
        private void OnNewRatesEvent(RateRecord rec, Candle candle)
        {
            SendNewRatesEvent(rec,candle);

            _strategyController.ProcessTick(rec);
            _orderController.CloseOrdersByLimits(rec);
        }
Ejemplo n.º 5
0
        public static void CloseCandle(IDictionary<string, IList<Candle>> candles, RateRecord rec, int candlesIintervalMinutes)
        {
            if (!candles.ContainsKey(rec.Name))
                candles.Add(rec.Name, new List<Candle>());

            DateTime openCandleDate = new DateTime(rec.UpdateTime.Year, rec.UpdateTime.Month, rec.UpdateTime.Day, rec.UpdateTime.Hour, (rec.UpdateTime.Minute / candlesIintervalMinutes) * candlesIintervalMinutes, 0);
            DateTime closeCandleDate = new DateTime(rec.UpdateTime.Year, rec.UpdateTime.Month, rec.UpdateTime.Day, rec.UpdateTime.Hour, (rec.UpdateTime.Minute / candlesIintervalMinutes) * candlesIintervalMinutes, 0).AddMinutes(candlesIintervalMinutes);

            Candle currentCandle=candles[rec.Name].Where(item=>item.OpenTime==openCandleDate).FirstOrDefault();

            if (currentCandle==null)
            {
                ClosePreviousCandles(candles[rec.Name].Where(item=>item.CandleData!=null).ToList());
                currentCandle = new Candle();
                currentCandle.RateName = rec.Name;
                currentCandle.OpenTime = openCandleDate;
                candles[rec.Name].Add(currentCandle);
            }

            currentCandle.AddNewData(rec.UpdateTime, rec.Value);
        }
Ejemplo n.º 6
0
 private void SendNewRatesEvent(RateRecord rec,Candle candle)
 {
     if (NewRatesEvent != null)
         NewRatesEvent(rec,candle);
 }
Ejemplo n.º 7
0
 private void OnNewRates(RateRecord rec,Candle candle)
 {
     try
     {
         if (rec != null)
         {
             if (_currentRates.Keys.Contains(rec.Name))
                 _currentRates[rec.Name] = rec.Value;
             else
                 _currentRates.Add(rec.Name, rec.Value);
             tbCurrentRates.Text = "";
             foreach (string key in _currentRates.Keys)
             {
                 tbCurrentRates.Text += string.Format("{0} - {3:dd-MM-yyyy HH-mm-ss} - {1}{2}", key, _currentRates[key], Environment.NewLine, rec.UpdateTime);
             }
         }
         DisplayCandle(candle);
     }
     catch (Exception ex)
     {
         OnMessage(ex.Message);
     }
 }
Ejemplo n.º 8
0
 protected Candle GetMinimumForInterval(string rateName, int interval,Candle to)
 {
     IList<Candle> candles = _candles[rateName].Where(item => item.OpenTime < to.OpenTime).OrderByDescending(item => item.OpenTime).Take(interval).ToList();
     if (candles.Count == 0)
         return null;
     decimal minPrice = candles.Min(item => item.LowPrice);
     return candles.Where(item => item.LowPrice == minPrice).FirstOrDefault();
 }
Ejemplo n.º 9
0
 protected Candle GetMinimumBreakout(string rateName, Candle from, decimal value)
 {
     IList<Candle> candles = _candles[rateName].Where(item => item.OpenTime > from.OpenTime).OrderBy(item => item.OpenTime).ToList();
     foreach (Candle candle in candles)
     {
         if (candle.LowPrice < value)
             return candle;
     }
     return null;
 }
Ejemplo n.º 10
0
        protected Candle GetMinimumBetween(string rateName, Candle from, Candle to)
        {
            IList<Candle> candles = _candles[rateName].Where(item => item.OpenTime < to.OpenTime && item.OpenTime > from.OpenTime).ToList();
            if (candles.Count == 0)
                return null;
            decimal minPrice = candles.Min(item => item.LowPrice);
            Candle minimum = candles.Where(item => item.LowPrice == minPrice).FirstOrDefault();
            if (minimum.OpenTime.AddMinutes(CANDLES_INTERVAL_MINUTES) == to.OpenTime)
            {
                return GetMinimumBetween(rateName, from, minimum);
            }
            if (minimum.OpenTime.AddMinutes(-CANDLES_INTERVAL_MINUTES) == from.OpenTime)
            {
                return GetMinimumBetween(rateName, minimum, to);
            }

            return minimum;
        }
Ejemplo n.º 11
0
 protected Candle GetMaximumForInterval(string rateName, int interval, Candle to, decimal fromValue, decimal toValue)
 {
     IList<Candle> candles = _candles[rateName].Where(item => item.OpenTime < to.OpenTime).OrderByDescending(item => item.OpenTime).Take(interval).ToList();
     if (candles.Count == 0)
         return null;
     decimal maxPrice = candles.Max(item => item.HighPrice);
     if (maxPrice < fromValue || maxPrice > toValue)
         return null;
     return candles.Where(item => item.HighPrice == maxPrice).FirstOrDefault();
 }