[Fix] bug in calculating daily backtesting pnl result

This commit is contained in:
vn.py 2019-01-30 04:39:04 +08:00
parent be8f57d039
commit 346368cd2e
3 changed files with 182 additions and 16 deletions

View File

@ -1,6 +1,84 @@
{
"cells": [],
"metadata": {},
"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

View File

@ -12,6 +12,8 @@ from pandas import DataFrame
from vnpy.trader.constant import Direction, Exchange, Interval, Status
from vnpy.trader.database import DbBarData, DbTickData
from vnpy.trader.object import OrderData, TradeData
from vnpy.trader.utility import round_to_pricetick
from .base import (
BacktestingMode,
CtaOrderType,
@ -698,6 +700,7 @@ class BacktestingEngine:
stop: bool = False,
):
""""""
price = round_to_pricetick(price, self.pricetick)
if stop:
return self.send_stop_order(order_type, price, volume)
else:
@ -846,18 +849,19 @@ class DailyResult:
):
""""""
# Holding pnl is the pnl from holding position at day start
self.start_pos = self.end_pos = start_pos
self.start_pos = start_pos
self.end_pos = start_pos
self.holding_pnl = self.start_pos * (self.close_price - self.pre_close) * size
# Trading pnl is the pnl from new trade during the day
self.trade_count = len(self.trades)
pos_change = 0
for trade in self.trades:
if trade.direction == Direction.LONG:
pos_change += trade.volume
pos_change = trade.volume
else:
pos_change -= trade.volume
pos_change = -trade.volume
turnover = trade.price * trade.volume * size
self.trading_pnl += pos_change * (self.close_price - trade.price) * size