Python MACD

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
PythonLKL
User
Beiträge: 4
Registriert: Samstag 9. Dezember 2017, 19:48

Hallo liebe Freunde,

ich nutze Python(Spyder) in der aktuellsten 32bit Version.

Im würde gerne diesen Code auf eine CSV Datei von mir anwenden, in der 2 Spalten angegeben sind, Datum und Preis (Bitcoin).

Allerdings bekomme ich ab:
signal = stock['macds']
macd = stock['macd']

immer den Error
File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'close'

Hoffe ihr könnt mir da weiter helfen.

Code: Alles auswählen

import pandas as pd
from stockstats import StockDataFrame as Sdf

data   = pd.read_csv("C:\\Users\\DerTuerke1\\Desktop\\BCHARTS.csv")

stock  = Sdf.retype(data)

signal = stock['macds']        # Your signal line
macd   = stock['macd']         # The MACD that need to cross the signal line
#                                              to give you a Buy/Sell signal
listLongShort = ["No data"]    # Since you need at least two days in the for loop

for i in range(1, len(signal)):
    #                          # If the MACD crosses the signal line upward
    if macd[i] > signal[i] and macd[i - 1] <= signal[i - 1]:
        listLongShort.append("BUY")
    #                          # The other way around
    elif macd[i] < signal[i] and macd[i - 1] >= signal[i - 1]:
        listLongShort.append("SELL")
    #                          # Do nothing if not crossed
    else:
        listLongShort.append("HOLD")

stock['Advice'] = listLongShort

# The advice column means "Buy/Sell/Hold" at the end of this day or
#  at the beginning of the next day, since the market will be closed

print(stock['Advice'])


Benutzeravatar
noisefloor
User
Beiträge: 3843
Registriert: Mittwoch 17. Oktober 2007, 21:40
Wohnort: WW
Kontaktdaten:

Hallo,

poste mal bitte den vollen Stacktrace, nicht nur eine Zeile der Fehlermeldung.

Gruß, noisefloor
PythonLKL
User
Beiträge: 4
Registriert: Samstag 9. Dezember 2017, 19:48

Hallo, danke für deine Antwort, wie gewünscht hier alles:

Code: Alles auswählen

signal = stock['macds']
close not found.
Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'macds'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 922, in __getitem__
    super(StockDataFrame, self).__getitem__(item))
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3590, in get
    loc = self.items.get_loc(item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'macds'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close_12_ema'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 922, in __getitem__
    super(StockDataFrame, self).__getitem__(item))
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3590, in get
    loc = self.items.get_loc(item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close_12_ema'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2442, in get_loc
    return self._engine.get_loc(key)
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 922, in __getitem__
    super(StockDataFrame, self).__getitem__(item))
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
    return self._getitem_column(key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
    values = self._data.get(item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3590, in get
    loc = self.items.get_loc(item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))
  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item
  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item
KeyError: 'close'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 925, in __getitem__
    self.init_columns(self, item)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 858, in init_columns
    StockDataFrame.__init_column(obj, columns)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 917, in __init_column
    StockDataFrame.__init_not_exist_column(df, key)
  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 909, in __init_not_exist_column
    getattr(cls, func_name)(df, r)
AttributeError: type object 'StockDataFrame' has no attribute '_get_None'
Traceback (most recent call last):

  File "<ipython-input-8-a6a7cf18a27f>", line 1, in <module>
    signal = stock['macds']

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 925, in __getitem__
    self.init_columns(self, item)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 858, in init_columns
    StockDataFrame.__init_column(obj, columns)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 917, in __init_column
    StockDataFrame.__init_not_exist_column(df, key)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 871, in __init_not_exist_column
    cls._get_macd(df)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 745, in _get_macd
    fast = df['close_12_ema']

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 925, in __getitem__
    self.init_columns(self, item)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 858, in init_columns
    StockDataFrame.__init_column(obj, columns)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 917, in __init_column
    StockDataFrame.__init_not_exist_column(df, key)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 906, in __init_not_exist_column
    getattr(cls, func_name)(df, c, r)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 700, in _get_ema
    if len(df[column]) > 0:

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\stockstats.py", line 929, in __getitem__
    super(StockDataFrame, self).__getitem__(item))

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1964, in __getitem__
    return self._getitem_column(key)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\frame.py", line 1971, in _getitem_column
    return self._get_item_cache(key)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\generic.py", line 1645, in _get_item_cache
    values = self._data.get(item)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\internals.py", line 3590, in get
    loc = self.items.get_loc(item)

  File "C:\Users\Beispiel\Anaconda3\lib\site-packages\pandas\core\indexes\base.py", line 2444, in get_loc
    return self._engine.get_loc(self._maybe_cast_indexer(key))

  File "pandas\_libs\index.pyx", line 132, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc

  File "pandas\_libs\hashtable_class_helper.pxi", line 1210, in pandas._libs.hashtable.PyObjectHashTable.get_item

  File "pandas\_libs\hashtable_class_helper.pxi", line 1218, in pandas._libs.hashtable.PyObjectHashTable.get_item

KeyError: 'close'
Benutzeravatar
noisefloor
User
Beiträge: 3843
Registriert: Mittwoch 17. Oktober 2007, 21:40
Wohnort: WW
Kontaktdaten:

Hallo,

der Fehler ist die Zeile `signal = stock['macds']`, weil es den Schlüssen `macds` nicht gibt. Sprich, du hast keine Spalte mit dem Namen `macds`.

Entweder ist das in deinen Daten nicht drin oder die Zeile `Sdf.retype(data)` generiert so eine Zeile nicht.

Gruß, noisefloor
PythonLKL
User
Beiträge: 4
Registriert: Samstag 9. Dezember 2017, 19:48

Hallo,

alles klar, denn die csv die ich habe beinhaltet nur 1 Spalte Datum und eine Spalte mit dem Preis. Was benötige ich denn alles für Spalten in meiner CSV für den MACD laut dem Code?

Mit freundlichen Grüßen
Benutzeravatar
noisefloor
User
Beiträge: 3843
Registriert: Mittwoch 17. Oktober 2007, 21:40
Wohnort: WW
Kontaktdaten:

Hallo,

keine Ahnung - das geht aus dem Code ja auch nicht hervor.

Da der Code ja stark so aussieht, als wäre er von irgendwo übernommen, müsstest du halt mal schauen, wie die Ausgangsdaten auf der Seite, wo das her kommt, aussieht.

Gruß, noisefloor
Antworten