确定时间序列中的熊市周期,股票数据

2024-05-18 18:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

如何识别(熊市)时间序列中的某个时段,在该时段内,价格下降幅度大于某个阈值,且数据时间不是周期性的

例如,确定的(熊市)时段应在2小时内以3%或以上的价格下跌

import yfinance as yf
import pandas as pd
from datetime import datetime
import random

def random_times(stime, etime, n):
    frmt = '%Y-%m-%d %H:%M:%S'
    stime = datetime.strptime(stime, frmt)
    etime = datetime.strptime(etime, frmt)
    td = etime - stime
    return [random.random() * td + stime for _ in range(n)]

# Getting historical, daily ohlc data from yfinance
msft = yf.Ticker("MSFT")
hist = msft.history(period="max")

# Generating random datetimes for rows in yfinance data
datetimes = random_times('2020-01-01 00:00:00','2021-01-30 00:00:00',len(hist.index))
datetimes = pd.to_datetime(datetimes)
datetimes = datetimes.sort_values()

# Dataframe with random datetimes and real MSTF close prices
df = pd.DataFrame({ 'time': datetimes,
                    'close': hist['Close']})
df = df.set_index('time')

df.plot(figsize=(50,30))

enter image description here


Tags: importdfdatetimeas时间randomhistpd

热门问题