python双均线策略,当五日均线位于十日均线上方则买入,反之卖出。(聚宽量化平台使用)

‘’’
**

python双均线策略,当五日均线位于十日均线上方则买入,反之卖出。(聚宽量化平台使用)

**
‘’’

初始化函数,设定要操作的股票、基准等等

def initialize(context):
# 定义一个全局变量, 保存要操作的股票
# 000002(股票:万科A)
g.security = ‘000400.XSHE’
# 设定沪深300作为基准
set_benchmark(‘000300.XSHG’)
# True为开启动态复权模式,使用真实价格交易
set_option(‘use_real_price’, True)
# 设定成交量比例
set_option(‘order_volume_ratio’, 1)
# 股票类交易手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
set_order_cost(OrderCost(open_tax=0, close_tax=0.001,
open_commission=0.0003, close_commission=0.0003,
close_today_commission=0, min_commission=5), type=‘stock’)
# 运行函数
run_daily(trade, ‘every_bar’)

交易程序

def trade(context):
security = g.security
# 设定均线窗口长度
n1 = 5
n2 = 10
# 获取股票的收盘价
close_data = attribute_history(security, n2+2, ‘1d’, [‘close’],df=False)
# 取得过去 ma_n1 天的平均价格
ma_n1 = close_data[‘close’][-n1:].mean()
# 取得过去 ma_n2 天的平均价格
ma_n2 = close_data[‘close’][-n2:].mean()
# 取得当前的现金
cash = context.portfolio.cash

# 如果当前有余额,并且n1日均线大于n2日均线
if ma_n1 > ma_n2:
    # 用所有 cash 买入股票
    order_value(security, cash)
    # 记录这次买入
    log.info("Buying %s" % (security))

# 如果n1日均线小于n2日均线,并且目前有头寸
elif ma_n1 < ma_n2 and context.portfolio.positions[security].closeable_amount > 0:
    # 全部卖出
    order_target(security, 0)
    # 记录这次卖出
    log.info("Selling %s" % (security))

# 绘制n1日均线价格
record(ma_n1=ma_n1)
# 绘制n2日均线价格
record(ma_n2=ma_n2)
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的聚宽期货量化交易均线策略及其源代码: 策略思路: - 策略采用均线交叉的方式进行买卖信号的产生。 - 当短期均线上穿长期均线时,产生买入信号。 - 当短期均线下穿长期均线时,产生卖出信号。 - 策略在每个交易日收盘价进行交易,不考虑交易成本和滑点。 源代码: # 导入聚宽数据包 import jqdata # 定义策略参数 fast_ma = 5 # 短期均线 slow_ma = 20 # 长期均线 # 初始化函数,设定基准等等 def initialize(context): # 设定沪深300作为基准 set_benchmark('IF88.CCFX') # 设定交易手续费,买入时万分之0.3,卖出时万分之0.3,最低交易费为5元 set_commission(PerTrade(buy_cost=0.00003, sell_cost=0.00003, min_cost=5)) # 设定策略参数 g.fast_ma = fast_ma g.slow_ma = slow_ma # 设定交易品种 g.security = 'IF88.CCFX' # 设定每个交易日下单时间 set_option('use_real_price', True) set_option('order_volume_ratio', 1) set_option('order_price_field', 'close') set_option('avoid_future_data', True) run_daily(trade, time='14:55') # 交易函数 def trade(context): # 获取当前价格 current_price = get_close_price(g.security) # 获取当前持仓 current_position = context.portfolio.positions[g.security] # 获取短期均线和长期均线 fast_ma = get_ma(g.security, g.fast_ma, '1d', 'close') slow_ma = get_ma(g.security, g.slow_ma, '1d', 'close') # 判断交易信号 if fast_ma > slow_ma and current_position is None: # 产生买入信号 order_value(g.security, context.portfolio.cash) elif fast_ma < slow_ma and current_position is not None: # 产生卖出信号 order_target(g.security, 0) # 获取均线函数 def get_ma(security, ma_length, frequency, field): # 获取历史价格 prices = attribute_history(security, ma_length+1, frequency, ['close']) # 计算均线 ma = sum(prices['close']) / ma_length return ma # 获取收盘价函数 def get_close_price(security): return get_price(security, end_date=context.current_dt, fields=['close'], count=1)[security][-1]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sirhzx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值