Skip to content

Commit

Permalink
Merge pull request shidenggui#38 from weichaoguo/master
Browse files Browse the repository at this point in the history
fix is_trade_date & its usage.
  • Loading branch information
shidenggui authored Jun 15, 2016
2 parents 3fe9476 + 8f176ec commit 41467e7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 5 deletions.
6 changes: 5 additions & 1 deletion easyquant/easydealutils/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ def is_holiday(now_time):
return _is_holiday(today)


def is_weekend(now_time):
return now_time.weekday() >= 5


def is_trade_date(now_time):
return not is_holiday(now_time)
return not (is_holiday(now_time) or is_weekend(now_time))


def get_next_trade_date(now_time):
Expand Down
9 changes: 5 additions & 4 deletions easyquant/push_engine/clock_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def update_next_time(self):
)

def is_active(self):
if self.is_trading_date and etime.is_holiday(self.clock_engine.now_dt):
if self.is_trading_date and not etime.is_trade_date(self.clock_engine.now_dt):
# 仅在交易日触发时的判断
return False
return self.next_time <= self.clock_engine.now_dt
Expand Down Expand Up @@ -116,11 +116,12 @@ def __init__(self, event_engine, now=None, tzinfo=None):
self.is_active = True
self.clock_engine_thread = Thread(target=self.clocktick)
self.sleep_time = 1
self.trading_state = True if etime.is_tradetime(datetime.datetime.now()) else False
self.trading_state = True if (etime.is_tradetime(datetime.datetime.now()) and etime.is_trade_date(datetime.datetime.now())) else False
self.clock_moment_handlers = deque()
self.clock_interval_handlers = set()

self._init_clock_handler()
if self.trading_state:
self._init_clock_handler()

def _init_clock_handler(self):
"""
Expand Down Expand Up @@ -192,7 +193,7 @@ def clocktick(self):
time.sleep(self.sleep_time)

def tock(self):
if etime.is_holiday(self.now_dt):
if not etime.is_trade_date(self.now_dt):
pass # 假日暂停时钟引擎
else:
self._tock()
Expand Down
53 changes: 53 additions & 0 deletions strategies/xq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
from easyquant import StrategyTemplate


class Strategy(StrategyTemplate):
name = 'xq'

def strategy(self, event):
self.log.info('\n\nxq strategy')
#self.log.info('检查持仓')
#self.log.info(self.user.balance)
#self.log.info('\n')
if self.clock_engine.trading_state:
self.log.info('trading')
else:
self.log.info('not trading')

def __tracing(self):
xqzh = {"ZH010389": 0.2, "ZH572114": 0.4, "ZH062130": 0.4}
stocks = {}
for pos in self.user.get_position():
stocks[pos['stock_code']] = pos['market_value']

for zh in xqzh:
for pos in self.user.get_position(zh):
stocks[pos['stock_code']] = stocks.get(pos['stock_code'], 0) - xqzh[zh] * pos['market_value']
for code in stocks:
if stocks[code] > 10000:
try:
self.user.sell(code, 0, 0, stocks[code])
except:
self.log.info('sell error')
for code in stocks:
if stocks[code] < -10000:
try:
self.user.buy(code, 0, 0, -stocks[code])
except:
self.log.info('buy error')

def clock(self, event):
"""在交易时间会定时推送 clock 事件
:param event: event.data.clock_event 为 [0.5, 1, 3, 5, 15, 30, 60] 单位为分钟, ['open', 'close'] 为开市、收市
event.data.trading_state bool 是否处于交易时间
"""
if event.data.clock_event == 'open':
# 开市了
self.log.info('open')
elif event.data.clock_event == 'close':
# 收市了
self.log.info('close')
elif event.data.clock_event == 5:
# 5 分钟的 clock
self.log.info("5分钟")
self.__tracing()
17 changes: 17 additions & 0 deletions xqzh.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import sys
from datetime import timedelta, timezone

import easyquotation
from easyquant.push_engine.clock_engine import ClockEngine

import easyquant
from easyquant import DefaultQuotationEngine, DefaultLogHandler, PushBaseEngine


quotation_engine = DefaultQuotationEngine

quotation_engine.PushInterval = 30

m = easyquant.MainEngine(broker='xq', need_data=sys.argv[1], quotation_engines=[quotation_engine], tzinfo=timezone(timedelta(hours=8)))
m.load_strategy()
m.start()

0 comments on commit 41467e7

Please sign in to comment.