Ticks to OHLC

Parva
I would like to store the tick data into candlesticks and I have written a code for the same. What happens is when I run the code, the data of different instruments gets mixed even though I have tried to make an object for each and every instrument. I am also checking if the instrument token is same or not at every step (which is not necessary I suppose) Here is my code, do let me know if anyone can diagnose where the problem is.

from kiteconnect import KiteTicker;
from kiteconnect import KiteConnect;
import logging
import time,os,datetime,math;

logging.basicConfig(level=logging.DEBUG)

trd_portfolio = {53835015:"CRUDEOIL18OCTFUT", 53987079:"ZINC18OCTFUT"}
buy_percentage_distribution = []
sell_percentage_distribution = []
trd_tkn1 = [];
live_open = {}
live_high = {}
live_low = {}
live_close = {}
buy_count = {}
sell_count = {}

for x in trd_portfolio:

trd_tkn1.append(x)
live_open[x] = 0
live_high[x] = 0
live_low[x] = 9999
live_close[x] = 0
buy_count[x] = 0
sell_count[x] = 0



open = []
high = []
low = []
close = []

c_id = "UA6607"
ak = "4qjrnd842guo5g5m"
asecret = "ia9nd8goqgtfylwn7spqjhafixg6s8xx"


kite = KiteConnect(api_key=ak)
print("[*] Generate access Token : ",kite.login_url())
request_tkn = input("[*] Enter Your Request Token Here : ")[-32:];
data = kite.generate_session(request_tkn, api_secret=asecret)
kite.set_access_token(data["access_token"])
kws = KiteTicker("4qjrnd842guo5g5m", data["access_token"])


ohlc = 0
got_open = 0
trailing_stoploss = 1
open_min = 1
close_min = 1
rhl = 0
sym = []

got_open = 0
open_min = 1
close_min = 1
rhl = 0
ll = 9999
hh = 0

class Company:



def __init__(self, token):
self.token = token

self.open = []
self.high = []
self.low = []
self.close = []
self.ohlc_list = {}
self.ohlc_list = [self.open,self.high,self.low,self.close]
self.ohlc = {}
self.ohlc = [0,0,0,0,True,60,0,];

def calc_ohlc(self, ticks, tf):


for company_data in ticks:


if((company_data['timestamp'].minute%tf == 0) and self.ohlc[4]==True):

if(self.token == company_data['instrument_token']):
self.ohlc[5] = company_data['timestamp'].minute + (tf-1)

if(self.token == company_data['instrument_token']):
self.ohlc[0] = company_data['last_price'];
self.ohlc[1] = company_data['last_price'];
self.ohlc[2] = company_data['last_price'];
self.ohlc[3] = company_data['last_price'];

if(self.token == company_data['instrument_token']):
self.ohlc_list[0].append(self.ohlc[0])
self.ohlc[4]=False

if(self.ohlc[4]==False):

if(company_data['last_price'] > self.ohlc[1]):

self.ohlc[1] = company_data['last_price']
print("NEW HIGH",self.ohlc[1])

if(company_data['last_price'] < self.ohlc[2]):

self.ohlc[2] = company_data['last_price']
print("NEW HIGH",self.ohlc[2])


if company_data['timestamp'].minute%self.ohlc[5] == 0 and company_data['timestamp'].second==59 and self.ohlc[4] == False:

if(self.token == company_data['instrument_token']):
self.ohlc[3] = company_data['last_price']
if(self.token == company_data['instrument_token']):
self.ohlc_list[1].append(self.ohlc[1])
self.ohlc_list[2].append(self.ohlc[2])
self.ohlc_list[3].append(self.ohlc[3])
self.ohlc[4] = True
print("TIME", company_data['timestamp'])
print(self.ohlc_list)

x = 0
def printer():
global x
if(x != len(sym[0].ohlc_list[1])):
print(sym[0].ohlc_list)
x = len(sym[0].ohlc_list[1])


for i in range(0,(len(trd_tkn1))):

sym.append(Company(trd_tkn1[i]))

for i in range(0,(len(trd_tkn1))):
print(sym[i].token)





def on_ticks(ws, ticks):



for i in range(0,(len(trd_tkn1))):
sym.calc_ohlc(ticks,3)

printer()
def on_connect(kws , response):
kws.subscribe(trd_tkn1)
kws.set_mode(kws.MODE_FULL, trd_tkn1)

kws.on_ticks = on_ticks
kws.on_connect = on_connect




kws.connect()



Sign In or Register to comment.