15 min candle capture

vippin_nair
A simple code to capture and trade using a 15 min candle on live data.
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker
import pandas as pd
from datetime import datetime,timedelta,date
import pdb

now = datetime.now()
system_date = date.today()
previous_date = date.today() - timedelta(days=1)
dt = str(system_date).split(" ")
dt = dt[0]
check = dt+" "+"09:15:00"
after = dt+" "+"09:30:00"

trd_portfolio = {12247042 : {'name' : 'BANKNIFTY'}}

ticks_storage = pd.DataFrame()
timeframe = 15
temp = {}


def on_ticks(ws, ticks):
for single_company in ticks:
inst_of_single_company = single_company['instrument_token']
name = trd_portfolio[inst_of_single_company]['name']
#print(single_company,name)
temp[single_company['timestamp']] = single_company
df = pd.DataFrame.from_dict(temp, orient='index')
ohlc = df['last_price'].resample(str(timeframe)+'Min').ohlc()
ohlc.index = pd.to_datetime(ohlc.index, unit='s')

#pdb.set_trace()
try:
candle1 = ohlc.loc[check]
candle2 = ohlc.loc[after]

if candle2['high']>candle1['high']:
print("sell")
elif candle2['low']<candle1['low']:
print("buy")
#pdb.set_trace()
except KeyError:
print('candle not ready')


subscribe = [12247042]


def on_connect(ws, response):
ws.subscribe(subscribe)
ws.set_mode(ws.MODE_FULL, subscribe)


kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect()
Tagged:
Sign In or Register to comment.