Python history backtest download data from kite zerodha
import json
import math
from kite_trade import *
from datetime import datetime,timedelta
import time
import pandas as pd
import pandas_ta as ta
enctoken = ""
kite = KiteApp(enctoken=enctoken)
date_object = datetime(2023, 5, 17, 9, 15)
# Convert to '%Y-%m-%d %H:%M' datetime format
formatted_date = date_object.strftime('%Y-%m-%d %H:%M')
value=kite.instruments("NFO")
print(value)
# original_date = datetime.strptime("2023-01-07 00:00:00", "%Y-%m-%d %H:%M:%S")
# Desired format
#
# instrument_token = 738561
#
# from_datetime = datetime.now() - timedelta(days=365) # From last 365 days
# to_datetime = datetime.now()
# price_data_combined = pd.DataFrame()
# res = to_datetime - from_datetime
# print(res)
#
# if res.days > 60:
# iteration = math.ceil(res.days / 60)
# print(iteration)
# k = 1
# targetdate1 = from_datetime
# while k <= iteration:
# source = targetdate1
# targetdate1 = targetdate1 + timedelta(days=60)
#
# print("Source:", source)
# print("Target Date:", targetdate1)
#
# interval = "minute"
# # print(kite.historical_data(instrument_token, from_datetime, to_datetime, interval, continuous=False, oi=False))
# historical_data = kite.historical_data(instrument_token, source, targetdate1, interval, continuous=False,
# oi=False)
#
# price_data = pd.DataFrame()
#
# for i in range(len(historical_data)):
# dataobj = historical_data[i]['date']
# reliance = dataobj.strftime('%Y-%m-%d %H:%M')
# row_to_append = {'open': float(historical_data[i]['open']), 'high': float(historical_data[i]['high']),
# 'low': float(historical_data[i]['low']), 'close': float(historical_data[i]['close']),
# 'Timestamp': reliance}
#
# price_data = pd.concat([price_data, pd.DataFrame(row_to_append, index=[0])], ignore_index=True)
#
# price_data_combined = pd.concat([price_data_combined, price_data], ignore_index=True) # Append to combined DataFrame
#
# price_data.to_csv('output.csv', index=False)
#
# k += 1
# print("Iteration:", k)
# print("Sleeping for 30 seconds...")
# time.sleep(30)
#
# # Export the combined DataFrame to a CSV file
# price_data_combined.to_csv('combined_output_reliance.csv', index=False)
#
# df=pd.read_csv("combined_output_reliance.csv")
#
#
# df["sup"]=ta.supertrend(high=df["high"],low=df["low"],close=df["close"],length=7,multiplier=3)["SUPERT_7_3.0"]
#
# df["BuySignal"]=0
# df["SellSignal"]=0
# n=6
#
# for i in range(n, len(df)):
# if df["close"][i - 1] <= df["sup"][i - 1] and df["close"][i] > df["sup"][i]:
# df.loc[i, "BuySignal"] = 1
# if df["close"][i - 1] >= df["sup"][i - 1] and df["close"][i] < df["sup"][i]:
# df.loc[i, "SellSignal"] = 1
#
#
# print(df.BuySignal.value_counts)
# print(df.SellSignal.value_counts)
#
# df.to_csv("Supertrend_results.csv")
#
#
# print(from_datetime)
# print(to_datetime)
#
#
# interval = "minute"
# # print(kite.historical_data(instrument_token, from_datetime, to_datetime, interval, continuous=False, oi=False))
# historical_data=kite.historical_data(instrument_token, from_datetime, to_datetime, interval, continuous=False, oi=False)
#
# price_data = pd.DataFrame()
#
#
# for i in range (len(historical_data)):
# dataobj=historical_data[i]['date']
# reliance= dataobj.strftime('%Y-%m-%d %H:%M')
# row_to_append = {'open':float(historical_data[i]['open']),'high':float(historical_data[i]['high']),'low':float(historical_data[i]['low']),'close':float(historical_data[i]['close']),'Timestamp':reliance}
#
#
# price_data = pd.concat([price_data, pd.DataFrame(row_to_append, index=[0])], ignore_index=True)
#
#
#
# price_data.to_csv('output.csv', index=False)
#
#
Comments
Post a Comment