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