连续红三次,然后第四次红的概率如何用代码‼️
由bq1ek5t7创建,最终由small_q 被浏览 12 用户
写了个连续红三次,然后第四次红的概率有多少,不知道怎么弄成回测的,呜呜呜,下面是我的代码
#连红三次,看第四次还是红的概率
import dai
df = dai.query("""
SELECT
date, instrument, ((close - open) / open) * 100 as price_change_percentage,
CASE
WHEN ((close - open) / open) * 100 > 0 THEN 1
ELSE 0
END as price_change_percentage_binary
FROM cn_cbond_bar1m
WHERE instrument = '128044.SZ'
ORDER BY date
""",
filters={"date": ["2024-01-01", "2024-06-25"]}
).df()
#提取price_change_percentage_binary列的数据
price_change_percentage_binary = df['price_change_percentage_binary']
#计算连续出现3个1后,第四个出现1的概率
probability_1 = 0
probability_not_1 = 0
for i in range(len(price_change_percentage_binary) - 3):
if price_change_percentage_binary[i] == 1 and price_change_percentage_binary[i + 1] == 1 and price_change_percentage_binary[i + 2] == 1:
if i==0 or price_change_percentage_binary[i-1] == 0:
#print(price_change_percentage_binary[i-1],price_change_percentage_binary[i], price_change_percentage_binary[i + 1] , price_change_percentage_binary[i + 2])
if price_change_percentage_binary[i + 3] == 1:
probability_1 += 1
#print("yes:",price_change_percentage_binary[i + 3])
else:
#print("no",price_change_percentage_binary[i + 3])
probability_not_1 += 1
print(probability_1)
print(probability_not_1)
\