public static void UpdateValues(this OhlcPoint point, BinanceKline newKline) { point.Open = (double)newKline.Open; point.High = (double)newKline.High; point.Low = (double)newKline.Low; point.Close = (double)newKline.Close; }
private static double?GetVariationFromKline(BinanceKline kline) { if (kline == null) { return(null); } return((kline.Close - kline.Open) / kline.Open); }
private void AddKline(KlineInterval interval, BinanceKline klineValue) { klinesDictionary[interval].Add(klineValue); if (interval == klineInterval) { RaisePropertyChangedEvent("Klines"); } }
protected override SimplePoint CalcNext(BinanceKline candle) { _buffer.Dequeue(); _buffer.Enqueue((double)candle.Close); SimplePoint nextPoint = new SimplePoint(candle.OpenTime, _buffer.Sum() / Period); return(nextPoint); }
/// <summary> /// Calculates the next indicator point, updates the ".Points" property /// </summary> /// <param name="nextCandle"></param> /// <returns>The resulting next indicator point</returns> public T Next(BinanceKline nextCandle) { var next = CalcNext(nextCandle); // Push the ring buffer by one Points.Add(next); Points.RemoveAt(0); return(next); }
private void printKline(BinanceKline data, String symbol) { //Console.Out.Write("PAR: " + symbol + Environment.NewLine); Console.Out.Write("OPEN TIME: " + data.OpenTime + Environment.NewLine); //Console.Out.Write("CLOSE TIME: " + data.CloseTime + Environment.NewLine); //Console.Out.Write("OPEN: " + data.Open + Environment.NewLine); //Console.Out.Write("CLOSE: " + data.Close + Environment.NewLine); //Console.Out.Write("HIGH: " + data.High + Environment.NewLine); //Console.Out.Write("LOW: " + data.Low + Environment.NewLine); Console.Out.Write("TRADES: " + data.TradeCount + Environment.NewLine); Console.Out.Write("VOLUME: " + data.Volume + Environment.NewLine); //Console.Out.Write("=============== " + Environment.NewLine); }
protected override BollingerPoint CalcNext(BinanceKline nextCandle) { _buffer.Dequeue(); _buffer.Enqueue((double)nextCandle.Close); double _total_average = _buffer.Sum(); double _total_squares = _buffer.Sum(x => Math.Pow(x, 2)); double average = _total_average / Period; double stdev = Math.Sqrt((_total_squares - Math.Pow(_total_average, 2) / Period) / Period); var result = new BollingerPoint(nextCandle.OpenTime, average - Std * stdev, average, average + Std * stdev); return(result); }
private void HandleKlineEvent(BinanceKlineData msg) { this.Time = msg.EventTime; if (FormingCandle != null && msg.Kline.StartTime > FormingCandle.StartTime) { //if this tick is a new candle and the last candle was not added to ticks //then let's add it var candle = new Candlestick() { Close = (double)FormingCandle.Close, High = (double)FormingCandle.High, CloseTime = FormingCandle.StartTime.AddSeconds(60), OpenTime = FormingCandle.StartTime, Low = (double)FormingCandle.Low, Open = (double)FormingCandle.Open, QuoteAssetVolume = (double)FormingCandle.QuoteVolume }; //HistoryDb.AddCandlesticks(HistoryId, new[] { candle }); this.OnData?.Invoke(this, candle); FormingCandle = null; } if (msg.Kline.IsBarFinal) { KlineWatchdog.Restart(); var candle = new Candlestick() { Close = (double)msg.Kline.Close, High = (double)msg.Kline.High, CloseTime = msg.Kline.StartTime.AddSeconds(60), OpenTime = msg.Kline.StartTime, Low = (double)msg.Kline.Low, Open = (double)msg.Kline.Open, QuoteAssetVolume = (double)msg.Kline.QuoteVolume }; //HistoryDb.AddCandlesticks(HistoryId, new[] { candle }); this.OnData?.Invoke(this, candle); FormingCandle = null; } else { FormingCandle = msg.Kline; } }
public CandleDBRow(BinanceKline klineCandle, string asset, string currency, KlineInterval interval) { Exchange = 1; // Binance Asset = asset; Currency = currency; Interval = interval; OpenTime = klineCandle.OpenTime; Open = klineCandle.Open; High = klineCandle.High; Low = klineCandle.Low; Close = klineCandle.Close; Volume = klineCandle.Volume; CloseTime = klineCandle.CloseTime; AssetVolume = klineCandle.AssetVolume; Trades = klineCandle.Trades; TakerBuyBaseAssetVolume = klineCandle.TakerBuyBaseAssetVolume; TakerBuyQuoteAssetVolume = klineCandle.TakerBuyQuoteAssetVolume; }
public static Candle ToCandle(this BinanceKline kline, string symbol, IPeriodCode period) { Candle candle = new Candle(); candle.ExchangeCode = ExchangeCode.BINANCE.Code; candle.Symbol = symbol; candle.OpenTime = kline.OpenTime; candle.Open = kline.Open; candle.CloseTime = kline.CloseTime; candle.Close = kline.Close; candle.High = kline.High; candle.Low = kline.Low; candle.Volume = kline.Volume; candle.NumberOfTrades = kline.TradeCount; candle.PeriodCode = period.Code; candle.Time = new DateTime(kline.OpenTime.Year, kline.OpenTime.Month, kline.OpenTime.Day, kline.OpenTime.Hour, kline.OpenTime.Minute, 0); return(candle); }
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JArray arr = JArray.Load(reader); BinanceKline entry = new BinanceKline { OpenTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)arr[0]), Open = (decimal)arr[1], High = (decimal)arr[2], Low = (decimal)arr[3], Close = (decimal)arr[4], Volume = (decimal)arr[5], CloseTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds((long)arr[6]), AssetVolume = (decimal)arr[7], Trades = (int)arr[8], TakerBuyBaseAssetVolume = (decimal)arr[9], TakerBuyQuoteAssetVolume = (decimal)arr[10] }; return(entry); }
public static OhlcPoint ToOhlcPoint(this BinanceKline kline) { return(new OhlcPoint((double)kline.Open, (double)kline.High, (double)kline.Low, (double)kline.Close)); }
protected override SimplePoint CalcNext(BinanceKline nextCandle) { _indicator += _coeff * ((double)nextCandle.Close - _indicator); return(new SimplePoint(nextCandle.OpenTime, _indicator)); }
/// <summary> /// Calculats the next value of the indicator /// </summary> /// <param name="nextCandle">The next candle</param> /// <returns>The resulting indicator point</returns> protected abstract T CalcNext(BinanceKline nextCandle);
protected override DMIPoint CalcNext(BinanceKline nextCandle) { // "Pop" the first value of each list to keep memory small TRs.RemoveAt(0); DM1Plus.RemoveAt(0); DM1Minus.RemoveAt(0); TR_period.RemoveAt(0); DMPlus_period.RemoveAt(0); DMMinus_period.RemoveAt(0); DPlus_period.RemoveAt(0); DMinus_period.RemoveAt(0); DX_period.RemoveAt(0); ADX_period.RemoveAt(0); // true range TRs.Add((double)Math.Max( nextCandle.High - nextCandle.Low, Math.Max( Math.Abs(nextCandle.High - _lastCandle.Close), Math.Abs(nextCandle.Low - _lastCandle.Close)))); // +DM1 DM1Plus.Add((double)((nextCandle.High - _lastCandle.High) > _lastCandle.Low - nextCandle.Low ? Math.Max(nextCandle.High - _lastCandle.High, 0) : 0)); // -DM1 DM1Minus.Add((double)((_lastCandle.Low - nextCandle.Low) > nextCandle.High - _lastCandle.High ? Math.Max(_lastCandle.Low - nextCandle.Low, 0) : 0)); // Summs double trperiod = TRs.Skip(TR_period.Count).Take(Period).Sum(); TR_period.Add(trperiod); double dmplusperiod = DM1Plus.Skip(DMPlus_period.Count).Take(Period).Sum(); DMPlus_period.Add(dmplusperiod); double dmminusperiod = DM1Minus.Skip(DMMinus_period.Count).Take(Period).Sum(); DMMinus_period.Add(dmminusperiod); // D+ and D- double dplusperiod = 100.0 * dmplusperiod / trperiod; DPlus_period.Add(dplusperiod); double dminusperiod = 100.0 * dmminusperiod / trperiod; DMinus_period.Add(dminusperiod); // Diff & sum double ddiffperiod = Math.Abs(dplusperiod - dminusperiod); double dsumperiod = dplusperiod + dminusperiod; // Fraction DX_period.Add(100.0 * ddiffperiod / dsumperiod); // Adx double adxperiod = DX_period.Skip(ADX_period.Count).Take(Period).Average(); ADX_period.Add(adxperiod); _lastCandle = nextCandle; return(new DMIPoint(nextCandle.OpenTime, dplusperiod, dminusperiod, adxperiod)); }
protected override IEnumerable <DMIPoint> CalcInit(IList <BinanceKline> initialCandles) { List <DMIPoint> result = new List <DMIPoint>(); TRs = new List <double>(Period * 2); DM1Plus = new List <double>(Period * 2); DM1Minus = new List <double>(Period * 2); TR_period = new List <double>(Period * 2); DMPlus_period = new List <double>(Period); DMMinus_period = new List <double>(Period); DPlus_period = new List <double>(Period); DMinus_period = new List <double>(Period); DX_period = new List <double>(Period); ADX_period = new List <double>(Period); for (int i = 1; i < initialCandles.Count; i++) { // true range TRs.Add((double)Math.Max( initialCandles[i].High - initialCandles[i].Low, Math.Max( Math.Abs(initialCandles[i].High - initialCandles[i - 1].Close), Math.Abs(initialCandles[i].Low - initialCandles[i - 1].Close)))); // +DM1 DM1Plus.Add((double)((initialCandles[i].High - initialCandles[i - 1].High) > initialCandles[i - 1].Low - initialCandles[i].Low ? Math.Max(initialCandles[i].High - initialCandles[i - 1].High, 0) : 0)); // -DM1 DM1Minus.Add((double)((initialCandles[i - 1].Low - initialCandles[i].Low) > initialCandles[i].High - initialCandles[i - 1].High ? Math.Max(initialCandles[i - 1].Low - initialCandles[i].Low, 0) : 0)); if (TRs.Count >= Period) { // Summs double trperiod = TRs.Skip(TR_period.Count).Take(Period).Sum(); TR_period.Add(trperiod); double dmplusperiod = DM1Plus.Skip(DMPlus_period.Count).Take(Period).Sum(); DMPlus_period.Add(dmplusperiod); double dmminusperiod = DM1Minus.Skip(DMMinus_period.Count).Take(Period).Sum(); DMMinus_period.Add(dmminusperiod); // D+ and D- double dplusperiod = 100.0 * dmplusperiod / trperiod; DPlus_period.Add(dplusperiod); double dminusperiod = 100.0 * dmminusperiod / trperiod; DMinus_period.Add(dminusperiod); // Diff & sum double ddiffperiod = Math.Abs(dplusperiod - dminusperiod); double dsumperiod = dplusperiod + dminusperiod; // Fraction DX_period.Add(100.0 * ddiffperiod / dsumperiod); } if (DX_period.Count >= Period) { double adxperiod = DX_period.Skip(ADX_period.Count).Take(Period).Average(); ADX_period.Add(adxperiod); result.Add(new DMIPoint(initialCandles[i].OpenTime, DPlus_period.Last(), DMinus_period.Last(), adxperiod)); } } _lastCandle = initialCandles.Last(); return(result); }
private void DoWork(object source) { _logger.LogInformation("Timed Background Service is working."); var timer = (KlineInterval)source; var clientt = new BinanceClient(); clientt.SetApiCredentials("7ICQp7LXtOdaFOTYyQV4GqjA2nwOzkwgOW3KgHCQTj5fyZHXNHD4XmRVW3BukcXZ", "MFr1NGf1rLwSaNZ5pOfndWG4GX4vIRVUBpiCXqckKhj44rBAjracQb44M1n0Ra7g"); var gg = new List <Quotation>(); var example = new BinanceKline(); using (var scope = Services.CreateScope()) { var QuotationsFive = scope.ServiceProvider .GetRequiredService <IQuotationFiveService>(); lock (locker) { if (timer == KlineInterval.FiveMinutes) { foreach (var pairs in ex) { var requestKliness = clientt.GetKlines(pairs.PairName, timer, null, null, 2); var checkOne = DateTime.Now.Minute - requestKliness.Data[0].OpenTime.Minute; var checkTwo = DateTime.Now.Minute - requestKliness.Data[1].OpenTime.Minute; if (checkOne == 10 || checkTwo == 5 || checkOne == -50 || checkTwo == -55) { example = requestKliness.Data[1]; } else { example = requestKliness.Data[0]; } var records = new Quotation { Date = example.OpenTime.ToLocalTime(), Open = example.Open, MaxPrice = example.High, MinPrice = example.Low, Close = example.Close, Pair = pairs.Pairs, Interval = timer }; gg.Add(records); } QuotationsFive.CreateArray(gg); Console.WriteLine(DateTime.Now); gg.Clear(); } else { foreach (var pairs in ex) { try { var requestKliness = clientt.GetKlines(pairs.PairName, timer, null, null, 2); example = requestKliness.Data[0]; var records = new Quotation { Date = example.OpenTime.ToLocalTime(), Open = example.Open, MaxPrice = example.High, MinPrice = example.Low, Close = example.Close, Pair = pairs.Pairs, Interval = timer }; gg.Add(records); } catch { return; } } QuotationsFive.CreateArray(gg); Console.WriteLine(DateTime.Now); gg.Clear(); } } } }