forked from EvgBitWhiskey/BitWhiskey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
421 lines (368 loc) · 17.2 KB
/
Form1.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Media;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;
using NLog;
using System.Threading.Tasks;
using System.Threading;
using System.Timers;
namespace BitWhiskey
{
public partial class Form1 : Form
{
public TradeRequestHandlers tradeHandlers;
DataGridViewExWrapper gvMarkets;
private AppTimer timerMarkets;
private AppTimer timerCheckAlerts;
private bool marketsLoaded = false;
// private AppTimer timerAlerts;
// Dictionary<string, TradePair> tradePairs;
public Form1()
{
InitializeComponent();
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls12;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
labelInfo.Text = "Please wait..Loading";
Helper.Init();
Global.uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
AppSettingsManager.LoadSettings();
Global.markets.Init();
Global.marketsState.Init();
StatusBar.Init(statusLabelDate, statusLabelMain);
toolStripComboBoxMarket.Items.Clear();
foreach (var market in Global.markets.GetMarketList())
{
toolStripComboBoxMarket.Items.Add(market.ToString());
}
RequestConsumer.requestManager.Create(Global.markets.GetMarketList());
RequestConsumer.CreateRequestThreads(Global.markets.GetMarketList());
timerMarkets = new AppTimer(26000, TimerMarkets_Tick,this);
timerCheckAlerts = new AppTimer(5000, TimerCheckAlerts_Tick, this);
timerCheckAlerts.Start();
toolStripComboBoxMarket.SelectedIndex = 0;
}
private void toolStripComboBoxMarket_SelectedIndexChanged(object sender, EventArgs e)
{
dgridMarkets.DataSource = null;
this.ActiveControl = dgridMarkets;
timerMarkets.Stop();
LoadTickers();
}
private void LoadTickers()
{
//toolDropDownTickerBtc.DropDown.Items.Clear();
//toolDropDownTickerUsdt.DropDown.Items.Clear();
//toolDropDownTickerBtc.Enabled = false;
//toolDropDownTickerUsdt.Enabled = false;
toolButtonTickerBtc.Enabled = false;
toolButtonTickerUsd.Enabled = false;
toolStripButtonChart.Enabled = false;
toolStripButtonAlerts.Enabled = false;
toolStripComboBoxMarket.Enabled = false;
toolStripButtonBalance.Enabled = false;
// if (!ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text).HaveKey())
// return;
Market market = ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text);
tradeHandlers = new TradeRequestHandlers(market);
/*
RequestItemGroup itemgroup = new RequestItemGroup(LoadTickers_UIResultHandler);
itemgroup.AddItem(market.GetTradePairsBegin(), tradeHandlers.GetTradePairs_RequestHandler);
RequestConsumer.requestManager.AddItemGroup(market.MarketName(), itemgroup);
*/
UpdateMarkets();
}
private void UpdateMarket(string marketName)
{
Market market = ExchangeManager.GetMarketByMarketName(marketName);
tradeHandlers = new TradeRequestHandlers(market);
RequestItemGroup itemgroup = new RequestItemGroup(GetMarketCurrent_UIResultHandler);
itemgroup.AddItem(market.GetMarketCurrentBegin(), tradeHandlers.GetMarketCurrent_RequestHandler);
RequestConsumer.requestManager.AddItemGroup(market.MarketName(), itemgroup);
}
private void UpdateMarkets()
{
if (marketsLoaded)
UpdateMarket(toolStripComboBoxMarket.Text);
else
{
foreach (var marketName in Global.markets.GetMarketList())
UpdateMarket(marketName);
}
}
public void LoadTickers_UIResultHandler(RequestItemGroup resultResponse)
{
/*
//Global.market.tradePairs = new Dictionary<string, TradePair>();
//Global.market.tradePairs.Add("BTC_LTC",new TradePair{currency1 = "BTC",currency2 = "LTC",ticker = "BTC_LTC",isActive =true});
int btctickerCount = 0;
foreach (KeyValuePair<string, TradePair> pair in tradePairs)
{
if (pair.Key.StartsWith("BTC"))
btctickerCount++;
}
ToolStripDropDown drop_downbtc = new ToolStripDropDown();
drop_downbtc.LayoutStyle = ToolStripLayoutStyle.Table;
// drop_downbtc.
((TableLayoutSettings)drop_downbtc.LayoutSettings).ColumnCount = btctickerCount / 23 + 1;
toolDropDownTickerBtc.DropDown = drop_downbtc;
ToolStripDropDown drop_downusdt = new ToolStripDropDown();
drop_downusdt.LayoutStyle = ToolStripLayoutStyle.Table;
((TableLayoutSettings)drop_downusdt.LayoutSettings).ColumnCount = (tradePairs.Count - btctickerCount) / 23 + 1;
toolDropDownTickerUsdt.DropDown = drop_downusdt;
foreach (KeyValuePair<string, TradePair> pair in tradePairs.OrderBy(p => p.Key))
{
if (pair.Key.StartsWith("BTC"))
toolDropDownTickerBtc.DropDown.Items.Add(pair.Key);
else //if (pair.Key.StartsWith("USDT"))
toolDropDownTickerUsdt.DropDown.Items.Add(pair.Key);
}
*/
/*
if (RequestManager.IsResultHasErrors(resultResponse))
return;
Dictionary<string, TradePair> pairs = (Dictionary<string, TradePair>)resultResponse.items[0].result.resultData;
tradePairs = new Dictionary<string, TradePair>();
foreach (KeyValuePair<string, TradePair> pair in pairs)
{
tradePairs.Add(pair.Key, pair.Value.Copy());
}
if (ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text).HaveKey())
{
toolButtonTickerBtc.Enabled = true;
toolButtonTickerUsd.Enabled = true;
// toolDropDownTickerBtc.Enabled = true;
// toolDropDownTickerUsdt.Enabled = true;
toolStripButtonAlerts.Enabled = true;
}
toolStripButtonChart.Enabled = true;
labelInfo.Text = "";
*/
}
public void GetMarketCurrent_UIResultHandler(RequestItemGroup resultResponse)
{
if (RequestManager.IsResultHasErrors(resultResponse))
return;
Dictionary<string, MarketCurrent> currenciesDict = (Dictionary<string, MarketCurrent>)resultResponse.items[0].result.resultData;
// Dictionary<string, TradePair> pairs = (Dictionary<string, TradePair>)resultResponse.items[0].result.resultData;
if (!marketsLoaded)
{
if (Global.marketsState.curMarketPairs[resultResponse.market] == null)
{
Dictionary<string, TradePair> tradePairs = new Dictionary<string, TradePair>();
foreach (KeyValuePair<string, MarketCurrent> pair in currenciesDict)
{
Pair pairinfo = new Pair(pair.Value.ticker);
TradePair tpair = new TradePair
{
currency1 = pairinfo.currency1,
currency2 = pairinfo.currency2,
isActive = true,
ticker = pair.Value.ticker
};
tradePairs.Add(pair.Key, tpair);
}
Global.marketsState.SetPairs(resultResponse.market, tradePairs);
}
bool allMarketsReady = true;
foreach (var marketName in Global.markets.GetMarketList())
{
if (Global.marketsState.curMarketPairs[marketName] == null)
{
allMarketsReady = false;
break;
}
}
if (allMarketsReady)
marketsLoaded = true;
}
if (marketsLoaded)
{
Market curmarket = ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text);
if (curmarket.HaveKey())
{
toolButtonTickerBtc.Enabled = true;
toolButtonTickerUsd.Enabled = true;
if (curmarket.Options().AllPairRatesSupported)
toolStripButtonAlerts.Enabled = true;
}
if(curmarket.Options().ChartDataSupported)
toolStripButtonChart.Enabled = true;
toolStripButtonBalance.Enabled = true;
toolStripComboBoxMarket.Enabled = true;
labelInfo.Text = "";
}
List<MarketCurrentView> currencies = currenciesDict.Values.Select(item => new MarketCurrentView
{
ticker = item.ticker,
origPrice = item.lastPrice,
lastPrice = item.lastPrice,
lastPriceUSD = 0,
percentChange = item.percentChange,
volumeBtc = item.volumeBtc,
volumeUSDT = item.volumeUSDT
}).ToList();
if (currenciesDict.ContainsKey("USDT_BTC"))
{
double btcPrice = currenciesDict["USDT_BTC"].lastPrice;
foreach (var item in currencies)
{
if (item.ticker.StartsWith("BTC"))
item.volumeUSDT = item.volumeBtc * btcPrice;
if (item.ticker.StartsWith("BTC"))
item.lastPriceUSD = item.lastPrice * btcPrice;
else if (item.ticker.StartsWith("USDT"))
{
double usdprice = item.lastPrice;
item.lastPriceUSD = usdprice;
item.lastPrice = item.lastPrice / btcPrice;
}
}
}
currencies = currencies.OrderByDescending(x => x.volumeUSDT).ToList();
List<MarketCurrentView> currenciesCopy = new List<MarketCurrentView>();
foreach (MarketCurrentView item in currencies)
{
MarketCurrentView newItem = new MarketCurrentView()
{
ticker = item.ticker,
origPrice = item.origPrice,
lastPrice = item.lastPrice,
lastPriceUSD = item.lastPriceUSD,
percentChange = item.percentChange,
volumeBtc = item.volumeBtc,
volumeUSDT = item.volumeUSDT
};
currenciesCopy.Add(newItem);
}
if (ExchangeManager.GetMarketByMarketName(resultResponse.market).Options().AllPairRatesSupported)
Global.marketsState.Update(resultResponse.market, currenciesCopy);
if (resultResponse.market != toolStripComboBoxMarket.Text)
return;
var dataView = currencies.Select(item => new
{
ticker = item.ticker,
lastPrice = item.lastPrice,
lastPriceUSDT = item.lastPriceUSD,
percentChange = item.percentChange,
volumeBtc = item.volumeBtc,
volumeUSDT = item.volumeUSDT,
lastPriceDisplay = Helper.PriceToStringBtc(item.lastPrice),
lastPriceUSDTDisplay = item.lastPriceUSD.ToString("N3")+" $",
percentChangeDisplay = item.percentChange.ToString("0") + " %",
volumeBtcDisplay = item.volumeBtc.ToString("N2"),
volumeUSDTDisplay = item.volumeUSDT.ToString("N0")+ " $"
}).ToList();
List<GVColumn> columns = new List<GVColumn>()
{
new GVColumn( "ticker", "Currency","string") ,
new GVColumn( "lastPrice", "Price BTC","DisplayField","lastPriceDisplay") ,
new GVColumn( "lastPriceUSDT", "Price $","DisplayField","lastPriceUSDTDisplay") ,
new GVColumn( "percentChange", "Change %","DisplayField","percentChangeDisplay") ,
new GVColumn( "volumeBtc", "Volume BTC","DisplayField","volumeBtcDisplay"),
new GVColumn( "volumeUSDT", "Volume $","DisplayField","volumeUSDTDisplay"),
new GVColumn( "lastPriceDisplay", "Price BTC","string","",false) ,
new GVColumn( "lastPriceUSDTDisplay", "Price $","string","",false) ,
new GVColumn( "percentChangeDisplay", "Change %","string","",false) ,
new GVColumn( "volumeBtcDisplay", "Volume BTC","string","",false),
new GVColumn( "volumeUSDTDisplay", "Volume $","string","",false)
};
if(gvMarkets==null)
gvMarkets = new DataGridViewExWrapper();
gvMarkets.Create(dgridMarkets,dataView, columns, true);
DataGridViewCellStyle styleTicker = new DataGridViewCellStyle { Font = new Font("Tahoma", 9.0F, FontStyle.Bold), ForeColor = Color.Black };
DataGridViewCellStyle stylePrice = new DataGridViewCellStyle { Font = new Font("Tahoma", 9.0F, FontStyle.Regular), ForeColor = Color.Black };
gvMarkets.SetColumnStyle("ticker", styleTicker);
gvMarkets.SetColumnStyle("lastPrice", stylePrice);
gvMarkets.SetColumnStyle("lastPriceUSDT", stylePrice);
//gvMarkets.AutoSizeFillExcept("volumeUSDT");
// gv.RowColorByCondition("orderType", (string s) => { return s == "1"; }, Color.LightPink);
timerMarkets.Start();
}
private void TimerMarkets_Tick(object sender, ElapsedEventArgs e)
{
timerMarkets.Stop();
UpdateMarkets();
}
private void TimerCheckAlerts_Tick(object sender, ElapsedEventArgs e)
{
if(marketsLoaded)
AlertManager.CheckAlerts();
}
private void dgridMarkets_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
gvMarkets.RowColorByCondition("percentChange", s => { return (double)s<0; }, Color.MistyRose);
}
private void listBoxTicker_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void ShowTradeForm(string ticker)
{
FormTrade form = new FormTrade(ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text), ticker);
form.parent = this;
form.Show();
}
private void ShowChartForm()
{
FormChart form = new FormChart(ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text));
form.parent = this;
form.Show();
}
private void toolDropDownTickerBtc_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// ShowTradeForm(e.ClickedItem.Text);
}
private void toolDropDownTickerUsdt_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
//ShowTradeForm(e.ClickedItem.Text);
}
private string ChooseTicker(string filter)
{
FormTicker form = new FormTicker(Global.marketsState.curMarketPairs[toolStripComboBoxMarket.Text], filter);
form.ShowDialog();
return form.selectedTicker;
// MessageBox.Show(form.selectedTicker);
}
private void toolButtonTickerBtc_Click(object sender, EventArgs e)
{
string ticker = ChooseTicker("BTC");
if(ticker!="")
ShowTradeForm(ticker);
}
private void toolButtonTickerUsd_Click(object sender, EventArgs e)
{
string ticker = ChooseTicker("USD");
if (ticker != "")
ShowTradeForm(ticker);
}
private void toolStripButtonChart_Click(object sender, EventArgs e)
{
ShowChartForm();
}
private void toolStripButtonSettings_Click(object sender, EventArgs e)
{
FormSettings form = new FormSettings();
form.parent = this;
form.ShowDialog();
}
private void toolStripButtonBalance_Click(object sender, EventArgs e)
{
FormBalance form = new FormBalance();
form.parent = this;
form.Show();
}
private void toolStripButtonAlerts_Click(object sender, EventArgs e)
{
FormAlertMain form = new FormAlertMain(ExchangeManager.GetMarketByMarketName(toolStripComboBoxMarket.Text));
// form.parent = this;
form.Show();
}
}
}