<jupyter_start><jupyter_text>Moonshot backtest and tear sheet with `up_minus_down.py` algo *This example notebook is part of QuantRocket's [Getting Started with Equities](https://www.quantrocket.com/tutorials/equities/) tutorial series.**The notebook assumes that you've already created a historical price database called "demo-stocks-1d". Please refer to the tutorial series if you haven't completed this step.*This notebook shows how to run a Moonshot backtest using QuantRocket then analyze the backtests results inside the notebook using Moonchart.<jupyter_code>from quantrocket.moonshot import backtest
import moonchart
import io<jupyter_output><empty_output><jupyter_text>Step 1: Run the backtestFirst, run the backtest and save the CSV to an in-memory file (using `io.StringIO`):<jupyter_code>f = io.StringIO()
backtest("umd-demo", start_date="2010-01-01", csv=True, filepath_or_buffer=f)<jupyter_output><empty_output><jupyter_text>Step 2: Create Tear SheetYou can use Moonchart, an open-source companion package to Moonshot, to create a performance tear sheet:<jupyter_code>tearsheet = moonchart.Tearsheet()
tearsheet.from_moonshot_csv(f)<jupyter_output><empty_output><jupyter_text>Optional: Analyze your backtest results  If you want to explore the Moonshot backtest results directly, you can do that as well:<jupyter_code>import pandas as pd
# Since the tear sheet already read the file, we need to seek back to the beginning
f.seek(0)
results = pd.read_csv(f, parse_dates=["Date"], index_col=["Field","Date"])
print("Available fields:", ", ".join(results.index.get_level_values(0).unique()))
print(results.loc["Return"].head())<jupyter_output>umd-demo
Date                
2010-01-04  0.001861
2010-01-05  0.006291
2010-01-06  0.015832
2010-01-07 -0.011583
2010-01-08  0.005346
{'fraction_non_alphanumeric': 0.09843400447427293, 'fraction_numerical': 0.049217002237136466, 'mean_word_length': 6.184738955823293, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '581845', 'license': 'Apache-2.0', 'n_tokens_mistral': 624, 'n_tokens_neox': 563, 'n_words': 174, 'path': 'moonshot/up_minus_down.ipynb', 'repo_name': 'quantrocket-llc/codeload-demo', 'size': '1788'}