-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistoricalData.py
More file actions
42 lines (34 loc) · 1.51 KB
/
Copy pathhistoricalData.py
File metadata and controls
42 lines (34 loc) · 1.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import pandas as pd
import datetime as dt
import pytz
import configuration as config
from fyers_apiv3 import fyersModel
# ============================================================
# Historical Data Fetching
# ============================================================
def fetch_historical_data(fyers: fyersModel.FyersModel) -> pd.DataFrame:
"""
Before starting the live chart, we need to know what happened earlier
in the day so the chart doesn't start from a blank screen.
"""
now = dt.datetime.now(pytz.timezone(config.timeZone))
start_of_day = now.replace(hour=0, minute=0, second=0, microsecond=0)
previous_time = start_of_day.timestamp()
current_time = now.timestamp()
nifty_data = {
"symbol": config.symbol,
"resolution": config.resolution,
"date_format": "0",
"range_from": int(previous_time),
"range_to": int(current_time),
"cont_flag": "1"
}
# Ask Fyers for the data and convert it into an Excel-like table (DataFrame)
response = fyers.history(data=nifty_data)
historical_data = response['candles']
df = pd.DataFrame(historical_data, columns=['date', 'open', 'high', 'low', 'close', 'volume'])
# Fix the time format so humans and computers can read it easily
df['date'] = pd.to_datetime(df['date'], unit='s')
df['date'] = df['date'].dt.tz_localize('UTC').dt.tz_convert(pytz.timezone(config.timeZone))
df.set_index('date', inplace=True)
return df