# coding=utf-8
from __future__ import print_function, absolute_import
from gm.api import *
import talib
# 策略中必须有init方法
def init(context):
context.FAST = 5
context.SLOW = 10
context.symbol = 'SZSE.000001'
context.period = context.SLOW + 1
subscribe(context.symbol, '1d', count = context.period)
def on_bar(context, bars):
print(bars[0].bob)
# 获取数据
prices = context.data(symbol = context.symbol, frequency = '1d', count = context.period, fields = 'close')
fast_avg = talib.SMA(prices.values.reshape(context.period), context.FAST)
slow_avg = talib.SMA(prices.values.reshape(context.period), context.SLOW)
amount = context.account.positions(symbol = context.symbol)
if fast_avg[-1] > slow_avg[-1]:
order_target_percent(symbol = context.symbol, percent = 1, position_side = 1, order_type = 2)
print('买入平安银行')
if fast_avg[-1] < slow_avg[-1]:
order_target_percent(symbol = context.symbol, percent = 0, position_side = 1, order_type = 2)
print('卖出平安银行')
if __name__ == '__main__':
run(strategy_id='91b5f3ff-f48c-11e9-8165-00ff1156bdc3',
filename='main.py',
mode=MODE_BACKTEST,
token='04c26c5eb2c04e48e2b112e227ae73f0adc68a88',
backtest_start_time='2016-06-17 13:00:00',
backtest_end_time='2017-08-21 15:00:00')