Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#32 Fixed occasional StaleExceptionError during runtime and added extended OHLC #36

Merged
merged 1 commit into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions IntelliTrader/IntelliTrader.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
os.remove(old_access_token_file)
except:
print("Error while deleting file : ", old_access_token_file)
exit()

# Begin a new connection
connect = Connection(config)
Expand All @@ -40,9 +41,3 @@

help = Helper(connection_kit)
fetch = Fetch(connection_kit)

fetch.fetch_instruments()
fetch.fetch_instruments('NSE')
fetch.fetch_instruments('NFO')
#fetch.instrument_lookup('NSE', 'RELIANCE')
#fetch.fetch_ohlc('NSE', 'RELIANCE', '5minute', 5)
3 changes: 2 additions & 1 deletion IntelliTrader/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def broker_login(self, KiteConnect):
continue_btn = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '//*[@id="container"]/div[2]/div/div/form/div[2]/button'))
)
continue_btn.click()

time.sleep(5)

# Request token generation
url = driver.current_url
Expand Down
64 changes: 54 additions & 10 deletions IntelliTrader/fetch.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from tkinter.tix import COLUMN
import pandas as pd
import os
import datetime as dt
Expand All @@ -17,17 +18,60 @@ def fetch_instruments(self, exchange=None):
return instruments_dump

# Lookup instrument token
def instrument_lookup(self, segment, symbol):
nse_instruments_dump = self.prop['k_token'].instruments(segment)
def instrument_lookup(self, exchange, symbol):
nse_instruments_dump = self.prop['k_token'].instruments(exchange)
instrument_df = pd.DataFrame(nse_instruments_dump)
try:
return instrument_df[instrument_df.tradingsymbol == symbol].instrument_token.values[0]
except:
return False

# Fetch historical data for a segment and symbol
def fetch_ohlc(self, segment, symbol, interval, duration):
instrument = self.instrument_lookup(segment, symbol)
data = pd.DataFrame(self.prop['k_token'].historical_data(instrument, dt.date.today()-dt.timedelta(duration), dt.date.today(), interval))
Helper.write_csv_output('historical_' + segment + '_' + symbol + '.csv', data)
return data
print('Please verify that the symbol name [%s] is present in the specified exchange.' %(symbol))
exit()

# Fetch historical data for an exchange and symbol
def fetch_ohlc(self, exchange, symbol, interval, duration):
instrument_token = self.instrument_lookup(exchange, symbol)
data = pd.DataFrame(self.prop['k_token'].historical_data(instrument_token, dt.date.today()-dt.timedelta(duration), dt.date.today(), interval))
Helper.write_csv_output('historical_' + exchange + '_' + symbol + '.csv', data)
return data

# Fetch extended historical data for an exchange and symbol with limits
def fetch_ohlc_extended(self, exchange, symbol, period_start, interval):
match interval:
case "minute":
lookback_period_threshold = 60
case "3minute":
lookback_period_threshold = 100
case "5minute":
lookback_period_threshold = 100
case "10minute":
lookback_period_threshold = 100
case "15minute":
lookback_period_threshold = 200
case "30minute":
lookback_period_threshold = 200
case "60minute":
lookback_period_threshold = 400
case "day":
lookback_period_threshold = 2000
case _:
lookback_period_threshold = 1


instrument_token = self.instrument_lookup(exchange, symbol)
start_date = dt.datetime.strptime(period_start, '%d-%m-%Y')
end_date = dt.date.today()
data = pd.DataFrame(columns=['date', 'open', 'high', 'low', 'close', 'volume'])
while True:
if start_date.date() >= (dt.date.today() - dt.timedelta(lookback_period_threshold)):
data = data._append(pd.DataFrame(self.prop['k_token'].historical_data(instrument_token, start_date, dt.date.today(), interval)), ignore_index=True)
break
else:
end_date = start_date + dt.timedelta(lookback_period_threshold)
data = data._append(pd.DataFrame(self.prop['k_token'].historical_data(instrument_token, start_date, end_date, interval)), ignore_index=True)
start_date = end_date

Helper.write_csv_output('historical_' + exchange + '_' + symbol + '_' + str(lookback_period_threshold) + '.csv', data)
return data