-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConnectToExchange.py
1322 lines (1284 loc) · 71.9 KB
/
ConnectToExchange.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
# This program is for connecting to and retrieving information from cryptocurrency exchanges using the CCXT library
import os
import sys
import traceback
import pickle
import math
import pathlib
import pandas as pd
from datetime import datetime
# AudioPlayer.py uses the playsound library to create audio alerts for errors or other significant events
from AudioPlayer import AudioPlayer
# GetCurrentTime.py has functions for easily acquiring the current date or time separately or together, and other time functions
from GetCurrentTime import GetCurrentTime
# CustomEncryptor.py is used to access encrypted API keys
from CustomEncryptor import CustomEncryptor
# CCXT is the fantastic library that makes the interaction with cryptocurrency exchanges possible
import ccxt
# This function will create the ConnectToExchange class in a non-local scope, making it more secure
def main():
CTE = ConnectToExchange()
CTE.main_loop()
del CTE
class ConnectToExchange:
def __init__(self):
# Toggle silent_mode to True to prevent all print statements that aren't error messages
self.silent_mode = False
self.GCT = GetCurrentTime()
self.AP = AudioPlayer()
self.CE = CustomEncryptor.CustomEncryptor()
# The improved_columns are simply the normal columns of an OHLCV, but with capitalization and a 'Time' column added. This variable helps with CSV exports
self.improved_columns = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume', 'Time']
# The timeframes_All dict contains the most common time durations that cryptocurrency exchanges use in graphs of price over time
self.timeframes_All = {'1m': '1m', '3m': '3m', '5m': '5m', '15m': '15m', '30m': '30m', '1h': '1h', '2h': '2h', '4h': '4h', '6h': '6h', '8h': '8h', '12h': '12h', '1d': '1d', '3d': '3d', '1w': '1w', '1M': '1M'}
# The activity logs are a history of every connection or other exchange-related action that has been made
# The Master Activity Log has every action from all time
# The Daily Activity Logs are broken up into individual days
self.activityLog_Current = {}
self.activity_log_location = 'Activity Logs/'
if not(os.path.isdir('ConnectToExchange/Activity Logs')):
os.makedirs('ConnectToExchange/Activity Logs')
# Information about any errors that occur are stored in error_log and exported to CSV
self.error_log = []
# This dict stores the API information for each account & exchange
# Fill this in with the exchanges & account names you use
# Enter your favorite exchange + account pair as 'Default' if you want the connect() function to connect with no inputs
self.exchangeAccounts = {'Coinbase': {'apiKey Length': 32, \
'secret Length': 88, \
'Main': {'apiKey': '', 'secret': ''}, \
'Long': {'apiKey': '', 'secret': ''}, \
'Short': {'apiKey': '', 'secret': ''}}, \
'Binance': {'apiKey Length': 64, \
'secret Length': 64, \
'Main': {'apiKey': '', 'secret': ''}}, \
'Kraken': {'apiKey Length': 36, \
'secret Length': 91, \
'Main': {'apiKey': '', 'secret': ''}, \
'Long 50x': {'apiKey': '', 'secret': ''}, \
'Short 50x': {'apiKey': '', 'secret': ''}, \
'Long 50x Quick': {'apiKey': '', 'secret': ''}, \
'Short 50x Quick': {'apiKey': '', 'secret': ''}, \
'Monty': {'apiKey': '', 'secret': ''}}, \
'Default': 'Kraken Main', \
'Default Exchange': 'Kraken', \
'Default Account': 'Main', \
'Default Type': 'spot'}
self.currentConnectionDetails = {'Exchange Name': '',
'Account Name': '', \
'Time of Acccess': str(datetime.now())}
# availableSymbols contains lists of the cryptocurrencies tradeable on a given exchange
# Whenever connect() is used, availableSymbols will be updated to have the exchange name as a new key and a list of the available symbols as the value
self.availableSymbols = {}
# EMA_smoother is an int that is used in calculating exponential moving averages. The most common value to use is 2. Feel free to change it
self.EMA_smoother = 2
self.balances = None
self.exchange = None
def main_loop(self):
print('CTE : main_loop initiated')
exchange = self.connect()
# I run getOHLCVs() on the 1 minute timeframe in the main loop to build up OHLCV data on my own machine via updateMasterOHLCVs()
# This is optional, but useful because it is usually difficult to get minute-to-minute data through APIs unless the data is very recent
self.getOHLCVs('default', 'BTC', '1m', 1999, {}, 'no')
# This is the primary function of the class. It creates the connections to cryptocurrency exchanges using API keys
def connect(self, *args):
connected = False
while not(connected):
# This is for the case where the user calls connect() with no inputs
if len(args) == 0:
try:
exchange_name = self.exchangeAccounts['Default Exchange']
account_name = self.exchangeAccounts['Default Account']
except:
exchange_name = input('To which exchange would you like to connect?\n Exchange Name: ')
account_name = input('Which account on that exchange would you like to use?\n Account Name: ')
elif len(args) == 1:
# This is for the case where the user inputs a dictionary containing the exchange & account names
if type(args[0]) == dict:
exchange_name = args[0]['Exchange Name']
account_name = args[0]['Account Name']
# This else is for the case where the user enters just one string
else:
if args[0].lower() == 'default':
args = [self.exchangeAccounts['Default']]
# This if is for the case where the string is both the exchange name and account name separated by a space
if ' ' in args[0]:
split_name = args[0].split(' ')
exchange_name = split_name[0]
split_account_name = split_name[1:len(split_name)]
account_name = ''
for word in split_account_name:
account_name += word
if not(word == split_account_name[len(split_account_name) - 1]):
account_name += ' '
# This else assumes the 1 string entered is the exchange name, and assigns the account_name to be the default account
else:
exchange_name = args[0]
account_name = self.exchangeAccounts['Default Account']
# This is for the case where the user calls connect() with 2 inputs - the exchange name followed by the account name
elif len(args) == 2:
exchange_name = args[0]
account_name = args[1]
# This swaps in the default exchange or account if 'default' was used as an input
if exchange_name.lower() == 'default':
exchange_name = self.exchangeAccounts['Default Exchange']
if account_name.lower() == 'default':
account_name = self.exchangeAccounts['Default Account']
# The exchange name & account have been chosen. Now the appropriate API information is retrieved
if self.exchangeAccounts[exchange_name][account_name]['apiKey'] == '':
self.fetch_API_key(exchange_name, account_name)
if len(self.exchangeAccounts[exchange_name][account_name]['apiKey']) > 100:
if self.CE:
try:
key = self.CE.decrypt(self.exchangeAccounts[exchange_name][account_name]['apiKey'])[0:36]
secret = self.CE.decrypt(self.exchangeAccounts[exchange_name][account_name]['secret'])[0:91]
except:
print('CTE : ERROR! Failed to decrypt API key file.')
connected = False
continue
else:
key = self.exchangeAccounts[exchange_name][account_name]['apiKey']
secret = self.exchangeAccounts[exchange_name][account_name]['secret']
else:
key = self.exchangeAccounts[exchange_name][account_name]['apiKey']
secret = self.exchangeAccounts[exchange_name][account_name]['secret']
try:
# The API key has been retrieved. Now the connection to the exchange will be made
# Connects to Binance
if exchange_name == 'Binance':
self.exchange = ccxt.binance({'apiKey': key, \
'secret': secret, \
'timeout': 30000, \
'enableRateLimit': True, \
'options': {'adjustForTimeDifference': True}})
# Connects to Kraken
elif exchange_name == 'Kraken':
self.exchange = ccxt.kraken({'apiKey': key, \
'secret': secret, \
'timeout': 30000, \
'enableRateLimit': True, \
'options': {'adjustForTimeDifference': True, \
'defaultType': 'spot', \
'postOnly': True}})
except:
print('CTE : ERROR! Failed to connect to ' + exchange_name)
connected = False
connected = True
del key, secret
# Now that the exchange has been connected to, variables are assigned and the ActivityLog is updated
self.currentConnectionDetails['Exchange Name'] = exchange_name
self.currentConnectionDetails['Account Name'] = account_name
self.exchange_name = exchange_name
self.account_name = account_name
date = self.GCT.getDateString()
timestamp = self.GCT.getTimeStamp()
time = self.GCT.getTimeString()
self.availableSymbols[exchange_name] = list(self.exchange.loadMarkets())
# If balances have already been fetched, they may refer to a different exchange or account. This updates self.balances to prevent confusion
if self.balances:
self.balances = self.getBalances()
# This section creates new entries in the Master Activity Log and Daily Activity Logs if they have not been used yet
try:
self.activityLog_Master = pickle.load(open(self.activity_log_location + exchange_name + '_ActivityLog_Master.pickle', 'rb'))
if not(self.silent_mode):
print('\nCTE : ' + exchange_name + ' ' + account_name + ' Master Activity Log loaded!')
largestTimestamp = max(self.activityLog_Master)
if not(self.silent_mode):
print('CTE : The most recent activity was on ' + self.activityLog_Master[largestTimestamp]['Date'] + '!')
except:
self.activityLog_Master = {}
if not(self.silent_mode):
print('CTE : No past Activity Log found!')
try:
self.activityLog_Daily = pickle.load(open(self.activity_log_location + exchange_name + '_ActivityLog_Daily_' + date + '.pickle', 'rb'))
if not(self.silent_mode):
print('CTE : Daily Activity Log loaded!')
except:
self.activityLog_Daily = {'Date': date, \
'Activity Log': {}}
if not(self.silent_mode):
print('CTE : No Daily Activity Log found for today!')
# This section creates new activity log entries and saves them
self.currentConnectionDetails['Time of Access'] = str(datetime.now())
self.activityLog_Current[timestamp] = {'Activity': 'Connected', \
'Date': date, \
'Time': time}
self.activityLog_Master.update(self.activityLog_Current)
self.activityLog_Daily['Activity Log'][time] = {'Activity': 'Connected', \
'Time': time, \
'Timestamp': timestamp}
pickle.dump(self.activityLog_Master, open(self.activity_log_location + exchange_name + '_ActivityLog_Master.pickle', 'wb'))
pickle.dump(self.activityLog_Daily, open(self.activity_log_location + exchange_name + '_ActivityLog_Daily_' + date + '.pickle', 'wb'))
daily_log_dataframe = pd.DataFrame(self.activityLog_Daily['Activity Log'])
daily_log_dataframe = daily_log_dataframe.T
daily_log_dataframe.to_csv(self.activity_log_location + exchange_name + '_ActivityLog_Daily_' + date + '.csv')
master_log_dataframe = pd.DataFrame(self.activityLog_Master)
master_log_dataframe = master_log_dataframe.T
master_log_dataframe.to_csv(self.activity_log_location + exchange_name + '_ActivityLog_Master.csv')
return(self.exchange)
# This is the primary function of the class. It creates the connections to cryptocurrency exchanges using API keys
def connect_NEW(self, exchange_name=None, account_name=None):
if not(exchange_name) or str(exchange_name).lower() == 'default':
exchange_name = self.exchangeAccounts['Default Exchange']
if not(account_name) or str(account_name).lower() == 'default':
account_name = self.exchangeAccounts['Default Account']
connected = False
# The API information matching exchange_name and account_name is retrieved and read
if self.exchangeAccounts[exchange_name][account_name]['apiKey'] == '':
self.fetch_API_key(exchange_name, account_name)
if len(self.exchangeAccounts[exchange_name][account_name]['apiKey']) > 100:
try:
key = self.CE.decrypt(self.exchangeAccounts[exchange_name][account_name]['apiKey'])[0:36]
secret = self.CE.decrypt(self.exchangeAccounts[exchange_name][account_name]['secret'])[0:91]
except Exception as error:
self.inCaseOfError(**{'error': error, \
'description': 'reading API key file', \
'program': 'CTE', \
'line_number': traceback.format_exc().split('line ')[1].split(',')[0]})
else:
key = self.exchangeAccounts[exchange_name][account_name]['apiKey']
secret = self.exchangeAccounts[exchange_name][account_name]['secret']
try:
# The API connection to the exchange is made
self.exchange = ccxt.kraken({'apiKey': key, \
'secret': secret, \
'timeout': 30000, \
'enableRateLimit': True, \
'options': {'adjustForTimeDifference': True, \
'defaultType': self.exchangeAccounts['Default Type'], \
'postOnly': True}})
connected = True
except Exception as error:
self.inCaseOfError(**{'error': error, \
'description': 'connecting to the exchange', \
'program': 'CTE', \
'line_number': traceback.format_exc().split('line ')[1].split(',')[0]})
connected = False
del key, secret
if not(connected):
print('CTE : ERROR! Failed to connect to exchange.')
return(None)
else:
# Variables are assigned and the ActivityLog is updated
self.currentConnectionDetails['Exchange Name'] = exchange_name
self.currentConnectionDetails['Account Name'] = account_name
self.exchange_name = exchange_name
self.account_name = account_name
date = self.GCT.getDateString()
timestamp = self.GCT.getTimeStamp()
time = self.GCT.getTimeString()
self.availableSymbols[exchange_name] = list(self.exchange.loadMarkets())
# Balances are automatically updated if they have been previously fetched
if self.balances:
self.balances = self.getBalances()
# New entries are added to the Master Activity Log and Daily Activity Logs
try:
self.activityLog_Master = pickle.load(open(self.activity_log_location + exchange_name + '_ActivityLog_Master.pickle', 'rb'))
if not(self.silent_mode):
print('\nCTE : ' + exchange_name + ' ' + account_name + ' Master Activity Log loaded!')
largestTimestamp = max(self.activityLog_Master)
if not(self.silent_mode):
print('CTE : The most recent activity was on ' + self.activityLog_Master[largestTimestamp]['Date'] + '!')
except FileNotFoundError:
self.activityLog_Master = {}
if not(self.silent_mode):
print('CTE : No past Activity Log found!')
try:
self.activityLog_Daily = pickle.load(open(self.activity_log_location + exchange_name + '_ActivityLog_Daily_' + date + '.pickle', 'rb'))
if not(self.silent_mode):
print('CTE : Daily Activity Log loaded!')
except FileNotFoundError:
self.activityLog_Daily = {'Date': date, \
'Activity Log': {}}
if not(self.silent_mode):
print('CTE : No Daily Activity Log found for today!')
# A new activity log entry is created and saved
self.currentConnectionDetails['Time of Access'] = str(datetime.now())
self.activityLog_Current[timestamp] = {'Activity': 'Connected', \
'Date': date, \
'Time': time}
self.activityLog_Master.update(self.activityLog_Current)
self.activityLog_Daily['Activity Log'][time] = {'Activity': 'Connected', \
'Time': time, \
'Timestamp': timestamp}
pickle.dump(self.activityLog_Master, open(self.activity_log_location + exchange_name + '_ActivityLog_Master.pickle', 'wb'))
pickle.dump(self.activityLog_Daily, open(self.activity_log_location + exchange_name + '_ActivityLog_Daily_' + date + '.pickle', 'wb'))
daily_log_dataframe = pd.DataFrame(self.activityLog_Daily['Activity Log'])
daily_log_dataframe = daily_log_dataframe.T
daily_log_dataframe.to_csv(self.activity_log_location + exchange_name + '_ActivityLog_Daily_' + date + '.csv')
master_log_dataframe = pd.DataFrame(self.activityLog_Master)
master_log_dataframe = master_log_dataframe.T
master_log_dataframe.to_csv(self.activity_log_location + exchange_name + '_ActivityLog_Master.csv')
return(self.exchange)
# This function is for retrieving API keys from a .txt file
# Each file should have the API information for one account, end in '_API.txt', and have the API key on the first line and the API secret on the second line
def fetch_API_key(self, exchange_name, account_name):
file_name = exchange_name + '_' + account_name + '_API.txt'
API_key_file = False
try:
API_key_file = open(file_name, 'r')
except:
drive_list = ['D', 'E', 'F', 'G', 'H', 'I']
for drive in drive_list:
try:
API_location = drive + ':/API Keys/'
API_key_file = open(API_location + file_name, 'r')
except:
del API_location
if API_key_file:
line_index = 0
for line in API_key_file:
if line_index == 0:
self.exchangeAccounts[exchange_name][account_name]['apiKey'] = line.split('\n')[0]
elif line_index == 1:
self.exchangeAccounts[exchange_name][account_name]['secret'] = line.split('\n')[0]
line_index += 1
else:
print('CTE : ERROR! Failed to find API Key file.')
# This function is for retrieving information about open trading positions
def getPositions(self, exchange=None):
if exchange:
self.connect(exchange)
elif not(self.exchange):
self.connect(self.exchangeAccounts['Default'])
positions = False
number_of_attempts = 0
while not(positions):
number_of_attempts += 1
try:
positions = self.exchange.fetch_positions(None, {'currency':'BTC'})
except Exception as error:
positions = False
self.inCaseOfError(**{'error': error, \
'description': 'fetching positions', \
'program': 'CTE', \
'line_number': traceback.format_exc().split('line ')[1].split(',')[0], \
'number_of_attempts': number_of_attempts})
# positions_dict is a tidier version of the raw position information retrieved by exchange.fetch_positions()
positions_dict = {}
positions_dict['Entry Price'] = float(positions[0]['avgEntryPrice'])
positions_dict['Side'] = positions[0]['side']
positions_dict['Leverage'] = float(positions[0]['leverage'])
positions_dict['Amount'] = float(positions[0]['size'])
positions_dict['Liqudation Price'] = float(positions[0]['liquidationPrice'])
try:
positions_dict['Stop-Loss'] = float(positions[0]['stopLoss'])
except:
positions_dict['Stop-Loss'] = False
positions_dict['Raw Positions List'] = positions
if not(self.silent_mode):
print('CTE : Current POSITION fetched')
for key in positions_dict:
if key != 'Raw Positions List':
print(' ' + key + ': ' + str(positions_dict[key]))
return(positions_dict)
# This function fetches the user's transaction history
# Users choose the symbol to look up, the type of transaction to look up, and the number of days of history they would like to see
def getTransactionHistory(self, *args):
if not(self.exchange):
self.connect()
if len(args) == 3:
symbol = args[0]
transaction_type = args[1]
number_of_days = args[2]
if len(args) == 2:
symbol = args[0]
transaction_type = args[1]
number_of_days = False
elif len(args) == 1:
symbol = args[0]
transaction_type = False
number_of_days = False
elif len(args) == 0:
symbol = False
transaction_type = False
number_of_days = False
while not(symbol):
symbol = input("\nWhich symbol's transaction history would you like?\n").upper()
if not(symbol.isalpha()):
print('INPUT ERROR! Please enter the abbreviation of a cryptocurrency.')
symbol = False
while not(transaction_type):
transaction_type = input("\nWhich type of transaction would you like?\n(1) Trade\n(2) Funding\n\nTransaction Type: ")
if transaction_type == '1' or transaction_type.lower() == 'trade':
transaction_type = 'Trade'
elif transaction_type == '2' or transaction_type.lower() == 'funding':
transaction_type = 'Funding'
if (transaction_type != 'Trade') and (transaction_type != 'Funding'):
print('INPUT ERROR! Please enter "1", "2", "trade" or "funding".')
transaction_type = False
while not(number_of_days):
number_of_days = input("\nHow many days of history would you like?\n")
try:
number_of_days = int(number_of_days)
square_root = math.sqrt(number_of_days)
except:
print('INPUT ERROR! Please enter a positive integer.')
number_of_days = False
if symbol == 'BTC':
symbol = 'BTC/USD'
elif len(symbol) < 5:
symbol = symbol + '/BTC'
# fetchMyTrades() retrieves the raw transaction history using CCXT
rawTransactionHistory = self.exchange.fetchMyTrades(symbol, since=self.exchange.milliseconds() - (86400000 * number_of_days))
transaction_history_dict = {}
cleaned_history_dict = {}
index = 0
# This for loop tidies up the transactions to be readable in a CSV
for transaction in rawTransactionHistory:
if transaction['info']['tradeType'] in transaction_type:
additional_dict = {}
obsolete_keys = []
for key in transaction:
if type(transaction[key]) == dict:
obsolete_keys.append(key)
for key_B in transaction[key]:
additional_dict[key_B + ' (' + key + ')'] = transaction[key][key_B]
transaction.update(additional_dict)
for key in obsolete_keys:
del transaction[key]
cleaned_history_dict[index] = transaction
index += 1
transaction_history_Dataframe = pd.DataFrame(cleaned_history_dict)
transaction_history_Dataframe = transaction_history_Dataframe.transpose()
transaction_history_Dataframe.to_csv('Transaction History.csv')
return(transaction_history_Dataframe)
# This function retrieves one's current account balances
def getBalances(self, *args):
self.balances = {}
# Optional inputs, such as a cryptocurrency symbol, an exchange name, and an account name are interpreted from the input(s)
if len(args) > 0:
if type(args[0]) == str:
symbol = args[0]
elif type(args[0]) == dict:
try:
symbol = args[0]['Symbol']
except:
symbol = 'all'
try:
exchange_name = args[0]['Exchange Name']
except:
exchange_name = False
try:
account_name = args[0]['Account Name']
except:
account_name = False
if exchange_name:
if account_name:
self.connect(exchange_name, account_name)
else:
self.connect(exchange_name, 'default')
else:
if account_name:
self.connect('default', account_name)
else:
self.connect('default')
else:
symbol = 'all'
if not(self.exchange):
self.connect()
exchange_name = self.currentConnectionDetails['Exchange Name']
# Spot Wallet balances are fetched and organized
try:
raw_spot_balances = self.exchange.fetch_balance()
spot_balances = {}
for key in raw_spot_balances:
if key == key.upper():
if (symbol == 'all') or (key == symbol.upper()):
spot_balances[key] = raw_spot_balances[key]
self.balances['Spot'] = spot_balances
except Exception as error:
self.inCaseOfError(**{'error': error, \
'description': 'fetching Spot balances', \
'program': 'CTE', \
'line_number': traceback.format_exc().split('line ')[1].split(',')[0]})
# Contract Trade Account balances are fetched and organized
contract_balances = {}
for available_symbol in self.availableSymbols[exchange_name]:
if (symbol == 'all') or (available_symbol == symbol.upper()):
available_symbol = available_symbol.split('/')[0]
contract_balances[available_symbol] = {}
raw_contract_balance = None
number_of_attempts = 0
while (raw_contract_balance == None) and (number_of_attempts < 2):
number_of_attempts += 1
try:
if available_symbol == 'BTC':
raw_contract_balance = self.exchange.fetch_balance(params={'type': 'swap', 'currency': available_symbol})
contract_balances[available_symbol]['free'] = float(raw_contract_balance[available_symbol]['free'])
contract_balances[available_symbol]['used'] = float(raw_contract_balance[available_symbol]['used'])
contract_balances[available_symbol]['total'] = float(raw_contract_balance[available_symbol]['total'])
contract_balances[available_symbol]['dict'] = raw_contract_balance
except Exception as error:
raw_contract_balance = None
self.inCaseOfError(**{'error': error, \
'description': 'fetching ' + available_symbol + ' Contract balance', \
'program': 'CTE', \
'pause_time': 3, \
'number_of_attempts': number_of_attempts, \
'line_number': traceback.format_exc().split('line ')[1].split(',')[0]})
self.balances['Contract'] = contract_balances
return(self.balances)
# This function retrieves the user's balances but leaves out symbols that the user doesn't have any of
def getNonzeroBalances(self, *args):
if len(args) == 1:
self.connect(args[0])
else:
try:
shit = self.exchange
except:
self.connect('binance')
nonzeroBalances = {}
balances = self.getBalances(self.exchange)
for key in balances:
try:
if (balances[key]['free'] > 0) or (balances[key]['used'] > 0) or (balances[key]['total'] > 0):
nonzeroBalances[key] = {'free': 0, 'used': 0, 'total': 0}
nonzeroBalances[key]['free'] = balances[key]['free']
nonzeroBalances[key]['used'] = balances[key]['used']
nonzeroBalances[key]['total'] = balances[key]['total']
except:
zero = 0
for key in nonzeroBalances:
if not(self.silent_mode):
print(key, nonzeroBalances[key]['free'])
return(nonzeroBalances)
# This function returns information about the user's currently open orders
def checkOrders(self, *args):
if len(args) == 1:
mainSymbol = args[0]
try:
shit = self.exchange
except:
self.connect('binance')
elif len(args) == 2:
mainSymbol = args[0]
self.connect(args[1])
else:
mainSymbol = 'all'
try:
shit = self.exchange
except:
self.connect('binance')
if mainSymbol == 'all':
symbolsInOrder = {}
for symbol in self.symbols_All:
if self.balances[symbol.split('/')[0]]['used'] > 0:
symbolsInOrder[symbol] = self.balances[symbol.split('/')[0]]['used']
#print(symbolsInOrder)
return(symbolsInOrder)
else:
quantityInOrder = self.balances[mainSymbol.split('/')[0]]['used']
#print(quantityInOrder)
return(quantityInOrder)
# This function returns the current bid price of an asset (which is essentially its current price)
def getCurrentBid(self, *args):
#arg 0 : symbol
try:
symbol = args[0]
except:
symbol = 'BTC/USD'
#arg 1 : exchange
try:
self.exchange = args[1]
self.exchange.fetchTicker('BTC/USDT')
except:
try:
self.exchange.fetchTicker('BTC/USDT')
except:
self.exchange = self.connect(self.exchangeAccounts['Default'])
current_bid = self.exchange.fetchTicker(symbol)['bid']
return(current_bid)
# This function returns the Open, High, Low, Close, and Volume values for a particular asset and with a particular timeframe
def getOHLCVs(self, *args):
#arg 0 : exchange
if not(self.exchange):
try:
self.exchange = self.connect(args[0])
except:
self.exchange = self.connect()
#arg 1 : symbol
try:
symbol = args[1]
except:
symbol = input("\nWhich symbol's OHLCV would you like?\nSymbol : ")
if symbol == 'BTC':
symbol = 'BTC/USD'
#arg 2 : timeframe
try:
timeframe = args[2]
except:
timeframe = input('\nWhich timeframe should be used for the OHLCV?\n(1) : 1m\n(2) : 1h\n(3) : 1D\n\nTimeframe : ')
if timeframe == '1':
timeframe = '1m'
elif timeframe == '2':
timeframe = '1h'
elif timeframe == '3':
timeframe = '1D'
#arg 3 : limit
try:
ohlcv_limit = args[3]
except:
ohlcv_limit = 1999
#arg 4 : improvement_modifiers
try:
improvement_modifiers = args[4]
except:
improvement_modifiers = {}
#arg 5 : export
try:
export = args[5]
except:
export = str(input('\nWould you like to export the OHLCVs to CSV?\n(1) Yes\n(2) No\nInput: '))
if export == '1' or export.lower() == 'yes':
export = True
else:
export = False
if not(self.silent_mode):
print('CTE : Fetching OHLCVs............................')
OHLCVs = self.exchange.fetchOHLCV(symbol, timeframe, limit=ohlcv_limit)
if not(self.silent_mode):
print('CTE : OHLCVs fetched!')
# This adds the date & time to each OHLCV
for OHLCV in OHLCVs:
OHLCV.append(self.GCT.convert_TimeStampToDateTime(OHLCV[0]))
self.updateMasterOHLCVs(OHLCVs, timeframe)
OHLCVs = pd.DataFrame(OHLCVs, columns=self.improved_columns)
if improvement_modifiers != {}:
improved_OHLCVs = self.improveOHLCVs(OHLCVs, improvement_modifiers)
if export:
current_time_string = self.GCT.getDateTimeString()
OHLCVs.to_csv('_OHLCV_Repository/OHLCVs ' + current_time_string + '.csv')
improved_OHLCVs.to_csv(path_or_buf='_OHLCV_Repository/ImprovedOHLCVs ' + current_time_string + '.csv')
return(improved_OHLCVs)
else:
if export:
current_time_string = self.GCT.getDateTimeString()
OHLCVs.to_csv('_OHLCV_Repository/OHLCVs ' + current_time_string + '.csv')
return(OHLCVs)
# This function calculates numerous moving averages and other analytical data using the raw OHLCVs
def improveOHLCVs(self, OHLCVs, modifiers):
if not(self.silent_mode):
print('\nCTE : Improving OHLCVs...')
if type(OHLCVs) == list:
OHLCVs = pd.DataFrame(OHLCVs, columns=self.improved_columns)
try:
self.which_OHLC = modifiers['Which OHLC']
except:
self.which_OHLC = 'Close'
mean_price = sum(OHLCVs[self.which_OHLC]) / len(OHLCVs[self.which_OHLC])
# This is no longer necessary because we always add the date & time
## # Adds the date to each OHLCV
## if not(self.silent_mode):
## print('improveOHLCVs : Dates are being added...')
## date_list = []
## for timestamp in OHLCVs['Timestamp']:
## date = self.GCT.convert_TimeStampToDate(int(timestamp))
## date_list.append(date)
## OHLCVs['Date'] = date_list
# Adds a Standard Deviation value to each OHLCV
if not(self.silent_mode):
print('CTE : The standard deviation is being added...')
difference_list = []
squared_difference_list = []
variance_list = []
standard_deviation_list = []
for price in OHLCVs[self.which_OHLC]:
difference_from_mean = price - mean_price
difference_list.append(difference_from_mean)
squared_difference_list.append(difference_from_mean * difference_from_mean)
variance = sum(squared_difference_list) / len(squared_difference_list)
standard_deviation = math.sqrt(variance)
for price in OHLCVs[self.which_OHLC]:
variance_list.append(variance)
standard_deviation_list.append(standard_deviation)
# Adds a "change" value and "% change" to each OHLCV (the amount the price rose or fell)
if not(self.silent_mode):
print('CTE : The change in price and & change in price since the last time interval is being added...')
change_list = []
change_percent_list = []
index = 0
for price in OHLCVs[self.which_OHLC]:
if index == 0:
change = 0
change_percent = 0
else:
change = price - last_price
change_percent = change / last_price
change_list.append(change)
change_percent_list.append(change_percent)
last_price = price
index += 1
OHLCVs['Change'] = change_list
OHLCVs['% Change'] = change_percent_list
if ('% Change 100x' in modifiers['Indicators']) or ('% Change 1000x' in modifiers['Indicators']) or \
('% Change 10000x' in modifiers['Indicators']) or ('% Change 100000x' in modifiers['Indicators']):
change_percent_100x_list = []
change_percent_1000x_list = []
change_percent_10000x_list = []
change_percent_100000x_list = []
for percent in change_percent_list:
change_percent_100x_list.append(int(percent * 100))
change_percent_1000x_list.append(int(percent * 1000))
change_percent_10000x_list.append(int(percent * 10000))
change_percent_100000x_list.append(int(percent * 100000))
OHLCVs['% Change 100x'] = change_percent_100x_list
OHLCVs['% Change 1000x'] = change_percent_1000x_list
OHLCVs['% Change 10000x'] = change_percent_10000x_list
OHLCVs['% Change 100000x'] = change_percent_100000x_list
del change_percent_100x_list[0]
del change_percent_1000x_list[0]
del change_percent_10000x_list[0]
del change_percent_100000x_list[0]
change_percent_100x_list.append(0)
change_percent_1000x_list.append(0)
change_percent_10000x_list.append(0)
change_percent_100000x_list.append(0)
if '% Change 100x' in modifiers['Indicators']:
OHLCVs['% Change 100x +'] = change_percent_100x_list
if '% Change 1000x' in modifiers['Indicators']:
OHLCVs['% Change 1000x +'] = change_percent_1000x_list
if '% Change 10000x' in modifiers['Indicators']:
OHLCVs['% Change 10000x +'] = change_percent_10000x_list
if '% Change 100000x' in modifiers['Indicators']:
OHLCVs['% Change 100000x +'] = change_percent_100000x_list
# OBV
if ('OBV' in modifiers['Indicators']) or ('Delta OBV' in modifiers['Indicators']) or ('MA OBV' in modifiers['Indicators']) or ('MA Delta OBV' in modifiers['Indicators']):
if not(self.silent_mode):
print('CTE : The OBV is being added...')
OBV_list = []
index = 0
for volume in OHLCVs['Volume']:
if index == 0:
OBV = 0
else:
if OHLCVs['Change'][index] > 0:
sign_variable = 1
elif OHLCVs['Change'][index] < 0:
sign_variable = -1
else:
sign_variable = 0
OBV_change = volume * sign_variable
OBV = last_OBV + OBV_change
OBV_list.append(OBV)
last_OBV = OBV
index += 1
OHLCVs['OBV'] = OBV_list
# Adds various time period-based values
if ('Indicator Time Intervals' in modifiers) and (len(modifiers['Indicator Time Intervals']) > 0):
OHLCVs = self.modifyOHLCVs_MovingTimeInterval(OHLCVs, modifiers)
# Adds various change-based values
if ('Delta Intervals' in modifiers) and (len(modifiers['Delta Intervals']) > 0):
OHLCVs = self.modifyOHLCVs_DeltaInterval(OHLCVs, modifiers)
# Creates moving averages and moving average exponentials of other indicators
if ('Moving Average Intervals' in modifiers) and (len(modifiers['Moving Average Intervals']) > 0):
OHLCVs = self.modifyOHLCVs_MovingAverages(OHLCVs, modifiers)
# Adds tiers to the amount prices changed
if ('Change Tiers' in modifiers) and (len(modifiers['Change Tiers']) > 0):
OHLCVs = self.modifyOHLCVs_ChangeTiers(OHLCVs, modifiers['Change Tiers'])
# Adds trough-peak values
if ('Trough-Peak Values' in modifiers) and modifiers['Trough-Peak Values']:
OHLCVs = self.findTP(OHLCVs)
# Adds a "should buy" value to each OHLCV (0 or 1)
if not(self.silent_mode):
print('CTE : A "Should Buy" column is being added...')
should_buy_list = []
index = 0
for change in OHLCVs['Change']:
if index > 0:
if change > 0:
should_buy_list.append(1)
else:
should_buy_list.append(0)
index += 1
should_buy_list.append(0)
OHLCVs['Should Buy'] = should_buy_list
return(OHLCVs)
# This function calculates moving averages for specific time durations
def modifyOHLCVs_MovingTimeInterval(self, OHLCVs, modifiers):
time_interval_list = modifiers['Indicator Time Intervals']
indicator_list = modifiers['Indicators']
for time_interval in time_interval_list:
OBV_list = []
MAV_list = []
MA_list = []
EMA_list = []
change_list = []
percent_change_list = []
should_buy_list = []
VWAP_list = []
most_recent_X = []
for row_index in OHLCVs.index:
OHLCV = OHLCVs.ix[row_index,]
OHLCV['index'] = row_index
most_recent_X.append(OHLCV)
if len(most_recent_X) > time_interval:
del most_recent_X[0]
# OBV-X
if ('OBV-X' in indicator_list) or ('Delta OBV-X' in indicator_list) or ('MA Delta OBV-X' in indicator_list) or ('EMA Delta OBV-X' in indicator_list) \
or ('MA OBV-X' in indicator_list) or ('EMA OBV-X' in indicator_list):
if row_index > 0:
OBV_X = 0
for recent_OHLCV in most_recent_X:
if recent_OHLCV['Change'] > 0:
sign_variable = 1
elif recent_OHLCV['Change'] < 0:
sign_variable = -1
else:
sign_variable = 0
OBV_change = recent_OHLCV['Volume'] * sign_variable
OBV_X = OBV_X + OBV_change
else:
OBV_X = 0
OBV_list.append(OBV_X)
# MAV-X
if ('MAV-X' in indicator_list) or ('Delta MAV-X' in indicator_list) or ('MA Delta MAV-X' in indicator_list) or ('EMA Delta MAV-X' in indicator_list) \
or ('MA MAV-X' in indicator_list) or ('EMA MAV-X' in indicator_list):
recent_volume_list = []
for recent_OHLCV in most_recent_X:
recent_volume_list.append(recent_OHLCV['Volume'])
MAV_X = sum(recent_volume_list) / len(recent_volume_list)
MAV_list.append(MAV_X)
# MA-X
if ('MA-X' in indicator_list) or ('Delta MA-X' in indicator_list) or ('MA Delta MA-X' in indicator_list) or ('EMA Delta MA-X' in indicator_list) \
or ('MA MA-X' in indicator_list) or ('EMA MA-X' in indicator_list):
recent_price_list = []
for recent_OHLCV in most_recent_X:
recent_price_list.append(recent_OHLCV[self.which_OHLC])
MA_X = sum(recent_price_list) / len(recent_price_list)
MA_list.append(MA_X)
# EMA-X
if ('EMA-X' in indicator_list) or ('Delta EMA-X' in indicator_list) or ('MA Delta EMA-X' in indicator_list) or ('EMA Delta EMA-X' in indicator_list) \
or ('MA EMA-X' in indicator_list) or ('EMA EMA-X' in indicator_list):
if row_index == 0:
EMA_X = OHLCV[self.which_OHLC]
else:
EMA_multiplier = self.EMA_smoother / (len(most_recent_X) + 1)
EMA_X = (OHLCV[self.which_OHLC] * EMA_multiplier) + (EMA_list[len(EMA_list) - 1] * (1 - EMA_multiplier))
EMA_list.append(EMA_X)
# Change-X
if ('Change-X' in indicator_list) or ('MA Change-X' in indicator_list) or ('EMA Change-X' in indicator_list):
change = OHLCV[self.which_OHLC] - most_recent_X[0][self.which_OHLC]
change_list.append(change)
# % Change-X
if ('% Change-X' in indicator_list) or ('MA % Change-X' in indicator_list) or ('EMA % Change-X' in indicator_list):
if row_index == 0:
percent_change = 0
else:
percent_change = 100 * (OHLCV[self.which_OHLC] - most_recent_X[0][self.which_OHLC]) / OHLCVs.ix[row_index - 1,][self.which_OHLC]
percent_change_list.append(percent_change)
# VWAP-X
if ('VWAP-X' in indicator_list) or ('Delta VWAP-X' in indicator_list) or ('MA Delta VWAP-X' in indicator_list) or ('EMA Delta VWAP-X' in indicator_list) \
or ('MA VWAP-X' in indicator_list) or ('EMA VWAP-X' in indicator_list):
recent_volume_list = []
for recent_OHLCV in most_recent_X:
recent_volume_list.append(recent_OHLCV['Volume'])
recent_volume_price_product_list = []
index = 0
for recent_OHLCV in most_recent_X:
recent_volume_price_product_list.append(recent_OHLCV[self.which_OHLC] * recent_volume_list[index])
index += 1
VWAP_X = sum(recent_volume_price_product_list) / sum(recent_volume_list)
VWAP_list.append(VWAP_X)
# Should Buy-X
if 'Should Buy-X' in indicator_list:
# This if makes the calculation of Should Buy-X delayed by X iterations
if len(most_recent_X) >= time_interval:
if row_index < len(OHLCVs.index) - 1:
recent_X_prices = []
for recent_OHLCV in most_recent_X:
if recent_X_prices == []:
oldest_price = recent_OHLCV[self.which_OHLC]
recent_X_prices.append(recent_OHLCV[self.which_OHLC])
if max(recent_X_prices) > oldest_price:
should_buy_list.append(1)
else:
should_buy_list.append(0)
# This if fills up the Should Buy-X's that we're missing because of the delay
else:
recent_X_prices = []
for recent_OHLCV in most_recent_X:
if recent_X_prices == []:
oldest_price = recent_OHLCV[self.which_OHLC]
recent_X_prices.append(recent_OHLCV[self.which_OHLC])
while len(recent_X_prices) > 0:
if max(recent_X_prices) > oldest_price:
should_buy_list.append(1)
else:
should_buy_list.append(0)
del recent_X_prices[0]
try:
oldest_price = recent_X_prices[0]
except:
shit = 'balls'
if len(OBV_list) > 0:
OHLCVs['OBV-' + str(time_interval)] = OBV_list
if len(MAV_list) > 0:
OHLCVs['MAV-' + str(time_interval)] = MAV_list
if len(MA_list) > 0:
OHLCVs['MA-' + str(time_interval)] = MA_list
if len(EMA_list) > 0:
OHLCVs['EMA-' + str(time_interval)] = EMA_list
if len(change_list) > 0:
OHLCVs['Change-' + str(time_interval)] = change_list
if len(percent_change_list) > 0:
OHLCVs['% Change-' + str(time_interval)] = percent_change_list
if len(VWAP_list) > 0:
OHLCVs['VWAP-' + str(time_interval)] = VWAP_list
if len(should_buy_list) > 0:
OHLCVs['Should Buy-' + str(time_interval)] = should_buy_list
for row_index in OHLCVs.index:
OHLCV = OHLCVs.ix[row_index,]
try:
del OHLCV['index']
except:
shit = 'balls'
return(OHLCVs)
# This function calculates the difference between an OHLCV value and that same value some time earlier
def modifyOHLCVs_DeltaInterval(self, OHLCVs, modifiers):
indicator_list = modifiers['Indicators']
delta_interval_list = modifiers['Delta Intervals']
time_interval_list = modifiers['Indicator Time Intervals']
delta_dict_Master = {}
for delta_interval in delta_interval_list:
delta_dict_Master[delta_interval] = {}
for indicator in indicator_list:
if 'Delta' in indicator:
raw_indicator_name = indicator.split('Delta ')[1]
if '-X' in raw_indicator_name:
raw_indicator_name = raw_indicator_name.split('X')[0]
for time_interval in time_interval_list:
if delta_interval > 1:
delta_dict_Master[delta_interval]['Delta-' + str(delta_interval) + ' ' + raw_indicator_name + str(time_interval)] = []
elif delta_interval == 1:
delta_dict_Master[delta_interval]['Delta ' + raw_indicator_name + str(time_interval)] = []
else:
if delta_interval > 1:
delta_dict_Master[delta_interval]['Delta-' + str(delta_interval) + ' ' + raw_indicator_name] = []
elif delta_interval == 1:
delta_dict_Master[delta_interval]['Delta ' + raw_indicator_name] = []
for delta_interval in delta_dict_Master:
for row_index in OHLCVs.index:
OHLCV = OHLCVs.ix[row_index,]
for delta_indicator in delta_dict_Master[delta_interval]:
if delta_interval == 1:
raw_indicator_name = delta_indicator.split('Delta ')[1]
else:
raw_indicator_name = delta_indicator.split('Delta-')[1].split(' ')[1]
if row_index > 0:
if row_index >= delta_interval:
delta_value = OHLCV[raw_indicator_name] - OHLCVs[raw_indicator_name][row_index - delta_interval]
else:
delta_value = OHLCV[raw_indicator_name] - OHLCVs[raw_indicator_name][0]
else:
delta_value = 0
delta_dict_Master[delta_interval][delta_indicator].append(delta_value)