Skip to content

Commit

Permalink
Binance trades load in OsData
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexWan committed May 8, 2019
1 parent c3f61db commit cfdd403
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 4 deletions.
8 changes: 8 additions & 0 deletions project/OsEngine/Language/DataLocal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,5 +132,13 @@ public class DataLocal
public string Label27 => OsLocalization.ConvertToLocString(
"Eng:You want to close the program. Are you sure?_" +
"Ru:Вы собираетесь закрыть программу. Вы уверены?_");

public string Label28 => OsLocalization.ConvertToLocString(
"Eng:We request trades on security _" +
"Ru:Запрашиваем трейды по бумаге _");

public string Label29 => OsLocalization.ConvertToLocString(
"Eng:Trades successfully loaded. Security _" +
"Ru:Трейды успешно закгужены. Бумага _");
}
}
13 changes: 12 additions & 1 deletion project/OsEngine/Market/Servers/AServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1290,7 +1290,18 @@ private void _candleManager_CandleUpdateEvent(CandleSeries series)

if (security == null)
{
return false;
for (int i = 0; _securities != null && i < _securities.Count; i++)
{
if (_securities[i].NameId == namePaper)
{
security = _securities[i];
break;
}
}
if (security == null)
{
return false;
}
}

List<Trade> trades = ServerRealization.GetTickDataToSecurity(security, startTime, endTime, actualTime);
Expand Down
57 changes: 57 additions & 0 deletions project/OsEngine/Market/Servers/Binance/BinanceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,63 @@ public List<Candle> GetCandlesForTimes(string nameSec, TimeSpan tf, DateTime tim
return null;
}

public List<Trade> GetTickHistoryToSecurity(Security security, string lastId)
{
try
{

List<Trade> newTrades = new List<Trade>();

Dictionary<string, string> param = new Dictionary<string, string>();

if (string.IsNullOrEmpty(lastId) == false)
{
param.Add("symbol=" + security.Name, "&limit=1000" + "&fromId=" + lastId);
}
else
{
param.Add("symbol=" + security.Name, "&limit=1000");
}


string endPoint = "api/v1/historicalTrades";

var res2 = CreateQuery(Method.GET, endPoint, param, false);

List<HistoryTrade> tradeHistory = JsonConvert.DeserializeAnonymousType(res2, new List<HistoryTrade>());

//tradeHistory.Reverse();

foreach (var trades in tradeHistory)
{
Trade trade = new Trade();
trade.SecurityNameCode = security.Name;
trade.Price =
Convert.ToDecimal(
trades.price.Replace(".", CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator),
CultureInfo.InvariantCulture);

trade.Id = trades.id.ToString();
trade.Time = new DateTime(1970, 1, 1).AddMilliseconds(Convert.ToDouble(trades.time));
trade.Volume =
Convert.ToDecimal(
trades.qty.Replace(".", CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator),
CultureInfo.InvariantCulture);
trade.Side = Convert.ToBoolean(trades.isBuyerMaker) == true ? Side.Buy : Side.Sell;

newTrades.Add(trade);
}

return newTrades;

}
catch (Exception error)
{
SendLogMessage(error.ToString(), LogMessageType.Error);
return null;
}
}

/// <summary>
/// take candles
/// взять свечи
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OsEngine.Market.Servers.Binance.BinanceEntity
{
public class HistoryTrade
{
public string id;
public string price;
public string qty;
public string quoteQty;
public string time;
public string isBuyerMaker;
public string isBuyerMatch;
}
}
39 changes: 37 additions & 2 deletions project/OsEngine/Market/Servers/Binance/BinanceServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using OsEngine.Entity;
using OsEngine.Language;
using OsEngine.Logging;
using System.Threading;
using OsEngine.Market.Servers.Entity;

namespace OsEngine.Market.Servers.Binance
Expand Down Expand Up @@ -216,9 +217,43 @@ public void Subscrible(Security security)
/// take ticks data on instrument for period
/// взять тиковые данные по инструменту за период
/// </summary>
public List<Trade> GetTickDataToSecurity(Security security, DateTime startTime, DateTime endTime, DateTime actualTime)
public List<Trade> GetTickDataToSecurity(Security security, DateTime startTime, DateTime endTime, DateTime lastDate)
{
return null;
List<Trade> lastTrades = new List<Trade>();

string tradeId = "";

DateTime lastTradeTime = DateTime.MaxValue;

while (lastTradeTime > startTime)
{
lastDate = TimeZoneInfo.ConvertTimeToUtc(lastDate);

List<Trade> trades = _client.GetTickHistoryToSecurity(security, tradeId);

if (trades == null ||
trades.Count == 0)
{
lastTradeTime = lastDate.AddSeconds(-1);
Thread.Sleep(2000);
continue;
}

DateTime uniTime = trades[trades.Count - 1].Time.ToUniversalTime();

lastTradeTime = trades[0].Time;

for (int i2 = 0; i2 < trades.Count; i2++)
{
lastTrades.Insert(i2, trades[i2]);
}

tradeId = (Convert.ToInt32(trades[0].Id) - 1000).ToString();

Thread.Sleep(100);
}

return lastTrades;
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion project/OsEngine/OsData/OsDataSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -823,12 +823,14 @@ private void StartSets()
{
for (int i = 0; i < SecuritiesNames.Count; i++)
{
SendNewLogMessage(OsLocalization.Data.Label28 + SecuritiesNames[i].Id, LogMessageType.System);
while (
(_myServer).GetTickDataToSecurity(SecuritiesNames[i].Id, TimeStart, TimeEnd,
GetActualTimeToTrade("Data\\" + SetName + "\\" + SecuritiesNames[i].Name.Replace("/", "") + "\\Tick"), NeadToUpdate) == false)
{
Thread.Sleep(5000);
}
SendNewLogMessage(OsLocalization.Data.Label29 + SecuritiesNames[i].Id, LogMessageType.System);
}
}

Expand Down Expand Up @@ -962,7 +964,7 @@ private void SaveData()
{

SaveThisTick(trades[i2],
path, SecuritiesNames[i].Name.Replace("*", ""), null, path + "\\" + "Tick");
path + "\\" + "Tick", SecuritiesNames[i].Name.Replace("*", ""), null, path + "\\" + "Tick");
}
}
else
Expand Down
1 change: 1 addition & 0 deletions project/OsEngine/OsEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@
<Compile Include="Market\Servers\Binance\BinanceEntity\ErrorMessage.cs" />
<Compile Include="Market\Servers\Binance\BinanceEntity\ExecutionReport.cs" />
<Compile Include="Market\Servers\Binance\BinanceEntity\HistoryOrderReport.cs" />
<Compile Include="Market\Servers\Binance\BinanceEntity\HistoryTrade.cs" />
<Compile Include="Market\Servers\Binance\BinanceEntity\ListenKey.cs" />
<Compile Include="Market\Servers\Binance\BinanceEntity\OutboundAccountInfo.cs" />
<Compile Include="Market\Servers\Binance\BinanceEntity\SecurityResponce.cs" />
Expand Down
Binary file modified project/OsEngine/bin/Debug/Interop.SmartCOM4Lib.dll
Binary file not shown.
Binary file modified project/OsEngine/bin/Debug/OsEngine.exe
Binary file not shown.

0 comments on commit cfdd403

Please sign in to comment.