Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a problem with comparing data in a Pandas dataframe

I have a data frame of stock data, but i want to compare the data using greater than or less than signs. I want to be able to compare the data like I would normally be able to with normal integers / floats.

Here is the code:

import time
import json
import requests
import pandas as pd
import numpy as np


money = 0
def runfunction():
  i = 0
  go = True
  while go == True:
    #try:
      bought = False
      if googl_macd["macd"][i] >= 4 & bought != True:
        print("here")
        #do strategy
        if googl_macd["macd"][i] > googl_macd["signal"] & googl_macd["hist"] > 2.5:
          print("here")
          #alpacaorder("GOOGL", BUY)
          print("Bought at", googl["close"])
          bought = True
          boughtprice = googl_macd["macd"]

      elif googl_macd["macd"] >= 4 and googl_macd["macd"] < googl_macd["signal"] & bought == True:
        #alpacaorder("GOOGL", SELL)
        print("Sold at", googl["close"])
        sellprice = googl_macd["macd"]
        money += sellprice - boughtprice
      i += 1
    #except Exception as e:
      #print(e)
      #go = False
      
runfunction()
print(money)

This is some of the Pandas data i am using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

         macd    signal      hist
2020-01-02  0.000000  0.000000  0.000000
2020-01-03 -0.571168 -0.114234 -0.456934
2020-01-06  1.882773  0.285168  1.597606
2020-01-07  3.568536  0.941841  2.626695
2020-01-08  5.640760  1.881625  3.759135

And this is my error

Traceback (most recent call last):
  File "c:\Users\zackz\Desktop\Zack-Emil.py", line 161, in <module>
    runfunction()
  File "c:\Users\zackz\Desktop\Zack-Emil.py", line 144, in runfunction
    if googl_macd["macd"][i] > googl_macd["signal"]: #& googl_macd["hist"] > 2.5:
  File "C:\Users\zackz\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\pandas\core\generic.py", line 1537, in __nonzero__
    raise ValueError(
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I tried to compare the different columns in the pandas data frame.

Sorry if this question has already been answered, i could not find anything specific to the problem that i had.

>Solution :

The line

if googl_macd["macd"][i] > googl_macd["signal"] & googl_macd["hist"] > 2.5:

is comparing a single value from column "macd" to a whole column "signal". Try changing this to

if googl_macd["macd"][i] > googl_macd["signal"][i] & googl_macd["hist"][i] > 2.5:
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading