[Add] add load history data function to cta live trading engine
This commit is contained in:
parent
e29345503b
commit
95052ce822
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,6 +1,9 @@
|
|||||||
# Python
|
# Python
|
||||||
*.pyc
|
*.pyc
|
||||||
|
|
||||||
|
# Jupyter
|
||||||
|
.ipynb_checkpoints
|
||||||
|
|
||||||
# IDE
|
# IDE
|
||||||
.vscode
|
.vscode
|
||||||
.idea
|
.idea
|
||||||
|
@ -1,84 +0,0 @@
|
|||||||
{
|
|
||||||
"cells": [
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"#%%\n",
|
|
||||||
"from vnpy.app.cta_strategy.backtesting import BacktestingEngine\n",
|
|
||||||
"from vnpy.app.cta_strategy.strategies.turtle_signal_strategy import (\n",
|
|
||||||
" TurtleSignalStrategy,\n",
|
|
||||||
")\n",
|
|
||||||
"from datetime import datetime"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"#%%\n",
|
|
||||||
"engine = BacktestingEngine()\n",
|
|
||||||
"engine.set_parameters(\n",
|
|
||||||
" vt_symbol=\"IF88.CFFEX\",\n",
|
|
||||||
" interval=\"1m\",\n",
|
|
||||||
" start=datetime(2013, 1, 1),\n",
|
|
||||||
" end=datetime(2015, 3, 30),\n",
|
|
||||||
" rate=0,\n",
|
|
||||||
" slippage=0,\n",
|
|
||||||
" size=300,\n",
|
|
||||||
" pricetick=0.2,\n",
|
|
||||||
" capital=1_000_000,\n",
|
|
||||||
")"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {
|
|
||||||
"scrolled": false
|
|
||||||
},
|
|
||||||
"outputs": [],
|
|
||||||
"source": [
|
|
||||||
"#%%\n",
|
|
||||||
"engine.add_strategy(TurtleSignalStrategy, {})\n",
|
|
||||||
"engine.load_data()\n",
|
|
||||||
"engine.run_backtesting()\n",
|
|
||||||
"df = engine.calculate_result()\n",
|
|
||||||
"engine.calculate_statistics()\n",
|
|
||||||
"engine.show_chart()"
|
|
||||||
]
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
|
||||||
],
|
|
||||||
"metadata": {
|
|
||||||
"kernelspec": {
|
|
||||||
"display_name": "Python 3",
|
|
||||||
"language": "python",
|
|
||||||
"name": "python3"
|
|
||||||
},
|
|
||||||
"language_info": {
|
|
||||||
"codemirror_mode": {
|
|
||||||
"name": "ipython",
|
|
||||||
"version": 3
|
|
||||||
},
|
|
||||||
"file_extension": ".py",
|
|
||||||
"mimetype": "text/x-python",
|
|
||||||
"name": "python",
|
|
||||||
"nbconvert_exporter": "python",
|
|
||||||
"pygments_lexer": "ipython3",
|
|
||||||
"version": "3.7.1"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"nbformat": 4,
|
|
||||||
"nbformat_minor": 2
|
|
||||||
}
|
|
File diff suppressed because one or more lines are too long
@ -165,13 +165,13 @@ class BacktestingEngine:
|
|||||||
mode: BacktestingMode = BacktestingMode.BAR,
|
mode: BacktestingMode = BacktestingMode.BAR,
|
||||||
):
|
):
|
||||||
""""""
|
""""""
|
||||||
self.mode = mode # 1
|
self.mode = mode
|
||||||
self.vt_symbol = vt_symbol # 2
|
self.vt_symbol = vt_symbol
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.rate = rate # 3
|
self.rate = rate
|
||||||
self.slippage = slippage # 4
|
self.slippage = slippage
|
||||||
self.size = size #
|
self.size = size
|
||||||
self.pricetick = pricetick #
|
self.pricetick = pricetick
|
||||||
self.start = start
|
self.start = start
|
||||||
|
|
||||||
self.symbol, exchange_str = self.vt_symbol.split(".")
|
self.symbol, exchange_str = self.vt_symbol.split(".")
|
||||||
@ -789,6 +789,12 @@ class BacktestingEngine:
|
|||||||
"""
|
"""
|
||||||
msg = f"{self.datetime}\t{msg}"
|
msg = f"{self.datetime}\t{msg}"
|
||||||
self.logs.append(msg)
|
self.logs.append(msg)
|
||||||
|
|
||||||
|
def send_email(self, msg: str, strategy: CtaTemplate = None):
|
||||||
|
"""
|
||||||
|
Send email to default receiver.
|
||||||
|
"""
|
||||||
|
pass
|
||||||
|
|
||||||
def get_engine_type(self):
|
def get_engine_type(self):
|
||||||
"""
|
"""
|
||||||
|
@ -7,6 +7,7 @@ import traceback
|
|||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Callable
|
from typing import Any, Callable
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from vnpy.event import Event, EventEngine
|
from vnpy.event import Event, EventEngine
|
||||||
from vnpy.trader.engine import BaseEngine, MainEngine
|
from vnpy.trader.engine import BaseEngine, MainEngine
|
||||||
@ -19,6 +20,7 @@ from vnpy.trader.object import (
|
|||||||
from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE
|
from vnpy.trader.event import EVENT_TICK, EVENT_ORDER, EVENT_TRADE
|
||||||
from vnpy.trader.constant import Direction, PriceType, Interval
|
from vnpy.trader.constant import Direction, PriceType, Interval
|
||||||
from vnpy.trader.utility import get_temp_path
|
from vnpy.trader.utility import get_temp_path
|
||||||
|
from vnpy.trader.database import DbTickData, DbBarData
|
||||||
|
|
||||||
from .base import (
|
from .base import (
|
||||||
EVENT_CTA_LOG,
|
EVENT_CTA_LOG,
|
||||||
@ -46,19 +48,19 @@ class CtaEngine(BaseEngine):
|
|||||||
super(CtaEngine, self).__init__(
|
super(CtaEngine, self).__init__(
|
||||||
main_engine, event_engine, "CtaStrategy")
|
main_engine, event_engine, "CtaStrategy")
|
||||||
|
|
||||||
self.setting_file = None # setting file object
|
self.setting_file = None # setting file object
|
||||||
|
|
||||||
self.classes = {} # class_name: stategy_class
|
self.classes = {} # class_name: stategy_class
|
||||||
self.strategies = {} # strategy_name: strategy
|
self.strategies = {} # strategy_name: strategy
|
||||||
|
|
||||||
self.symbol_strategy_map = defaultdict(
|
self.symbol_strategy_map = defaultdict(
|
||||||
list) # vt_symbol: strategy list
|
list) # vt_symbol: strategy list
|
||||||
self.orderid_strategy_map = {} # vt_orderid: strategy
|
self.orderid_strategy_map = {} # vt_orderid: strategy
|
||||||
self.strategy_orderid_map = defaultdict(
|
self.strategy_orderid_map = defaultdict(
|
||||||
set) # strategy_name: orderid list
|
set) # strategy_name: orderid list
|
||||||
|
|
||||||
self.stop_order_count = 0 # for generating stop_orderid
|
self.stop_order_count = 0 # for generating stop_orderid
|
||||||
self.stop_orders = {} # stop_orderid: stop_order
|
self.stop_orders = {} # stop_orderid: stop_order
|
||||||
|
|
||||||
def init_engine(self):
|
def init_engine(self):
|
||||||
"""
|
"""
|
||||||
@ -320,11 +322,40 @@ class CtaEngine(BaseEngine):
|
|||||||
self, vt_symbol: str, days: int, interval: Interval, callback: Callable
|
self, vt_symbol: str, days: int, interval: Interval, callback: Callable
|
||||||
):
|
):
|
||||||
""""""
|
""""""
|
||||||
pass
|
end = datetime.now()
|
||||||
|
start = end - timedelta(days)
|
||||||
|
|
||||||
|
s = (
|
||||||
|
DbBarData.select()
|
||||||
|
.where(
|
||||||
|
(DbBarData.vt_symbol == vt_symbol) &
|
||||||
|
(DbBarData.interval == interval) &
|
||||||
|
(DbBarData.datetime >= start) &
|
||||||
|
(DbBarData.datetime <= end)
|
||||||
|
)
|
||||||
|
.order_by(DbBarData.datetime)
|
||||||
|
)
|
||||||
|
|
||||||
|
for bar in s:
|
||||||
|
callback(bar)
|
||||||
|
|
||||||
def load_tick(self, vt_symbol: str, days: int, callback: Callable):
|
def load_tick(self, vt_symbol: str, days: int, callback: Callable):
|
||||||
""""""
|
""""""
|
||||||
pass
|
end = datetime.now()
|
||||||
|
start = end - timedelta(days)
|
||||||
|
|
||||||
|
s = (
|
||||||
|
DbTickData.select()
|
||||||
|
.where(
|
||||||
|
(DbBarData.vt_symbol == vt_symbol) &
|
||||||
|
(DbBarData.datetime >= start) &
|
||||||
|
(DbBarData.datetime <= end)
|
||||||
|
)
|
||||||
|
.order_by(DbBarData.datetime)
|
||||||
|
)
|
||||||
|
|
||||||
|
for tick in s:
|
||||||
|
callback(tick)
|
||||||
|
|
||||||
def call_strategy_func(
|
def call_strategy_func(
|
||||||
self, strategy: CtaTemplate, func: Callable, params: Any = None
|
self, strategy: CtaTemplate, func: Callable, params: Any = None
|
||||||
@ -586,3 +617,14 @@ class CtaEngine(BaseEngine):
|
|||||||
log = LogData(msg=msg, gateway_name="CtaStrategy")
|
log = LogData(msg=msg, gateway_name="CtaStrategy")
|
||||||
event = Event(type=EVENT_CTA_LOG, data=log)
|
event = Event(type=EVENT_CTA_LOG, data=log)
|
||||||
self.event_engine.put(event)
|
self.event_engine.put(event)
|
||||||
|
|
||||||
|
def send_email(self, msg: str, strategy: CtaTemplate = None):
|
||||||
|
"""
|
||||||
|
Send email to default receiver.
|
||||||
|
"""
|
||||||
|
if strategy:
|
||||||
|
subject = f"{strategy.name}"
|
||||||
|
else:
|
||||||
|
subject = "CTA策略引擎"
|
||||||
|
|
||||||
|
self.main_engine.send_email(subject, msg)
|
||||||
|
@ -226,3 +226,14 @@ class CtaTemplate(ABC):
|
|||||||
Put an strategy data event for ui update.
|
Put an strategy data event for ui update.
|
||||||
"""
|
"""
|
||||||
self.cta_engine.put_strategy_event(self)
|
self.cta_engine.put_strategy_event(self)
|
||||||
|
|
||||||
|
def send_email(self, msg):
|
||||||
|
"""
|
||||||
|
Send email to default receiver.
|
||||||
|
"""
|
||||||
|
self.cta_engine.send_email(msg, self)
|
||||||
|
|
||||||
|
def save_variables(self):
|
||||||
|
"""
|
||||||
|
"""
|
||||||
|
self.cta_engine.save_strategy_variables(self)
|
||||||
|
Loading…
Reference in New Issue
Block a user