Open JohnSparks2024 opened 3 months ago
please try with this https://github.com/stefan-jansen/zipline-reloaded/issues/227#issuecomment-1791995323
tzinfo=None in datetime should do the trick
Regrettably
start=np.datetime64('1996-01-01',tzinfo=None) end =np.datetime64('2017-12-31',tzinfo=None)
gets the error
TypeError: 'tzinfo' is an invalid keyword argument for this function
Any guidance would be appreciated.
--JJS
From: gnzsnz @.> Sent: Tuesday, April 23, 2024 5:35 AM To: stefan-jansen/zipline-reloaded @.> Cc: JohnSparks2024 @.>; Author @.> Subject: Re: [stefan-jansen/zipline-reloaded] Trouble With Dates (Issue #240)
please try with this #227 (comment)https://github.com/stefan-jansen/zipline-reloaded/issues/227#issuecomment-1791995323
tzinfo=None in datetime should do the trick
— Reply to this email directly, view it on GitHubhttps://github.com/stefan-jansen/zipline-reloaded/issues/240#issuecomment-2071966359, or unsubscribehttps://github.com/notifications/unsubscribe-auth/BFDCQCDDWTQOSIPZW2GBPA3Y6Y2OPAVCNFSM6AAAAABFFP4T22VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDANZRHE3DMMZVHE. You are receiving this because you authored the thread.Message ID: @.***>
Dear Zipline Maintainers,
Before I tell you about my issue, let me describe my environment:
Environment
Now that you know a little about me, let me tell you about the issue I am having:
I am trying to run the momentum program for Trading Evolved (Chapter 12 – Momentum/Momentum Model.ipynb), contained below. I am having a terrible time with the dates. Running the code as it was originally provided gives me the error AttributeError: 'UTC' object has no attribute 'key' After a large amount of google searching I was able to get around this error for a different program (First Zipleine Backtest.ipynb) by changing start_date = datetime(1996, 1, 1, tzinfo=pytz.UTC) end_date = datetime(2017, 12, 31, tzinfo=pytz.UTC)
to
start_date=pd.to_datetime('1996-01-01') end_date =pd.to_datetime('2017-12-31')
However, when I make this change to the Momentum Model program changing
start = datetime(1997, 1, 1, 8, 15, 12, 0, pytz.UTC) end = datetime(2017, 12, 31, 8, 15, 12, 0, pytz.UTC)
which initially produces the error
NameError: name 'datetime' is not defined
So I change the format of the start and end to
start=pd.to_datetime('1996-01-01') end =pd.to_datetime('2017-12-31')
I get the error
TypeError: Invalid comparison between dtype=datetime64[ns] and Timestamp
After searching the program for references to ‘start’, I came across
Now, I know that dates in python can take a couple of different forms and after I made the change, the form of start is
type(start) Out[21]: pandas._libs.tslibs.timestamps.Timestamp But I don’t know the format of the date in
So I don’t know how to make the comparison legitimate, if this indeed the source of the error.
I am trying to develop these programs into teaching material at the university at which I am employed but can’t do that if I can’t get passed the sticking point of the date formats. I would really, really, appreciate some help. Thanks.
--John Sparks
Program is below
%reset -f %clear
import zipline from zipline.api import order_target_percent, symbol, \ set_commission, set_slippage, schedule_function, \ date_rules, time_rules, attach_pipeline, pipeline_output, set_benchmark from pandas import Timestamp import matplotlib.pyplot as plt import pyfolio as pf import pandas as pd import numpy as np
from scipy import stats
from zipline.finance.commission import PerDollar from zipline.finance.slippage import VolumeShareSlippage, FixedSlippage
from zipline_norgatedata.pipelines import NorgateDataIndexConstituent
from zipline.pipeline import Pipeline
import norgatedata
intial_portfolio = 10000000 momentum_window = 125 minimum_momentum = 40 portfolio_size = 30 vola_window = 20
""" Commission and Slippage Settings """ enable_commission = True commission_pct = 0.001 enable_slippage = True slippage_volume_limit = 0.25 slippage_impact = 0.1
""" Helper functions. """ def momentum_score(ts): """ Input: Price time series. Output: Annualized exponential regression slope, multiplied by the R2 """
Make a list of consecutive numbers
def volatility(ts): return ts.pct_change().rolling(vola_window).std().iloc[-1]
def output_progress(context): """ Output some performance numbers during backtest run This code just prints out the past month's performance so that we have something to look at while the backtest runs. """
""" Initialization and trading logic """ def initialize(context):
def rebalance(context, data):
Write some progress output during the backtest
def analyze(context, perf):
start=Timestamp('1996-01-01')
end =Timestamp('2017-12-31')
start = datetime(1997, 1, 1, 8, 15, 12, 0, pytz.UTC)
end = datetime(2017, 12, 31, 8, 15, 12, 0, pytz.UTC)
start=pd.to_datetime('1996-01-01') end =pd.to_datetime('2017-12-31')
perf = zipline.run_algorithm( start=start, end=end, initialize=initialize, analyze=analyze, capital_base=intial_portfolio,
data_frequency = 'daily', bundle='quandl' )
print(start) type(start)
Here is how you can reproduce this issue on your machine:
Reproduction Steps
...
What steps have you taken to resolve this already?
Changed the format of the date ...
Anything else?
...
Sincerely, John J. Sparks, Ph.D.