通道突破

通道突破策略利用价格对通道的突破产生交易信号,以布林带突破策略为例
布林带突破 - 无止盈止损
利用前收盘价格precloseprice对于布林带上边带 upperband 和下边带lowerband突破生成开平仓信号:

precloseprice 上穿 upperband,形成做多信号,买入开仓;
precloseprice 下穿 lowerband,形成做空信号,卖出开仓。

************************************

布林带突破 - 无止盈止损

价格上穿布林上边带,形成做多信号,买入开仓;

价格下穿布林下边带,形成做空信号,卖出开仓;

************************************

import talib
import pandas as pd
import numpy as np
import quartz_futures as qf
from quartz_futures.api import *

参数初始化

universe = [‘RB1610’] # 策略证券池
start = pd.datetime(2016, 6, 1) # 回测开始时间
end = pd.datetime(2016, 9, 1) # 回测结束时间
capital_base = 1e4 # 初试可用资金
refresh_rate = 1 # 调仓周期
freq = ‘d’ # 调仓频率:s -> 秒;m-> 分钟;d-> 日;

自动生成保证金比例: margin_rate

margin_ratio = DataAPI.FutuGet(ticker = universe, field = [‘ticker’,’tradeMarginRatio’], pandas = ‘1’)
margin_rate = dict(zip(margin_ratio.ticker.tolist(), [0.01*index for index in margin_ratio.tradeMarginRatio.tolist()]))

策略初始化函数,一般用于设置计数器,回测辅助变量等。

def initialize(futures_account):
pass

回测调仓逻辑,每个调仓周期运行一次,可在此函数内实现信号生产,生成调仓指令。

def handle_data(futures_account):
symbol, amount = universe[0], 1
history_data = get_symbol_history(symbol = symbol, time_range = 20)[symbol]
upper_band, middle_band, lower_band = talib.BBANDS(history_data[‘closePrice’].apply(float).values, timeperiod = 10, nbdevup = 0.5, nbdevdn = 0.5)
pre_close_price = history_data[‘closePrice’][-1]

current_long = futures_account.position.get(symbol, dict()).get('long_position', 0)
current_short = futures_account.position.get(symbol, dict()).get('short_position', 0)    
if pre_close_price > upper_band[-1]:
    if current_short > 0:
        print futures_account.current_date, futures_account.current_time, '买入平仓'
        order(symbol, current_short, 'close')
    if current_long < amount:
        print futures_account.current_date, futures_account.current_time, '买入开仓'
        order(symbol, amount, 'open')

if pre_close_price < lower_band[-1]:
    if current_long > 0:
        print futures_account.current_date, futures_account.current_time,'卖出平仓'
        order(symbol, -current_long, 'close')
    if current_short < amount:
        print futures_account.current_date, futures_account.current_time, '卖出开仓'
        order(symbol, -amount, 'open')
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值