[Mod] reverse certain code formatting caused by black

This commit is contained in:
vn.py 2019-01-30 12:40:49 +08:00
parent d05b558f87
commit d7c79327dc
15 changed files with 106 additions and 92 deletions

View File

@ -1,5 +1,4 @@
from time import time
from datetime import datetime
import rqdatac as rq

File diff suppressed because one or more lines are too long

View File

@ -1,29 +0,0 @@
#%%
from vnpy.app.cta_strategy.backtesting import BacktestingEngine
from vnpy.app.cta_strategy.strategies.turtle_signal_strategy import (
TurtleSignalStrategy,
)
from datetime import datetime
#%%
engine = BacktestingEngine()
engine.set_parameters(
vt_symbol="IF88.CFFEX",
interval="1m",
start=datetime(2013, 1, 1),
end=datetime(2019, 1, 30),
rate=0,
slippage=0,
size=300,
pricetick=0.2,
capital=1_000_000,
)
#%%
engine.add_strategy(TurtleSignalStrategy, {})
engine.load_data()
engine.run_backtesting()
df = engine.calculate_result()
engine.calculate_statistics()
engine.show_chart()

View File

@ -19,14 +19,15 @@ from vnpy.trader.object import (
from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE
from vnpy.trader.constant import Direction, PriceType, Interval
from vnpy.trader.utility import get_temp_path
from .base import (
EVENT_CTA_LOG,
EVENT_CTA_STRATEGY,
EVENT_CTA_STOPORDER,
CtaOrderType,
EngineType,
StopOrder,
StopOrderStatus,
EVENT_CTA_LOG,
EVENT_CTA_STRATEGY,
EVENT_CTA_STOPORDER,
ORDER_CTA2VT,
STOPORDER_PREFIX
)
@ -42,16 +43,19 @@ class CtaEngine(BaseEngine):
def __init__(self, main_engine: MainEngine, event_engine: EventEngine):
""""""
super(CtaEngine, self).__init__(main_engine, event_engine, "CtaStrategy")
super(CtaEngine, self).__init__(
main_engine, event_engine, "CtaStrategy")
self.setting_file = None # setting file object
self.classes = {} # class_name: stategy_class
self.strategies = {} # strategy_name: strategy
self.symbol_strategy_map = defaultdict(list) # vt_symbol: strategy list
self.symbol_strategy_map = defaultdict(
list) # vt_symbol: strategy list
self.orderid_strategy_map = {} # vt_orderid: strategy
self.strategy_orderid_map = defaultdict(set) # strategy_name: orderid list
self.strategy_orderid_map = defaultdict(
set) # strategy_name: orderid list
self.stop_order_count = 0 # for generating stop_orderid
self.stop_orders = {} # stop_orderid: stop_order
@ -197,7 +201,8 @@ class CtaEngine(BaseEngine):
price=price,
volume=volume,
)
vt_orderid = self.main_engine.send_limit_order(req, contract.gateway_name)
vt_orderid = self.main_engine.send_limit_order(
req, contract.gateway_name)
# Save relationship between orderid and strategy.
self.orderid_strategy_map[vt_orderid] = strategy
@ -374,7 +379,8 @@ class CtaEngine(BaseEngine):
# Subscribe market data
contract = self.main_engine.get_contract(strategy.vt_symbol)
if contract:
req = SubscribeRequest(symbol=contract.symbol, exchange=contract.exchange)
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)
@ -439,7 +445,8 @@ class CtaEngine(BaseEngine):
Load strategy class from source code.
"""
path1 = Path(__file__).parent.joinpath("strategies")
self.load_strategy_class_from_folder(path1, "vnpy.app.cta_strategy.strategies")
self.load_strategy_class_from_folder(
path1, "vnpy.app.cta_strategy.strategies")
path2 = Path.cwd().joinpath("strategies")
self.load_strategy_class_from_folder(path2, "strategies")
@ -450,7 +457,8 @@ class CtaEngine(BaseEngine):
"""
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
module_name = ".".join([module_name, filename.replace(".py", "")])
module_name = ".".join(
[module_name, filename.replace(".py", "")])
self.load_strategy_class_from_module(module_name)
def load_strategy_class_from_module(self, module_name: str):

View File

@ -6,7 +6,6 @@ from vnpy.app.cta_strategy import (
BarData,
TradeData,
OrderData,
StopOrder,
BarGenerator,
ArrayManager,
)
@ -161,4 +160,3 @@ class TurtleSignalStrategy(CtaTemplate):
if t > -4:
self.short(price - self.atr_value * 1.5, self.fixed_size, True)

View File

@ -1,10 +1,10 @@
""""""
from abc import ABC
from typing import Any, Callable
from vnpy.trader.constant import Interval
from vnpy.trader.object import BarData, OrderData, TickData, TradeData
from vnpy.trader.object import BarData, TickData, OrderData, TradeData
from .base import CtaOrderType, StopOrder

View File

@ -5,16 +5,16 @@ from vnpy.trader.engine import MainEngine
from vnpy.trader.ui import QtCore, QtGui, QtWidgets
from vnpy.trader.ui.widget import (
BaseCell,
BaseMonitor,
EnumCell,
MsgCell,
TimeCell,
BaseMonitor
)
from ..base import (
APP_NAME,
EVENT_CTA_LOG,
EVENT_CTA_STOPORDER,
EVENT_CTA_STRATEGY,
EVENT_CTA_STRATEGY
)
from ..engine import CtaEngine

View File

@ -30,10 +30,10 @@ from vnpy.trader.event import EVENT_TIMER
from vnpy.trader.gateway import BaseGateway
from vnpy.trader.object import (
TickData,
OrderData,
TradeData,
AccountData,
ContractData,
OrderData,
PositionData,
SubscribeRequest,
OrderRequest,

View File

@ -19,14 +19,14 @@ 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,
AccountData,
ContractData,
OrderRequest,
CancelRequest,
SubscribeRequest
)
from vnpy.trader.constant import (
@ -347,7 +347,8 @@ class IbApi(EWrapper):
orderid = str(orderId)
order = OrderData(
symbol=ib_contract.conId,
exchange=EXCHANGE_IB2VT.get(ib_contract.exchange, ib_contract.exchange),
exchange=EXCHANGE_IB2VT.get(
ib_contract.exchange, ib_contract.exchange),
orderid=orderid,
direction=DIRECTION_IB2VT[ib_order.action],
price=ib_order.lmtPrice,
@ -372,7 +373,8 @@ class IbApi(EWrapper):
accountid = f"{accountName}.{currency}"
account = self.accounts.get(accountid, None)
if not account:
account = AccountData(accountid=accountid, gateway_name=self.gateway_name)
account = AccountData(accountid=accountid,
gateway_name=self.gateway_name)
self.accounts[accountid] = account
name = ACCOUNTFIELD_IB2VT[key]
@ -491,7 +493,8 @@ class IbApi(EWrapper):
self.clientid = setting["clientid"]
self.client.connect(setting["host"], setting["port"], setting["clientid"])
self.client.connect(
setting["host"], setting["port"], setting["clientid"])
self.thread.start()
@ -592,7 +595,7 @@ class IbClient(EClient):
def run(self):
"""
Reimplement the original run message loop of eclient.
Remove all unnecessary try...catch... and allow exceptions to interrupt loop.
"""
while not self.done and self.isConnected():

View File

@ -8,10 +8,10 @@ class BaseApp(ABC):
Absstract class for app.
"""
app_name = "" # Unique name used for creating engine and widget
app_module = "" # App module string used in import_module
app_path = "" # Absolute path of app folder
display_name = "" # Name for display on the menu.
engine_class = None # App engine class
widget_name = "" # Class name of app widget
icon_name = "" # Icon file name of app widget
app_name = "" # Unique name used for creating engine and widget
app_module = "" # App module string used in import_module
app_path = "" # Absolute path of app folder
display_name = "" # Name for display on the menu.
engine_class = None # App engine class
widget_name = "" # Class name of app widget
icon_name = "" # Icon file name of app widget

View File

@ -13,13 +13,13 @@ from typing import Any
from vnpy.event import Event, EventEngine
from .app import BaseApp
from .event import (
EVENT_TICK,
EVENT_ORDER,
EVENT_TRADE,
EVENT_POSITION,
EVENT_ACCOUNT,
EVENT_CONTRACT,
EVENT_LOG,
EVENT_ORDER,
EVENT_POSITION,
EVENT_TICK,
EVENT_TRADE,
EVENT_LOG
)
from .gateway import BaseGateway
from .object import CancelRequest, LogData, OrderRequest, SubscribeRequest

View File

@ -8,8 +8,8 @@ from typing import Any
from vnpy.event import Event, EventEngine
from .event import (
EVENT_TICK,
EVENT_TRADE,
EVENT_ORDER,
EVENT_TRADE,
EVENT_POSITION,
EVENT_ACCOUNT,
EVENT_CONTRACT,

View File

@ -7,10 +7,12 @@ from logging import CRITICAL
SETTINGS = {
"font.family": "Arial",
"font.size": 12,
"log.active": True,
"log.level": CRITICAL,
"log.console": True,
"log.file": True,
"email.server": "smtp.qq.com",
"email.port": 465,
"email.username": "",

View File

@ -10,7 +10,6 @@ from PyQt5 import QtCore, QtGui, QtWidgets
from vnpy.event import EventEngine
from .widget import (
AboutDialog,
TickMonitor,
OrderMonitor,
TradeMonitor,
@ -21,6 +20,7 @@ from .widget import (
ConnectDialog,
ContractManager,
TradingWidget,
AboutDialog,
)
from ..engine import MainEngine
from ..utility import get_icon_path, get_trader_path

View File

@ -12,12 +12,12 @@ from vnpy.event import Event, EventEngine
from ..constant import Direction, Exchange, Offset, PriceType
from ..engine import MainEngine
from ..event import (
EVENT_ACCOUNT,
EVENT_TICK,
EVENT_TRADE,
EVENT_ORDER,
EVENT_POSITION,
EVENT_LOG,
EVENT_TICK,
EVENT_TRADE
EVENT_ACCOUNT,
EVENT_LOG
)
from ..object import OrderRequest, SubscribeRequest
from ..utility import load_setting, save_setting