-
Notifications
You must be signed in to change notification settings - Fork 31
/
Binance Detect Moonings.py
1596 lines (1257 loc) · 72 KB
/
Binance Detect Moonings.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
"""
Olorin Sledge Fork
Version: 1.28
Disclaimer
All investment strategies and investments involve risk of loss.
Nothing contained in this program, scripts, code or repositoy should be
construed as investment advice.Any reference to an investment's past or
potential performance is not, and should not be construed as, a recommendation
or as a guarantee of any specific outcome or profit.
By using this program you accept all liabilities,
and that no claims can be made against the developers,
or others connected with the program.
See requirements.txt for versions of modules needed
!! IMPORTANT INFORMATION ABOUT EXTERNAL SIGNAL MODULES !!
Please note this very important difference. If you use any external signals, they need to be modified as follows:
1) If it is a buy signal, you need to replace .exs with .buy so it creates a signals/whatever.buy (where "whatever" is anything you want)
2) If it is a sell signal, you need to replace .exs with .sell so it creates a signals/whatever.sell (where "whatever" is anything you want)
3) If it is a pausebot signal, you need to create a signals/pausebot.pause file
All these changes are within the external signal itself and is really easy to do via Find/Replace (advice you manually review any replace you do).
FUNCTIONALITY
- Changed way profit % is calculated to be based on ROI
- More details provided on screen on state of bot (i.e. unrealised session profit, session profit, all time profit, bot paused or not etc)
- Totally reworked external signals. NOTE: you CANNOT use the default signals anymore with my bot unless you modify them to work with it
- Sell all coins on stopping bot functionality
- Stop bot on session profit / session stop loss trigger
- Only sell based on an external signal i.e. Stop Loss and Take Profit are ignored
- Discord support
- Better reporting in trades.txt
- A history.txt that records state of bot every minute (useful for past analysis /charting)
- Better error trapping on certain exceptions
- BNB is no longer used as the reference for TIME_DIFFERENCE, this allows one to not have it in their tickers.txt list.
- Tickers list can now auto reload (if set in the config.yml file)
- Held coins displayed in a Table format
- Market profit vs Bot profit comparison
- Restart an external signal every hour
- Sell a specific coin on stopping bot funciton
- Bot can reinvest any profits from coins sold so as to compound your profits. Please note, this will also compound any losses so use with care.
Configurable in configy.yml with REINVEST_PROFITS flag.
Added version 1.20:
- Has a "Market Profit". This is a comparison between your bots profits and if you had just bought BTC instead when you started your bot.
Please note: If your bot has already been running for a period of time, you will need to manually modify your bots_stat.json and update
the "market_startprice" variable. This needs to be the price of BTC when your bot originally started.
Added version 1.21:
- Ability to "restart" an external signal via the RESTART_EXTSIGNALS setting. Please only use this is you know what you are doing. 99% of the time
you will want this to be False
Added version 1.25:
- "BUYING MODE" added to summary info so you can easily tell if you are in Test mode or Live mode
- "External Signals" added to summary info so you can tell which external signals you have running
Added version 1.26:
- "Sell A Specific Coin" feature enhancement added. When you end the bot, it gives you the option to sell a specific coin.
1. The bot will display the coins you can sell in a table
2. Type in the SYMBOL including the pair and the bot will sell it.
3. It will loop 1 to 2 until you choose N
4. Bot ends
Added version 1.27
- Menu system on stopping (CTRL+C) the bot for options to: Exit bot, sell all coins, sell specific coin, resume bot
Added version 1.28
- Reinvest profits, and losses, to compound capital
DONATIONS
If you feel you would like to donate to me, for all the above improvements, I would greatly appreciate it. Please see donation options below.
Bitcoin (BTC network): 1DMRzMWXRXLeTQ9mfN9uvMTeJHmkkG5oS8
Etherium (ERC-20 network): 0x69566c866817c593d8a40a1b672afa3b7cfd69bf
Matic (Polygon network): 0x69566c866817c593d8a40a1b672afa3b7cfd69bf
BNB (BEP20 network): 0x69566c866817c593d8a40a1b672afa3b7cfd69bf
Fantom (FTM network): 0x69566c866817c593d8a40a1b672afa3b7cfd69bf
Algo (Algorand network): ML72MOJ7N3O4G4EGDLKICNOCMIBCH4U5I34WCXZ4B4HREPBA3ME7BOYPB4
Nano (Nano network): nano_1en6m9rx9wgwqu5e1otedzprpgjbnrjs43gi9g94r5nu31ikc1heytt8qd74
ORIGINAL BOT CREATOR
This bot was forked from the original creation by CyberPunkMetalHead.
You can find his repository of projects at https://github.com/CyberPunkMetalHead.
You can find details to donate to him at his website, https://www.cryptomaton.org.
"""
# use for environment variables
import os
# use if needed to pass args to external modules
import sys
# used for math functions
import math
# used to create threads & dynamic loading of modules
import threading
import multiprocessing
import importlib
# used for directory handling
import glob
#discord needs import request
import requests
# Needed for colorful console output Install with: python3 -m pip install colorama (Mac/Linux) or pip install colorama (PC)
from colorama import init
init()
# needed for the binance API / websockets / Exception handling
from binance.client import Client
from binance.exceptions import BinanceAPIException
from binance.helpers import round_step_size
from requests.exceptions import ReadTimeout, ConnectionError
# used for dates
from datetime import date, datetime, timedelta
import time
# used to repeatedly execute the code
from itertools import count
# used to store trades and sell assets
import json
# used to display holding coins in an ascii table
from prettytable import PrettyTable
# Load helper modules
from helpers.parameters import (
parse_args, load_config
)
# Load creds modules
from helpers.handle_creds import (
load_correct_creds, test_api_key,
load_discord_creds
)
# my helper utils
from helpers.os_utils import(rchop)
# for colourful logging to the console
class txcolors:
BUY = '\033[92m'
WARNING = '\033[93m'
SELL_LOSS = '\033[91m'
SELL_PROFIT = '\033[32m'
DIM = '\033[2m\033[35m'
DEFAULT = '\033[39m'
YELLOW = '\033[33m'
CYAN = '\033[96m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
ENDC = '\033[0m'
# tracks profit/loss each session
global session_profit_incfees_perc, session_profit_incfees_total, session_tpsl_override_msg, is_bot_running
session_profit_incfees_perc = 0
session_profit_incfees_total = 0
session_tpsl_override_msg = ""
is_bot_running = True
global historic_profit_incfees_perc, historic_profit_incfees_total, trade_wins, trade_losses
global sell_all_coins, bot_started_datetime, market_startprice, market_currprice, sell_specific_coin
try:
historic_profit_incfees_perc
except NameError:
historic_profit_incfees_perc = 0 # or some other default value.
try:
historic_profit_incfees_total
except NameError:
historic_profit_incfees_total = 0 # or some other default value.
try:
trade_wins
except NameError:
trade_wins = 0 # or some other default value.
try:
trade_losses
except NameError:
trade_losses = 0 # or some other default value.
bot_started_datetime = ""
market_startprice = 0
# print with timestamps
old_out = sys.stdout
class St_ampe_dOut:
"""Stamped stdout."""
nl = True
def write(self, x):
"""Write function overloaded."""
if x == '\n':
old_out.write(x)
self.nl = True
elif self.nl:
old_out.write(f'{txcolors.DIM}[{str(datetime.now().replace(microsecond=0))}]{txcolors.DEFAULT} {x}')
self.nl = False
else:
old_out.write(x)
def flush(self):
pass
sys.stdout = St_ampe_dOut()
def is_fiat():
# check if we are using a fiat as a base currency
global hsp_head
PAIR_WITH = parsed_config['trading_options']['PAIR_WITH']
#list below is in the order that Binance displays them, apologies for not using ASC order
fiats = ['USDT', 'BUSD', 'AUD', 'BRL', 'EUR', 'GBP', 'RUB', 'TRY', 'TUSD', 'USDC', 'PAX', 'BIDR', 'DAI', 'IDRT', 'UAH', 'NGN', 'VAI', 'BVND']
if PAIR_WITH in fiats:
return True
else:
return False
def decimals():
# set number of decimals for reporting fractions
if is_fiat():
return 4
else:
return 8
def print_table(table):
global old_out
print('')
sys.stdout = old_out
print(table)
sys.stdout = St_ampe_dOut()
def print_notimestamp(msg):
global old_out
sys.stdout = old_out
print(msg, end = ' ')
sys.stdout = St_ampe_dOut()
def get_price(add_to_historical=True):
'''Return the current price for all coins on binance'''
global historical_prices, hsp_head, market_startprice, market_currprice
initial_price = {}
prices = client.get_all_tickers()
for coin in prices:
if coin['symbol'] == "BTCUSDT":
if market_startprice == 0:
market_startprice = float(coin['price'])
market_currprice = float(coin['price'])
if CUSTOM_LIST:
if any(item + PAIR_WITH == coin['symbol'] for item in tickers) and all(item not in coin['symbol'] for item in FIATS):
initial_price[coin['symbol']] = { 'price': coin['price'], 'time': datetime.now()}
else:
if PAIR_WITH in coin['symbol'] and all(item not in coin['symbol'] for item in FIATS):
initial_price[coin['symbol']] = { 'price': coin['price'], 'time': datetime.now()}
if add_to_historical:
hsp_head += 1
if hsp_head == RECHECK_INTERVAL:
hsp_head = 0
historical_prices[hsp_head] = initial_price
return initial_price
def wait_for_price():
'''calls the initial price and ensures the correct amount of time has passed
before reading the current price again'''
global historical_prices, hsp_head, volatility_cooloff
volatile_coins = {}
externals = {}
coins_up = 0
coins_down = 0
coins_unchanged = 0
pause_bot()
# get first element from the dictionary
firstcoin = next(iter(historical_prices[hsp_head]))
#BBif historical_prices[hsp_head]['BNB' + PAIR_WITH]['time'] > datetime.now() - timedelta(minutes=float(TIME_DIFFERENCE / RECHECK_INTERVAL)):
if historical_prices[hsp_head][firstcoin]['time'] > datetime.now() - timedelta(minutes=float(TIME_DIFFERENCE / RECHECK_INTERVAL)):
# sleep for exactly the amount of time required
#BBtime.sleep((timedelta(minutes=float(TIME_DIFFERENCE / RECHECK_INTERVAL)) - (datetime.now() - historical_prices[hsp_head]['BNB' + PAIR_WITH]['time'])).total_seconds())
time.sleep((timedelta(minutes=float(TIME_DIFFERENCE / RECHECK_INTERVAL)) - (datetime.now() - historical_prices[hsp_head][firstcoin]['time'])).total_seconds())
# retrieve latest prices
#last_price = get_price()
last_price = wrap_get_price()
# calculate the difference in prices
for coin in historical_prices[hsp_head]:
# minimum and maximum prices over time period
try:
min_price = min(historical_prices, key = lambda x: float("inf") if x is None else float(x[coin]['price']))
max_price = max(historical_prices, key = lambda x: -1 if x is None else float(x[coin]['price']))
threshold_check = (-1.0 if min_price[coin]['time'] > max_price[coin]['time'] else 1.0) * (float(max_price[coin]['price']) - float(min_price[coin]['price'])) / float(min_price[coin]['price']) * 100
#if coin == "BTCUSDT" or coin == "ETHUSDT":
#print(f"coin: {coin} min_price: {min_price[coin]['price']} max_price: {max_price[coin]['price']}")
except KeyError:
if DEBUG:
print(f"wait_for_price(): Got a KeyError for {coin}. If this coin was just added to your tickers file, no need to worry about this KeyError.")
continue
# FOR NEGATIVE PRICE CHECKING
#if threshold_check>0 and CHANGE_IN_PRICE<0: threshold_check=0
# each coin with higher gains than our CHANGE_IN_PRICE is added to the volatile_coins dict if less than TRADE_SLOTS is not reached.
# FOR NEGATIVE PRICE CHECKING
#if abs(threshold_check) > abs(CHANGE_IN_PRICE):
if threshold_check > CHANGE_IN_PRICE:
coins_up +=1
if coin not in volatility_cooloff:
volatility_cooloff[coin] = datetime.now() - timedelta(minutes=TIME_DIFFERENCE)
# volatility_cooloff[coin] = datetime.now() - timedelta(minutes=COOLOFF_PERIOD)
# only include coin as volatile if it hasn't been picked up in the last TIME_DIFFERENCE minutes already
if datetime.now() >= volatility_cooloff[coin] + timedelta(minutes=TIME_DIFFERENCE):
#if datetime.now() >= volatility_cooloff[coin] + timedelta(minutes=COOLOFF_PERIOD):
volatility_cooloff[coin] = datetime.now()
if len(coins_bought) + len(volatile_coins) < TRADE_SLOTS or TRADE_SLOTS == 0:
volatile_coins[coin] = round(threshold_check, 3)
print(f'{coin} has gained {volatile_coins[coin]}% within the last {TIME_DIFFERENCE} minutes, purchasing ${TRADE_TOTAL} {PAIR_WITH} of {coin}!')
else:
print(f'{txcolors.WARNING}{coin} has gained {round(threshold_check, 3)}% within the last {TIME_DIFFERENCE} minutes, but you are using all available trade slots!{txcolors.DEFAULT}')
#else:
#if len(coins_bought) == TRADE_SLOTS:
# print(f'{txcolors.WARNING}{coin} has gained {round(threshold_check, 3)}% within the last {TIME_DIFFERENCE} minutes, but you are using all available trade slots!{txcolors.DEFAULT}')
#else:
# print(f'{txcolors.WARNING}{coin} has gained {round(threshold_check, 3)}% within the last {TIME_DIFFERENCE} minutes, but failed cool off period of {COOLOFF_PERIOD} minutes! Curr COP is {volatility_cooloff[coin] + timedelta(minutes=COOLOFF_PERIOD)}{txcolors.DEFAULT}')
elif threshold_check < CHANGE_IN_PRICE:
coins_down +=1
else:
coins_unchanged +=1
# Disabled until fix
#print(f'Up: {coins_up} Down: {coins_down} Unchanged: {coins_unchanged}')
# Here goes new code for external signalling
externals = buy_external_signals()
exnumber = 0
for excoin in externals:
if excoin not in volatile_coins and excoin not in coins_bought and \
(len(coins_bought) + len(volatile_coins)) < TRADE_SLOTS:
#(len(coins_bought) + exnumber + len(volatile_coins)) < TRADE_SLOTS:
volatile_coins[excoin] = 1
exnumber +=1
print(f"External signal received on {excoin}, purchasing ${TRADE_TOTAL} {PAIR_WITH} value of {excoin}!")
balance_report(last_price)
return volatile_coins, len(volatile_coins), historical_prices[hsp_head]
def buy_external_signals():
external_list = {}
signals = {}
# check directory and load pairs from files into external_list
signals = glob.glob("signals/*.buy")
for filename in signals:
for line in open(filename):
symbol = line.strip()
external_list[symbol] = symbol
try:
os.remove(filename)
except:
if DEBUG: print(f'{txcolors.WARNING}Could not remove external signalling file{txcolors.DEFAULT}')
return external_list
def sell_external_signals():
external_list = {}
signals = {}
# check directory and load pairs from files into external_list
signals = glob.glob("signals/*.sell")
for filename in signals:
for line in open(filename):
symbol = line.strip()
external_list[symbol] = symbol
if DEBUG: print(f'{symbol} added to sell_external_signals() list')
try:
os.remove(filename)
except:
if DEBUG: print(f'{txcolors.WARNING}Could not remove external SELL signalling file{txcolors.DEFAULT}')
return external_list
def balance_report(last_price):
global trade_wins, trade_losses, session_profit_incfees_perc, session_profit_incfees_total
unrealised_session_profit_incfees_perc = 0
unrealised_session_profit_incfees_total = 0
BUDGET = TRADE_SLOTS * TRADE_TOTAL
exposure_calcuated = 0
for coin in list(coins_bought):
LastPrice = float(last_price[coin]['price'])
sellFee = (LastPrice * (TRADING_FEE/100))
BuyPrice = float(coins_bought[coin]['bought_at'])
buyFee = (BuyPrice * (TRADING_FEE/100))
exposure_calcuated = exposure_calcuated + round(float(coins_bought[coin]['bought_at']) * float(coins_bought[coin]['volume']),0)
#PriceChangeIncFees_Total = float(((LastPrice+sellFee) - (BuyPrice+buyFee)) * coins_bought[coin]['volume'])
PriceChangeIncFees_Total = float(((LastPrice-sellFee) - (BuyPrice+buyFee)) * coins_bought[coin]['volume'])
# unrealised_session_profit_incfees_perc = float(unrealised_session_profit_incfees_perc + PriceChangeIncFees_Perc)
unrealised_session_profit_incfees_total = float(unrealised_session_profit_incfees_total + PriceChangeIncFees_Total)
unrealised_session_profit_incfees_perc = (unrealised_session_profit_incfees_total / BUDGET) * 100
DECIMALS = int(decimals())
# CURRENT_EXPOSURE = round((TRADE_TOTAL * len(coins_bought)), DECIMALS)
CURRENT_EXPOSURE = round(exposure_calcuated, 0)
INVESTMENT_TOTAL = round((TRADE_TOTAL * TRADE_SLOTS), DECIMALS)
# truncating some of the above values to the correct decimal places before printing
WIN_LOSS_PERCENT = 0
if (trade_wins > 0) and (trade_losses > 0):
WIN_LOSS_PERCENT = round((trade_wins / (trade_wins+trade_losses)) * 100, 2)
if (trade_wins > 0) and (trade_losses == 0):
WIN_LOSS_PERCENT = 100
market_profit = ((market_currprice - market_startprice)/ market_startprice) * 100
mode = "Live (REAL MONEY)"
discord_mode = "Live"
if TEST_MODE:
mode = "Test (no real money used)"
discord_mode = "Test"
font = f'{txcolors.ENDC}{txcolors.YELLOW}{txcolors.BOLD}{txcolors.UNDERLINE}'
extsigs = ""
try:
for module in SIGNALLING_MODULES:
if extsigs == "":
extsigs = module
else:
extsigs = extsigs + ', ' + module
except Exception as e:
pass
if extsigs == "":
extsigs = "No external signals running"
print(f'')
print(f'--------')
print(f"STARTED : {str(bot_started_datetime).split('.')[0]} | Running for: {str(datetime.now() - bot_started_datetime).split('.')[0]}")
print(f'CURRENT HOLDS : {len(coins_bought)}/{TRADE_SLOTS} ({float(CURRENT_EXPOSURE):g}/{float(INVESTMENT_TOTAL):g} {PAIR_WITH})')
if REINVEST_PROFITS:
print(f'ADJ TRADE TOTAL : {TRADE_TOTAL:.2f} (Current TRADE TOTAL adjusted to reinvest profits)')
print(f'BUYING MODE : {font if mode == "Live (REAL MONEY)" else txcolors.DEFAULT}{mode}{txcolors.DEFAULT}{txcolors.ENDC}')
print(f'Buying Paused : {bot_paused}')
print(f'')
print(f'SESSION PROFIT (Inc Fees)')
print(f'Realised : {txcolors.SELL_PROFIT if session_profit_incfees_perc > 0. else txcolors.SELL_LOSS}{session_profit_incfees_perc:.4f}% Est:${session_profit_incfees_total:.4f} {PAIR_WITH}{txcolors.DEFAULT}')
print(f'Unrealised : {txcolors.SELL_PROFIT if unrealised_session_profit_incfees_perc > 0. else txcolors.SELL_LOSS}{unrealised_session_profit_incfees_perc:.4f}% Est:${unrealised_session_profit_incfees_total:.4f} {PAIR_WITH}{txcolors.DEFAULT}')
print(f' Total : {txcolors.SELL_PROFIT if (session_profit_incfees_perc + unrealised_session_profit_incfees_perc) > 0. else txcolors.SELL_LOSS}{session_profit_incfees_perc + unrealised_session_profit_incfees_perc:.4f}% Est:${session_profit_incfees_total+unrealised_session_profit_incfees_total:.4f} {PAIR_WITH}{txcolors.DEFAULT}')
print(f'')
print(f'ALL TIME DATA :')
print(f"Market Profit : {txcolors.SELL_PROFIT if market_profit > 0. else txcolors.SELL_LOSS}{market_profit:.4f}% (BTCUSDT Since STARTED){txcolors.DEFAULT}")
print(f'Bot Profit : {txcolors.SELL_PROFIT if historic_profit_incfees_perc > 0. else txcolors.SELL_LOSS}{historic_profit_incfees_perc:.4f}% Est:${historic_profit_incfees_total:.4f} {PAIR_WITH}{txcolors.DEFAULT}')
print(f'Completed Trades: {trade_wins+trade_losses} (Wins:{trade_wins} Losses:{trade_losses})')
print(f'Win Ratio : {float(WIN_LOSS_PERCENT):g}%')
print(f'')
print(f'External Signals: {extsigs}')
print(f'--------')
print(f'')
#msg1 = str(bot_started_datetime) + " | " + str(datetime.now() - bot_started_datetime)
msg1 = str(datetime.now()).split('.')[0]
msg2 = " | " + str(len(coins_bought)) + "/" + str(TRADE_SLOTS) + " | PBOT: " + str(bot_paused) + " | MODE: " + str(discord_mode)
msg2 = msg2 + ' SPR%: ' + str(round(session_profit_incfees_perc,2)) + ' SPR$: ' + str(round(session_profit_incfees_total,4))
msg2 = msg2 + ' SPU%: ' + str(round(unrealised_session_profit_incfees_perc,2)) + ' SPU$: ' + str(round(unrealised_session_profit_incfees_total,4))
msg2 = msg2 + ' SPT%: ' + str(round(session_profit_incfees_perc + unrealised_session_profit_incfees_perc,2)) + ' SPT$: ' + str(round(session_profit_incfees_total+unrealised_session_profit_incfees_total,4))
msg2 = msg2 + ' ATP%: ' + str(round(historic_profit_incfees_perc,2)) + ' ATP$: ' + str(round(historic_profit_incfees_total,4))
msg2 = msg2 + ' CTT: ' + str(trade_wins+trade_losses) + ' CTW: ' + str(trade_wins) + ' CTL: ' + str(trade_losses) + ' CTWR%: ' + str(round(WIN_LOSS_PERCENT,2))
msg_discord_balance(msg1, msg2)
history_log(session_profit_incfees_perc, session_profit_incfees_total, unrealised_session_profit_incfees_perc, unrealised_session_profit_incfees_total, session_profit_incfees_perc + unrealised_session_profit_incfees_perc, session_profit_incfees_total+unrealised_session_profit_incfees_total, historic_profit_incfees_perc, historic_profit_incfees_total, trade_wins+trade_losses, trade_wins, trade_losses, WIN_LOSS_PERCENT)
return msg1 + msg2
def history_log(sess_profit_perc, sess_profit, sess_profit_perc_unreal, sess_profit_unreal, sess_profit_perc_total, sess_profit_total, alltime_profit_perc, alltime_profit, total_trades, won_trades, lost_trades, winloss_ratio):
global last_history_log_date
time_between_insertion = datetime.now() - last_history_log_date
# only log balance to log file once every 60 seconds
if time_between_insertion.seconds > 60:
last_history_log_date = datetime.now()
timestamp = datetime.now().strftime("%y-%m-%d %H:%M:%S")
if not os.path.exists(HISTORY_LOG_FILE):
with open(HISTORY_LOG_FILE,'a+') as f:
f.write('Datetime\tCoins Holding\tTrade Slots\tPausebot Active\tSession Profit %\tSession Profit $\tSession Profit Unrealised %\tSession Profit Unrealised $\tSession Profit Total %\tSession Profit Total $\tAll Time Profit %\tAll Time Profit $\tTotal Trades\tWon Trades\tLost Trades\tWin Loss Ratio\n')
with open(HISTORY_LOG_FILE,'a+') as f:
f.write(f'{timestamp}\t{len(coins_bought)}\t{TRADE_SLOTS}\t{str(bot_paused)}\t{str(round(sess_profit_perc,2))}\t{str(round(sess_profit,4))}\t{str(round(sess_profit_perc_unreal,2))}\t{str(round(sess_profit_unreal,4))}\t{str(round(sess_profit_perc_total,2))}\t{str(round(sess_profit_total,4))}\t{str(round(alltime_profit_perc,2))}\t{str(round(alltime_profit,4))}\t{str(total_trades)}\t{str(won_trades)}\t{str(lost_trades)}\t{str(winloss_ratio)}\n')
def msg_discord_balance(msg1, msg2):
global last_msg_discord_balance_date, discord_msg_balance_data
time_between_insertion = datetime.now() - last_msg_discord_balance_date
# only put the balance message to discord once every 60 seconds and if the balance information has changed since last times
if time_between_insertion.seconds > 60:
if msg2 != discord_msg_balance_data:
msg_discord(msg1 + msg2)
discord_msg_balance_data = msg2
else:
# ping msg to know the bot is still running
msg_discord(".")
def msg_discord(msg):
message = msg + '\n\n'
if MSG_DISCORD:
#Webhook of my channel. Click on edit channel --> Webhooks --> Creates webhook
mUrl = "https://discordapp.com/api/webhooks/"+DISCORD_WEBHOOK
data = {"content": message}
response = requests.post(mUrl, json=data)
#BB
# print(response.content)
def pause_bot():
'''Pause the script when external indicators detect a bearish trend in the market'''
global bot_paused, session_profit_incfees_perc, hsp_head, session_profit_incfees_total
# start counting for how long the bot has been paused
start_time = time.perf_counter()
while os.path.exists("signals/pausebot.pause"):
# do NOT accept any external signals to buy while in pausebot mode
remove_external_signals('buy')
if bot_paused == False:
print(f'{txcolors.WARNING}Buying paused due to negative market conditions, stop loss and take profit will continue to work...{txcolors.DEFAULT}')
msg = str(datetime.now()) + ' | PAUSEBOT. Buying paused due to negative market conditions, stop loss and take profit will continue to work.'
msg_discord(msg)
bot_paused = True
# Sell function needs to work even while paused
coins_sold = sell_coins()
remove_from_portfolio(coins_sold)
last_price = get_price(True)
# pausing here
if hsp_head == 1:
# print(f'Paused...Session profit: {session_profit_incfees_perc:.2f}% Est: ${session_profit_incfees_total:.{decimals()}f} {PAIR_WITH}')
balance_report(last_price)
time.sleep((TIME_DIFFERENCE * 60) / RECHECK_INTERVAL)
else:
# stop counting the pause time
stop_time = time.perf_counter()
time_elapsed = timedelta(seconds=int(stop_time-start_time))
# resume the bot and ser pause_bot to False
if bot_paused == True:
print(f'{txcolors.WARNING}Resuming buying due to positive market conditions, total sleep time: {time_elapsed}{txcolors.DEFAULT}')
msg = str(datetime.now()) + ' | PAUSEBOT. Resuming buying due to positive market conditions, total sleep time: ' + str(time_elapsed)
msg_discord(msg)
bot_paused = False
return
def convert_volume():
'''Converts the volume given in TRADE_TOTAL from USDT to the each coin's volume'''
volatile_coins, number_of_coins, last_price = wait_for_price()
lot_size = {}
volume = {}
for coin in volatile_coins:
# Find the correct step size for each coin
# max accuracy for BTC for example is 6 decimal points
# while XRP is only 1
try:
info = client.get_symbol_info(coin)
step_size = info['filters'][2]['stepSize']
lot_size[coin] = step_size.index('1') - 1
if lot_size[coin] < 0:
lot_size[coin] = 0
except:
pass
# calculate the volume in coin from TRADE_TOTAL in PAIR_WITH (default)
volume[coin] = float(TRADE_TOTAL / float(last_price[coin]['price']))
# define the volume with the correct step size
if coin not in lot_size:
# original code: volume[coin] = float('{:.1f}'.format(volume[coin]))
volume[coin] = int(volume[coin])
else:
# if lot size has 0 decimal points, make the volume an integer
if lot_size[coin] == 0:
volume[coin] = int(volume[coin])
else:
#volume[coin] = float('{:.{}f}'.format(volume[coin], lot_size[coin]))
volume[coin] = truncate(volume[coin], lot_size[coin])
return volume, last_price
def buy():
'''Place Buy market orders for each volatile coin found'''
volume, last_price = convert_volume()
orders = {}
for coin in volume:
if coin not in coins_bought:
print(f"{txcolors.BUY}Preparing to buy {volume[coin]} of {coin} @ ${last_price[coin]['price']}{txcolors.DEFAULT}")
msg1 = str(datetime.now()) + ' | BUY: ' + coin + '. V:' + str(volume[coin]) + ' P$:' + str(last_price[coin]['price'])
msg_discord(msg1)
if TEST_MODE:
orders[coin] = [{
'symbol': coin,
'orderId': 0,
'time': datetime.now().timestamp()
}]
# Log trade
#if LOG_TRADES:
write_log(f"\tBuy\t{coin}\t{volume[coin]}\t{last_price[coin]['price']}\t{PAIR_WITH}")
#write_signallsell(coin.removesuffix(PAIR_WITH))
write_signallsell(rchop(coin, PAIR_WITH))
continue
# try to create a real order if the test orders did not raise an exception
try:
order_details = client.create_order(
symbol = coin,
side = 'BUY',
type = 'MARKET',
quantity = volume[coin]
)
# error handling here in case position cannot be placed
except Exception as e:
print(f'buy() exception: {e}')
# run the else block if the position has been placed and return order info
else:
orders[coin] = client.get_all_orders(symbol=coin, limit=1)
# binance sometimes returns an empty list, the code will wait here until binance returns the order
while orders[coin] == []:
print('Binance is being slow in returning the order, calling the API again...')
orders[coin] = client.get_all_orders(symbol=coin, limit=1)
time.sleep(1)
else:
print('Order returned, saving order to file')
if not TEST_MODE:
orders[coin] = extract_order_data(order_details)
write_log(f"\tBuy\t{coin}\t{orders[coin]['volume']}\t{orders[coin]['avgPrice']}\t{PAIR_WITH}")
else:
write_log(f"\tBuy\t{coin}\t{volume[coin]}\t{last_price[coin]['price']}\t{PAIR_WITH}")
write_signallsell(coin)
else:
print(f'Signal detected, but there is already an active trade on {coin}')
return orders, last_price, volume
def sell_coins(tpsl_override = False, specific_coin_to_sell = ""):
'''sell coins that have reached the STOP LOSS or TAKE PROFIT threshold'''
global hsp_head, session_profit_incfees_perc, session_profit_incfees_total, coin_order_id, trade_wins, trade_losses, historic_profit_incfees_perc, historic_profit_incfees_total, sell_all_coins, sell_specific_coin
global TRADE_TOTAL
externals = sell_external_signals()
last_price = get_price(False) # don't populate rolling window
#last_price = get_price(add_to_historical=True) # don't populate rolling window
coins_sold = {}
BUDGET = TRADE_TOTAL * TRADE_SLOTS
# table stuff
my_table = PrettyTable()
my_table.field_names = ["Symbol", "Volume", "Bought At", "Now At", "TP %", "SL %", "Change %", "Profit $", "Time Held"]
my_table.align["Symbol"] = "l"
my_table.align["Volume"] = "r"
my_table.align["Bought At"] = "r"
my_table.align["Now At"] = "r"
my_table.align["TP %"] = "r"
my_table.align["SL %"] = "r"
my_table.align["Change %"] = "r"
my_table.align["Profit $"] = "r"
my_table.align["Time Held"] = "l"
for coin in list(coins_bought):
if sell_specific_coin and not specific_coin_to_sell == coin:
continue
#time_held = timedelta(seconds=datetime.now().timestamp()-coins_bought[coin]['timestamp'])
time_held = timedelta(seconds=datetime.now().timestamp()-int(str(coins_bought[coin]['timestamp'])[:10]))
#if HODLMODE_ENABLED and (time_held >= HODLMODE_TIME_THRESHOLD):
# move_coin_to_hodl(coin)
# continue
LastPrice = float(last_price[coin]['price'])
sellFee = (LastPrice * (TRADING_FEE/100))
sellFeeTotal = (coins_bought[coin]['volume'] * LastPrice) * (TRADING_FEE/100)
LastPriceLessFees = LastPrice - sellFee
BuyPrice = float(coins_bought[coin]['bought_at'])
buyFee = (BuyPrice * (TRADING_FEE/100))
buyFeeTotal = (coins_bought[coin]['volume'] * BuyPrice) * (TRADING_FEE/100)
BuyPricePlusFees = BuyPrice + buyFee
ProfitAfterFees = LastPriceLessFees - BuyPricePlusFees
PriceChange_Perc = float((LastPrice - BuyPrice) / BuyPrice * 100)
#PriceChangeIncFees_Perc = float(((LastPrice+sellFee) - (BuyPrice+buyFee)) / (BuyPrice+buyFee) * 100)
PriceChangeIncFees_Perc = float(((LastPriceLessFees - BuyPricePlusFees) / BuyPricePlusFees) * 100)
#PriceChangeIncFees_Unit = float((LastPrice+sellFee) - (BuyPrice+buyFee))
PriceChangeIncFees_Unit = float(LastPriceLessFees - BuyPricePlusFees)
# define stop loss and take profit
TP = float(coins_bought[coin]['bought_at']) + ((float(coins_bought[coin]['bought_at']) * (coins_bought[coin]['take_profit']) / 100))
SL = float(coins_bought[coin]['bought_at']) + ((float(coins_bought[coin]['bought_at']) * (coins_bought[coin]['stop_loss']) / 100))
# check that the price is above the take profit and readjust SL and TP accordingly if trialing stop loss used
#if LastPrice > TP and USE_TRAILING_STOP_LOSS and not sell_all_coins and not tpsl_override:
if LastPriceLessFees > TP and USE_TRAILING_STOP_LOSS and not sell_all_coins and not tpsl_override and not sell_specific_coin:
# increasing TP by TRAILING_TAKE_PROFIT (essentially next time to readjust SL)
#if PriceChange_Perc >= 0.8:
if PriceChangeIncFees_Perc >= 0.8:
# price has changed by 0.8% or greater, a big change. Make the STOP LOSS trail closely to the TAKE PROFIT
# so you don't lose this increase in price if it falls back
#coins_bought[coin]['take_profit'] = PriceChange_Perc + TRAILING_TAKE_PROFIT
coins_bought[coin]['take_profit'] = PriceChangeIncFees_Perc + TRAILING_TAKE_PROFIT
coins_bought[coin]['stop_loss'] = coins_bought[coin]['take_profit'] - TRAILING_STOP_LOSS
else:
# price has changed by less than 0.8%, a small change. Make the STOP LOSS trail loosely to the TAKE PROFIT
# so you don't get stopped out of the trade prematurely
coins_bought[coin]['stop_loss'] = coins_bought[coin]['take_profit'] - TRAILING_STOP_LOSS
#coins_bought[coin]['take_profit'] = PriceChange_Perc + TRAILING_TAKE_PROFIT
coins_bought[coin]['take_profit'] = PriceChangeIncFees_Perc + TRAILING_TAKE_PROFIT
# we've got a negative stop loss - not good, we don't want this.
if coins_bought[coin]['stop_loss'] <= 0:
coins_bought[coin]['stop_loss'] = coins_bought[coin]['take_profit'] * .25
#if DEBUG: print(f"{coin} TP reached, adjusting TP {coins_bought[coin]['take_profit']:.{decimals()}f} and SL {coins_bought[coin]['stop_loss']:.{decimals()}f} accordingly to lock-in profit")
my_table.add_row([f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coin + ' TP up!'}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coins_bought[coin]['volume']:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{BuyPrice:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{LastPrice:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coins_bought[coin]['take_profit']:.4f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coins_bought[coin]['stop_loss']:.4f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{PriceChangeIncFees_Perc:.4f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{((float(coins_bought[coin]['volume'])*float(coins_bought[coin]['bought_at']))*PriceChangeIncFees_Perc)/100:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{str(time_held).split('.')[0]}{txcolors.DEFAULT}"])
continue
# check that the price is below the stop loss or above take profit (if trailing stop loss not used) and sell if this is the case
sellCoin = False
sell_reason = ""
if SELL_ON_SIGNAL_ONLY:
# only sell if told to by external signal
if coin in externals:
sellCoin = True
sell_reason = 'External Sell Signal'
else:
#if LastPrice < SL:
if LastPriceLessFees < SL:
sellCoin = True
if USE_TRAILING_STOP_LOSS:
#if PriceChange_Perc >= 0:PriceChangeIncFees_Perc
if PriceChangeIncFees_Perc >= 0:
sell_reason = "TTP " + str(SL) + " reached"
else:
sell_reason = "TSL " + str(SL) + " reached"
else:
sell_reason = "SL " + str(SL) + " reached"
sell_reason = sell_reason
#if LastPrice > TP:
if LastPriceLessFees > TP:
sellCoin = True
sell_reason = "TP " + str(TP) + " reached"
if coin in externals:
sellCoin = True
sell_reason = 'External Sell Signal'
if sell_all_coins:
sellCoin = True
sell_reason = 'Sell All Coins'
if sell_specific_coin:
sellCoin = True
sell_reason = 'Sell Specific Coin'
if tpsl_override:
sellCoin = True
sell_reason = session_tpsl_override_msg
if sellCoin:
print(f"{txcolors.SELL_PROFIT if PriceChangeIncFees_Perc >= 0. else txcolors.SELL_LOSS}Sell: {coins_bought[coin]['volume']} of {coin} | {sell_reason} | ${float(LastPrice):g} - ${float(BuyPrice):g} | Profit: {PriceChangeIncFees_Perc:.2f}% Est: {((float(coins_bought[coin]['volume'])*float(coins_bought[coin]['bought_at']))*PriceChangeIncFees_Perc)/100:.{decimals()}f} {PAIR_WITH} (Inc Fees){txcolors.DEFAULT}")
msg1 = str(datetime.now()) + '| SELL: ' + coin + '. R:' + sell_reason + ' P%:' + str(round(PriceChangeIncFees_Perc,2)) + ' P$:' + str(round(((float(coins_bought[coin]['volume'])*float(coins_bought[coin]['bought_at']))*PriceChangeIncFees_Perc)/100,4))
msg_discord(msg1)
# try to create a real order
try:
if not TEST_MODE:
#lot_size = coins_bought[coin]['step_size']
#if lot_size == 0:
# lot_size = 1
#lot_size = lot_size.index('1') - 1
#if lot_size < 0:
# lot_size = 0
order_details = client.create_order(
symbol = coin,
side = 'SELL',
type = 'MARKET',
quantity = coins_bought[coin]['volume']
)
# error handling here in case position cannot be placed
except Exception as e:
#if repr(e).upper() == "APIERROR(CODE=-1111): PRECISION IS OVER THE MAXIMUM DEFINED FOR THIS ASSET.":
print(f"sell_coins() Exception occured on selling the coin! Coin: {coin}\nSell Volume coins_bought: {coins_bought[coin]['volume']}\nPrice:{LastPrice}\nException: {e}")
# run the else block if coin has been sold and create a dict for each coin sold
else:
if not TEST_MODE:
coins_sold[coin] = extract_order_data(order_details)
LastPrice = coins_sold[coin]['avgPrice']
sellFee = coins_sold[coin]['tradeFeeUnit']
coins_sold[coin]['orderid'] = coins_bought[coin]['orderid']
priceChange = float((LastPrice - BuyPrice) / BuyPrice * 100)
# update this from the actual Binance sale information
#PriceChangeIncFees_Unit = float((LastPrice+sellFee) - (BuyPrice+buyFee))
PriceChangeIncFees_Unit = float((LastPrice-sellFee) - (BuyPrice+buyFee))
else:
coins_sold[coin] = coins_bought[coin]
# prevent system from buying this coin for the next TIME_DIFFERENCE minutes
volatility_cooloff[coin] = datetime.now()
if DEBUG:
print(f"sell_coins() | Coin: {coin} | Sell Volume: {coins_bought[coin]['volume']} | Price:{LastPrice}")
# Log trade
#BB profit = ((LastPrice - BuyPrice) * coins_sold[coin]['volume']) * (1-(buyFee + sellFeeTotal))
profit_incfees_total = coins_sold[coin]['volume'] * PriceChangeIncFees_Unit
#write_log(f"Sell: {coins_sold[coin]['volume']} {coin} - {BuyPrice} - {LastPrice} Profit: {profit_incfees_total:.{decimals()}f} {PAIR_WITH} ({PriceChange_Perc:.2f}%)")
#write_log(f"\tSell\t{coin}\t{coins_sold[coin]['volume']}\t{BuyPrice}\t{PAIR_WITH}\t{LastPrice}\t{profit_incfees_total:.{decimals()}f}\t{PriceChange_Perc:.2f}\t{sell_reason}")
write_log(f"\tSell\t{coin}\t{coins_sold[coin]['volume']}\t{BuyPrice}\t{PAIR_WITH}\t{LastPrice}\t{profit_incfees_total:.{decimals()}f}\t{PriceChangeIncFees_Perc:.2f}\t{sell_reason}")
#reinvest profits
if REINVEST_PROFITS:
TRADE_TOTAL += (profit_incfees_total / TRADE_SLOTS)
#this is good
session_profit_incfees_total = session_profit_incfees_total + profit_incfees_total
session_profit_incfees_perc = session_profit_incfees_perc + ((profit_incfees_total/BUDGET) * 100)
historic_profit_incfees_total = historic_profit_incfees_total + profit_incfees_total
historic_profit_incfees_perc = historic_profit_incfees_perc + ((profit_incfees_total/BUDGET) * 100)
#TRADE_TOTAL*PriceChangeIncFees_Perc)/100
#if (LastPrice+sellFee) >= (BuyPrice+buyFee):
if (LastPrice-sellFee) >= (BuyPrice+buyFee):
trade_wins += 1
else:
trade_losses += 1
update_bot_stats()
if not sell_all_coins and not sell_specific_coin:
# within sell_all_coins, it will print display to screen
balance_report(last_price)
# sometimes get "rate limited" errors from Binance if we try to sell too many coins at once
# so wait 1 second in between sells
time.sleep(1)
continue
# no action; print once every TIME_DIFFERENCE
if hsp_head == 1:
if len(coins_bought) > 0:
#print(f"Holding: {coins_bought[coin]['volume']} of {coin} | {LastPrice} - {BuyPrice} | Profit: {txcolors.SELL_PROFIT if PriceChangeIncFees_Perc >= 0. else txcolors.SELL_LOSS}{PriceChangeIncFees_Perc:.4f}% Est: ({((float(coins_bought[coin]['volume'])*float(coins_bought[coin]['bought_at']))*PriceChangeIncFees_Perc)/100:.{decimals()}f} {PAIR_WITH}){txcolors.DEFAULT}")
my_table.add_row([f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coin}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coins_bought[coin]['volume']:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{BuyPrice:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{LastPrice:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coins_bought[coin]['take_profit']:.4f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{coins_bought[coin]['stop_loss']:.4f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{PriceChangeIncFees_Perc:.4f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{((float(coins_bought[coin]['volume'])*float(coins_bought[coin]['bought_at']))*PriceChangeIncFees_Perc)/100:.6f}{txcolors.DEFAULT}", f"{txcolors.SELL_PROFIT if ProfitAfterFees >= 0. else txcolors.SELL_LOSS}{str(time_held).split('.')[0]}{txcolors.DEFAULT}"])
my_table.sortby = 'Change %'
#my_table.reversesort = True
if len(coins_bought) == 0:
if hsp_head == 1:
print(f"No trade slots are currently in use")
else:
if len(my_table._rows) > 0: print_table(my_table)
# if tpsl_override: is_bot_running = False
return coins_sold
def extract_order_data(order_details):
global TRADING_FEE, STOP_LOSS, TAKE_PROFIT
transactionInfo = {}
# This code is from GoranJovic - thank you!
#
# adding order fill extractions here
#
# just to explain what I am doing here:
# Market orders are not always filled at one price, we need to find the averages of all 'parts' (fills) of this order.
#
# reset other variables to 0 before use
FILLS_TOTAL = 0
FILLS_QTY = 0
FILLS_FEE = 0
BNB_WARNING = 0
# loop through each 'fill':
for fills in order_details['fills']:
FILL_PRICE = float(fills['price'])
FILL_QTY = float(fills['qty'])
FILLS_FEE += float(fills['commission'])
# check if the fee was in BNB. If not, log a nice warning:
if (fills['commissionAsset'] != 'BNB') and (TRADING_FEE == 0.075) and (BNB_WARNING == 0):
print(f"WARNING: BNB not used for trading fee, please enable it in Binance!")
BNB_WARNING += 1
# quantity of fills * price
FILLS_TOTAL += (FILL_PRICE * FILL_QTY)
# add to running total of fills quantity
FILLS_QTY += FILL_QTY
# increase fills array index by 1
# calculate average fill price:
FILL_AVG = (FILLS_TOTAL / FILLS_QTY)
#tradeFeeApprox = (float(FILLS_QTY) * float(FILL_AVG)) * (TRADING_FEE/100)
# Olorin Sledge: I only want fee at the unit level, not the total level
tradeFeeApprox = float(FILL_AVG) * (TRADING_FEE/100)
# the volume size is sometimes outside of precision, correct it
try:
info = client.get_symbol_info(order_details['symbol'])
step_size = info['filters'][2]['stepSize']
lot_size = step_size.index('1') - 1
if lot_size <= 0:
FILLS_QTY = int(FILLS_QTY)
else:
FILLS_QTY = truncate(FILLS_QTY, lot_size)
except Exception as e:
print(f"extract_order_data(): Exception getting coin {order_details['symbol']} step size! Exception: {e}")
# create object with received data from Binance
transactionInfo = {
'symbol': order_details['symbol'],
'orderId': order_details['orderId'],
'timestamp': order_details['transactTime'],
'avgPrice': float(FILL_AVG),