{"id":13699867,"url":"https://github.com/quantrocket-llc/moonshot","last_synced_at":"2025-04-13T00:48:52.402Z","repository":{"id":25214518,"uuid":"103468104","full_name":"quantrocket-llc/moonshot","owner":"quantrocket-llc","description":"Vectorized backtester and trading engine for QuantRocket","archived":false,"fork":false,"pushed_at":"2024-12-19T14:15:15.000Z","size":650,"stargazers_count":217,"open_issues_count":0,"forks_count":50,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-04-04T04:09:32.636Z","etag":null,"topics":["algorithmic-trading","interactive-brokers","pandas","python","quantitative-finance","trading-platform"],"latest_commit_sha":null,"homepage":"https://www.quantrocket.com","language":"Python","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/quantrocket-llc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-09-14T01:04:59.000Z","updated_at":"2025-03-24T14:47:11.000Z","dependencies_parsed_at":"2023-02-14T12:45:57.055Z","dependency_job_id":"773f3be3-8147-4163-841b-ad2703bad65f","html_url":"https://github.com/quantrocket-llc/moonshot","commit_stats":null,"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantrocket-llc%2Fmoonshot","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantrocket-llc%2Fmoonshot/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantrocket-llc%2Fmoonshot/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantrocket-llc%2Fmoonshot/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quantrocket-llc","download_url":"https://codeload.github.com/quantrocket-llc/moonshot/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248650436,"owners_count":21139672,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithmic-trading","interactive-brokers","pandas","python","quantitative-finance","trading-platform"],"created_at":"2024-08-02T20:00:44.846Z","updated_at":"2025-04-13T00:48:52.382Z","avatar_url":"https://github.com/quantrocket-llc.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":["Trading \u0026 Backtesting","交易与回测"],"readme":"# Moonshot\n\nMoonshot is a backtester designed for data scientists, created by and for [QuantRocket](https://www.quantrocket.com).\n\n## Key features\n\n**Pandas-based**: Moonshot is based on Pandas, the centerpiece of the Python data science stack. If you love Pandas you'll love Moonshot. Moonshot can be thought of as a set of conventions for organizing Pandas code for the purpose of running backtests.\n\n**Lightweight**: Moonshot is simple and lightweight because it relies on the power and flexibility of Pandas and doesn't attempt to re-create functionality that Pandas can already do. No bloated codebase full of countless indicators and models to import and learn. Most of Moonshot's code is contained in a single `Moonshot` class.\n\n**Fast**: Moonshot is fast because Pandas is fast. No event-driven backtester can match Moonshot's speed. Speed promotes alpha discovery by facilitating rapid experimentation and research iteration.\n\n**Multi-asset class, multi-time frame**: Moonshot supports end-of-day and intraday strategies using equities, futures, and FX.\n\n**Machine learning support**: Moonshot [supports machine learning and deep learning strategies](#machine-learning-example) using scikit-learn or Keras.\n\n**Live trading**: Live trading with Moonshot can be thought of as running a backtest on up-to-date historical data and generating a batch of orders based on the latest signals produced by the backtest.\n\n**No black boxes, no magic**: Moonshot provides many conveniences to make backtesting easier, but it eschews hidden behaviors and complex, under-the-hood simulation rules that are hard to understand or audit. What you see is what you get.\n\n## Example\n\nA basic Moonshot strategy is shown below:\n\n```python\nfrom moonshot import Moonshot\n\nclass DualMovingAverageStrategy(Moonshot):\n\n    CODE = \"dma-tech\"\n    DB = \"tech-giants-1d\"\n    LMAVG_WINDOW = 300\n    SMAVG_WINDOW = 100\n\n    def prices_to_signals(self, prices):\n        closes = prices.loc[\"Close\"]\n\n        # Compute long and short moving averages\n        lmavgs = closes.rolling(self.LMAVG_WINDOW).mean()\n        smavgs = closes.rolling(self.SMAVG_WINDOW).mean()\n\n        # Go long when short moving average is above long moving average\n        signals = smavgs \u003e lmavgs\n\n        return signals.astype(int)\n\n    def signals_to_target_weights(self, signals, prices):\n        # spread our capital equally among our trades on any given day\n        daily_signal_counts = signals.abs().sum(axis=1)\n        weights = signals.div(daily_signal_counts, axis=0).fillna(0)\n        return weights\n\n    def target_weights_to_positions(self, weights, prices):\n        # we'll enter in the period after the signal\n        positions = weights.shift()\n        return positions\n\n    def positions_to_gross_returns(self, positions, prices):\n        # Our return is the security's close-to-close return, multiplied by\n        # the size of our position\n        closes = prices.loc[\"Close\"]\n        gross_returns = closes.pct_change() * positions.shift()\n        return gross_returns\n```\n\nSee the [QuantRocket docs](https://www.quantrocket.com/docs/#moonshot-backtesting) for a fuller discussion.\n\n## Machine Learning Example\n\nMoonshot supports machine learning strategies using [scikit-learn](https://scikit-learn.org) or [Keras](https://keras.io/). The model must be trained outside of Moonshot, either using QuantRocket or by training the model manually and persisting it to disk:\n\n```python\nfrom sklearn.tree import DecisionTreeClassifier\nimport pickle\n\nmodel = DecisionTreeClassifier()\nX = np.array([[1,1],[0,0]])\nY = np.array([1,0])\nmodel.fit(X, Y)\n\nwith open(\"my_ml_model.pkl\", \"wb\") as f:\n    pickle.dump(model, f)\n```\n\nA basic machine learning strategy is shown below:\n\n```python\nfrom moonshot import MoonshotML\n\nclass DemoMLStrategy(MoonshotML):\n\n    CODE = \"demo-ml\"\n    DB = \"demo-stk-1d\"\n    MODEL = \"my_ml_model.pkl\"\n\n    def prices_to_features(self, prices):\n        closes = prices.loc[\"Close\"]\n        # create a dict of DataFrame features\n        features = {}\n        features[\"returns_1d\"]= closes.pct_change()\n        features[\"returns_2d\"] = (closes - closes.shift(2)) / closes.shift(2)\n        # targets is used by QuantRocket for training model, can be None if using\n        # an already trained model\n        targets = closes.pct_change().shift(-1)\n        return features, targets\n\n    def predictions_to_signals(self, predictions, prices):\n        signals = predictions \u003e 0\n        return signals.astype(int)\n```\n\nSee the [QuantRocket docs](https://www.quantrocket.com/docs/#ml) for a fuller discussion.\n\n## FAQ\n\n### Can I use Moonshot without QuantRocket?\n\nMoonshot depends on QuantRocket for querying historical data in backtesting and for live trading. In the future we hope to add support for running Moonshot on a CSV of data to allow backtesting outside of QuantRocket.\n\n## See also\n\n[Moonchart](https://github.com/quantrocket-llc/moonchart) is a companion library for creating performance tear sheets from a Moonshot backtest.\n\n## License\n\nMoonshot is distributed under the Apache 2.0 License. See the LICENSE file in the release for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantrocket-llc%2Fmoonshot","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquantrocket-llc%2Fmoonshot","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantrocket-llc%2Fmoonshot/lists"}