Hi there,
I am currently wondering if zipline-reloaded really handles dividends correctly??
To show this I can implement a simple buy-and-hold strategy on Apple, say in the following way:
from zipline import run_algorithm
import pandas as pd
from zipline.api import symbol, order_target_percent
from zipline.finance import commission, slippage
def initialize(context):
context.asset = symbol('AAPL')
context.set_commission(commission.PerDollar(cost=0.00))
context.set_slippage(slippage.FixedSlippage(spread=0.0))
context.has_ordered = False
def handle_data(context, data):
if not context.has_ordered:
order_target_percent(context.asset, 1.0)
context.has_ordered = True
def analyze(context, perf):
pass
if __name__ == "__main__":
start_date = pd.Timestamp('2014-01-01')
end_date = pd.Timestamp('2018-12-31')
result = run_algorithm(
start=start_date.tz_localize('UTC'),
end=end_date.tz_localize('UTC'),
initialize=initialize,
handle_data=handle_data,
analyze=analyze,
capital_base=20000,
data_frequency='daily',
bundle='quandl'
)
I can see that Apple had dividends paid over this period (for instance one on the 6th of February 2014 being the first. Here, I would either expect another order for the dividends to be reinvested, or my cash account to increase. However, none of this is happening:
result.loc['2014-02-05':'2014-02-10',['starting_cash','ending_cash']]
Out[3]:
starting_cash ending_cash
2014-02-05 21:00:00+00:00 524.72 524.72
2014-02-06 21:00:00+00:00 524.72 524.72
2014-02-07 21:00:00+00:00 524.72 524.72
2014-02-10 21:00:00+00:00 524.72 524.72
result.loc['2014-02-05':'2014-02-10',['orders']]
Out[4]:
orders
2014-02-05 21:00:00+00:00 []
2014-02-06 21:00:00+00:00 []
2014-02-07 21:00:00+00:00 []
2014-02-10 21:00:00+00:00 []
Does anybody know where I am going wrong here?
I have also gone through the source code which I believe is handled within zipline.gens.tradesimulation.py → AlgorithmSimulator.transform(), but here I also don’t seem to find any logic for handling dividends (at least not any obvious one to me…).
Would be great if anybody could help.
Many thanks.
Best,
Chris