Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How To Access Dictionary with Pandas?

Right now my algorithm will just create the MACD value for one stock and save it to a dictionary named "macds". I can't figure out how to access that data. I want to pull the MACD values from the dataframe and use them for trading signals. Here is my code:

import talib  
import numpy as np  
import pandas as pd

# Setup our variables  
def initialize(context):  
    context.stock = sid(24)

    schedule_function(rebalance, date_rules.every_day(), time_rules.market_open())

# Rebalance daily.  
def rebalance(context, data):  
    # Load historical data for the stocks  
    prices = data.history(context.stock, 'price', 40, '1d')  
    macds = {}  
    macd_raw, signal, hist = talib.MACD(prices, fastperiod=12,  
                                        slowperiod=26, signalperiod=9)  
    macd = macd_raw[-1] - signal[-1]  
    macds[context.stock] = macd  
    current_position = context.portfolio.positions[context.stock].amount  
    if macd < 0 and current_position > 0 and data.can_trade(context.stock):  
        order_target_percent(context.stock, 0)  
    elif macd > 0 and current_position == 0 and data.can_trade(context.stock):  
        order_target_percent(context.stock, 1)  
    print macds  

When I print "macds" this comes out:
2017-06-05 08:31 PRINT {Equity(24 [AAPL]): -0.49990557826970017}
2017-06-06 08:31 PRINT {Equity(24 [AAPL]): -0.58226460109200362}
2017-06-07 08:31 PRINT {Equity(24 [AAPL]): -0.567854136450753}
2017-06-08 08:31 PRINT {Equity(24 [AAPL]): -0.55096374492923506}
2017-06-09 08:31 PRINT {Equity(24 [AAPL]): -0.58756635328447704}
.......

How can I access those values that are being returned? I've tried .iloc and .loc and all kinds of stuff but errors keep getting thrown back the dictionaries don't have the .iloc capability. Any help would be great!

Joshua

4 responses

This may help you:

# MACD indicator

import talib

def initialize(context):  
    schedule_function(record_MACD, date_rules.every_day(), time_rules.market_close())

def record_MACD(context, data):  
    stock = symbol('QQQ')  
    fast, slow, sig = 12, 26, 9  
    period = fast + slow + sig

    prices = data.history(stock, 'price', period, '1d')  
    macd, signal, hist = talib.MACD(prices, fast, slow, sig)

    MACD_Raw = macd[-1]  
    SIGNAL = signal[-1]  
    HIST = hist[-1]

    print (MACD_Raw, SIGNAL, HIST)  
    record(MACD_Raw = MACD_Raw, signal = SIGNAL, hist = HIST)  

2007-06-01 12:59 PRINT (0.45737182922085395, 0.46891148632894575, -0.011539657108091794)
2007-06-04 12:59 PRINT (0.48496561578968311, 0.47853348928160361, 0.0064321265080795054)
2007-06-05 12:59 PRINT (0.49086486462974221, 0.4825965919577454, 0.0082682726719968125)
2007-06-06 12:59 PRINT (0.46200303746174853, 0.48055505747605404, -0.018552020014305504)
2007-06-07 12:59 PRINT (0.36466325543550937, 0.45546378407542515, -0.090800528639915778)

Thanks that works great! I'd like to grab the maximum MACD value of the past 15 periods. I've tried max_macd = max(macd[-15:-1]) print (max_macd) But when it prints it just says "nan". How could I grab the past 15 values and pull out the max value from those 15?

Try this way:

    stock = symbol('QQQ')  
    fast, slow, sig, period = 12, 26, 9, 15  
    bars = (fast + slow + sig) + period  

    prices = data.history(stock, 'price', bars, '1d')  
    macd, signal, hist = talib.MACD(prices, fast, slow, sig)  
    MACD_MAX = max(macd[-period :])

    print (MACD_MAX)  
    record(MACD_MAX = MACD_MAX)  

Yes! That worked! Thanks a ton Vladimir.