-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkc_renko.py
55 lines (46 loc) · 1.84 KB
/
kc_renko.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from kiteconnect import KiteConnect
import pandas as pd
import datetime as dt
import os
import numpy as np
from stocktrends import Renko
#generate trading session
access_token = open("access_token.txt",'r').read()
key_secret = open("api_key.txt",'r').read().split()
kite = KiteConnect(api_key=key_secret[0])
kite.set_access_token(access_token)
#get dump of all NSE instruments
instrument_dump = kite.instruments("NSE")
instrument_df = pd.DataFrame(instrument_dump)
def instrumentLookup(instrument_df,symbol):
"""Looks up instrument token for a given script from instrument dump"""
try:
return instrument_df[instrument_df.tradingsymbol==symbol].instrument_token.values[0]
except:
return -1
def fetchOHLC(ticker,interval,duration):
"""extracts historical data and outputs in the form of dataframe"""
instrument = instrumentLookup(instrument_df,ticker)
data = pd.DataFrame(kite.historical_data(instrument,dt.date.today()-dt.timedelta(duration), dt.date.today(),interval))
data.set_index("date",inplace=True)
return data
def atr(DF,n):
"function to calculate True Range and Average True Range"
df = DF.copy()
df['H-L']=abs(df['high']-df['low'])
df['H-PC']=abs(df['high']-df['close'].shift(1))
df['L-PC']=abs(df['low']-df['close'].shift(1))
df['TR']=df[['H-L','H-PC','L-PC']].max(axis=1,skipna=False)
df['ATR'] = df['TR'].ewm(com=n,min_periods=n).mean()
#df['ATR'] = df['TR'].ewm(span=n,adjust=False,min_periods=n).mean()
return df['ATR']
def renko_DF(DF):
"function to convert ohlc data into renko bricks"
df = DF.copy()
df.reset_index(inplace=True)
df2 = Renko(df)
df2.brick_size = round(atr(DF,120)[-1],0)
renko_df = df2.get_ohlc_data()
return renko_df
ohlc = fetchOHLC("HDFC","5minute",5)
renko = renko_DF(ohlc)