-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnew_stock_1D.py
executable file
·138 lines (114 loc) · 5.26 KB
/
new_stock_1D.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import time
import pandas as pd
import yfinance as yf
from constants import *
from sqlalchemy import text
from datetime import datetime
from datetime import timedelta
from my_fyers_model import MyFyersModel
from db_connection import get_mysql_connection
fy_model = MyFyersModel()
today_date = datetime.today().strftime("%Y-%m-%d")
def get_table_name_last_date():
query = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'fnodatabase';"
engine = get_mysql_connection()
table_date_time = {}
old_date_time = {}
with engine.connect() as connection:
result = connection.execute(text(query))
all_tables = [table[0] for table in list(result) if table[0].endswith('_1d') or table[0].endswith('_1D')]
for table_name in all_tables:
query = f"SELECT * FROM {table_name};"
table_data = connection.execute(text(query))
last_date = list(table_data)[-1]
if issubclass(type(last_date[1]), str):
last_date = last_date[1].split('+')[0]
datetime_object = datetime.strptime(last_date, '%Y-%m-%d %H:%M:%S')
else:
datetime_object = last_date[1]
old_date_time[table_name] = str(datetime_object.__format__("%Y-%m-%d"))
# Yesterday date
last_date = datetime_object + timedelta(days=1)
table_date_time[table_name] = str(last_date.__format__("%Y-%m-%d"))
print(table_date_time)
return table_date_time
def tata_last_record():
return {'tatapower_1d': '2013-4-2'}
init = 40
count = 1
master_data = []
def store_new_stock():
global init, count, master_data
try:
for table_name, last_date in tata_last_record().items():
print(last_date)
if last_date != today_date:
dates = pd.date_range(start=last_date, end=today_date).tolist()
# master_data = []
symbol = None
for range_from, range_to in zip(dates, dates[1:]):
print(range_from.strftime("%Y-%m-%d"), range_to.strftime("%Y-%m-%d"))
if table_name == 'tatapower_1d':
symbol = stocks_option_symbols['tata_power']
else:
print(f'Invalid symbol name : "{table_name}"')
data = {
"symbol": symbol,
"resolution": "1D",
"date_format": "1",
"range_from": range_from.strftime("%Y-%m-%d"),
"range_to": range_to.strftime("%Y-%m-%d"),
"cont_flag": "1"
}
response = fy_model.get_history(data=data)
master_data += response['candles']
if len(master_data) != 0:
print(master_data[-1])
if count == init:
time.sleep(10)
init += 40
count += 1
print(count, init)
df = pd.DataFrame(master_data, columns=["epoc", "open", "high", "low", "close", "volume"])
df['timestamp'] = pd.to_datetime(df['epoc'], unit='s', utc=True).map(lambda x: x.tz_localize(None))
df = df[["timestamp", "open", "high", "low", "close", "volume"]]
df.drop_duplicates(inplace=True)
df['volume'] = 0
df.to_sql(name=table_name, con=get_mysql_connection(), index=False, if_exists='append')
print(df.head())
print(df.tail())
time.sleep(2)
print(f'{table_name} is updated...')
print('\n')
else:
print(f'{table_name} : table is already up to date ...')
except KeyboardInterrupt as ki:
df = pd.DataFrame(master_data, columns=["epoc", "open", "high", "low", "close", "volume"])
df['timestamp'] = pd.to_datetime(df['epoc'], unit='s', utc=True).map(lambda x: x.tz_localize(None))
df = df[["timestamp", "open", "high", "low", "close", "volume"]]
df.drop_duplicates(inplace=True)
df['volume'] = 0
df.to_csv(str(datetime.now().time()) + '.csv', index=False)
# df.to_sql(name='ongc_1d', con=get_mysql_connection(), index=False, if_exists='append')
print(df.head())
print(df.tail())
time.sleep(2)
# print(f'{table_name} is updated...')
print(ki, 'Error occurred...')
except Exception as e:
df = pd.DataFrame(master_data, columns=["epoc", "open", "high", "low", "close", "volume"])
df['timestamp'] = pd.to_datetime(df['epoc'], unit='s', utc=True).map(lambda x: x.tz_localize(None))
df = df[["timestamp", "open", "high", "low", "close", "volume"]]
df.drop_duplicates(inplace=True)
df['volume'] = 0
df.to_csv(str(datetime.now().time()) + '.csv', index=False)
df.to_sql(name='tatapower_1d', con=get_mysql_connection(), index=False, if_exists='append')
print(df.head())
print(df.tail())
time.sleep(2)
print('tatapower_1d is updated...')
print(e, ' : Error occurred...')
ask = input("do you want to update 1 day data (Y/n) : ")
if ask.lower() == 'y':
store_new_stock()