No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. Max_daily_drawdown = daily_drawdown.rolling(window, min_periods=1).min() Plot the results Rolling_max = aapl.rolling(window, min_periods=1).max()ĭaily_drawdown = aapl/rolling_max - 1.0 Calculate the minimum (negative) daily drawdown Window = 252 Calculate the max drawdown in the past window days for each day Print(sharpe_ratio) Define a trailing 252 trading day window Sharpe_ratio = np.sqrt(252) * (an() / returns.std()) Print the Sharpe ratio Returns = portfolio annualized Sharpe ratio Plt.show() Isolate the returns of your strategy 'v', markersize=10, color='k') Show the plot Portfolio = portfolio.pct_change() Create a figureĪx1 = fig.add_subplot(111, ylabel='Portfolio value in $') Plot the equity curve in dollarsĪx1.plot(, Portfolio = portfolio + portfolio Add returns to portfolio Portfolio = initial_capital - (pos_diff.multiply(aapl, axis=0)).sum(axis=1).cumsum() Add total to portfolio Portfolio = (positions.multiply(aapl, axis=0)).sum(axis=1) Add cash to portfolio Pos_diff = positions.diff() Add holdings to portfolio Portfolio = positions.multiply(aapl, axis=0) Store the difference in shares owned Positions = 1000*signals Initialize the portfolio with value owned Positions = pd.DataFrame(index=signals.index).fillna(0.0) Buy a 1000 shares Initial_capital= float(100000.0) Create a DataFrame positions 'v', markersize=10, color='k') Set the initial capital '^', markersize=10, color='m') Plot the sell signals Signals].plot(ax=ax1, lw=2.) Plot the buy signalsĪx1.plot(, Signals = signals.diff() Initialize the plot figureįig = plt.figure() Add a subplot and label for y-axisĪx1 = fig.add_subplot(111, ylabel='Price in $') Plot the closing priceĪapl.plot(ax=ax1, color='r', lw=2.) Plot the short and long moving averages > signals, 1.0, 0.0) Generate trading orders Signals = aapl.rolling(window=long_window, min_periods=1, center=False).mean() Create signals Signals = aapl.rolling(window=short_window, min_periods=1, center=False).mean() Create long simple moving average over the long window Signals = 0.0 Create short simple moving average over the short window Long_window = 100 Initialize the signals DataFrame with the signal column Return(pd.concat(datas, keys=tickers, names=))Īll_data = get(tickers, datetime.datetime(2006, 10, 1), datetime.datetime(2017, 10, 27))Įnd=datetime.datetime(2017, 10, 27)) Initialize the short and long windows Return (pdr.get_data_yahoo(ticker, start=startdate, end=enddate)) Can anyone help provide with a potential cleaner/simpler MA crossover strategy code with Python? I'm also trying to figure out how to get the stock performance to plot along with the equity curve in one.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |