Spaces:
Runtime error
Runtime error
OfirMatzlawi
commited on
Commit
•
46ee02f
1
Parent(s):
72ea042
Update main.py
Browse files
main.py
CHANGED
@@ -130,3 +130,44 @@ def read_item(ticker: str):
|
|
130 |
stock_data = get_stock_data(ticker)
|
131 |
result = stock_data.to_json(orient="records")
|
132 |
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
stock_data = get_stock_data(ticker)
|
131 |
result = stock_data.to_json(orient="records")
|
132 |
return result
|
133 |
+
|
134 |
+
## TimeGPT
|
135 |
+
|
136 |
+
@app.get("/ticker/{ticker}")
|
137 |
+
def get_data_from_yahoo(ticker: str):
|
138 |
+
yf.pdr_override()
|
139 |
+
data = pdr.get_data_yahoo(ticker, start="2023-01-01", end=None)
|
140 |
+
df = pd.DataFrame(data).reset_index()
|
141 |
+
return df
|
142 |
+
|
143 |
+
##TimeGPT APIkey validation
|
144 |
+
|
145 |
+
api_key = 'nixt-7nixiWJtV1TwD3vp9K8WqLrgkIZmsXZ0gxBqrOSI1E3XPpkVSakyPYgMtWdtNKeBgStnPncgzpGqQzoG'
|
146 |
+
|
147 |
+
def apikey(api_key):
|
148 |
+
nixtla_client = NixtlaClient(
|
149 |
+
api_key = api_key
|
150 |
+
)
|
151 |
+
key_valid = nixtla_client.validate_api_key()
|
152 |
+
return key_valid
|
153 |
+
|
154 |
+
def forecasting(key_valid,df):
|
155 |
+
if key_valid == True:
|
156 |
+
## forecasting 3 months
|
157 |
+
timegpt_fcst_df = nixtla_client.forecast(df=df, h=3, freq='MS', time_col='Date', target_col='Adj Close')
|
158 |
+
else:
|
159 |
+
timegpt_fcst_df= 'API key is not Valid'
|
160 |
+
|
161 |
+
return timegpt_fcst_df, nixtla_client.plot(df, timegpt_fcst_df, time_col='Date', target_col='Adj Close')
|
162 |
+
|
163 |
+
|
164 |
+
|
165 |
+
## Anomaly Detection
|
166 |
+
|
167 |
+
def anomaly_detect(df):
|
168 |
+
timegpt_anomalies_df = nixtla_client.detect_anomalies(df, time_col='Date', target_col='Adj Close', freq='D', level = 95, date_features=True)
|
169 |
+
return nixtla_client.plot(df,
|
170 |
+
timegpt_anomalies_df,
|
171 |
+
time_col='Date',
|
172 |
+
target_col='Adj Close')
|
173 |
+
|