Skip to content

Commit

Permalink
- change : 更新了 readme 添加了 shutdown 机制的例子
Browse files Browse the repository at this point in the history
  • Loading branch information
lamter committed Aug 19, 2016
1 parent f50f84b commit 5bab975
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,33 @@ class Strategy(StrategyTemplate):

```python
# 引入策略模板
import time
import datetime as dt
from dateutil import tz
from easyquant import DefaultLogHandler
from easyquant import StrategyTemplate

# 定义策略类

class Strategy(StrategyTemplate):
name = '测试策略1' # 定义策略名字
name = '测试策略1'

def init(self):
# 进行相关的初始化定义
self.buy_stocks = [] # 假如一个股票一天只想买入一次。定义一个列表用来存储策略买过的股票

# 策略函数,收到行情推送后会自动调用
# 通过下面的方式来获取时间戳
now_dt = self.clock_engine.now_dt
now = self.clock_engine.now
now = time.time()

# 注册时钟事件
clock_type = "盘尾"
moment = dt.time(14, 56, 30, tzinfo=tz.tzlocal())
self.clock_engine.register_moment(clock_type, moment)

# 注册时钟间隔事件, 不在交易阶段也会触发, clock_type == minute_interval
minute_interval = 1.5
self.clock_engine.register_interval(minute_interval, trading=False)

def strategy(self, event):
""":param event event.data 为所有股票行情的字典,结构如下
""":param event event.data 为所有股票的信息,结构如下
{'162411':
{'ask1': '0.493',
'ask1_volume': '75500',
Expand Down Expand Up @@ -189,18 +203,15 @@ class Strategy(StrategyTemplate):
'turnover': '420004912',
'volume': '206390073.351'}}
"""
# 使用 self.user 来操作账户,用法见 easytrader 用法
# 使用 self.user 来操作账户,用法同 easytrader 用法
# 使用 self.log.info('message') 来打印你所需要的 log
print('demo1 的 log 使用自定义 log 的方式记录在 demo1.log')
self.log.info('\n\n策略1触发')
self.log.info('行情数据: 万科价格: %s' % event.data['000002'])
self.log.info('检查持仓')
self.log.info(self.user.balance)
# 在未买入的情况下买入万科
if '000002' not in self.buy_stocks:
# self.user.buy('000002')
self.buy_stocks.append('000002')
self.log.info('\n')

# 时钟引擎,用于推送开市、收市以及其他定时事件
def clock(self, event):
"""在交易时间会定时推送 clock 事件
:param event: event.data.clock_event 为 [0.5, 1, 3, 5, 15, 30, 60] 单位为分钟, ['open', 'close'] 为开市、收市
Expand All @@ -212,11 +223,20 @@ class Strategy(StrategyTemplate):
elif event.data.clock_event == 'close':
# 收市了
self.log.info('close')
# 收盘时清空已买股票列表 self.buy_stocks
self.buy_stocks.clear()
elif event.data.clock_event == 5:
# 5 分钟的 clock
self.log.info("5分钟")

def log_handler(self):
"""自定义 log 记录方式"""
return DefaultLogHandler(self.name, log_type='stdout', filepath='demo1.log')

def shutdown(self):
"""
关闭进程前的调用
:return:
"""
self.log.info("假装在关闭前保存了策略数据")
```

#### 加载指定策略
Expand Down

0 comments on commit 5bab975

Please sign in to comment.