forked from and7000/ArbitrageNacional
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
188 lines (152 loc) · 6.31 KB
/
Program.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Created by SharpDevelop.
* User: mifus_000
* Date: 20/05/2017
* Time: 09:00
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Data;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Collections.Generic;
using System.Xml;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Runtime.InteropServices;
using System.Globalization;
using System.Threading.Tasks;
class Program
{
static int totalThread = 0;
public static string location = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location) + "\\";
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
// Unix timestamp is seconds past epoch
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddSeconds(unixTimeStamp).ToLocalTime();
return dtDateTime;
}
class EntryArbitrage
{
public IExchange exchangeBuy;
public string pairBuy = "btc_brl";
public string pairSell = "btc_brl";
public IExchange exchangeSell;
public decimal perc = 1m;
public decimal amount = 0.001m;
public int sleep = 100;
}
static readonly Object objLock = new Object();
public static void arbitrageDetail(Object obj)
{
EntryArbitrage entry = (EntryArbitrage)obj;
lock (objLock)
{
entry.exchangeBuy.getBalances();
entry.exchangeSell.getBalances();
}
while (true)
{
try
{
decimal[] buy = null;
decimal[] sell = null;
Task.Run(() =>
{
buy = entry.exchangeBuy.getLowestAsk(entry.pairBuy, entry.amount);
});
Task.Run(() =>
{
sell = entry.exchangeSell.getHighestBid(entry.pairSell, entry.amount);
});
while (true)
{
if (buy != null && sell != null)
break;
System.Threading.Thread.Sleep(50);
}
decimal perc = (((sell[0] * 100) / buy[0]) - 100);
Logger.log(Math.Round(perc, 2) + "% " + entry.exchangeBuy.getName() + " > " + entry.exchangeSell.getName() + " ");
if (perc > entry.perc)
{
if (entry.exchangeBuy.getBalance("USDT") >= (buy[0] * entry.amount) && entry.exchangeSell.getBalance("BTC") >= entry.amount)
{
Task.Run(() =>
{
entry.exchangeBuy.order("buy", entry.pairBuy, entry.amount, buy[1], false);
});
Task.Run(() =>
{
entry.exchangeSell.order("sell", entry.pairSell, entry.amount, sell[1], false);
});
System.Threading.Thread.Sleep(2000);
lock (objLock)
{
entry.exchangeBuy.getBalances();
entry.exchangeSell.getBalances();
}
Logger.log(entry.exchangeBuy.getName() + " > " + entry.exchangeSell.getName() + " | Buy[0]" + buy[0] + " Buy[1]" + buy[1] + "| Sell[0]" + sell[0] + " Sell[1]" + sell[1] + " | " + perc + "%");
System.Threading.Thread.Sleep(2000);
}
Logger.log(entry.exchangeBuy.getName() + " > " + entry.exchangeSell.getName() + " | Buy[0]" + buy[0] + " Buy[1]" + buy[1] + "| Sell[0]" + sell[0] + " Sell[1]" + sell[1] + " | " + perc + "%");
}
}
catch
{
}
System.Threading.Thread.Sleep(500);
System.Threading.Thread.Sleep(entry.sleep);
}
}
public static void arbitrageHFT()
{
try
{
EntryArbitrage entryBraziliexBitCointrade = new EntryArbitrage();
entryBraziliexBitCointrade.exchangeBuy = new ExchangeBraziliex();
entryBraziliexBitCointrade.pairBuy = "btc_brl";
entryBraziliexBitCointrade.exchangeSell = new ExchangeBitcoinTrade();
entryBraziliexBitCointrade.pairSell = "BRLBTC";
entryBraziliexBitCointrade.sleep = int.Parse(Program.jConfig["sleep_default"].ToString()); ;
entryBraziliexBitCointrade.perc = decimal.Parse(Program.jConfig["arbitrage_percent"].ToString()); ;
entryBraziliexBitCointrade.amount = decimal.Parse(Program.jConfig["arbitrage_amount"].ToString());
System.Threading.Thread tBraziliexBitCointrade = new System.Threading.Thread(arbitrageDetail);
tBraziliexBitCointrade.Start(entryBraziliexBitCointrade);
EntryArbitrage entryBitCointradeBraziliex = new EntryArbitrage();
entryBitCointradeBraziliex.exchangeBuy = new ExchangeBitcoinTrade();
entryBitCointradeBraziliex.pairBuy = "BRLBTC";
entryBitCointradeBraziliex.exchangeSell = new ExchangeBraziliex();
entryBitCointradeBraziliex.pairSell = "btc_brl";
entryBitCointradeBraziliex.sleep = int.Parse(Program.jConfig["sleep_default"].ToString());
entryBitCointradeBraziliex.perc = decimal.Parse(Program.jConfig["arbitrage_percent"].ToString()); ;
entryBitCointradeBraziliex.amount = decimal.Parse(Program.jConfig["arbitrage_amount"].ToString());
System.Threading.Thread tBitCointradeBraziliex = new System.Threading.Thread(arbitrageDetail);
tBitCointradeBraziliex.Start(entryBitCointradeBraziliex);
}
catch
{
}
while (true)
{
System.Threading.Thread.Sleep(600000);
}
}
public static JContainer jConfig = null;
public static void Main(string[] args)
{
try
{
String jsonConfig = System.IO.File.ReadAllText(location + "key.txt");
jConfig = (JContainer)JsonConvert.DeserializeObject(jsonConfig, (typeof(JContainer)));
arbitrageHFT();
}
catch (Exception ex)
{
return;
}
}
}