2019-01-29 07:35:37 +00:00
|
|
|
{
|
|
|
|
"cells": [
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2019-05-03 08:10:11 +00:00
|
|
|
"execution_count": null,
|
2019-01-29 07:35:37 +00:00
|
|
|
"metadata": {},
|
2019-01-29 20:39:04 +00:00
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"#%%\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
"from vnpy.app.cta_strategy.backtesting import BacktestingEngine, OptimizationSetting\n",
|
2019-03-27 06:44:48 +00:00
|
|
|
"from vnpy.app.cta_strategy.strategies.atr_rsi_strategy import (\n",
|
|
|
|
" AtrRsiStrategy,\n",
|
2019-01-29 20:39:04 +00:00
|
|
|
")\n",
|
|
|
|
"from datetime import datetime"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2019-05-03 08:10:11 +00:00
|
|
|
"execution_count": null,
|
2019-01-29 20:39:04 +00:00
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"#%%\n",
|
|
|
|
"engine = BacktestingEngine()\n",
|
|
|
|
"engine.set_parameters(\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
" vt_symbol=\"IF88.CFFEX\",\n",
|
2019-01-29 20:39:04 +00:00
|
|
|
" interval=\"1m\",\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
" start=datetime(2019, 1, 1),\n",
|
2019-03-27 06:44:48 +00:00
|
|
|
" end=datetime(2019, 4, 30),\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
" rate=0.3/10000,\n",
|
2019-02-16 02:13:22 +00:00
|
|
|
" slippage=0.2,\n",
|
2019-01-29 20:39:04 +00:00
|
|
|
" size=300,\n",
|
|
|
|
" pricetick=0.2,\n",
|
|
|
|
" capital=1_000_000,\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
")\n",
|
|
|
|
"engine.add_strategy(AtrRsiStrategy, {})"
|
2019-01-29 20:39:04 +00:00
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
2019-04-15 15:32:04 +00:00
|
|
|
"execution_count": null,
|
2019-01-29 20:39:04 +00:00
|
|
|
"metadata": {
|
|
|
|
"scrolled": false
|
|
|
|
},
|
2019-04-15 15:32:04 +00:00
|
|
|
"outputs": [],
|
|
|
|
"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",
|
2019-05-03 08:10:11 +00:00
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {
|
|
|
|
"scrolled": true
|
|
|
|
},
|
|
|
|
"outputs": [],
|
2019-01-29 07:35:37 +00:00
|
|
|
"source": [
|
2019-04-15 15:32:04 +00:00
|
|
|
"setting = OptimizationSetting()\n",
|
2019-05-03 07:17:32 +00:00
|
|
|
"setting.set_target(\"sharpe_ratio\")\n",
|
2019-05-03 08:10:11 +00:00
|
|
|
"setting.add_parameter(\"atr_length\", 3, 39, 1)\n",
|
|
|
|
"setting.add_parameter(\"atr_ma_length\", 10, 30, 1)\n",
|
2019-04-15 15:32:04 +00:00
|
|
|
"\n",
|
2019-05-02 14:32:05 +00:00
|
|
|
"engine.run_ga_optimization(setting)"
|
2019-01-29 07:35:37 +00:00
|
|
|
]
|
|
|
|
},
|
2019-05-03 08:10:11 +00:00
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"result = _"
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"cell_type": "code",
|
|
|
|
"execution_count": null,
|
|
|
|
"metadata": {},
|
|
|
|
"outputs": [],
|
|
|
|
"source": [
|
|
|
|
"print(result)"
|
|
|
|
]
|
|
|
|
},
|
2019-01-29 07:35:37 +00:00
|
|
|
{
|
|
|
|
"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
|
|
|
|
}
|