vnpy/examples/spread_backtesting/backtesting.ipynb

121 lines
2.7 KiB
Plaintext
Raw Normal View History

{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"#%%\n",
"from vnpy.app.spread_trading.backtesting import BacktestingEngine\n",
"from vnpy.app.spread_trading.strategies.statistical_arbitrage_strategy import (\n",
" StatisticalArbitrageStrategy\n",
")\n",
"from vnpy.app.spread_trading.base import LegData, SpreadData\n",
"from datetime import datetime"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"spread = SpreadData(\n",
" name=\"IF-Spread\",\n",
" legs=[LegData(\"IF1911.CFFEX\"), LegData(\"IF1912.CFFEX\")],\n",
" price_multipliers={\"IF1911.CFFEX\": 1, \"IF1912.CFFEX\": -1},\n",
" trading_multipliers={\"IF1911.CFFEX\": 1, \"IF1912.CFFEX\": -1},\n",
" active_symbol=\"IF1911.CFFEX\",\n",
" inverse_contracts={\"IF1911.CFFEX\": False, \"IF1912.CFFEX\": False},\n",
" min_volume=1\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"#%%\n",
"engine = BacktestingEngine()\n",
"engine.set_parameters(\n",
" spread=spread,\n",
" interval=\"1m\",\n",
" start=datetime(2019, 6, 10),\n",
" end=datetime(2019, 11, 10),\n",
" rate=0,\n",
" slippage=0,\n",
" size=300,\n",
" pricetick=0.2,\n",
" capital=1_000_000,\n",
")\n",
"engine.add_strategy(StatisticalArbitrageStrategy, {})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2019-11-10 16:09:03.822440\t开始加载历史数据\n"
]
}
],
"source": [
"#%%\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": [
"for trade in engine.trades.values():\n",
" print(trade)"
]
},
{
"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
}