[Mod]reformat code

This commit is contained in:
vn.py 2019-02-23 16:01:19 +08:00
parent a9f652607c
commit e6f8060e99
12 changed files with 297 additions and 272 deletions

View File

@ -0,0 +1 @@
__version__ == "2.0"

View File

@ -201,10 +201,10 @@ class BacktestingEngine:
s = (
DbBarData.select()
.where(
(DbBarData.vt_symbol == self.vt_symbol) &
(DbBarData.interval == self.interval) &
(DbBarData.datetime >= self.start) &
(DbBarData.datetime <= self.end)
(DbBarData.vt_symbol == self.vt_symbol)
& (DbBarData.interval == self.interval)
& (DbBarData.datetime >= self.start)
& (DbBarData.datetime <= self.end)
)
.order_by(DbBarData.datetime)
)
@ -213,9 +213,9 @@ class BacktestingEngine:
s = (
DbTickData.select()
.where(
(DbTickData.vt_symbol == self.vt_symbol) &
(DbTickData.datetime >= self.start) &
(DbTickData.datetime <= self.end)
(DbTickData.vt_symbol == self.vt_symbol)
& (DbTickData.datetime >= self.start)
& (DbTickData.datetime <= self.end)
)
.order_by(DbTickData.datetime)
)
@ -567,15 +567,15 @@ class BacktestingEngine:
# Check whether limit orders can be filled.
long_cross = (
order.direction == Direction.LONG and
order.price >= long_cross_price and
long_cross_price > 0
order.direction == Direction.LONG
and order.price >= long_cross_price
and long_cross_price > 0
)
short_cross = (
order.direction == Direction.SHORT and
order.price <= short_cross_price and
short_cross_price > 0
order.direction == Direction.SHORT
and order.price <= short_cross_price
and short_cross_price > 0
)
if not long_cross and not short_cross:
@ -635,13 +635,13 @@ class BacktestingEngine:
for stop_order in list(self.active_stop_orders.values()):
# Check whether stop order can be triggered.
long_cross = (
stop_order.direction == Direction.LONG and
stop_order.price <= long_cross_price
stop_order.direction == Direction.LONG
and stop_order.price <= long_cross_price
)
short_cross = (
stop_order.direction == Direction.SHORT and
stop_order.price >= short_cross_price
stop_order.direction == Direction.SHORT
and stop_order.price >= short_cross_price
)
if not long_cross and not short_cross:

View File

@ -405,10 +405,10 @@ class CtaEngine(BaseEngine):
s = (
DbBarData.select()
.where(
(DbBarData.vt_symbol == vt_symbol) &
(DbBarData.interval == interval) &
(DbBarData.datetime >= start) &
(DbBarData.datetime <= end)
(DbBarData.vt_symbol == vt_symbol)
& (DbBarData.interval == interval)
& (DbBarData.datetime >= start)
& (DbBarData.datetime <= end)
)
.order_by(DbBarData.datetime)
)
@ -425,9 +425,9 @@ class CtaEngine(BaseEngine):
s = (
DbTickData.select()
.where(
(DbBarData.vt_symbol == vt_symbol) &
(DbBarData.datetime >= start) &
(DbBarData.datetime <= end)
(DbBarData.vt_symbol == vt_symbol)
& (DbBarData.datetime >= start)
& (DbBarData.datetime <= end)
)
.order_by(DbBarData.datetime)
)

View File

@ -1,7 +1,6 @@
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,
@ -31,10 +30,10 @@ class AtrRsiStrategy(CtaTemplate):
intra_trade_high = 0
intra_trade_low = 0
parameters= [ 'atr_length', 'atr_ma_length', 'rsi_length', 'rsi_entry', 'trailing_percent','fixed_size']
parameters = ['atr_length', 'atr_ma_length', 'rsi_length',
'rsi_entry', 'trailing_percent', 'fixed_size']
variables = ['atr_value', 'atr_ma', 'rsi_value', 'rsi_buy', 'rsi_sell']
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
super(AtrRsiStrategy, self).__init__(
@ -102,14 +101,16 @@ class AtrRsiStrategy(CtaTemplate):
self.intra_trade_high = max(self.intra_trade_high, bar.high_price)
self.intra_trade_low = bar.low_price
long_stop = self.intra_trade_high * (1-self.trailing_percent/100)
long_stop = self.intra_trade_high * \
(1 - self.trailing_percent / 100)
self.sell(long_stop, abs(self.pos), stop=True)
elif self.pos < 0:
self.intra_trade_low = min(self.intra_trade_low, bar.low_price)
self.intra_trade_high = bar.high_price
short_stop = self.intra_trade_low * (1+self.trailing_percent/100)
short_stop = self.intra_trade_low * \
(1 + self.trailing_percent / 100)
self.cover(short_stop, abs(self.pos), stop=True)
self.put_event()

View File

@ -1,7 +1,6 @@
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,
@ -33,8 +32,10 @@ class BollChannelStrategy(CtaTemplate):
long_stop = 0
short_stop = 0
parameters = [ 'boll_window', 'boll_dev', 'cci_window', 'atr_window', 'sl_multiplier', 'fixed_size']
variables = ['boll_up', 'boll_down', 'cci_value', 'atr_value', 'intra_trade_high', 'intra_trade_low', 'long_stop', 'short_stop']
parameters = ['boll_window', 'boll_dev', 'cci_window',
'atr_window', 'sl_multiplier', 'fixed_size']
variables = ['boll_up', 'boll_down', 'cci_value', 'atr_value',
'intra_trade_high', 'intra_trade_low', 'long_stop', 'short_stop']
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):
""""""
@ -131,4 +132,3 @@ class BollChannelStrategy(CtaTemplate):
Callback of stop order update.
"""
pass

View File

@ -1,7 +1,6 @@
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,

View File

@ -2,7 +2,6 @@ from datetime import time
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,
@ -35,7 +34,6 @@ class DualThrustStrategy(CtaTemplate):
long_entered = False
short_entered = False
parameters = ['k1', 'k2', "fixed_size"]
variables = ['range', 'long_entry', 'short_entry', 'exit_time']
@ -113,7 +111,8 @@ class DualThrustStrategy(CtaTemplate):
self.buy(self.long_entry, self.fixed_size, stop=True)
else:
if not self.short_entered:
self.short(self.short_entry, self.fixed_size, stop=True)
self.short(self.short_entry,
self.fixed_size, stop=True)
elif self.pos > 0:
self.long_entered = True

View File

@ -1,7 +1,6 @@
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,
@ -10,6 +9,7 @@ from vnpy.app.cta_strategy import (
ArrayManager,
)
class KingKeltnerStrategy(CtaTemplate):
""""""
@ -41,10 +41,6 @@ class KingKeltnerStrategy(CtaTemplate):
self.bg = BarGenerator(self.on_bar, 5, self.on_5min_bar)
self.am = ArrayManager()
buy_orderidList = []
short_orderidList = []
orderList = []
def on_init(self):
"""
Callback when strategy is inited.

View File

@ -1,7 +1,5 @@
from vnpy.app.cta_strategy import (
CtaTemplate,
StopOrder,
Direction,
TickData,
BarData,
TradeData,
@ -16,7 +14,7 @@ from vnpy.app.cta_strategy import (
class RsiSignal(CtaSignal):
""""""
def __init__(self, rsi_window , rsi_level):
def __init__(self, rsi_window: int, rsi_level: float):
"""Constructor"""
super(RsiSignal, self).__init__()
@ -51,10 +49,11 @@ class RsiSignal(CtaSignal):
else:
self.set_signal_pos(0)
class CciSignal(CtaSignal):
""""""
def __init__(self, cci_window, cci_level):
def __init__(self, cci_window: int, cci_level: float):
""""""
super(CciSignal, self).__init__()
@ -89,10 +88,11 @@ class CciSignal(CtaSignal):
else:
self.set_signal_pos(0)
class MaSignal(CtaSignal):
""""""
def __init__(self, fast_window, slow_window):
def __init__(self, fast_window: int, slow_window: int):
""""""
super(MaSignal, self).__init__()
@ -145,7 +145,8 @@ class MultiSignalStrategy(TargetPosTemplate):
signal_pos = {}
parameters = ['rsi_window','rsi_level','cci_window','cci_level','fast_window','slow_window']
parameters = ['rsi_window', 'rsi_level', 'cci_window',
'cci_level', 'fast_window', 'slow_window']
variables = ['signal_pos', 'target_pos']
def __init__(self, cta_engine, strategy_name, vt_symbol, setting):

View File

@ -2,10 +2,10 @@
from abc import ABC
from typing import Any, Callable
from vnpy.trader.constant import Interval
from vnpy.trader.constant import Interval, Status
from vnpy.trader.object import BarData, TickData, OrderData, TradeData
from .base import CtaOrderType, StopOrder
from .base import CtaOrderType, StopOrder, EngineType
class CtaTemplate(ABC):
@ -260,7 +260,6 @@ class CtaSignal(ABC):
"""
pass
def set_signal_pos(self, pos):
""""""
self.signal_pos = pos

View File

@ -4,7 +4,39 @@
from datetime import datetime
from vnpy.api.ctp import *
from vnpy.api.ctp import (
MdApi,
TdApi,
THOST_FTDC_OAS_Submitted,
THOST_FTDC_OAS_Accepted,
THOST_FTDC_OAS_Rejected,
THOST_FTDC_OST_NoTradeQueueing,
THOST_FTDC_OST_PartTradedQueueing,
THOST_FTDC_OST_AllTraded,
THOST_FTDC_OST_Canceled,
THOST_FTDC_D_Buy,
THOST_FTDC_D_Sell,
THOST_FTDC_PD_Long,
THOST_FTDC_PD_Short,
THOST_FTDC_OPT_LimitPrice,
THOST_FTDC_OPT_AnyPrice,
THOST_FTDC_OF_Open,
THOST_FTDC_OFEN_Close,
THOST_FTDC_OFEN_CloseYesterday,
THOST_FTDC_OFEN_CloseToday,
THOST_FTDC_PC_Futures,
THOST_FTDC_PC_Options,
THOST_FTDC_CP_CallOptions,
THOST_FTDC_CP_PutOptions,
THOST_FTDC_HF_Speculation,
THOST_FTDC_CC_Immediately,
THOST_FTDC_FCC_NotForceClose,
THOST_FTDC_TC_GFD,
THOST_FTDC_VC_AV,
THOST_FTDC_TC_IOC,
THOST_FTDC_VC_CV,
THOST_FTDC_AF_Delete
)
from vnpy.trader.constant import (
Direction,
Offset,
@ -84,6 +116,7 @@ symbol_exchange_map = {}
symbol_name_map = {}
symbol_size_map = {}
class CtpGateway(BaseGateway):
"""
VN Trader Gateway for CTP .
@ -735,6 +768,3 @@ class CtpTdApi(TdApi):
""""""
if self.connect_status:
self.exit()

View File

@ -322,8 +322,7 @@ class FutuGateway(BaseGateway):
account = AccountData(
accountid=f"{self.gateway_name}_{self.market}",
balance=float(row["total_assets"]),
frozen=(float(row["total_assets"]) -
float(row["avl_withdrawal_cash"])),
frozen=(float(row["total_assets"]) - float(row["avl_withdrawal_cash"])),
gateway_name=self.gateway_name,
)
self.on_account(account)