-
Notifications
You must be signed in to change notification settings - Fork 0
/
renko.py
38 lines (33 loc) · 1.34 KB
/
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
# =============================================================================
# Import OHLCV data and transform it to Renko
# Author : Mayank Rasu
# Please report bug/issues in the Q&A section
# =============================================================================
# Import necesary libraries
import pandas_datareader.data as pdr
import datetime
from stocktrends import Renko
# Download historical data for required stocks
ticker = "AAPL"
ohlcv = pdr.get_data_yahoo(ticker,datetime.date.today()-datetime.timedelta(364),datetime.date.today())
df = ohlcv.copy()
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['Adj Close'].shift(1))
df['L-PC']=abs(df['Low']-df['Adj Close'].shift(1))
df['TR']=df[['H-L','H-PC','L-PC']].max(axis=1,skipna=False)
df['ATR'] = df['TR'].rolling(n).mean()
df2 = df.drop(['H-L','H-PC','L-PC'],axis=1)
return df2
def renko_DF(DF):
"function to convert ohlc data into renko bricks"
df = DF.copy()
df.reset_index(inplace=True)
df = df.iloc[:,[0,1,2,3,5,6]]
df.columns = ["date","open","high","low","close","volume"]
df2 = Renko(df)
df2.brick_size = round(ATR(DF,120)["ATR"][-1],0)
renko_df = df2.get_bricks()
return renko_df