-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathstrat.py
100 lines (67 loc) · 2.48 KB
/
strat.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
from datetime import datetime
from dateutil import tz
import signal
import schedule
import threading
from time import sleep
from market_maker.utils import log
from bot import FundingBot
import settings
logger = log.setup_custom_logger('strat')
def half_funding(bot: FundingBot) -> None:
"""4 hours until funding: enter a position
if funding is negative, go long
if funding is positive, go short
"""
bot.could_hedge = False
bot.exit_position(market=False, wait_for_fill=True)
bot.cancel_open_orders()
funding_rate = bot.get_funding_rate()
logger.info('funding rate: %.4f%%' % (funding_rate * 100))
if funding_rate < 0:
side = 'Buy'
quantity = settings.POSITION_SIZE_BUY
else:
side = 'Sell'
quantity = settings.POSITION_SIZE_SELL
bot.enter_position(side, quantity, market=False)
def funding_over(bot: FundingBot) -> None:
"""funding is over, exit all positions"""
sleep(1)
bot.exit_position(market=False, wait_for_fill=True)
def main() -> None:
"""place bitmex orders based on current funding rate
if 4 hours until funding: enter a position
if funding is negative, go long
if funding is positive, go short
if the price moves negatively 1.5% away from a position, exit the position
if funding is over, exit all positions
"""
bot = FundingBot()
signal.signal(signal.SIGTERM, bot.exit)
signal.signal(signal.SIGINT, bot.exit)
def convert_utc(utc_time : str) -> str:
utc = datetime.strptime(utc_time, '%H:%M')
utc = utc.replace(tzinfo=tz.tzutc())
local = utc.astimezone(tz.tzlocal())
return local.strftime('%H:%M')
schedule.every().day.at(convert_utc('23:50')).do(half_funding, bot)
schedule.every().day.at(convert_utc('04:00')).do(funding_over, bot)
schedule.every().day.at(convert_utc('07:50')).do(half_funding, bot)
schedule.every().day.at(convert_utc('12:00')).do(funding_over, bot)
schedule.every().day.at(convert_utc('15:50')).do(half_funding, bot)
schedule.every().day.at(convert_utc('20:00')).do(funding_over, bot)
def run_scheduled() -> None:
while True:
schedule.run_pending()
sleep(1)
sched = threading.Thread(target=run_scheduled)
sched.daemon = True
sched.start()
try:
bot.run_loop()
except Exception as e:
logger.error('bot exiting with exception: %s' % str(e))
bot.exit()
if __name__ == '__main__':
main()