等级: 新手上路
- 注册:
- 2024-8-12
- 曾用名:
|
from PythonApi import *
import numpy as np
def init(context):
context.s1 = "SZ000001" # 设定关注的股票
context.position = 0 # 初始化持仓状态
context.last_trend = 0 # 上一次趋势标记
context.days_since_last_trend = 0 # 距离上一次趋势的天数
context.highest_price_3_days_ago = 0 # 三天前最高价
context.consecutive_red_days = 0 # 连续收绿天数
context.price_5_days_ago = 0 # 5天前的价格
context.consecutive_green_days = 0 # 连续收阳天数
def handle_bar(context):
N = 100 # 设定时间窗口为5天
close_prices = history_bars(context.s1, N, 'self', 'close', include_now=True) # 获取最近5天的收盘价
if len(close_prices) < N:
return
ma5 = np.mean(close_prices) # 计算5日均线
# 计算涨跌幅
close_today = close_prices[-1]
close_yesterday = close_prices[-2]
change_pct = (close_today - close_yesterday) / close_yesterday * 100
color = 'red' if change_pct > 0 else 'green'
print(f"涨跌幅:{change_pct:.2f}%", color=color)
# 判断当前持仓状态并执行相应操作
if context.position == 0:
# 判断是否满足买入条件
if context.days_since_last_trend >= 5 and close_today > ma5 and (context.price_5_days_ago - close_prices[-5]) / context.price_5_days_ago < 0.02:
buy_open(context.s1, "Market", 0, int(get_account(6) * 0.1 / close_today),serial_id = 1)
context.position = 1
context.highest_price_3_days_ago = max(close_prices[-3:])
context.consecutive_red_days = 0
else:
if context.position == 1:
# 判断是否满足平多条件
if close_today / close_yesterday - 1 < -0.02 or close_today < context.highest_price_3_days_ago or context.consecutive_red_days >= 2:
sell_close(context.s1, "Market", 0, get_portfolio(context.s1, 2).buy_quantity,serial_id = 2)
context.position = 0
context.days_since_last_trend = 0
context.last_trend = 1
context.price_5_days_ago = close_prices[-5]
else: # context.position == -1
# 判断是否满足平空条件
if close_today > close_yesterday and close_yesterday > close_prices[-3]:
buy_close(context.s1, "Market", 0, get_portfolio(context.s1, 2).sell_quantity,serial_id = 3)
context.position = 0
context.days_since_last_trend = 0
context.last_trend = -1
context.price_5_days_ago = close_prices[-5]
# 更新连续收阳/绿天数和三天前最高价
if close_today < close_yesterday:
context.consecutive_red_days += 1
context.consecutive_green_days = 0
else:
context.consecutive_red_days = 0
context.consecutive_green_days += 1
context.highest_price_3_days_ago = max(close_prices[-3:])
# 更新距离上一次趋势的天数
if context.last_trend != 0:
context.days_since_last_trend += 1
# 判断趋势
if all(close_today > price for price in close_prices[:-1]):
trend = "上涨趋势"
elif all(close_today < price for price in close_prices[:-1]):
trend = "下跌趋势"
elif context.consecutive_red_days < 3 and context.consecutive_green_days < 3:
if close_today > ma5:
trend = "高位震荡"
else:
trend = "底部震荡"
else:
trend = "无明显趋势"
print(f"当前趋势:{trend}")
# 判断是否满足新的卖空条件
if context.days_since_last_trend >= 3 and context.last_trend == 1 and (close_today - close_prices[-3]) / close_prices[-3] < 0.02:
sell_open(context.s1, "Market", 0, int(get_account(6) * 0.1 / close_today),serial_id = 4)
context.position = -1
context.days_since_last_trend = 0
# 更新5天前的价格
context.price_5_days_ago = close_prices[-5] |
|