-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.py
1144 lines (825 loc) · 41.7 KB
/
main.py
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import pandas as pd
import re
import tkinter as tk
from tkinter import ttk
import urllib.request
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.animation as animation
import matplotlib.dates as mdates
from matplotlib.finance import candlestick_ohlc
from matplotlib import pyplot as plt
from matplotlib import style
import matplotlib.ticker as mticker
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg,
NavigationToolbar2TkAgg)
import numpy as np
style.use('ggplot')
f = plt.figure()
TITLE_FONT = ("Helvetica", 11)
NORM_FONT = ("Helvetica", 10)
#TK = tk.Tk()
#tk.Tk.iconbitmap(default='favicon.ico')
#lab = Label(TK, text='Window with transparent icon.')
##DataPace = '1d'
##paneCount = 1
##chartLoad = True
##refreshRate = 2000
##resampleSize = '15Min'
##candleWidth = .008
##exchange = 'BTC-e'
##programName = 'btce'
##topIndicator = "none"
##bottomIndicator = "none"
##EMAs = []
##SMAs = []
DataPace = '1d'
paneCount = 1
chartLoad = True
refreshRate = 2000
resampleSize = '15Min'
candleWidth = .008
exchange = 'BTC-e'
programName = 'btce'
topIndicator = "none"
bottomIndicator = "none"
middleIndicators = "none"
EMAs = []
SMAs = []
darkColor = '#183A54'
lightColor = '#00A3E0'
# OVER 9000!!! lol.
DatCounter = 9000
def popupmsg(msg):
popup = tk.Tk()
def leavemini():
popup.destroy()
popup.wm_title("!")
label = ttk.Label(popup, text=msg, font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(popup, text = "Okay", command = leavemini)
B1.pack()
popup.mainloop()
def addTopIndicator(what):
global topIndicator
global DatCounter
if DataPace == "tick":
popupmsg("Indicators in Tick Data not available, choose 1 minute tf if you want short term.")
if what == "none":
topIndicator = what
DatCounter = 9000
elif what == "rsi":
rsiQ = tk.Tk()
rsiQ.wm_title("Periods?")
label = ttk.Label(rsiQ, text="Choose how many periods you want each RSI calculation to consider.\nThese periods are contingent on your current time settings on the chart. 1 period = 1 OHLC candlestick.", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
e = ttk.Entry(rsiQ)
e.insert(0,14)
e.pack()
e.focus_set()
def callback():
periods = (e.get())
group = []
group.append("rsi")
group.append(periods)
topIndicator = group
DatCounter = 9000
print("set top indicator to",group)
rsiQ.destroy()
b = ttk.Button(rsiQ, text="Submit", width=10, command=callback)
b.pack()
tk.mainloop()
elif what == "macd":
topIndicator = "macd"
DatCounter = 9000
def addMiddleIndicator(what):
global middleIndicators
global DatCounter
if DataPace == "tick":
popupmsg("Indicators in Tick Data not available, choose 1 minute tf if you want short term.")
if what != "none":
if middleIndicators == "none":
if what == "sma":
midIQ = tk.Tk()
midIQ.wm_title("Periods?")
label = ttk.Label(midIQ, text="Choose how many periods you want each SMA calculation to consider.\nThese periods are contingent on your current time settings on the chart.\n1 period = 1 OHLC candlestick.", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
e = ttk.Entry(midIQ)
e.insert(0,10)
e.pack()
e.focus_set()
def callback():
middleIndicators = []
periods = (e.get())
group = []
group.append("sma")
group.append(int(periods))
middleIndicators.append(group)
DatCounter = 9000
print("mid indicator",middleIndicators)
midIQ.destroy()
b = ttk.Button(midIQ, text="Submit", width=10, command=callback)
b.pack()
tk.mainloop()
if what == "ema":
midIQ = tk.Tk()
midIQ.wm_title("Periods?")
label = ttk.Label(midIQ, text="Choose how many periods you want each EMA calculation to consider.\nThese periods are contingent on your current time settings on the chart.\n1 period = 1 OHLC candlestick.", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
e = ttk.Entry(midIQ)
e.insert(0,10)
e.pack()
e.focus_set()
def callback():
middleIndicators = []
periods = (e.get())
group = []
group.append("ema")
group.append(int(periods))
middleIndicators.append(group)
DatCounter = 9000
print("mid indicator",middleIndicators)
midIQ.destroy()
b = ttk.Button(midIQ, text="Submit", width=10, command=callback)
b.pack()
tk.mainloop()
else:
if what == "sma":
midIQ = tk.Tk()
midIQ.wm_title("Periods?")
label = ttk.Label(midIQ, text="Choose how many periods you want each SMA calculation to consider.\nThese periods are contingent on your current time settings on the chart.\n1 period = 1 OHLC candlestick.", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
e = ttk.Entry(midIQ)
e.insert(0,10)
e.pack()
e.focus_set()
def callback():
periods = (e.get())
group = []
group.append("sma")
group.append(int(periods))
middleIndicators.append(group)
DatCounter = 9000
print("mid indicator",middleIndicators)
midIQ.destroy()
b = ttk.Button(midIQ, text="Submit", width=10, command=callback)
b.pack()
tk.mainloop()
if what == "ema":
midIQ = tk.Tk()
midIQ.wm_title("Periods?")
label = ttk.Label(midIQ, text="Choose how many periods you want each EMA calculation to consider.\nThese periods are contingent on your current time settings on the chart.\n1 period = 1 OHLC candlestick.", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
e = ttk.Entry(midIQ)
e.insert(0,10)
e.pack()
e.focus_set()
def callback():
periods = (e.get())
group = []
group.append("ema")
group.append(int(periods))
middleIndicators.append(group)
DatCounter = 9000
print("mid indicator",middleIndicators)
midIQ.destroy()
b = ttk.Button(midIQ, text="Submit", width=10, command=callback)
b.pack()
tk.mainloop()
else:
middleIndicators = "none"
def addBottomIndicator(what):
global bottomIndicator
global DatCounter
if DataPace == "tick":
popupmsg("Indicators in Tick Data not available, choose 1 minute tf if you want short term.")
if what == "none":
bottomIndicator = what
DatCounter = 9000
elif what == "rsi":
rsiQ = tk.Tk()
rsiQ.wm_title("Periods?")
label = ttk.Label(rsiQ, text="Choose how many periods you want each RSI calculation to consider.\nThese periods are contingent on your current time settings on the chart. 1 period = 1 OHLC candlestick.", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
e = ttk.Entry(rsiQ)
e.insert(0,14)
e.pack()
e.focus_set()
def callback():
periods = (e.get())
group = []
group.append("rsi")
group.append(periods)
bottomIndicator = group
DatCounter = 9000
print("set top indicator to",group)
rsiQ.destroy()
b = ttk.Button(rsiQ, text="Submit", width=10, command=callback)
b.pack()
tk.mainloop()
elif what == "macd":
bottomIndicator = "macd"
DatCounter = 9000
def tutorial():
def leavemini(what):
what.destroy()
def page2():
leavemini(tut)
tut2 = tk.Tk()
def leavemini2(what):
what.destroy()
def page3():
leavemini2(tut2)
tut3 = tk.Tk()
tut3.wm_title("part 3!")
label = ttk.Label(tut3, text="Part 3", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(tut3, text = "Done!", command = tut3.destroy)
B1.pack()
tut3.mainloop()
tut2.wm_title("part 2!")
label = ttk.Label(tut2, text="Part 2", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(tut2, text = "next!", command = page3)
B1.pack()
tut.mainloop()
tut = tk.Tk()
tut.wm_title("Tutorial")
label = ttk.Label(tut, text="What do you need help with?", font=NORM_FONT)
label.pack(side="top", fill="x", pady=10)
B1 = ttk.Button(tut, text = "Overview of the application", command = page2)
B1.pack()
B2 = ttk.Button(tut, text = "How do I trade here?", command=lambda: popupmsg('Not supported just yet!'))
B2.pack()
B3 = ttk.Button(tut, text = "Indicator questions/help", command=lambda: popupmsg('Not supported just yet!'))
B3.pack()
tut.mainloop()
def changeSampleSize(size,width):
global resampleSize
global DatCounter
global candleWidth
if DataPace == '7d' and size == '1Min':
popupmsg("Too much data chosen, choose a smaller Data Time Frame or higher OHLC Interval!")
if DataPace == 'tick':
popupmsg("You are currently viewing tick data, not OHLC. Choose a larger Data Time Frame!")
else:
resampleSize = size
DatCounter = 9000
candleWidth = width
def changeExchange(toWhat,pn):
global exchange
global DatCounter
global programName
exchange = toWhat
programName = pn
DatCounter = 9000
def changeTimeFrame(tf):
global DataPace
global DatCounter
if tf == '7d' and resampleSize == '1Min':
popupmsg("Too much data chosen, choose a smaller data time frame or higher OHLC Interval!")
else:
DataPace = tf
DatCounter = 9000
def loadChart(run):
global chartLoad
if run == 'start':
chartLoad = True
elif run == 'stop':
chartLoad = False
# ... I know... This is my work around for cx_freeze saying quit is not defined.
def quit():
quit()
def animate(i):
global refreshRate
global DatCounter
def moving_average(x, n, type='simple'):
x = np.asarray(x)
if type=='simple':
weights = np.ones(n)
else:
weights = np.exp(np.linspace(-1., 0., n))
weights /= weights.sum()
a = np.convolve(x, weights, mode='full')[:len(x)]
a[:n] = a[n]
return a
def computeMACD(x, slow=26, fast=12,location="bottom"):
"""
compute the MACD (Moving Average Convergence/Divergence) using a fast and slow exponential moving avg'
return value is emaslow, emafast, macd which are len(x) arrays
"""
values = {'key': 1,'prices':x}
url = "http://seaofbtc.com/api/indicator/macd"
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
resp = urllib.request.urlopen(req)
respData = resp.read()
newData = str(respData).replace("b","").replace('[','').replace(']','').replace("'",'')
#print(newData)
split = newData.split('::')
macd = split[0]
ema9 = split[1]
hist = split[2]
macd = macd.split(", ")
ema9 = ema9.split(", ")
hist = hist.split(", ")
try:
macd = [float(i) for i in macd]
except Exception as e:
print(str(e)+" macd")
try:
ema9 = [float(i) for i in ema9]
except Exception as e:
print(str(e)+" ema9")
try:
hist = [float(i) for i in hist]
except Exception as e:
print(str(e)+" hist")
print("call!!!")
if location == "top":
try:
a0.plot(OHLC['MPLDates'][fast:], macd[fast:], color=darkColor, lw=2)
a0.plot(OHLC['MPLDates'][fast:], ema9[fast:], color=lightColor, lw=1)
a0.fill_between(OHLC['MPLDates'][fast:], hist[fast:], 0, alpha=0.5, facecolor=darkColor, edgecolor=darkColor)
datLabel = "MACD"
a0.set_ylabel(datLabel)
except Exception as e:
print(str(e))
topIndicator = "none"
elif location == "bottom":
try:
a3.plot(OHLC['MPLDates'][fast:], macd[fast:], color=darkColor, lw=2)
a3.plot(OHLC['MPLDates'][fast:], ema9[fast:], color=lightColor, lw=1)
a3.fill_between(OHLC['MPLDates'][fast:], hist[fast:], 0, alpha=0.5, facecolor=darkColor, edgecolor=darkColor)
datLabel = "MACD"
a3.set_ylabel(datLabel)
except Exception as e:
print(str(e))
bottomIndicator = "none"
def rsiIndicator(priceData,location="top"):
if location == "top":
values = {'key': 1,'prices':priceData,'periods':topIndicator[1]}
elif location == "bottom":
values = {'key': 1,'prices':priceData,'periods':bottomIndicator[1]}
url = "http://seaofbtc.com/api/indicator/rsi"
data = urllib.parse.urlencode(values)
data = data.encode('utf-8')
req = urllib.request.Request(url, data)
resp = urllib.request.urlopen(req)
respData = resp.read()
newData = str(respData).replace("b","").replace('[','').replace(']','').replace("'",'')
priceList = newData.split(', ')
rsiData = [float(i) for i in priceList]
print("call!!!")
if location == "top":
a0.plot_date(OHLC['MPLDates'], rsiData,lightColor, label ="RSI")
datLabel = "RSI("+str(topIndicator[1])+")"
a0.set_ylabel(datLabel)
elif location == "bottom":
a3.plot_date(OHLC['MPLDates'], rsiData,lightColor, label ="RSI")
datLabel = "RSI("+str(bottomIndicator[1])+")"
a3.set_ylabel(datLabel)
print(exchange)
if chartLoad:
if paneCount == 1:
if DataPace == 'tick':
try:
if exchange == 'BTC-e':
a = plt.subplot2grid((6,4), (0,0), rowspan=5, colspan=4)
a2 = plt.subplot2grid((6,4), (5,0), rowspan=1, colspan=4, sharex = a)
dataLink = 'https://btc-e.com/api/3/trades/btc_usd?limit=2000'
data = urllib.request.urlopen(dataLink)
data = data.readall().decode('utf-8')
data = json.loads(data)
data = data["btc_usd"]
data = pd.DataFrame(data)
data["datestamp"] = np.array(data['timestamp']).astype('datetime64[s]')
allDates = data["datestamp"].tolist()
buys = data[(data['type']=='bid')]
#buys["datestamp"] = np.array(buys['timestamp']).astype('datetime64[s]')
buyDates = (buys["datestamp"]).tolist()
sells = data[(data['type']=='ask')]
#sells["datestamp"] = np.array(sells['timestamp']).astype('datetime64[s]')
sellDates = (sells["datestamp"]).tolist()
volume = data["amount"]
a.clear()
a.plot_date(buyDates,buys["price"], '#00A3E0', label ="buys")
a.plot_date(sellDates,sells["price"], '#183A54', label = "sells")
a2.fill_between(allDates,0, volume, facecolor='#183A54')
a.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, borderaxespad=0.)
a.xaxis.set_major_locator(mticker.MaxNLocator(5))
a.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
plt.setp(a.get_xticklabels(), visible=False)
title = exchange+' Tick Data\nLast Price: '+str(data["price"][0])
a.set_title(title)
priceData = data["price"].apply(float).tolist()
if exchange == 'Bitstamp':
a = plt.subplot2grid((6,4), (0,0), rowspan=5, colspan=4)
a2 = plt.subplot2grid((6,4), (5,0), rowspan=1, colspan=4, sharex = a)
dataLink = 'https://www.bitstamp.net/api/transactions/'
data = urllib.request.urlopen(dataLink)
data = data.readall().decode('utf-8')
data = json.loads(data)
data = pd.DataFrame(data)
data["datestamp"] = np.array(data['date'].apply(int)).astype('datetime64[s]')
datestamps = data["datestamp"].tolist()
volume = data["amount"].apply(float).tolist()
a.clear()
a.plot_date(datestamps,data["price"], '#183A54')
a2.fill_between(datestamps,0, volume, facecolor='#183A54')
a.xaxis.set_major_locator(mticker.MaxNLocator(5))
a.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
plt.setp(a.get_xticklabels(), visible=False)
title = exchange+' Tick Data\nLast Price: '+str(data["price"][0])
a.set_title(title)
priceData = data["price"].apply(float).tolist()
if exchange == 'Bitfinex':
a = plt.subplot2grid((6,4), (0,0), rowspan=5, colspan=4)
a2 = plt.subplot2grid((6,4), (5,0), rowspan=1, colspan=4, sharex = a)
dataLink = 'https://api.bitfinex.com/v1/trades/btcusd?limit=2000'
data = urllib.request.urlopen(dataLink)
data = data.readall().decode('utf-8')
data = json.loads(data)
data = pd.DataFrame(data)
volume = data["amount"].apply(float).tolist()
#print(data)
data["datestamp"] = np.array(data['timestamp']).astype('datetime64[s]')
allDates = data["datestamp"].tolist()
buys = data[(data['type']=='buy')]
#buys["datestamp"] = np.array(buys['timestamp']).astype('datetime64[s]')
buyDates = (buys["datestamp"]).tolist()
sells = data[(data['type']=='sell')]
#sells["datestamp"] = np.array(sells['timestamp']).astype('datetime64[s]')
sellDates = (sells["datestamp"]).tolist()
a.clear()
a.plot_date(buyDates,buys["price"], lightColor, label ="buys")
a.plot_date(sellDates,sells["price"], darkColor, label = "sells")
a2.fill_between(allDates,0, volume, facecolor='#183A54')
a.xaxis.set_major_locator(mticker.MaxNLocator(5))
a.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
plt.setp(a.get_xticklabels(), visible=False)
a.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
ncol=2, borderaxespad=0.)
title = exchange+' Tick Data\nLast Price: '+str(data["price"][0])
a.set_title(title)
priceData = data["price"].apply(float).tolist()
if exchange == 'Huobi':
try:
a = plt.subplot2grid((6,4), (0,0), rowspan=6, colspan=4)
data = urllib.request.urlopen('http://seaofbtc.com/api/basic/price?key=1&tf=1d&exchange='+programName).read()
data = str(data).replace('b','').replace("'",'')
data = json.loads(data)
dateStamp = np.array(data[0]).astype('datetime64[s]')
dateStamp = dateStamp.tolist()
print('here')
df = pd.DataFrame({'Datetime':dateStamp})
df['Price'] = data[1]
df['Volume'] = data[2]
df['Symbol'] = "BTCUSD"
df['MPLDate'] = df['Datetime'].apply(lambda date: mdates.date2num(date.to_pydatetime()))
df = df.set_index('Datetime')
lastPrice = df['Price'][-1]
a.plot_date(df['MPLDate'][-4500:],df['Price'][-4500:], lightColor, label ="price")
a.xaxis.set_major_locator(mticker.MaxNLocator(5))
a.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
title = exchange+' Tick Data\nLast Price: '+str(lastPrice)
a.set_title(title)
priceData = df['Price'].apply(float).tolist()
except Exception as e:
print(str(e))
except:
DatCounter = 9000
###### BEGIN NON-TICK GRAPHING#################################################################################
else:
if DatCounter > 12:
try:
if exchange == 'Huobi':
if topIndicator != "none":
a = plt.subplot2grid((6,4), (1,0), rowspan=5, colspan=4)
a0 = plt.subplot2grid((6,4), (0,0), sharex=a, rowspan=1, colspan=4)
else:
a = plt.subplot2grid((6,4), (0,0), rowspan=6, colspan=4)
else:
if topIndicator != "none" and bottomIndicator != "none":
# actual price chart.
a = plt.subplot2grid((6,4), (1,0), rowspan=3, colspan=4)
# volume!
a2 = plt.subplot2grid((6,4), (4,0), sharex=a, rowspan=1, colspan=4)
# top indicator
a0 = plt.subplot2grid((6,4), (0,0), sharex=a, rowspan=1, colspan=4)
# bottom indicator
a3 = plt.subplot2grid((6,4), (5,0), sharex=a, rowspan=1, colspan=4)
elif topIndicator != "none":
a = plt.subplot2grid((6,4), (1,0), rowspan=4, colspan=4)
a2 = plt.subplot2grid((6,4), (5,0), sharex=a, rowspan=1, colspan=4)
a0 = plt.subplot2grid((6,4), (0,0), sharex=a, rowspan=1, colspan=4)
elif bottomIndicator != "none":
a = plt.subplot2grid((6,4), (0,0), rowspan=4, colspan=4)
a2 = plt.subplot2grid((6,4), (4,0), sharex=a, rowspan=1, colspan=4)
#a0 = plt.subplot2grid((6,4), (0,0), sharex=a, rowspan=1, colspan=4)
a3 = plt.subplot2grid((6,4), (5,0), sharex=a, rowspan=1, colspan=4)
else:
a = plt.subplot2grid((6,4), (0,0), rowspan=5, colspan=4)
a2 = plt.subplot2grid((6,4), (5,0), sharex=a, rowspan=1, colspan=4)
print('http://seaofbtc.com/api/basic/price?key=1&tf='+DataPace+'&exchange='+programName)
data = urllib.request.urlopen('http://seaofbtc.com/api/basic/price?key=1&tf='+DataPace+'&exchange='+programName).read()
data = str(data).replace('b','').replace("'",'')
data = json.loads(data)
dateStamp = np.array(data[0]).astype('datetime64[s]')
dateStamp = dateStamp.tolist()
df = pd.DataFrame({'Datetime':dateStamp})
df['Price'] = data[1]
df['Volume'] = data[2]
df['Symbol'] = "BTCUSD"
df['MPLDate'] = df['Datetime'].apply(lambda date: mdates.date2num(date.to_pydatetime()))
df = df.set_index('Datetime')
OHLC = df['Price'].resample(resampleSize, how='ohlc')
OHLC = OHLC.dropna()
volumeData = df['Volume'].resample(resampleSize, how={'volume':'sum'})
OHLC['dateCopy'] = OHLC.index
OHLC['MPLDates'] = OHLC['dateCopy'].apply(lambda date: mdates.date2num(date.to_pydatetime()))
del OHLC['dateCopy']
volumeData['dateCopy'] = volumeData.index
volumeData['MPLDates'] = volumeData['dateCopy'].apply(lambda date: mdates.date2num(date.to_pydatetime()))
del volumeData['dateCopy']
priceData = OHLC['close'].apply(float).tolist()
a.clear()
if middleIndicators != "none":
for eachMA in middleIndicators:
ewma = pd.stats.moments.ewma
#print("type:",eachMA[0],"periods:",eachMA[1])
if eachMA[0] == "sma":
sma = pd.rolling_mean(OHLC["close"],eachMA[1])
label = str(eachMA[1])+" SMA"
a.plot(OHLC['MPLDates'],sma, label=label)
if eachMA[0] == "ema":
ewma = pd.stats.moments.ewma
label = str(eachMA[1])+" EMA"
a.plot(OHLC['MPLDates'],ewma(OHLC["close"], eachMA[1]), label=label)
#a.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
# ncol=2, borderaxespad=0.)
a.legend(loc=0)
if topIndicator[0] == "rsi":
rsiIndicator(priceData,"top")
elif topIndicator == "macd":
try:
computeMACD(priceData,location="top")
except:
print("failed macd")
if bottomIndicator[0] == "rsi":
rsiIndicator(priceData,"bottom")
elif bottomIndicator == "macd":
try:
computeMACD(priceData,location="bottom")
except:
print("failed macd")
csticks = candlestick_ohlc(a, OHLC[['MPLDates', 'open', 'high', 'low', 'close']].values, width=candleWidth, colorup=lightColor, colordown=darkColor)
a.set_ylabel("price")
if exchange != 'Huobi':
a2.fill_between(volumeData['MPLDates'],0, volumeData['volume'], facecolor='#183A54')#, alpha=.4)
a2.set_ylabel("volume")
a.xaxis.set_major_locator(mticker.MaxNLocator(3))
a.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
plt.setp(a.get_xticklabels(), visible=False)
if topIndicator != "none":
plt.setp(a0.get_xticklabels(), visible=False)
if bottomIndicator != "none":
plt.setp(a2.get_xticklabels(), visible=False)
x = (len(OHLC['close']))-1
if DataPace == '1d':
title = exchange+' 1 Day Data with '+resampleSize+' Bars\nLast Price: '+str(OHLC['close'][x])
if DataPace == '3d':
title = exchange+' 3 Day Data with '+resampleSize+' Bars\nLast Price: '+str(OHLC['close'][x])
if DataPace == '7d':
title = exchange+' 7 Day Data with '+resampleSize+' Bars\nLast Price: '+str(OHLC['close'][x])
if topIndicator != "none":
a0.set_title(title)
else:
a.set_title(title)
print('NewGraph!')
DatCounter = 0
except Exception as e:
print(str(e),"main animate non tick")
DatCounter = 9000
else:
DatCounter += 1
class SeaofBTCapp(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
#menubar = tk.Menu(tk.Tk)
style = ttk.Style()
style.layout("Button", [
("Menubutton.background", None),
("Menubutton.foreground", None),
("Menubutton.button", {"children":
[("Menubutton.focus", {"children":
[("Menubutton.padding", {"children":
[("Menubutton.label", {"side": "left", "expand": 1})]
})]
})]
}),
])
style.layout("TMenubutton", [
("Menubutton.background", None),
("Menubutton.button", {"children":
[("Menubutton.focus", {"children":
[("Menubutton.padding", {"children":
[("Menubutton.label", {"side": "left", "expand": 1})]
})]
})]
}),
])
# the container is where we'll stack a bunch of frames
# on top of each other, then the one we want visible
# will be raised above the others
container = tk.Frame(self, width=1280, height=720)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
menubar = tk.Menu(container)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="Save settings", command=lambda: popupmsg('Not supported just yet!'))
filemenu.add_separator()
filemenu.add_command(label="Exit", command=quit)
menubar.add_cascade(label="File", menu=filemenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Tutorial", command=tutorial)
menubar.add_cascade(label="Help", menu=helpmenu)
tk.Tk.config(self, menu=menubar)
#editmenu.add_separator()
#menubar.add_cascade(label="Edit", menu=filemenu)
#container.title('test')
self.frames = {}
for F in (StartPage, dashboard):
frame = F(container, self)
self.frames[F] = frame
# put all of the pages in the same location;
# the one on the top of the stacking order
# will be the one that is visible.
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
try:
tk.Tk.iconbitmap(self,default='clienticon.ico')
tk.Tk.wm_title(self, "Sea of BTC Client")
except Exception as e:
print(str(e))
def show_frame(self, c):
'''Show a frame for the given class'''
frame = self.frames[c]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="""The Sea of BTC trading client is a client intended to help traders
interact with their exchanges. We do this by allowing you to enter
your API keys into the program. We, as in Sea of BTC, never
see your API information. The program may save them locally, however,
to make things easier on you. Keep in mind that it is a fantastic idea
to enable 'IP Whitelisting' if your exchange supports it, and only allow
trading via your specific IP address. On most exchanges, even if someone
was to acquire your API key, withdrawals are not possible. Some still
give the option, so make sure this is turned OFF if your exchange allows it.
Sea of BTC makes no promise of warranty, satisfaction, performance, or
anything else. Understand that your use of this client is completely
at your own risk.""", font=TITLE_FONT)
label.pack(side="top", fill="x", pady=10)
button1 = ttk.Button(self, text="Agree",
command=lambda: controller.show_frame(dashboard))
button2 = ttk.Button(self, text="Disagree",
command=quit)
button1.pack()
button2.pack()
### Deprecated, lost the point of needing this page leaving mostly for future reference.
##class HomePage(tk.Frame):
## def __init__(self, parent, controller):
## tk.Frame.__init__(self, parent)
##
## label = tk.Label(self, text="Sea of BTC Client", font=TITLE_FONT)
## label.pack(side="top", fill="x", pady=10)
## label = tk.Label(self, text="Choose your Exchange", font=NORM_FONT)
## label.pack(side="top", fill="x", pady=10)
##
##
## button = ttk.Button(self, text="BTC-e",
## command=lambda: controller.show_frame(BTCe_main))
## button.pack(pady=10)
##
## button = ttk.Button(self, text="*NOT ACTIVE* Bitstamp",
## command=lambda: controller.show_frame(StartPage))
## button.pack(pady=10)
##
## button = ttk.Button(self, text="*NOT ACTIVE* Bitfinex",