This commit is contained in:
msincenselee 2020-03-26 00:07:14 +08:00
parent a7c0e3273b
commit df17ecccb4
7 changed files with 88 additions and 50 deletions

13
kill_celery.sh Normal file
View File

@ -0,0 +1,13 @@
#!/bin/bash
PROGRAM_NAME=celery
# Kill old processes
echo "-------PROGRAM_NAME1--------"
ID=`ps -ef | grep $PROGRAM_NAME | grep -v "$0" | grep -v "grep" | grep "python" | awk '{print $2}'`
echo $ID
for id in $ID
do
kill -9 $id
echo "killed $id"
done

View File

@ -1 +1 @@
celery -A vnpy.task.celery_app worker --max-tasks-per-child 1 -l debug > tests/celery/worker.log 2>tests/celery/worker-error.log & celery -A vnpy.task.celery_app worker --max-tasks-per-child 1 -l info > tests/celery/worker.log 2>tests/celery/worker-error.log &

View File

@ -600,7 +600,7 @@ class CtaEngine(BaseEngine):
order = self.main_engine.get_order(vt_orderid) order = self.main_engine.get_order(vt_orderid)
if not order: if not order:
self.write_log(msg=f"撤单失败,找不到委托{vt_orderid}", self.write_log(msg=f"撤单失败,找不到委托{vt_orderid}",
strategy_Name=strategy.strategy_name, strategy_name=strategy.strategy_name,
level=logging.ERROR) level=logging.ERROR)
return False return False

View File

@ -638,6 +638,16 @@ class CtaFutureTemplate(CtaTemplate):
self.gt.save() self.gt.save()
self.display_grids() self.display_grids()
def sync_data(self):
"""同步更新数据"""
if not self.backtesting:
self.write_log(u'保存k线缓存数据')
self.save_klines_to_cache()
if self.inited and self.trading:
self.write_log(u'保存policy数据')
self.policy.save()
def get_positions(self): def get_positions(self):
""" """
获取策略当前持仓(重构使用主力合约 获取策略当前持仓(重构使用主力合约
@ -781,7 +791,7 @@ class CtaFutureTemplate(CtaTemplate):
grid.open_status = True grid.open_status = True
self.write_log(f'{grid.direction.value}单已开仓完毕,order_price:{order.price}' self.write_log(f'{grid.direction.value}单已开仓完毕,order_price:{order.price}'
+ f',volume:{order.volume}') + f',volume:{order.volume}')
self.gt.save()
# 网格的所有委托单部分执行完毕 # 网格的所有委托单部分执行完毕
else: else:
old_traded_volume = grid.traded_volume old_traded_volume = grid.traded_volume
@ -794,6 +804,7 @@ class CtaFutureTemplate(CtaTemplate):
self.write_log(f'剩余委托单号:{grid.order_ids}') self.write_log(f'剩余委托单号:{grid.order_ids}')
# 在策略得活动订单中,移除 # 在策略得活动订单中,移除
self.write_log(f'移除活动订单:{order.vt_orderid}')
self.active_orders.pop(order.vt_orderid, None) self.active_orders.pop(order.vt_orderid, None)
def on_order_open_canceled(self, order: OrderData): def on_order_open_canceled(self, order: OrderData):
@ -1059,9 +1070,10 @@ class CtaFutureTemplate(CtaTemplate):
# 发出平多委托 # 发出平多委托
if grid.traded_volume > 0: if grid.traded_volume > 0:
grid.volume -= grid.traded_volume grid.volume -= grid.traded_volume
grid.volume = round(grid.volume, 7)
grid.traded_volume = 0 grid.traded_volume = 0
grid.volume = round(grid.volume, 7)
if 0 < self.account_pos.long_pos < grid.volume: if 0 < self.account_pos.long_pos < grid.volume:
self.write_error(u'当前{}的多单持仓:{},不满足平仓目标:{},强制降低' self.write_error(u'当前{}的多单持仓:{},不满足平仓目标:{},强制降低'
.format(self.vt_symbol, .format(self.vt_symbol,
@ -1110,9 +1122,10 @@ class CtaFutureTemplate(CtaTemplate):
# 发出cover委托 # 发出cover委托
if grid.traded_volume > 0: if grid.traded_volume > 0:
grid.volume -= grid.traded_volume grid.volume -= grid.traded_volume
grid.volume = round(grid.volume, 7)
grid.traded_volume = 0 grid.traded_volume = 0
grid.volume = round(grid.volume, 7)
if 0 < abs(self.account_pos.short_pos) < grid.volume: if 0 < abs(self.account_pos.short_pos) < grid.volume:
self.write_error(u'当前{}的空单持仓:{},不满足平仓目标:{}, 强制降低' self.write_error(u'当前{}的空单持仓:{},不满足平仓目标:{}, 强制降低'
.format(self.vt_symbol, .format(self.vt_symbol,

View File

@ -38,48 +38,35 @@ class CtaGrid(object):
包括交易方向开仓价格平仓价格止损价格开仓状态平仓状态 包括交易方向开仓价格平仓价格止损价格开仓状态平仓状态
""" """
def __init__(self, def __init__(self, **kwargs ):
direction: Direction = None,
open_price: float = 0,
close_price: float = 0,
stop_price: float = 0,
vt_symbol: str = '',
volume: float = 0,
traded_volume: float = 0,
order_status: bool = False,
open_status: bool = False,
close_status: bool = False,
open_time: datetime = None,
order_time: datetime = None,
reuse_count: int = 0,
type: str = ''
):
self.id: str = str(uuid.uuid1()) # gid self.id: str = kwargs.get('id', str(uuid.uuid1())) # gid
self.direction = direction # 交易方向LONG正套SHORT反套 self.direction = kwargs.get('direction', None) # 交易方向LONG正套SHORT反套
self.open_price = open_price # 开仓价格 if isinstance(self.direction, str):
self.close_price = close_price # 止盈价格 self.direction = Direction(self.direction)
self.stop_price = stop_price # 止损价格 self.open_price = kwargs.get('open_price', 0) # 开仓价格
self.vt_symbol = vt_symbol # 品种合约 self.close_price = kwargs.get('close_price', 0) # 止盈价格
self.volume = volume # 开仓数量( 兼容数字货币 ) self.stop_price = kwargs.get('stop_price', 0) # 止损价格
self.traded_volume = traded_volume # 已成交数量 开仓时,为开仓数量,平仓时,为平仓数量 self.vt_symbol = kwargs.get('vt_symbol', '') # 品种合约
self.order_status = order_status # 挂单状态: True,已挂单False未挂单 self.volume = kwargs.get('volume', 0.0) # 开仓数量( 兼容数字货币 )
self.order_ids = [] # order_id list self.traded_volume = kwargs.get('traded_volume', 0.0) # 已成交数量 开仓时,为开仓数量,平仓时,为平仓数量
self.open_status = open_status # 开仓状态 self.order_status = kwargs.get('order_status', False) # 挂单状态: True,已挂单False未挂单
self.close_status = close_status # 平仓状态 self.order_ids = kwargs.get('order_ids',[]) # order_id list
self.open_time = open_time # 开仓时间 self.open_status = kwargs.get('open_status', False) # 开仓状态
self.order_time = order_time # 委托时间 self.close_status = kwargs.get('close_status', False) # 平仓状态
self.lock_grid_ids = [] # 锁单的网格,[gid,gid] self.open_time = kwargs.get('open_time', None) # 开仓时间
self.reuse_count = reuse_count # 重用次数0 平仓后是否删除) self.order_time = kwargs.get('order_time', None) # 委托时间
self.type = type # 网格类型标签 self.lock_grid_ids = kwargs.get('lock_grid_ids', []) # 锁单的网格,[gid,gid]
self.snapshot = {} # 切片数据,如记录开仓点时的某些状态数据 self.reuse_count = kwargs.get('reuse_count', 0) # 重用次数0 平仓后是否删除)
self.type = kwargs.get('type', '') # 网格类型标签
self.snapshot = kwargs.get('snapshot',{}) # 切片数据,如记录开仓点时的某些状态数据
def to_json(self): def to_json(self):
"""输出JSON""" """输出JSON"""
j = OrderedDict() j = OrderedDict()
j['id'] = self.id j['id'] = self.id
j['direction'] = self.direction.value if self.direction else '' j['direction'] = self.direction.value if isinstance(self.direction, Direction) else ''
j['open_price'] = self.open_price # 开仓价格 j['open_price'] = self.open_price # 开仓价格
j['close_price'] = self.close_price # 平仓价格 j['close_price'] = self.close_price # 平仓价格
j['stop_price'] = self.stop_price # 止损价格 j['stop_price'] = self.stop_price # 止损价格
@ -90,7 +77,7 @@ class CtaGrid(object):
j['order_ids'] = self.order_ids # OrderId j['order_ids'] = self.order_ids # OrderId
j['open_status'] = self.open_status # 开仓状态 j['open_status'] = self.open_status # 开仓状态
j['close_status'] = self.close_status # 平仓状态 j['close_status'] = self.close_status # 平仓状态
j['lockGrids'] = self.lock_grid_ids # 对锁的网格 j['lock_grid_ids'] = self.lock_grid_ids # 对锁的网格
j['reuse_count'] = self.reuse_count # 是否重用 j['reuse_count'] = self.reuse_count # 是否重用
j['type'] = self.type # 类型 j['type'] = self.type # 类型
j['snapshot'] = self.snapshot # 切片数据 j['snapshot'] = self.snapshot # 切片数据
@ -124,7 +111,7 @@ class CtaGrid(object):
self.vt_symbol = j.get('vt_symbol', '') self.vt_symbol = j.get('vt_symbol', '')
self.volume = j.get('volume', 0.0) self.volume = j.get('volume', 0.0)
self.traded_volume = j.get('traded_volume', 0.0) # 已交易的合约数量 self.traded_volume = j.get('traded_volume', 0.0) # 已交易的合约数量
self.lock_grid_ids = j.get('lockGrids', []) self.lock_grid_ids = j.get('lock_grid_ids', [])
self.type = j.get('type', '') self.type = j.get('type', '')
self.reuse_count = j.get('reuse_count', 0) self.reuse_count = j.get('reuse_count', 0)
@ -900,8 +887,8 @@ class CtaGridTrade(CtaComponent):
data[u'up_grids'] = up_grids data[u'up_grids'] = up_grids
data[u'dn_grids'] = dn_grids data[u'dn_grids'] = dn_grids
with open(grid_json_file, 'w') as f: with open(grid_json_file, 'w', encoding='utf8') as f:
json_data = json.dumps(data, indent=4) json_data = json.dumps(data, indent=4, ensure_ascii=True)
f.write(json_data) f.write(json_data)
self.write_log(u'GrideTrade保存文件{}完成'.format(grid_json_file)) self.write_log(u'GrideTrade保存文件{}完成'.format(grid_json_file))
@ -928,7 +915,7 @@ class CtaGridTrade(CtaComponent):
self.write_log(u'{}不存在,新建保存保存'.format(grid_json_file)) self.write_log(u'{}不存在,新建保存保存'.format(grid_json_file))
try: try:
with open(grid_json_file, 'w') as f: with open(grid_json_file, 'w') as f:
json_data = json.dumps(data, indent=4) json_data = json.dumps(data, indent=4, ensure_ascii=True)
f.write(json_data) f.write(json_data)
except Exception as ex: except Exception as ex:
self.write_log(u'写入网格文件{}异常:{}'.format(grid_json_file, str(ex))) self.write_log(u'写入网格文件{}异常:{}'.format(grid_json_file, str(ex)))
@ -951,7 +938,7 @@ class CtaGridTrade(CtaComponent):
grids = [] grids = []
for grid_obj in json_grids: for grid_obj in json_grids:
grid = CtaGrid(grid_obj) grid = CtaGrid(**grid_obj)
self.write_log(grid.to_str()) self.write_log(grid.to_str())

25
vnpy/data/renko/readme.md Normal file
View File

@ -0,0 +1,25 @@
期货砖块图数据
1.重建
砖图高度为3510, K3, K5, K10个最小跳动
从mongo数据库获取最后一笔记录得日期/时间
从tdx数据源获取历史分笔数据开始时间结束时间/当前时间, 合约代码)=》 Queue
从Queue=》 renko on Tick
2. 查询
从mongo数据库获取数据。
缓存至本地(数量切片缓存)
返回K线记录
股票砖图数据
1.重建
砖图高度为max(价格千分之一,0.01)得3510个最小跳动
从mongo数据库获取最后一笔记录得日期/时间
从tdx数据源获取历史分笔数据开始时间结束时间/当前时间, 合约代码)=》 Queue
从Queue=》 renko on Tick
2. 查询
从mongo数据库获取数据。
缓存至本地(数量切片缓存)
返回K线记录

View File

@ -412,7 +412,7 @@ class BinancefRestApi(RestClient):
"side": DIRECTION_VT2BINANCEF[req.direction], "side": DIRECTION_VT2BINANCEF[req.direction],
"type": ORDERTYPE_VT2BINANCEF[req.type], "type": ORDERTYPE_VT2BINANCEF[req.type],
"price": float(req.price), "price": float(req.price),
"quantity": int(req.volume), "quantity": round(req.volume, 7),
"newClientOrderId": orderid, "newClientOrderId": orderid,
"newOrderRespType": "ACK" "newOrderRespType": "ACK"
} }
@ -513,7 +513,7 @@ class BinancefRestApi(RestClient):
self.gateway.write_log(json.dumps(position, indent=2)) self.gateway.write_log(json.dumps(position, indent=2))
self.contracts.update({symbol: position}) self.contracts.update({symbol: position})
self.gateway.write_log("账户资金查询成功") # self.gateway.write_log("账户资金查询成功")
def on_query_position(self, data: dict, request: Request) -> None: def on_query_position(self, data: dict, request: Request) -> None:
"""""" """"""
@ -581,7 +581,7 @@ class BinancefRestApi(RestClient):
self.gateway.on_position(long_position) self.gateway.on_position(long_position)
self.gateway.on_position(short_position) self.gateway.on_position(short_position)
self.gateway.write_log("持仓信息查询成功") # self.gateway.write_log("持仓信息查询成功")
def on_query_order(self, data: dict, request: Request) -> None: def on_query_order(self, data: dict, request: Request) -> None:
"""""" """"""
@ -850,7 +850,7 @@ class BinancefTradeWebsocketApi(WebsocketClient):
symbol=pos_data["s"], symbol=pos_data["s"],
exchange=Exchange.BINANCE, exchange=Exchange.BINANCE,
direction=Direction.NET, direction=Direction.NET,
volume=int(float(pos_data["pa"])), volume=round(float(pos_data["pa"]), 7),
price=float(pos_data["ep"]), price=float(pos_data["ep"]),
pnl=float(pos_data["cr"]), pnl=float(pos_data["cr"]),
gateway_name=self.gateway_name, gateway_name=self.gateway_name,