这个代码怎末提高近一年的收益啊,求求大佬指点

用户头像小新芭比朵拉2
2024-06-16 发布

import pandas as pd
import numpy as np
from talib import CMO

初始化函数,全局只运行一次

def init(context):
set_benchmark('000300.SH')
log.info('策略开始运行,初始化函数全局只运行一次')
set_commission(PerShare(type='stock',cost=0.0002))
set_slippage(PriceSlippage(0.002))
set_volume_limit(0.25,0.5)
context.security = '000300.SH'

context.max_stocks=15

context.stop_loss_threshold = 0.95 # 设置止损阈值,例如10%
context.profit_target = 1.05 # 设置止盈点,例如105%
def before_trading(context):
date = get_datetime().strftime('%Y-%m')
log.info('{} 盘前运行'.format(date))

打印日期

class StockPosition:
def init(self, data):

self.data = data
self.data.fillna(method='ffill', inplace=True)
self.avg_cost = self.data['close'].mean()

开盘时运行函数

def handle_bar(context, bar_dict):
time = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
log.info('{} 盘中运行'.format(time))
closeprice = history(context.security, ['close'], 20, '1d', False, 'pre', is_panel=1)
cmo = CMO(closeprice['close'].values, timeperiod=14)
position = context.portfolio.positions[context.security].quantity
current_price = closeprice['close'][-1]
stock_position = StockPosition(closeprice)
cost_price = stock_position.avg_cost

if cmo[-1] > 50 and position == 0:
order_target_percent(context.security, 1)

log.info("CMO大于50, 买入 {}".format(context.security))

CMO大于50且当前无持仓,则买入

elif cmo[-1] < -50 and position > 0:

order_target(context.security, 0)
log.info("CMO小于-50, 卖出 {}".format(context.security))

def handle_bar(context, bar_dict):

 time = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
 log.info('{} 盘中运行'.format(time))
 closeprice = history(context.security, ['close'], 20, '1d', False, 'pre', is_panel=1)
 cmo = CMO(closeprice['close'].values, timeperiod=21)
 position = context.portfolio.positions[context.security].quantity
 current_price = closeprice['close'][-1]
 stock_position = StockPosition(closeprice)
 cost_price = stock_position.avg_cost

if cmo[-1] > 50 and position == 0:
order_target_percent(context.security, 1)

log.info("CMO大于50, 买入 {}".format(context.security))

CMO大于50且当前无持仓,则买入

elif cmo[-1] < -50 and position > 0:

order_target(context.security, 0)
log.info("CMO小于-20, 卖出 {}".format(context.security))

elif current_price <= cost_price * context.stop_loss_threshold:
order_target(context.security, 0)
log.info("触发止损,卖出 {}".format(context.security))
elif current_price >= cost_price * context.profit_target:
order_target(context.security, 0)
log.info("达到止盈点,卖出 {}".format(context.security))

添加代码

stock_list = get_index_stocks('000300.SH') # 获取沪深300成分股
stock_data = []
for stock in stock_list:
closeprice = history(stock, ['close'], 20, '1d', False, 'pre', is_panel=1)
cmo = CMO(closeprice['close'].values, timeperiod=14)
stock_position = StockPosition(closeprice)
stock_data.append((stock, cmo[-1], stock_position.avg_cost))
stock_data.sort(key=lambda x: x[1], reverse=True) # 按cmo降序排列
selected_stocks = stock_data[:15] # 选取前5只股票

for stock, cmo_value, cost_price in selected_stocks:
if cmo_value > 50:
order_target_percent(stock, 1 / context.max_stocks)
log.info("CMO大于50, 买入 {}".format(stock))
elif cmo_value < -50:
order_target(stock, 0)
log.info("CMO小于-50, 卖出 {}".format(stock))

elif current_price <= cost_price * context.stop_loss_threshold:
    order_target(context.security, 0)
    log.info("触发止损,卖出 {}".format(context.security))
elif current_price >= cost_price * context.profit_target:
    order_target(context.security, 0)
    log.info("达到止盈点,卖出 {}".format(context.security))

def after_trading(context):
time = get_datetime().strftime('%Y-%m-%d %H:%M:%S')
log.info('{} 盘后运行'.format(time))
log.info('一天结束')

评论

用户头像
2024-06-22 11:21:23

先对照大盘3个指数 看看有没有大幅度低开或上涨

如果大盘不跌或微涨

在所有ETF中筛选出集合竞价时成交额大于10W

跌幅大于1%的ETF进行买入

次日开盘卖出

评论
用户头像
mx_**896sgf回复:一路向南啊
2024-08-15 21:35:39

先对照大盘3个指数 看看有没有大幅度低开或上涨

如果大盘不跌或微涨

在所有ETF中筛选出集合竞价时成交额大于10W

跌幅大于1%的ETF进行买入

次日开盘卖出

评论

需要帮助?

试试AI小助手吧