# coding=utf-8
from __future__ import print_function, absolute_import
from import *
"""
This strategy uses Bollinger Bands for mean regression trading. Sell when the price touches the upper track of the Bollinger band, and when it touches the lower track, buy.
Backtest was performed using 600004 at 2009-09-17 13:00:00 to 2020-03-21 15:00:00.
Notice:
1: During the real trading session, if the bar or tick triggers the trading signal at the closing, it needs to be processed by itself, and the real trading may not be completed.
"""
# There must be an init method in the policy
def init(context):
# Set three parameters of Bollinger bands
= 26# Calculate the parameters of the middle track of the BOLL Bollinger Band
= 26# Calculate the parameters of BOLL standard deviation
= 1# Calculate the parameters of the distance between the upper and lower rails and the middle rails of BOLL
# Set up the contract to be backtested
= 'SHSE.600004'# Subscribe&Transaction object, subscribe here is600004
= max(, , ) + 1# Subscribe to data sliding window length
# Subscribe to the quote
subscribe(symbols= , frequency='1d', count=)
def on_bar(context, bars):
# Get the data slide window. As long as you have a subscription in init, you can get it here. The return value is
data = context.data(symbol=, frequency='1d', count=, fields='close')
# Calculate the upper and lower bounds of boll
bollUpper = data['close'].rolling().mean() \
+ * data['close'].rolling().std()
bollBottom = data['close'].rolling().mean() \
- * data['close'].rolling().std()
# Obtain existing positions
pos = ().position(symbol=, side=PositionSide_Long)
# Trading logic and ordering
# Sell stocks when there is a position and the stock price passes through the upper boundary of BOLL.
if data.close.values[-1] > bollUpper.values[-1] and data.close.values[-2] < bollUpper.values[-2]:
ifpos: # Sell stocks at the market price if you have a position.
order_volume(symbol=, volume=100, side=OrderSide_Sell,
order_type=OrderType_Market, position_effect=PositionEffect_Close)
print('Sell it at the market price')
# Buy stocks when there is no position and the stock price passes through the lower boundary of BOLL.
elif data.close.values[-1] < bollBottom.values[-1] and data.close.values[-2] > bollBottom.values[-2]:
if notpos: # Buy one hundred shares without holding.
order_volume(symbol=, volume=100, side=OrderSide_Buy,
order_type=OrderType_Market, position_effect=PositionEffect_Open)
print('Buy one at the market price')
if __name__ == '__main__':
'''
strategy_id policy ID, generated by the system
filename filename, please keep it consistent with this filename
mode real-time mode: MODE_LIVE backtest mode: MODE_BACKTEST
The ID of the token bound computer can be generated in the system settings-key management
backtest_start_time backtest start time
backtest_end_time backtest end time
Backtest_adjust Stock reproperty method is no longer reproperty: ADJUST_NONE pre-reproperty: ADJUST_PREV post-reproperty: ADJUST_POST
backtest_initial_cash backtest initial fund
backtest_commission_ratio backtest commission ratio
backtest_slippage_ratio backtest slippage ratio
'''
run(strategy_id='strategy_id',
filename='',
mode=MODE_BACKTEST,
token='{{token}}',
backtest_start_time='2009-09-17 13:00:00',
backtest_end_time='2020-03-21 15:00:00',
backtest_adjust=ADJUST_PREV,
backtest_initial_cash=1000,
backtest_commission_ratio=0.0001,backtest_slippage_ratio=0.0001)