def margin_account_log(self): return TimeSeries(self._margin_account_log)
self.last_price = price if __name__ == '__main__': start = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2013, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=[SYMBOL], indexes={}, start=start, end=end, adjusted=True) simple_algo = BuyStock() results = simple_algo.run(data) ax1 = plt.subplot(211) ax2 = plt.subplot(212) TRACK_STRIPPED = [x for x in TRACK if type(x) == tuple] futures_indexes = [timestamp for (_, _, timestamp) in TRACK_STRIPPED] futures_quantity_data = [ quantity_owned for (_, quantity_owned, _) in TRACK_STRIPPED ] futures_margin_data = [margin for (margin, _, _) in TRACK_STRIPPED] futures_margin_series = TimeSeries(index=futures_indexes, data=futures_margin_data) futures_margin_series.plot(ax=ax1) futures_quantity_series = TimeSeries(index=futures_indexes, data=futures_quantity_data) futures_quantity_series.plot(ax=ax2) plt.gcf().set_size_inches(18, 8)
class BuyGoogleAsFuture(FuturesTradingAlgorithm): def initialize_futures(self, *args, **kwargs): pass def handle_futures_data(self, data): self.order("GOOG", 1, initial_margin=data['GOOG']['initial_margin']) def _handle_margin_call(self): self._liquidate_random_positions() if __name__ == '__main__': start = datetime(2008, 1, 1, 0, 0, 0, 0, pytz.utc) end = datetime(2013, 1, 1, 0, 0, 0, 0, pytz.utc) data = load_from_yahoo(stocks=["GOOG"], indexes={}, start=start, end=end, adjusted=True) simple_algo = BuyGoogleAsFuture() results = simple_algo.run(data) ax1 = plt.subplot(211) futures_indexes = list(simple_algo.margin_account_log.keys()) futures_margin_data = list(simple_algo.margin_account_log.values) futures_margin_series = TimeSeries(index=futures_indexes, data=futures_margin_data) futures_margin_series.plot(ax=ax1) ax2 = plt.subplot(212, sharex=ax1) data.GOOG.plot(ax=ax2) plt.gcf().set_size_inches(18, 8)