def initialize(context): # 设置回测日期区间 set_benchmark('000300.XSHG') set_option('use_real_price', True) # 设置买入的股票数量上限 g.max_stock_count = 5 def handle_data(context, data): # 获取当前日期 current_date = context.current_dt.date() # 获取股票池中的股票列表 stocks = get_index_stocks('000852.XSHG') # 按照股票池中的股票进行遍历 for stock in stocks: # 判断股票是否满足买入条件 if check_buy_condition(stock, current_date, context): buy_stock(stock, context) # 判断持有的股票是否满足卖出条件 if check_sell_condition(stock, current_date, context): sell_stock(stock, context) def check_buy_condition(stock, current_date, context): # 判断股票是否连续下跌三天 prices = attribute_history(stock, 3, '1d', ['close']) if len(prices) == 3 and prices['close'][-1] < prices['close'][-2] < prices['close'][-3]: return True else: return False def buy_stock(stock, context): # 判断当前持仓的股票数量是否已达上限 if len(context.portfolio.positions) >= g.max_stock_count: return buy_date = context.current_dt.date() # 买入股票 order_value(stock, context.portfolio.cash / g.max_stock_count) def check_sell_condition(stock, current_date, context): # 获取持有股票的买入日期 buy_date = context.current_dt.date() time_diff = current_date - buy_date threshold = timedelta(days=3) # 判断是否满足卖出条件 if time_diff >= threshold or ((context.portfolio.positions[stock].last_price - context.portfolio.positions[stock].avg_cost) / context.portfolio.positions[stock].avg_cost <= -0.05): # 判断是否亏损超过5% return order_target(stock, 0) 报错 type object 'UserObject' has no attribute '__getattr__'
时间: 2024-04-10 10:28:32 浏览: 155
initialize_plotting_parameters.rar_fdtd_fdtd MATLAB
这个错误是由于在代码中使用了错误的函数来下单。在buy_stock函数中,应该使用order_target_value函数而不是order_value函数来下单。同样,在check_sell_condition函数中,应该使用order_target函数而不是order_target_value函数来卖出股票。以下是修改后的代码:
```python
def initialize(context):
# 设置回测日期区间
set_benchmark('000300.XSHG')
set_option('use_real_price', True)
# 设置买入的股票数量上限
g.max_stock_count = 5
def handle_data(context, data):
# 获取当前日期
current_date = context.current_dt.date()
# 获取股票池中的股票列表
stocks = get_index_stocks('000852.XSHG')
# 按照股票池中的股票进行遍历
for stock in stocks:
# 判断股票是否满足买入条件
if check_buy_condition(stock, current_date, context):
buy_stock(stock, context)
# 判断持有的股票是否满足卖出条件
if check_sell_condition(stock, current_date, context):
sell_stock(stock, context)
def check_buy_condition(stock, current_date, context):
# 判断股票是否连续下跌三天
prices = attribute_history(stock, 3, '1d', ['close'])
if len(prices) == 3 and prices['close'][-1] < prices['close'][-2] < prices['close'][-3]:
return True
else:
return False
def buy_stock(stock, context):
# 判断当前持仓的股票数量是否已达上限
if len(context.portfolio.positions) >= g.max_stock_count:
return
buy_date = context.current_dt.date()
# 买入股票
order_target_value(stock, context.portfolio.cash / g.max_stock_count)
def check_sell_condition(stock, current_date, context):
# 获取持有股票的买入日期
buy_date = context.current_dt.date()
time_diff = current_date - buy_date
threshold = timedelta(days=3)
# 判断是否满足卖出条件
if time_diff >= threshold or ((context.portfolio.positions[stock].last_price - context.portfolio.positions[stock].avg_cost) / context.portfolio.positions[stock].avg_cost <= -0.05):
# 判断是否亏损超过5%
return
order_target(stock, 0)
```
请尝试使用这个修改后的代码,并检查是否还有其他错误。
阅读全文