-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
111 lines (99 loc) · 3.74 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import datetime
import time
import ephem
from binance import Client
import parameters
from dotenv import load_dotenv
import os
if __name__ == '__main__':
def moon_phase_flag(today_date):
# Full Moon -> Buy Signal
# New Moon -> Sell Signal
# Other -> No action
# Calculate the dates of the upcoming major moon phases.
next_full_moon = ephem.next_full_moon(today_date).datetime().date()
next_new_moon = ephem.next_new_moon(today_date).datetime().date()
# Calculate Flag
if next_full_moon == today_date:
return True
elif next_new_moon == today_date:
return False
else:
return None
#endIf
#endDef
def buy_quantity(client, buy_fraction):
busd_amount = float(client.get_asset_balance(asset = 'BUSD')['free'])
btc_to_busd = float(client.get_symbol_ticker(symbol="BTCBUSD")['price'])
quantity = buy_fraction * busd_amount / btc_to_busd
return quantity
#endDef
def sell_quantity(client, sell_fraction):
btc_amount = float(client.get_asset_balance(asset = 'BTC')['free'])
quantity = sell_fraction * btc_amount
return quantity
#endDef
def buy_action(client, pair_symbol, order_quantity):
order = client.order_market_buy(symbol = pair_symbol, quantity = order_quantity)
print(order)
return order
#endDef
def sell_action(client, pair_symbol, order_quantity):
order = client.order_market_sell(symbol = pair_symbol, quantity = order_quantity)
print(order)
return order
#endDef
def configure():
load_dotenv()
#endDef
## Grab API data from .env file.
configure()
api_key = os.getenv('api_key')
api_secret = os.getenv('api_secret')
# For the Binance Client
## Add tld='us' as an argument if using binance.us instead of binance.com .
## Add testnet=True as an argument if working with testnet.
client = Client(api_key, api_secret)
# Grab trading infromation.
symbol = parameters.symbol
buy_fraction = parameters.buy_fraction
sell_fraction = parameters.sell_fraction
while True:
today_date = datetime.date.today()
flag = moon_phase_flag(today_date)
print('The flag is '+flag+'.')
if flag is None:
print('No action taken today.')
try:
print(client.get_account())
except:
print("Error retrieving account information. Retrying now.")
time.sleep(60)
continue
# sleep for 13 hours and try again.
time.sleep(13*60*60)
elif flag is False:
try:
sell_quantity = sell_quantity(client, sell_fraction)
sell_action(client = client, pair_symbol = symbol, order_quantity = sell_quantity)
except:
print('Something went wrong, retrying now.')
continue
btc_price = client.get_symbol_ticker(symbol="BTCBUSD")['price']
print('Sold '+sell_quantity+' BTC at '+btc_price+' BUSD.')
# sleep for 24 hours and try again.
time.sleep(24*60*60)
elif flag is True:
try:
buy_quantity = buy_quantity(client, buy_fraction)
buy_action(client = client, pair_symbol = symbol, order_quantity = buy_quantity)
except:
print('Something went wrong, retrying now.')
continue
btc_price = client.get_symbol_ticker(symbol="BTCBUSD")['price']
print('Sold '+sell_quantity+' BTC at '+btc_price+' BUSD.')
# sleep for 24 hours and try again.
time.sleep(24*60*60)
#endIf
#endWhile
#endIf