program to convert tick data into ohlc data

Imran
hii traders:
i want to share a program that converts tick data into ohlc data:
import logging
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker

import datetime,time,os,random;

trd_portfolio = {325121:"AMBUJACEM",40193:"APOLLOHOSP"}


kws = ;
kite = "";

def get_login(api_k,api_s): # log in to zerodha API panel
global kws,kite;
kite = KiteConnect(api_key=api_k)

print("[*] Generate access Token : ",kite.login_url())
request_tkn = input("[*] Enter Your Request Token Here : ");

data = kite.generate_session(request_tkn, api_secret=api_s)
kite.set_access_token(data["access_token"])
kws = KiteTicker(api_k, data["access_token"])

api_k = "xxxxxxxxxxxxxx"; #api_key
api_s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; #api_secret

get_login(api_k,api_s); #function that used to get connected with API

ohlc = {}; # python dictionary to store the ohlc data in it

for x in trd_portfolio:
ohlc[x] = [0,0,0,0,True,60,0]; # [o,h,l,c,isThisIsNewCandle,NextTimeToRenewLastCandle,order_id]

def calculate_ohlc_one_minute(company_data):
try:
if company_data['timestamp'].second == 0:
print(trd_portfolio[company_data['instrument_token']]," : ",ohlc[company_data['instrument_token']][0]," : ",ohlc[company_data['instrument_token']][1]," : ",ohlc[company_data['instrument_token']][2]," : ",ohlc[company_data['instrument_token']][3]) #printing last candle

print("------------------------------------------------------------------\n")

#making ohlc for new candle
ohlc[company_data['instrument_token']][0] = company_data['last_price']; #open
ohlc[company_data['instrument_token']][1] = company_data['last_price']; #high
ohlc[company_data['instrument_token']][2] = company_data['last_price']; #low
ohlc[company_data['instrument_token']][3] = company_data['last_price']; #close

if ohlc[company_data['instrument_token']][1] < company_data['last_price']: #calculating high
ohlc[company_data['instrument_token']][1] = company_data['last_price']

if ohlc[company_data['instrument_token']][2] > company_data['last_price'] or ohlc[company_data['instrument_token']][2] == 0: #calculating low
ohlc[company_data['instrument_token']][2] = company_data['last_price']

ohlc[company_data['instrument_token']][3] = company_data['last_price'] #closing price
ohlc[company_data['instrument_token']][5] = company_data['timestamp'].second;

except Exception as e:
print(e);

def calculate_ohlc(company_data,timestamp):
try:
if company_data['timestamp'].minute%timestamp == 0 and ohlc[company_data['instrument_token']][4]:
print(trd_portfolio[company_data['instrument_token']]," : ",ohlc[company_data['instrument_token']][0]," : ",ohlc[company_data['instrument_token']][1]," : ",ohlc[company_data['instrument_token']][2]," : ",ohlc[company_data['instrument_token']][3]) #printing last candle

print("------------------------------------------------------------------\n")
ohlc[company_data['instrument_token']][4] = False;

#making ohlc for new candle
ohlc[company_data['instrument_token']][0] = company_data['last_price']; #open
ohlc[company_data['instrument_token']][1] = company_data['last_price']; #high
ohlc[company_data['instrument_token']][2] = company_data['last_price']; #low
ohlc[company_data['instrument_token']][3] = company_data['last_price']; #close

ohlc[company_data['instrument_token']][5] = company_data['timestamp'].minute+1;

if ohlc[company_data['instrument_token']][1] < company_data['last_price']: #calculating high
ohlc[company_data['instrument_token']][1] = company_data['last_price']

if ohlc[company_data['instrument_token']][2] > company_data['last_price'] or ohlc[company_data['instrument_token']][2] == 0: #calculating low
ohlc[company_data['instrument_token']][2] = company_data['last_price']

ohlc[company_data['instrument_token']][3] = company_data['last_price'] #closing price

if company_data['timestamp'].minute%ohlc[company_data['instrument_token']][5] == 0:
ohlc[company_data['instrument_token']][4] = True;


except Exception as e:
print(e);


def on_ticks(ws, ticks): #retrive continius ticks in JSON format
for company_data in ticks:
print(company_data['volume']) # print tick by tick data for a perticular company

calculate_ohlc(company_data,3); #finding OHLC for `n` minute candle

#calculate_ohlc_one_minute(company_data); #special function to calculate 1 minute `ohlc`

#ohlc[company_data['instrument_token']][6] = kite.place_order(tradingsymbol=trd_portfolio[company_data['instrument_token']],price=company_data['last_price'],variety=kite.VARIETY_BO,exchange=kite.EXCHANGE_NSE,transaction_type=kite.TRANSACTION_TYPE_SELL,quantity=10,squareoff=4, stoploss=2,order_type=kite.ORDER_TYPE_LIMIT,product=kite.PRODUCT_BO) #order placement with sl tp
print("ORDER PLACED FOR ",trd_portfolio[company_data['instrument_token']]," WITH ORDER ID ",ohlc[company_data['instrument_token']][6])


def on_connect(ws, response):
ws.subscribe([x for x in trd_portfolio])
ws.set_mode(ws.MODE_FULL,[x for x in trd_portfolio])

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect

kws.connect()
Sign In or Register to comment.