-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinanceService.cs
85 lines (75 loc) · 3.3 KB
/
BinanceService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using BinanceExchange.API.Client;
using BinanceExchange.API.Market;
using BinanceExchange.API.Models.Request;
using BinanceExchange.API.Models.Response;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using static BinanceWrapper.Enums;
namespace BinanceWrapper
{
public class BinanceService
{
HttpClient client = null;
IndicatorService indicatorService = null;
public BinanceService()
{
client = new HttpClient();
indicatorService = new IndicatorService();
}
public BinanceSymbolData GetSymbolData(string symbol, string interval, string limit)
{
BinanceSymbolData symbolData = new BinanceSymbolData(symbol);
string url = string.Concat(Constants.binanceBaseUrl, Constants.candleStickEndpoint, "?", "symbol=", symbol, "&interval=", interval, "&limit=", limit);
HttpResponseMessage response = client.GetAsync(url).GetAwaiter().GetResult();
//Console.WriteLine("Status Code:" + response.StatusCode);
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
JArray json = JsonConvert.DeserializeObject<JArray>(result);
for (int i = 0; i < json.Count; i++)
{
JArray stickData = JsonConvert.DeserializeObject<JArray>(json[i].ToString());
symbolData.Periods.Add(new BinanceCandleStickData(stickData));
}
symbolData.TimePolled = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
}
return symbolData;
}
public List<BinanceSymbolData> GetAllSymbolData(string interval, string limit)
{
List<BinanceSymbolData> symbols = new List<BinanceSymbolData>();
foreach (Symbol symbol in Enum.GetValues(typeof(Symbol)))
{
var symbolData = GetSymbolData(symbol.ToString(), interval, limit);
indicatorService.PopulateSymbolDataListMetaData(symbolData.Periods);
if (symbolData.Periods.Last().Open <= .0002m)
symbols.Add(symbolData);
}
return symbols;
}
public List<BinanceSymbolData> FetchPopulateBinanceSymbolData(int limit)
{
Console.WriteLine("Currently Fetching Data from Binance...");
List<BinanceSymbolData> symbols = GetAllSymbolData("30m", limit.ToString());
int numberOfSymbolsUpdated = symbols.Count;
for (int i = 0; i < symbols.Count; i++)
{
indicatorService.PopulateSymbolDataListMetaData(symbols[i].Periods);
}
return symbols;
}
public BinanceSymbolData FetchPopulateBinanceSymbolData(Enums.Symbol symbol, int limit)
{
Console.WriteLine("Currently Fetching Data from Binance...");
BinanceSymbolData symbolData = GetSymbolData(symbol.ToString(),"30m", limit.ToString());
indicatorService.PopulateSymbolDataListMetaData(symbolData.Periods);
return symbolData;
}
}
}