聚宽代码如何转移到 supermind

用户头像mx_*084q58
2023-05-24 发布

最近有在看聚宽的量化代码,想着如何转移到同花顺supermind中,在此分享一个案例供大家根据需求进行对应修改

以聚宽的双均线策略为例

# 导入函数库
from jqdata import *

# 初始化函数,设定基准等等
def initialize(context):
    # 设定沪深300作为基准
    set_benchmark('000300.XSHG')
    # 开启动态复权模式(真实价格)
    set_option('use_real_price', True)
    # 输出内容到日志 log.info()
    log.info('初始函数开始运行且全局只运行一次')
    ### 股票相关设定 ###
    # 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱
    set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock')

## 开盘前运行函数
def before_market_open(context):
    # 输出运行时间
    log.info('函数运行时间(before_market_open):'+str(context.current_dt.time()))

    # 要操作的股票:平安银行(g.为全局变量)
    g.security = '000001.XSHE'

## 开盘时运行函数
def market_open(context):
    log.info('函数运行时间(market_open):'+str(context.current_dt.time()))
    security = g.security
    # 获取股票的收盘价
    close_data = get_bars(security, count=5, unit='1d', fields=['close'])
    # 取得过去五天的平均价格
    MA5 = close_data['close'].mean()
    # 取得上一时间点价格
    current_price = close_data['close'][-1]
    # 取得当前的现金
    cash = context.portfolio.available_cash

    # 如果上一时间点价格高出五天平均价1%, 则全仓买入
    if (current_price > 1.01*MA5) and (cash > 0):
        # 记录这次买入
        log.info("价格高于均价 1%%, 买入 %s" % (security))
        print("当前可用资金为{0}, position_value为{0}".format(cash, context.portfolio.positions_value))
        # 用所有 cash 买入股票
        order_value(security, cash)
    # 如果上一时间点价格低于五天平均价, 则空仓卖出
    elif current_price < MA5 and context.portfolio.positions[security].closeable_amount > 0:
        # 记录这次卖出
        log.info("价格低于均价, 卖出 %s" % (security))
        # 卖出所有股票,使这只股票的最终持有量为0
        order_target(security, 0)

## 收盘后运行函数
def after_market_close(context):
    log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
    log.info('一天结束')

通过下列表格,能通过修改聚宽的代码在同花顺SuperMind平台中运行:

注释 聚宽 同花顺
# 导入函数库 from jqdata import * 删除
# 初始化函数,设定基准等等 def initialize(context): def init(context):
# 设定沪深300作为基准 set_benchmark('000300.XSHG') set_benchmark('000300.SH')
# 股票类每笔交易时的手续费是:买入时佣金万分之三,卖出时佣金万分之三加千分之一印花税, 每笔交易佣金最低扣5块钱 set_order_cost(OrderCost(close_tax=0.001, open_commission=0.0003, close_commission=0.0003, min_commission=5), type='stock') set_commission(PerShare( type='stock', cost=0.0003, min_trade_cost=5.0))
# 开启动态复权模式(真实价格) set_option('use_real_price', True) 删除
# 设置股票交易滑点0.5%,表示买入价为实际价格乘1.005,卖出价为实际价格乘0.995 set_slippage(PriceSlippage(0.005))
# 设置日级最大成交比例25%,分钟级最大成交比例50% set_volume_limit(0.25,0.5)
# 输出内容到日志 log.info() log.info('初始函数开始运行且全局只运行一次') log.info('策略开始运行,初始化函数全局只运行一次')
# 设置要操作的股票:中国平安 g.security = '000001.XSHE' context.security = '000001.SZ'
# 开盘前运行 run_daily(before_market_open, time='before_open', reference_security='000300.XSHG') 日级策略下删除
# 开盘时运行 run_daily(market_open, time='open', reference_security='000300.XSHG') 日级策略下删除
# 收盘后运行 run_daily(after_market_close, time='after_close', reference_security='000300.XSHG') 日级策略下删除
## 开盘前运行函数 def before_market_open(context): def before_trading(context):
# 输出运行时间 log.info('函数运行时间(before_market_open):'+str(context.current_dt.time())) date = get_datetime().strftime('%Y-%m-%d %H:%M:%S') log.info('{} 盘前运行'.format(date))
## 开盘时运行函数 def market_open(context): def handle_bar(context, bar_dict):
#打印时间 log.info('函数运行时间(market_open):'+str(context.current_dt.time())) time = get_datetime().strftime('%Y-%m-%d %H:%M:%S') log.info('{} 盘中运行'.format(time))
#导入交易标的 security = g.security 删除
# 获取股票的收盘价 close_data = get_bars(security, count=5, unit='1d', fields=['close']) closeprice = history(context.security, ['close'], 5, '1d', False, 'pre', is_panel=1)
# 取得过去五天的平均价格 MA5 = close_data['close'].mean() MA5 = closeprice['close'].mean()
# 取得上一时间点价格 current_price = close_data['close'][-1] MA1 = closeprice['close'].iloc[-1:] .mean()
# 取得当前的现金 cash = context.portfolio.available_cash cash = context.portfolio.stock_account.available_cash
# 获取当前账户当前持仓市值 market_value = context.portfolio.stock_account.market_value
# 获取账户持仓股票列表 stocklist = list(context.portfolio.stock_account.positions)
# 如果上一时间点价格高出五天平均价1%, 则全仓买入 if (current_price > 1.01*MA5) and (cash > 0): if MA1 > 1.01*MA5 and cash > 0 :
# 记录这次买入 log.info("价格高于均价 1%%, 买入 %s" % (security)) log.info("价格高于均价 1%%, 买入 %s" % (context.security))
# 用所有 cash 买入股票 order_value(security, cash) order_value(context.security, cash)
# 如果上一时间点价格低于五天平均价, 则空仓卖出 elif current_price < MA5 and context.portfolio.positions[security].closeable_amount > 0: elif MA1 > MA5 and market_value > 0:
# 记录这次卖出 log.info("价格低于均价, 卖出 %s" % (security)) log.info("价格低于均价, 卖出 %s" % (context.security))
# 卖出所有股票,使这只股票的最终持有量为0 order_target(security, 0) order_target(context.security, 0)
## 收盘后运行函数 def after_market_close(context): def after_trading(context):
# 打印时间 log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time()))) time = get_datetime().strftime('%Y-%m-%d %H:%M:%S') log.info('{} 盘后运行'.format(time))

对于上述未涉及的代码修改,可结合同花顺API文档进行查询使用

收益&风险
源码

评论

需要帮助?

试试AI小助手吧