error on stock indicator code on balance volume - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: error on stock indicator code on balance volume (/thread-11341.html) Pages:
1
2
|
error on stock indicator code on balance volume - yatish - Jul-04-2018 # On-balance Volume def on_balance_volume(aapl, n): i = 0 OBV = [0] #I have a dataframe with open high low close volume of stock prices and wanted to calaculate obv of parameter 10 and 20 while (i < (len(aapl.index)-1)): if aapl.iloc[i + 1]['Close'] - aapl.iloc[i]['Close'] > 0: OBV.append(aapl.iloc[i]['Volume']) elif aapl.iloc[i + 1]['Close'] - aapl.iloc[i]['Close'] < 0: OBV.append(-aapl.iloc[i + 1]['Volume']) else : OBV.append(0) i = i + 1 OBV = pd.Series(OBV) #print(obv) #obv is calculated till here but not passed to the rolling function OBV_ma = pd.Series(OBV.rolling(n, min_periods=n).mean(), name='OBV_' + str(n)) aapl = aapl.join(OBV_ma) return aaplthe output for obv is displaying nan values tried several other ways...please can someone check my whole code and guide me I have dataframe of open high low close volume of stock prices RE: error on stock indicator code on balance volume - gontajones - Jul-04-2018 Could you post the content of aapl ?Just few lines will be good for us to analyse it and help you. RE: error on stock indicator code on balance volume - yatish - Jul-04-2018 python Date Open high Low close Volume 2006-10-02 75.10 75.870 74.30 74.86 25451400.0 the dataframe is in this format actually when I am pasting it it is getting pasted vertically so only one sample I wrote on own. below is the obv theory on which code is written If the closing price is above the prior close price then: Current OBV = Previous OBV + Current Volume If the closing price is below the prior close price then: Current OBV = Previous OBV - Current Volume If the closing prices equals the prior close price then: Current OBV = Previous OBV (no change) RE: error on stock indicator code on balance volume - gontajones - Jul-04-2018 Your return is inside the while loop.I think this is not what you want, right? To test your OBV I need more then 1 line of aapl .
RE: error on stock indicator code on balance volume - yatish - Jul-05-2018 below is the data on which I want to calculate obv value for parameter 10 and 20 Date,Open,High,Low,Close,Adj Close,Volume 2009-12-31,30.447144,30.478571000000002,30.08,30.104286,26.986492,88102700 2010-01-04,30.49,30.642857,30.34,30.572857,27.406532000000002,123432400 2010-01-05,30.657142999999998,30.798571000000003,30.464284999999997,30.625713,27.453915000000002,150476200 2010-01-06,30.625713,30.747142999999998,30.107143,30.138571000000002,27.017222999999998,138040000 2010-01-07,30.25,30.285715000000003,29.864286,30.082857,26.967278000000004,119282800 2010-01-08,30.042856,30.285715000000003,29.865715,30.282858,27.146565999999996,111902700 2010-01-11,30.4,30.428571999999996,29.778571999999997,30.015715000000004,26.907093,115557400 2010-01-12,29.884285,29.967142,29.488571000000004,29.674286,26.601022999999998,148614900 2010-01-13,29.695715000000003,30.132856,29.157142999999998,30.092857000000002,26.976244,151473000 2010-01-14,30.015715000000004,30.065714,29.860001,29.918571000000004,26.820007,108223500 2010-01-15,30.132856,30.228571000000002,29.41,29.418571000000004,26.371792,148516900 2010-01-19,29.761428999999996,30.741428000000003,29.605715000000004,30.719998999999998,27.538433,182501900 2010-01-20,30.701428999999997,30.792856,29.928571999999996,30.247142999999998,27.114555,153038200 2010-01-21,30.297141999999997,30.472857,29.601429,29.724285,26.645842,152038600 2010-01-22,29.540001,29.642857,28.165714,28.25,25.324244,220441900 2010-01-25,28.93,29.242857,28.598571999999997,29.01,26.005541,266424900 2010-01-26,29.421428999999996,30.530001000000002,28.940001000000002,29.42,26.373071999999997,466777500 RE: error on stock indicator code on balance volume - gontajones - Jul-05-2018 With those inputs and this code: def on_balance_volume(aapl, n): i = 0 OBV = [0] # I have a dataframe with open high low close volume of stock prices and wanted to calaculate obv of parameter 10 and 20 for i in range(len(aapl.index) - 1): if aapl.iloc[i + 1]['Close'] - aapl.iloc[i]['Close'] > 0: OBV.append(aapl.iloc[i]['Volume']) elif aapl.iloc[i + 1]['Close'] - aapl.iloc[i]['Close'] < 0: OBV.append(-aapl.iloc[i + 1]['Volume']) else: OBV.append(0) OBV = pd.Series(OBV) # obv is calculated till here but not passed to the rolling function OBV_ma = pd.Series(OBV.rolling(n, min_periods=n).mean(), name='OBV_' + str(n)) aapl = aapl.join(OBV_ma) return aapl # obv.csv has the inputs aapl = pd.read_csv("obv.csv") n = 1 print(on_balance_volume(aapl, n))And the output was:
RE: error on stock indicator code on balance volume - yatish - Jul-05-2018 can you please tell me how can I append it to my other index date open high low close volume obv in this way RE: error on stock indicator code on balance volume - gontajones - Jul-05-2018 Do you want to remove the Adj Close column?my_aapl = on_balance_volume(aapl, n) print(my_aapl) my_aapl = my_aapl.drop('Adj Close', 1) print(my_aapl) RE: error on stock indicator code on balance volume - yatish - Jul-06-2018 I don't wANT TO DROP any column sir I want to to get obv in the same dataframe of appl in continuation right now it is getting below the data frame with same index Date Open High Low Close Adj Close \ 0 2009-12-31 30.447144 30.478571 30.080000 30.104286 26.986492 1 2010-01-04 30.490000 30.642857 30.340000 30.572857 27.406532 2 2010-01-05 30.657143 30.798571 30.464285 30.625713 27.453915 3 2010-01-06 30.625713 30.747143 30.107143 30.138571 27.017223 4 2010-01-07 30.250000 30.285715 29.864286 30.082857 26.967278 Volume OBV_1 0 88102700 0.0 1 123432400 88102700.0 2 150476200 123432400.0 3 138040000 -138040000.0 4 119282800 -119282800.0 in these way it is coming this format I want Date Open High Low Close Adj Close Volume OBV_1 2009-12-31 30.447144 30.478571 30.080000 30.104286 26.986492 88102700 0.0 2010-01-04 30.490000 30.642857 30.340000 30.572857 27.406532 123432400 88102700.0 2010-01-05 30.657143 30.798571 30.464285 30.625713 27.453915 150476200 123432400.0 2010-01-06 30.625713 30.747143 30.107143 30.138571 27.017223 138040000 -138040000.0 2010-01-07 30.250000 30.285715 29.864286 30.082857 26.967278 119282800 -119282800.0 RE: error on stock indicator code on balance volume - gontajones - Jul-06-2018 But the OBV_1 column is already inside the aapl DataFrame.This "way" to print is a default feature of Pandas. To print everything on the same line, you can use this: with pd.option_context('expand_frame_repr', False): print(my_aapl)
|