i’ve made this code for making range bar charting pattern , to trade using it in my algorithm using zerodha API.
“”“RANGE_BARS BRICK SIZE”""
def Range(ticker):
“”“Range bar brick size is unique for every stock.
So it’s need to updated for every stock you trade.”""
return round(0.5*(fetchOHLC(ticker,"10minute",25)),0)
“”“RANGE_BARS OPERATION”""
def Rangebaroperation(ticks):
for tick in ticks:
try:
ticker = tickerLookup(int(tick[‘instrument_token’]))
range_param[ticker]["highTarget"] = None,
range_param[ticker]["lowTarget"] = None
def createBar(close):
newTick = {
['open']: int(float(round(tick['last_price'][-1]),2)),
['close']: int(float(round(tick['close']),2)),
['high']: int(round(range_param[ticker]["highTarget"]),2),
['low']: int(round(range_param[ticker]["lowTarget"]),2),
['volume']: int(float(round(tick['volume']),2))
}
newTick[float(tick['close'][-1])] = newTick[float(tick['open'])]
# We translate directional movements O -> H -> L -> C -> O ...
def processMove(q, b):
while (1):
if float(tick['last_price']) < b:
# direction is upward
gap = (float(tick['last_price'] - range_param[ticker]["highTarget"]))//range_param[ticker]["brick_size"]
tick['last_price'] = min(b, range_param[ticker]["highTarget"])
range_param[ticker]["lowTarget"] = max(range_param[ticker]["lowTarget"], float(tick['last_price']) - range_param[ticker]["brick_size"])
range_param[ticker]["brick"] = max(1,range_param[ticker]["brick"]+(1+gap))
if (b < range_param[ticker]["highTarget"]): break
elif float(tick['last_price']) >= b:
# direction is downward
gap = (range_param[ticker]["lowTarget"] - float(tick['last_price']))//range_param[ticker]["brick_size"]
tick['last_price'] = max(b, range_param[ticker]["lowTarget"])
range_param[ticker]["highTarget"] = min(range_param[ticker]["highTarget"], float(tick['last_price']) + range_param[ticker]["brick_size"])
range_param[ticker]["brick"] = min(-1,range_param[ticker]["brick"]-(1+gap))
if (b > range_param[ticker]["lowTarget"]): break
createBar(float(tick['last_price']))
resetTargets()
def resetTargets():
range_param[ticker]["highTarget"] = float(tick['last_price']) + range_param[ticker]["brick_size"]
range_param[ticker]["lowTarget"] = float(tick['last_price']) - range_param[ticker]["brick_size"]
tick['close'][-1] = float(tick['last_price'])
for i in range_param[ticker]["brick_size"](0,len(tick)):
i=i+1
q = tick[i]
if not (q): continue
q1 = tick[i - 1]
if not i:
if q1:
tick['last_price'] = q1. float(tick['close'])
if (float(tick['last_price']) or float(tick['last_price']) == 0): resetTargets()
if not (q1): continue
C = q(tick['close']),
O = q(tick['open']),
H = q(tick['high']),
L = q(tick['low'])
if (not C and C != 0): continue
O = O if O or O == 0 else C
H = H if H or H == 0 else C
L = L if L or L == 0 else C
if not float(tick['last_price']) and float(tick['last_price']) != 0:
tick = math.floor(O / range_param[ticker]["brick_size"]) * range_param[ticker]["brick_size"]
tick['last_price'] = O if tick.isnumeric() else tick
resetTargets()
processMove(q1, O)
if (i): processMove(q, O)
# shortest distance between open and either high or low determines initial direction
if ([H - O] < [O - L]):
if (H): processMove(q, H)
if (L): processMove(q, L)
else:
if (L): processMove(q, L)
if (H): processMove(q, H)
processMove(q, C)
if i == len(tick) - 1 and C != float(tick['close'][-1]):
range_param[ticker]["tempHighTarget"] = range_param[ticker]["highTarget"]
range_param[ticker]["highTarget"] = range_param[ticker]["lowTarget"] + range_param[ticker]["brick_size"]
range_param[ticker]["lowTarget"] = range_param[ticker]["tempHighTarget"] - range_param[ticker]["brick_size"]
createBar(C)
print("{}: brick number = {},last price ={}, highTarget ={}, lowTarget ={},temphighTarget ={}"\
.format(ticker,range_param[ticker]["brick_size"],tick['last_price'],range_param[ticker]["highTarget"],range_param[ticker]["lowTarget"],range_param[ticker]["tempHighTarget"]))
except Exception as e:
print(e)
pass
whenevr i try to call this function to do live streaming to fetch OHLC in the form of range bar i get ‘DataFrame’ object is not callable as the output instead of RANGE BARS.
can anyone please help me.