Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 error on stock indicator code on balance volume
#1
# 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 aapl
the 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
buran wrote Jul-04-2018, 10:02 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#2
Could you post the content of aapl?
Just few lines will be good for us to analyse it and help you.
Quote
#3
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)
Quote
#4
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.
Quote
#5
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
Quote
#6
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:

Output:
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 5 2010-01-08 30.042856 30.285715 29.865715 30.282858 27.146566 6 2010-01-11 30.400000 30.428572 29.778572 30.015715 26.907093 7 2010-01-12 29.884285 29.967142 29.488571 29.674286 26.601023 8 2010-01-13 29.695715 30.132856 29.157143 30.092857 26.976244 9 2010-01-14 30.015715 30.065714 29.860001 29.918571 26.820007 10 2010-01-15 30.132856 30.228571 29.410000 29.418571 26.371792 11 2010-01-19 29.761429 30.741428 29.605715 30.719999 27.538433 12 2010-01-20 30.701429 30.792856 29.928572 30.247143 27.114555 13 2010-01-21 30.297142 30.472857 29.601429 29.724285 26.645842 14 2010-01-22 29.540001 29.642857 28.165714 28.250000 25.324244 15 2010-01-25 28.930000 29.242857 28.598572 29.010000 26.005541 16 2010-01-26 29.421429 30.530001 28.940001 29.420000 26.373072 Volume OBV_1 0 88102700 0.0 1 123432400 88102700.0 2 150476200 123432400.0 3 138040000 -138040000.0 4 119282800 -119282800.0 5 111902700 119282800.0 6 115557400 -115557400.0 7 148614900 -148614900.0 8 151473000 148614900.0 9 108223500 -108223500.0 10 148516900 -148516900.0 11 182501900 148516900.0 12 153038200 -153038200.0 13 152038600 -152038600.0 14 220441900 -220441900.0 15 266424900 220441900.0 16 466777500 266424900.0
Quote
#7
can you please tell me how can I append it to my other index
date open high low close volume obv
in this way
Quote
#8
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)
Quote
#9
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
Quote
#10
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)
Output:
Date Open High Low Close Adj Close Volume OBV_1 0 2009-12-31 30.447144 30.478571 30.080000 30.104286 26.986492 88102700 0.0 1 2010-01-04 30.490000 30.642857 30.340000 30.572857 27.406532 123432400 88102700.0 2 2010-01-05 30.657143 30.798571 30.464285 30.625713 27.453915 150476200 123432400.0 3 2010-01-06 30.625713 30.747143 30.107143 30.138571 27.017223 138040000 -138040000.0 4 2010-01-07 30.250000 30.285715 29.864286 30.082857 26.967278 119282800 -119282800.0 5 2010-01-08 30.042856 30.285715 29.865715 30.282858 27.146566 111902700 119282800.0 6 2010-01-11 30.400000 30.428572 29.778572 30.015715 26.907093 115557400 -115557400.0 7 2010-01-12 29.884285 29.967142 29.488571 29.674286 26.601023 148614900 -148614900.0 8 2010-01-13 29.695715 30.132856 29.157143 30.092857 26.976244 151473000 148614900.0 9 2010-01-14 30.015715 30.065714 29.860001 29.918571 26.820007 108223500 -108223500.0 10 2010-01-15 30.132856 30.228571 29.410000 29.418571 26.371792 148516900 -148516900.0 11 2010-01-19 29.761429 30.741428 29.605715 30.719999 27.538433 182501900 148516900.0 12 2010-01-20 30.701429 30.792856 29.928572 30.247143 27.114555 153038200 -153038200.0 13 2010-01-21 30.297142 30.472857 29.601429 29.724285 26.645842 152038600 -152038600.0 14 2010-01-22 29.540001 29.642857 28.165714 28.250000 25.324244 220441900 -220441900.0 15 2010-01-25 28.930000 29.242857 28.598572 29.010000 26.005541 266424900 220441900.0 16 2010-01-26 29.421429 30.530001 28.940001 29.420000 26.373072 466777500 266424900.0
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Newbie at using python and tensorflow getting error when running simple code FeatherineAu 0 884 Sep-28-2018, 02:09 PM
Last Post: FeatherineAu
  Code error - new user CarlG 4 1,136 May-20-2018, 04:00 PM
Last Post: CarlG
  "Use proper inputs to download stock data" Alberto 15 2,538 Jan-29-2018, 02:03 PM
Last Post: Alberto
  Psychopy- Python code error Iva09 0 736 Sep-20-2017, 11:15 PM
Last Post: Iva09
  How to pick random entries of stock prices? Alberto 1 1,167 Jul-16-2017, 09:51 PM
Last Post: wavic
  how can rectify error from the below code desul 5 1,777 Feb-28-2017, 10:00 AM
Last Post: buran

Forum Jump:


Users browsing this thread: 1 Guest(s)