Python全系列 教程
3567个小节阅读:5930.6k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
在炒股实战中,往往多条均线一起使用,如5日、10日和30日均线。
当5日均线大于10日均线、10日均线大于30日均线且收盘价格高于5日均线价格的1.01
倍时,买入股票;当5日均线小于10日均线、10日均线小于30日均线且收盘价格低于5日均
线价格的0.99倍时,卖出股票。
xxxxxxxxxx
# 导入函数库
import jqdata
from jqlib.technical_analysis import *
def initialize(context):
# 定义一个全局变量, 保存要操作的股票
# 000538(股票:云南白药)
g.security = '000538.XSHE'
# 设定沪深300作为基准
set_benchmark('000300.XSHG')
# True为开启动态复权模式,使用真实价格交易
set_option('use_real_price', True)
# 设定成交量比例,成交量不超过总成交量的100%
set_option('order_volume_ratio', 1)
'''
set_order_cost()是用来设置佣金/印花税的函数,用来设定每笔交易收取的手续费
第一个参数是OrderCost对象,各项子参数意义如下:
open_tax,买入时印花税(只有股票类标的收取,基金与期货不收)
close_tax,卖出时印花税(只有股票类标的收取,基金与期货不收)
open_commission,买入时佣金
close_commission,卖出时佣金
close_today_commission,平今仓佣金
min_commission,最低佣金,不包含印花税
第二个参数type是类型,'stock'代表股票
'''
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
n3 = 30
# 获取股票的收盘价
close_data = attribute_history(security, n3+2, '1d', ['close'],df=False)
# 取得过去 ma_n1 天的平均价格
ma_n1 = close_data['close'][-n1:].mean()
# 取得过去 ma_n2 天的平均价格
ma_n2 = close_data['close'][-n2:].mean()
# 取得过去 ma_n3 天的平均价格
ma_n3 = close_data['close'][-n3:].mean()
# 取得上一时间点价格
current_price = close_data['close'][-1]
# 取得当前的现金
cash = context.portfolio.cash
# 如果当前有余额,并且n1日均线大于n2日均线,n2日均线大于n3日均线,上一时间点价格高出五天平均价1%, 则全仓买入
if ma_n1 > ma_n2 and ma_n2 > ma_n3 and current_price> 1.01*ma_n1:
# 用所有 cash 买入股票
order_value(security, cash)
# 记录这次买入
log.info("Buying %s" % (security))
# 如果n1日均线小于n2日均线,n2日均线小于n3日均线, 上一时间点价格低于五天平均价1%,closeable_amount获取可卖出的仓位
elif ma_n1 < ma_n2 and ma_n2 < ma_n3 and current_price< 0.99*ma_n1 and context.portfolio.positions[security].closeable_amount > 0:
# 全部卖出
order_target(security, 0)
# 记录这次卖出
log.info("Selling %s" % (security))