forked from zyairelai/buy-low-sell-high
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.py
106 lines (91 loc) · 4.76 KB
/
run.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
import os, socket, requests, urllib3, config
from datetime import datetime
from termcolor import colored
from binance.client import Client
from binance.exceptions import BinanceAPIException
from apscheduler.schedulers.blocking import BlockingScheduler
# Get environment variables
api_key = os.environ.get('BINANCE_KEY')
api_secret = os.environ.get('BINANCE_SECRET')
client = Client(api_key, api_secret)
# Value from config.py
live_trade = config.live_trade
base = config.base
core = config.core
quote = config.quote
margin_percentage = config.margin_percentage
enable_scheduler = config.enable_scheduler
# Trading Setup
pair,round_off = [], []
for i in range(len(base)):
if len(quote) > 1 : my_quote_asset = quote[i]
else: my_quote_asset = quote[0]
pair.append(base[i] + quote[0])
for coin in quote:
if coin == "USDT": decimal = 2
elif coin == "BTC": decimal = 6
elif coin == "ETH": decimal = 5
elif coin == "BNB": decimal = 3
else: decimal == 4
round_off.append(decimal)
def buy_low_sell_high():
for i in range(len(pair)):
# Auto Adjust FIXED or DYNAMIC variable
if len(quote) > 1 : my_quote_asset = quote[i]
else: my_quote_asset = quote[0]
if len(core) > 1 : my_core_number = core[i]
else: my_core_number = core[0]
if len(round_off) > 1 : my_round_off = round_off[i]
else: my_round_off = round_off[0]
# Retrieve Current Asset INFO
asset_info = client.get_symbol_ticker(symbol=pair[i])
asset_price = float(asset_info.get("price"))
asset_balance = float(client.get_asset_balance(asset=base[i]).get("free"))
# Computing for Trade Quantity
current_holding = round(asset_balance * asset_price, my_round_off)
change_percent = round(((current_holding - my_core_number) / my_core_number * 100), 4)
trade_amount = round(abs(current_holding - my_core_number), my_round_off)
# Output Console and Placing Order
if (current_holding > my_core_number) and (abs(change_percent) > margin_percentage):
if live_trade: client.order_market_sell(symbol=pair[i], quoteOrderQty=trade_amount)
print(colored(asset_info, "green"))
print(colored("Created at : " + str(datetime.today().strftime("%d-%m-%Y @ %H:%M:%S")), "green"))
print(colored("Prefix Core : " + str(my_core_number) + " " + my_quote_asset, "green"))
print(colored("Current Core : " + str(current_holding) + " " + my_quote_asset, "green"))
print(colored("Percentage Changed : " + str(change_percent) + " %", "green"))
print(colored("Action : SELL " + str(trade_amount) + " " + my_quote_asset + "\n", "green"))
elif (current_holding < my_core_number) and (abs(change_percent) > margin_percentage):
if live_trade: client.order_market_buy(symbol=pair[i], quoteOrderQty=trade_amount)
print(colored(asset_info, "red"))
print(colored("Created at : " + str(datetime.today().strftime("%d-%m-%Y @ %H:%M:%S")), "red"))
print(colored("Prefix Core : " + str(my_core_number) + " " + my_quote_asset, "red"))
print(colored("Current Core : " + str(current_holding) + " " + my_quote_asset, "red"))
print(colored("Percentage Changed : " + str(change_percent) + " %", "red"))
print(colored("Action : BUY " + str(trade_amount) + " " + my_quote_asset + "\n", "red"))
else:
print(asset_info)
print("Created at : " + str(datetime.today().strftime("%d-%m-%Y @ %H:%M:%S")))
print("Prefix Core : " + str(my_core_number) + " " + my_quote_asset)
print("Current Core : " + str(current_holding) + " " + my_quote_asset)
print("Percentage Changed : " + str(change_percent) + " %")
print("Action : Do Nothing\n")
try:
if live_trade and enable_scheduler:
print(colored("The program is running.\n", "green"))
scheduler = BlockingScheduler()
scheduler.add_job(buy_low_sell_high, 'cron', hour='0,6,12,18')
scheduler.start()
else: buy_low_sell_high()
except (KeyError,
socket.timeout,
BinanceAPIException,
ConnectionResetError,
urllib3.exceptions.ProtocolError,
urllib3.exceptions.ReadTimeoutError,
requests.exceptions.ConnectionError,
requests.exceptions.ConnectTimeout,
requests.exceptions.ReadTimeout) as e:
with open("Error_Message.txt", "a") as error_message:
error_message.write("[!] Created at : " + datetime.today().strftime("%d-%m-%Y @ %H:%M:%S") + "\n")
error_message.write(str(e) + "\n\n")
except KeyboardInterrupt: print("\n\nAborted.\n")