
等级: 新手上路
- 注册:
- 2025-1-26
- 曾用名:
|
# 初始化参数INITIAL_PRICE = 2750; # 建仓价格BUY_STEP = 50; # 买入步长(每降50点)SELL_STEP = 50; # 卖出步长(每涨50点)LOT_SIZE = 5; # 每次买入或卖出的手数POSITION_LIMIT = 20; # 最大持仓手数限制# 初始化变量position = 0; # 当前持仓手数buy_levels = []; # 记录买入的价格层级current_price = INITIAL_PRICE; # 当前价格(假设从建仓价格开始)def execute_strategy(price): global position, current_price, buy_levels; # 更新当前价格 current_price = price; # 买入逻辑:价格每降50点,买入5手 if price <= INITIAL_PRICE - len(buy_levels) * BUY_STEP: if position + LOT_SIZE <= POSITION_LIMIT: # 检查持仓限制 position += LOT_SIZE; buy_levels.append(price); print(f"买入{LOT_SIZE}手,价格:{price},当前持仓:{position}手"); else: print("已达到最大持仓限制,无法买入"); else: print("未达到买入条件"); # 卖出逻辑:价格超过2800后,每涨50点,卖出5手 if price > 2800 and len(buy_levels) > 0: # 找到最后一个符合卖出条件的层级 for i in range(len(buy_levels) - 1, -1, -1): if price >= buy_levels + SELL_STEP: if position >= LOT_SIZE: # 检查是否有足够的持仓 position -= LOT_SIZE; sold_price = buy_levels.pop(i); print(f"卖出{LOT_SIZE}手,价格:{price},当前持仓:{position}手"); else: print("持仓不足,无法卖出"); break; else: print("未达到卖出条件"); else: print("价格未超过2800,不执行卖出");# 模拟价格变化price_changes = [2700, 2650, 2600, 2550, 2800, 2850, 2900, 2950, 3000];# 运行策略for price in price_changes: print(f"当前价格:{price}"); execute_strategy(price); print("-" * 30);
|
|