[Fix] check typo bug with flake8
This commit is contained in:
parent
346368cd2e
commit
d05b558f87
File diff suppressed because one or more lines are too long
@ -22,15 +22,12 @@ from .base import (
|
||||
STOPORDER_PREFIX,
|
||||
StopOrder,
|
||||
StopOrderStatus,
|
||||
BacktestingMode,
|
||||
ORDER_CTA2VT,
|
||||
)
|
||||
from .template import CtaTemplate
|
||||
|
||||
sns.set_style("whitegrid")
|
||||
|
||||
|
||||
|
||||
class OptimizationSetting:
|
||||
"""
|
||||
Setting for runnning optimization.
|
||||
@ -77,8 +74,8 @@ class OptimizationSetting:
|
||||
products = list(product(*values))
|
||||
|
||||
settings = []
|
||||
for product in products:
|
||||
setting = dict(zip(keys, product))
|
||||
for p in products:
|
||||
setting = dict(zip(keys, p))
|
||||
settings.append(setting)
|
||||
|
||||
return settings
|
||||
@ -419,7 +416,7 @@ class BacktestingEngine:
|
||||
if not df:
|
||||
df = self.daily_df
|
||||
|
||||
fig = plt.figure(figsize=(10, 16))
|
||||
plt.figure(figsize=(10, 16))
|
||||
|
||||
balance_plot = plt.subplot(4, 1, 1)
|
||||
balance_plot.set_title("Balance")
|
||||
@ -472,7 +469,7 @@ class BacktestingEngine:
|
||||
self.capital,
|
||||
self.end,
|
||||
self.mode
|
||||
)))
|
||||
)))
|
||||
results.append(result)
|
||||
|
||||
pool.close()
|
||||
@ -746,7 +743,7 @@ class BacktestingEngine:
|
||||
|
||||
return order.vt_orderid
|
||||
|
||||
def cancel_order(self, vt_orderid: str):
|
||||
def cancel_order(self, strategy: CtaTemplate, vt_orderid: str):
|
||||
"""
|
||||
Cancel order by vt_orderid.
|
||||
"""
|
||||
@ -755,7 +752,7 @@ class BacktestingEngine:
|
||||
else:
|
||||
self.cancel_limit_order(vt_orderid)
|
||||
|
||||
def cancel_stop_order(self, vt_orderid: str):
|
||||
def cancel_stop_order(self, strategy: CtaTemplate, vt_orderid: str):
|
||||
""""""
|
||||
if vt_orderid not in self.active_stop_orders:
|
||||
return
|
||||
@ -764,7 +761,7 @@ class BacktestingEngine:
|
||||
stop_order.status = StopOrderStatus.CANCELLED
|
||||
self.strategy.on_stop_order(stop_order)
|
||||
|
||||
def cancel_limit_order(self, vt_orderid: str):
|
||||
def cancel_limit_order(self, strategy: CtaTemplate, vt_orderid: str):
|
||||
""""""
|
||||
if vt_orderid not in self.active_limit_orders:
|
||||
return
|
||||
@ -779,11 +776,11 @@ class BacktestingEngine:
|
||||
"""
|
||||
vt_orderids = list(self.active_limit_orders.keys())
|
||||
for vt_orderid in vt_orderids:
|
||||
self.cancel_limit_order(vt_orderid)
|
||||
self.cancel_limit_order(strategy, vt_orderid)
|
||||
|
||||
stop_orderids = list(self.active_stop_orders.keys())
|
||||
for vt_orderid in stop_orderids:
|
||||
self.cancel_stop_order(vt_orderid)
|
||||
self.cancel_stop_order(strategy, vt_orderid)
|
||||
|
||||
def write_log(self, msg: str, strategy: CtaTemplate = None):
|
||||
"""
|
||||
@ -905,7 +902,7 @@ def optimize(
|
||||
capital=capital,
|
||||
end=end,
|
||||
mode=mode
|
||||
)
|
||||
)
|
||||
|
||||
engine.add_strategy(strategy_class, setting)
|
||||
engine.load_data()
|
||||
@ -913,7 +910,5 @@ def optimize(
|
||||
engine.calculate_result()
|
||||
statistics = engine.calculate_statistics()
|
||||
|
||||
target_value = result[target_name]
|
||||
target_value = statistics[target_name]
|
||||
return (str(setting), target_value, statistics)
|
||||
|
||||
|
||||
|
@ -9,17 +9,15 @@ from pathlib import Path
|
||||
from typing import Any, Callable
|
||||
|
||||
from vnpy.event import Event, EventEngine
|
||||
from vnpy.trader.constant import Direction, Interval, PriceType
|
||||
from vnpy.trader.engine import BaseEngine, MainEngine
|
||||
from vnpy.trader.object import (
|
||||
OrderRequest,
|
||||
CancelRequest,
|
||||
SubscribeRequest,
|
||||
LogData,
|
||||
TickData,
|
||||
)
|
||||
from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE
|
||||
from vnpy.trader.constant import Direction, Offset, Exchange, PriceType, Interval
|
||||
from vnpy.trader.constant import Direction, PriceType, Interval
|
||||
from vnpy.trader.utility import get_temp_path
|
||||
from .base import (
|
||||
CtaOrderType,
|
||||
@ -27,9 +25,10 @@ from .base import (
|
||||
StopOrder,
|
||||
StopOrderStatus,
|
||||
EVENT_CTA_LOG,
|
||||
EVENT_CTA_STOPORDER,
|
||||
EVENT_CTA_STRATEGY,
|
||||
EVENT_CTA_STOPORDER,
|
||||
ORDER_CTA2VT,
|
||||
STOPORDER_PREFIX
|
||||
)
|
||||
from .template import CtaTemplate
|
||||
|
||||
@ -127,10 +126,10 @@ class CtaEngine(BaseEngine):
|
||||
continue
|
||||
|
||||
long_triggered = (
|
||||
so.direction == Direction.LONG and tick.last_price >= stop_order.price
|
||||
stop_order.direction == Direction.LONG and tick.last_price >= stop_order.price
|
||||
)
|
||||
short_triggered = (
|
||||
so.direction == Direction.SHORT and tick.last_price <= stop_order.price
|
||||
stop_order.direction == Direction.SHORT and tick.last_price <= stop_order.price
|
||||
)
|
||||
|
||||
if long_triggered or short_triggered:
|
||||
@ -139,7 +138,7 @@ class CtaEngine(BaseEngine):
|
||||
# To get excuted immediately after stop order is
|
||||
# triggered, use limit price if available, otherwise
|
||||
# use ask_price_5 or bid_price_5
|
||||
if so.direction == Direction.LONG:
|
||||
if stop_order.direction == Direction.LONG:
|
||||
if tick.limit_up:
|
||||
price = tick.limit_up
|
||||
else:
|
||||
@ -160,8 +159,8 @@ class CtaEngine(BaseEngine):
|
||||
self.stop_orders.pop(stop_order.stop_orderid)
|
||||
|
||||
vt_orderids = self.strategy_orderid_map[strategy.name]
|
||||
if stop_orderid in vt_orderids:
|
||||
vt_orderids.remove(stop_orderid)
|
||||
if stop_order.stop_orderid in vt_orderids:
|
||||
vt_orderids.remove(stop_order.stop_orderid)
|
||||
|
||||
# Change stop order status to cancelled and update to strategy.
|
||||
stop_order.status = StopOrderStatus.TRIGGERED
|
||||
@ -241,7 +240,7 @@ class CtaEngine(BaseEngine):
|
||||
|
||||
return stop_orderid
|
||||
|
||||
def cancel_limit_order(self, vt_orderid: str):
|
||||
def cancel_limit_order(self, strategy: CtaTemplate, vt_orderid: str):
|
||||
"""
|
||||
Cancel existing order by vt_orderid.
|
||||
"""
|
||||
@ -253,7 +252,7 @@ class CtaEngine(BaseEngine):
|
||||
req = order.create_cancel_request()
|
||||
self.main_engine.cancel_limit_order(req, order.gateway_name)
|
||||
|
||||
def cancel_stop_order(self, stop_orderid: str):
|
||||
def cancel_stop_order(self, strategy: CtaTemplate, stop_orderid: str):
|
||||
"""
|
||||
Cancel a local stop order.
|
||||
"""
|
||||
@ -289,13 +288,13 @@ class CtaEngine(BaseEngine):
|
||||
else:
|
||||
return self.send_limit_order(strategy, order_type, price, volume)
|
||||
|
||||
def cancel_order(self, vt_orderid: str):
|
||||
def cancel_order(self, strategy: CtaTemplate, vt_orderid: str):
|
||||
"""
|
||||
"""
|
||||
if vt_orderid.startswith(STOPORDER_PREFIX):
|
||||
self.cancel_stop_order(vt_orderid)
|
||||
self.cancel_stop_order(strategy, vt_orderid)
|
||||
else:
|
||||
self.cancel_limit_order(vt_orderid)
|
||||
self.cancel_limit_order(strategy, vt_orderid)
|
||||
|
||||
def cancel_all(self, strategy: CtaTemplate):
|
||||
"""
|
||||
@ -306,7 +305,7 @@ class CtaEngine(BaseEngine):
|
||||
return
|
||||
|
||||
for vt_orderid in vt_orderids:
|
||||
self.cancel_limit_order(vt_orderid)
|
||||
self.cancel_limit_order(strategy, vt_orderid)
|
||||
|
||||
def get_engine_type(self):
|
||||
""""""
|
||||
@ -374,7 +373,10 @@ class CtaEngine(BaseEngine):
|
||||
|
||||
# Subscribe market data
|
||||
contract = self.main_engine.get_contract(strategy.vt_symbol)
|
||||
if not contract:
|
||||
if contract:
|
||||
req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange)
|
||||
self.main_engine.subscribe(req, contract.gateway_name)
|
||||
else:
|
||||
self.write_log(f"行情订阅失败,找不到合约{strategy.vt_symbol}", strategy)
|
||||
|
||||
self.put_strategy_event(strategy)
|
||||
|
@ -181,7 +181,7 @@ class CtaTemplate(ABC):
|
||||
"""
|
||||
Cancel an existing order.
|
||||
"""
|
||||
self.cta_engine.cancel_order(vt_orderid)
|
||||
self.cta_engine.cancel_order(self, vt_orderid)
|
||||
|
||||
def cancel_all(self):
|
||||
"""
|
||||
|
@ -94,11 +94,11 @@ class BitmexGateway(BaseGateway):
|
||||
""""""
|
||||
self.ws_api.subscribe(req)
|
||||
|
||||
def send_order(self, req):
|
||||
def send_order(self, req: OrderRequest):
|
||||
""""""
|
||||
return self.rest_api.send_order(req)
|
||||
|
||||
def cancel_order(self, req):
|
||||
def cancel_order(self, req: CancelRequest):
|
||||
""""""
|
||||
self.rest_api.cancel_order(req)
|
||||
|
||||
@ -197,7 +197,7 @@ class BitmexRestApi(RestClient):
|
||||
|
||||
self.gateway.write_log("REST API启动成功")
|
||||
|
||||
def send_order(self, req: SubscribeRequest):
|
||||
def send_order(self, req: OrderRequest):
|
||||
""""""
|
||||
self.order_count += 1
|
||||
orderid = str(self.connect_time + self.order_count)
|
||||
|
@ -22,22 +22,22 @@ from futu import (
|
||||
RET_OK,
|
||||
StockQuoteHandlerBase,
|
||||
TradeDealHandlerBase,
|
||||
TradeOrderHandlerBase,
|
||||
TradeDealHandlerBase
|
||||
TradeOrderHandlerBase
|
||||
)
|
||||
|
||||
from vnpy.trader.constant import Direction, Exchange, Product, Status
|
||||
from vnpy.trader.event import EVENT_TIMER
|
||||
from vnpy.trader.gateway import BaseGateway
|
||||
from vnpy.trader.object import (
|
||||
TickData,
|
||||
TradeData,
|
||||
AccountData,
|
||||
ContractData,
|
||||
OrderData,
|
||||
PositionData,
|
||||
AccountData,
|
||||
SubscribeRequest,
|
||||
OrderRequest,
|
||||
CancelRequest,
|
||||
CancelRequest
|
||||
)
|
||||
|
||||
EXCHANGE_VT2FUTU = {
|
||||
@ -232,7 +232,7 @@ class FutuGateway(BaseGateway):
|
||||
self.trade_ctx.start()
|
||||
self.write_log("交易接口连接成功")
|
||||
|
||||
def subscribe(self, req):
|
||||
def subscribe(self, req: SubscribeRequest):
|
||||
""""""
|
||||
for data_type in ["QUOTE", "ORDER_BOOK"]:
|
||||
futu_symbol = convert_symbol_vt2futu(req.symbol, req.exchange)
|
||||
@ -241,7 +241,7 @@ class FutuGateway(BaseGateway):
|
||||
if code:
|
||||
self.write_log(f"订阅行情失败:{data}")
|
||||
|
||||
def send_order(self, req):
|
||||
def send_order(self, req: OrderRequest):
|
||||
""""""
|
||||
side = DIRECTION_VT2FUTU[req.direction]
|
||||
price_type = OrderType.NORMAL # Only limit order is supported.
|
||||
@ -274,7 +274,7 @@ class FutuGateway(BaseGateway):
|
||||
self.on_order(order)
|
||||
return order.vt_orderid
|
||||
|
||||
def cancel_order(self, req):
|
||||
def cancel_order(self, req: CancelRequest):
|
||||
""""""
|
||||
code, data = self.trade_ctx.modify_order(
|
||||
ModifyOrderOp.CANCEL, req.orderid, 0, 0, trd_env=self.env
|
||||
|
@ -8,25 +8,26 @@ from threading import Thread
|
||||
|
||||
from ibapi import comm
|
||||
from ibapi.client import EClient
|
||||
from ibapi.common import MAX_MSG_LEN, OrderId, TickAttrib, TickerId
|
||||
from ibapi.common import MAX_MSG_LEN, NO_VALID_ID, OrderId, TickAttrib, TickerId
|
||||
from ibapi.contract import Contract, ContractDetails
|
||||
from ibapi.execution import Execution
|
||||
from ibapi.order import Order
|
||||
from ibapi.order_state import OrderState
|
||||
from ibapi.ticktype import TickType
|
||||
from ibapi.wrapper import EWrapper
|
||||
from ibapi.errors import BAD_LENGTH
|
||||
|
||||
from vnpy.trader.gateway import BaseGateway
|
||||
from vnpy.trader.object import (
|
||||
AccountData,
|
||||
CancelRequest,
|
||||
ContractData,
|
||||
TickData,
|
||||
OrderData,
|
||||
TradeData,
|
||||
OrderRequest,
|
||||
PositionData,
|
||||
SubscribeRequest,
|
||||
OrderRequest,
|
||||
CancelRequest,
|
||||
SubscribeRequest
|
||||
)
|
||||
from vnpy.trader.constant import (
|
||||
Product,
|
||||
|
@ -18,8 +18,6 @@ from .event import (
|
||||
EVENT_LOG,
|
||||
EVENT_ORDER,
|
||||
EVENT_POSITION,
|
||||
EVENT_ACCOUNT,
|
||||
EVENT_CONTRACT,
|
||||
EVENT_TICK,
|
||||
EVENT_TRADE,
|
||||
)
|
||||
|
@ -13,10 +13,8 @@ from ..constant import Direction, Exchange, Offset, PriceType
|
||||
from ..engine import MainEngine
|
||||
from ..event import (
|
||||
EVENT_ACCOUNT,
|
||||
EVENT_LOG,
|
||||
EVENT_ORDER,
|
||||
EVENT_POSITION,
|
||||
EVENT_CONTRACT,
|
||||
EVENT_LOG,
|
||||
EVENT_TICK,
|
||||
EVENT_TRADE
|
||||
|
Loading…
Reference in New Issue
Block a user