-
Notifications
You must be signed in to change notification settings - Fork 6
/
strategy.py
59 lines (47 loc) · 1.82 KB
/
strategy.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
# -*- coding:utf-8 -*-
# 策略实现
from quant.utils import logger
from quant.config import config
from quant.heartbeat import heartbeat
from quant.order import ORDER_ACTION_BUY
from quant.order_manager import OrderManager
from quant.const import BINANCE
class MyStrategy:
def __init__(self):
""" 初始化
"""
self.platfrom = BINANCE
self.account = config.platforms.get(self.platfrom, {}).get("account")
self.access_key = config.platforms.get(self.platfrom, {}).get("access_key")
self.secret_key = config.platforms.get(self.platfrom, {}).get("secret_key")
self.symbol = config.symbol
self.name = config.strategy
self.order_manager = None
def initialize(self):
""" 初始化
"""
self.order_manager = OrderManager(self.platfrom, self.account, self.access_key, self.secret_key, self.symbol,
self.name)
# 注册订单状态更新回调
self.order_manager.register_callback(self.on_event_order_update)
# 注册定时任务
heartbeat.register(self.do_task)
async def on_event_order_update(self, order):
""" 订单状态更新
"""
logger.info("order update:", order, caller=self)
async def create_order(self):
""" 创建订单
"""
action = ORDER_ACTION_BUY
price = "0.031898"
quantity = "0.2"
order_no = await self.order_manager.create_order(action, price, quantity)
logger.info("create new order:", order_no, caller=self)
async def do_task(self, *args, **kwargs):
""" 定时任务
* NOTE: 每秒钟执行一次,假设在程序启动12秒的时候,挂一个买单
"""
if heartbeat.count != 12:
return
await self.create_order()