需要帮助?

试试AI小助手吧

一个基于指数优化得网格交易策略,谁能调下参数

用户头像韭菜来割
2025-02-12 发布

from jqdata import *

初始化函数,设定基准等等

def initialize(context):

全局变量初始化

g.cash = 100000 # 初始资金
g.security = '159919.XSHE' # 交易标的(沪深300 ETF)
g.bottom_price = get_initial_bottom_price(g.security) # 初始底部价格

设定自身走势作为基准

set_benchmark(g.security)

开启动态复权模式(真实价格)

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=1), type='stock')

初始底仓设置

set_initial_position(context)

运行函数

run_daily(before_market_open, time='before_open') # 开盘前运行
run_daily(market_open, time='open') # 开盘时运行
run_daily(after_market_close, time='after_close') # 收盘后运行

获取初始底部价格

def get_initial_bottom_price(security):
close_data = attribute_history(security, 1, '1d', ['close'])
return close_data['close'][0]

设置初始底仓

def set_initial_position(context):
SHZS = attribute_history('000001.XSHG', 1, '1d', ['close'])['close'][0] # 获取上证指数点位
if SHZS <= 2800:
order_target_value(g.security, g.cash * 0.9)
elif SHZS >= 3600:
order_target_value(g.security, g.cash * 0.1)
else:
order_target_value(g.security, g.cash * (0.5 + (3200 - SHZS) / 1000))
log.info(f"初始点位:{SHZS}, 初始底仓设置完成")

开盘前运行函数

def before_market_open(context):
log.info(f'函数运行时间(before_market_open):{context.current_dt.time()}')

开盘时运行函数

def market_open(context):
log.info(f'函数运行时间(market_open): {context.current_dt.time()}')
security = g.security

获取当前价格

current_data = get_current_data()
current_price = current_data[security].last_price

计算涨跌幅

returns = (current_price - g.bottom_price) / g.bottom_price

获取网格交易参数和买卖比例

bench_buy, bench_sell = bench_switch(context)
buy_ratio, sell_ratio = get_trade_ratios(context)

计算每次交易的金额

buy_value = context.portfolio.total_value * buy_ratio
sell_value = context.portfolio.total_value * sell_ratio

网格交易逻辑

try:
# 卖出逻辑
if context.portfolio.positions_value > sell_value and returns > bench_sell:
order_value(security, -sell_value)
log.info(f"卖出操作:当前价格={current_price}, 底部价格={g.bottom_price}, 卖出比例={sell_ratio}")
g.bottom_price = current_price

# 买入逻辑
if returns < bench_buy:
    if context.portfolio.available_cash > buy_value:
        order_value(security, buy_value)
    else:
        order_value(security, context.portfolio.available_cash)
    log.info(f"买入操作:当前价格={current_price}, 底部价格={g.bottom_price}, 买入比例={buy_ratio}")
    g.bottom_price = current_price

except Exception as e:
log.error(f"交易失败:{e}")

收盘后运行函数

def after_market_close(context):
log.info(f'函数运行时间(after_market_close): {context.current_dt.time()}')
trades = get_trades()
for _trade in trades.values():
log.info(f'成交记录:{_trade}')
log.info(f'一天结束,当前现金:{context.portfolio.cash}')
log.info('##############################################################')

网格大小的选取

def bench_switch(context):
SH_INDEX = attribute_history('000001.XSHG', 1, '1d', ['close'])['close'][0]
if SH_INDEX <= 2800:
bench_buy, bench_sell = -0.03, 0.20
elif SH_INDEX <= 2900:
bench_buy, bench_sell = -0.04, 0.16
elif SH_INDEX <= 3000:
bench_buy, bench_sell = -0.05, 0.12
elif SH_INDEX <= 3100:
bench_buy, bench_sell = -0.06, 0.10
elif SH_INDEX <= 3200:
bench_buy, bench_sell = -0.07, 0.08
elif SH_INDEX <= 3300:
bench_buy, bench_sell = -0.08, 0.08
elif SH_INDEX <= 3400:
bench_buy, bench_sell = -0.09, 0.07
elif SH_INDEX <= 3500:
bench_buy, bench_sell = -0.10, 0.06
else:
bench_buy, bench_sell = -0.12, 0.05
return bench_buy, bench_sell

获取买卖比例

def get_trade_ratios(context):
SH_INDEX = attribute_history('000001.XSHG', 1, '1d', ['close'])['close'][0]
if SH_INDEX < 3000:
buy_ratio, sell_ratio = 0.15, 0.10 # 3000点以下,买入15%,卖出10%
elif SH_INDEX >= 3600:
buy_ratio, sell_ratio = 0.08, 0.10 # 3600点以上,买入8%,卖出10%
else:
buy_ratio, sell_ratio = 0.10, 0.10 # 3000点到3400点,对称买卖10%
return buy_ratio, sell_ratio

评论

用户头像
2025-03-16 15:31:46

你这个是在云上执行的,还是在本地执行的。
我在云端执行的时候发现,如果你是在云端jupyterlab执行那我想问:

from jqdata import * 也就是 ModuleNotFoundError: No module named 'jqdata'

但我却无法执行 云端的: !pip install jqdatasdk
因此我想调取terminal(终端)但是也找不到入口

评论