{"id":13601563,"url":"https://github.com/Grademark/grademark","last_synced_at":"2025-04-11T04:31:05.818Z","repository":{"id":40294294,"uuid":"153887464","full_name":"Grademark/grademark","owner":"Grademark","description":"An API for backtesting trading strategies in JavaScript and TypeScript.","archived":false,"fork":false,"pushed_at":"2023-03-02T10:48:46.000Z","size":467,"stargazers_count":273,"open_issues_count":13,"forks_count":33,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-09-19T09:48:34.531Z","etag":null,"topics":["algorithmic-trading","backtesting","backtesting-trading-strategies","data-forge","drawdown","drawdown-chart","equity-curve","javascript","plot-charts","quantitative-trading","trade","trading","trading-strategies","trading-strategy","trading-system","typescript"],"latest_commit_sha":null,"homepage":"http://www.the-data-wrangler.com/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Grademark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"ashleydavis"}},"created_at":"2018-10-20T08:57:40.000Z","updated_at":"2024-09-16T10:37:20.000Z","dependencies_parsed_at":"2024-01-13T19:46:57.770Z","dependency_job_id":"febab22c-38e1-4789-b8ac-3ea732d078ee","html_url":"https://github.com/Grademark/grademark","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grademark%2Fgrademark","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grademark%2Fgrademark/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grademark%2Fgrademark/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Grademark%2Fgrademark/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Grademark","download_url":"https://codeload.github.com/Grademark/grademark/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223458421,"owners_count":17148465,"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","backtesting","backtesting-trading-strategies","data-forge","drawdown","drawdown-chart","equity-curve","javascript","plot-charts","quantitative-trading","trade","trading","trading-strategies","trading-strategy","trading-system","typescript"],"created_at":"2024-08-01T18:01:04.597Z","updated_at":"2024-11-07T04:31:07.459Z","avatar_url":"https://github.com/Grademark.png","language":"TypeScript","funding_links":["https://github.com/sponsors/ashleydavis"],"categories":["TypeScript","algorithmic-trading"],"sub_categories":[],"readme":"# Grademark\n\nToolkit for algo trading and backtesting in JavaScript and TypeScript.\n\nThis API builds on [Data-Forge](http://data-forge-js.com/) and is best used from [Data-Forge Notebook](http://www.data-forge-notebook.com/) (making it easy to plot charts and visualize).\n\nCheck out the [release notes](release-notes.md) to see updates and breaking changes.\n\n[Click here to support my work](https://www.codecapers.com.au/about#support-my-work)\n\nPlease see what this looks like in [the Grademark first example](https://github.com/grademark/grademark-first-example) and the unit tests in this repo.\n\nLove this? Please star this repo!\n\n## First example\n\nFrom the [Grademark first example](https://github.com/grademark/grademark-first-example) here's some example output. [Click to see the first example as a notebook.](https://grademark.github.io/grademark-first-example/)\n\nAnalysis of a sequence of trades looks like this:\n\n![Analysis of trades screenshot](https://raw.githubusercontent.com/grademark/grademark-first-example/master/output/analysis-screenshot.png)\n\nHere's a chart that visualizes the equity curve for the example strategy:\n\n![Equity curve](https://raw.githubusercontent.com/grademark/grademark-first-example/master/output/my-equity-curve-pct.png)\n\nHere's another chart, this one is a visualization of the drawdown for the example strategy:\n\n![Drawdown](https://raw.githubusercontent.com/grademark/grademark-first-example/master/output/my-drawdown-pct.png)\n\n## Pre-requisites\n\n- Make sure your data is sorted in forward chronological order. \n\n## Data format\n\nYour data needs to be loaded into memory in the following format:\n  \n```typescript\ninterface IBar {\n    time: Date;\n    open: number;\n    high: number;\n    low: number;\n    close: number;\n}\n\nconst data: IBar[] = ... load your data ...\n```\n\n## Features\n\n- Define a trading strategy with entry and exit rules.\n- Backtest a trading strategy on a single financial instrument.\n- Apply custom indicators to your input data series.\n- Specify lookback period.\n- Built-in intrabar stop loss.\n- Compute and plot equity curve and drawdown charts.\n- Throughly covered by automated tests.\n- Calculation of risk and rmultiples.\n- Intrabar profit target.\n- Intrabar trailing stop loss.\n- Conditional buy on price level (intrabar).\n- Monte carlo simulation.\n- Multiple parameter optimization based on permutations of parameters (using grid search and hill-climb algorithms).\n- Walk forward optimization and backtesting.\n- Plot a chart of trailing stop loss.\n- Short selling.\n\n[Data-Forge Notebook](https://www.data-forge-notebook.com/) comes with example JavaScript notebooks that demonstrate many of these features.\n\nIf you need help with new features please reach out!\n\n## Maybe coming later\n\n- Support for precise decimal numbers.\n- Fees.\n- Slippage.\n- Position sizing.\n- Testing multiple instruments / portfolio simulation / ranking instruments.\n- Market filters.\n\n## Complete examples\n\nFor a ready to go example please see the repo [grademark-first-example](https://github.com/grademark/grademark-first-example).\n\n## Usage\n\nInstructions here are for JavaScript, but this library is written in TypeScript and so it can also be used from TypeScript.\n\n### Installation\n\n    npm install --save grademark\n\n### Import modules\n\n```javascript\nconst dataForge = require('data-forge');\nrequire('data-forge-fs'); // For file loading capability.\nrequire('data-forge-indicators'); // For the moving average indicator.\nrequire('data-forge-plot'); // For rendering charts.\nconst { backtest, analyze, computeEquityCurve, computeDrawdown } = require('grademark');\n```\n\n### Load your data\n\nUse [Data-Forge](http://data-forge-js.com/) to load and prep your data, make sure your data is sorted in forward chronological order.\n\nThis example loads a CSV file, but feel free to load your data from REST API, database or wherever you want!\n\n```javascript\nlet inputSeries = dataForge.readFileSync(\"STW.csv\")\n    .parseCSV()\n    .parseDates(\"date\", \"D/MM/YYYY\")\n    .parseFloats([\"open\", \"high\", \"low\", \"close\", \"volume\"])\n    .setIndex(\"date\") // Index so we can later merge on date.\n    .renameSeries({ date: \"time\" });\n```\nThe example data file is available in [the example repo](https://github.com/grademark/grademark-first-example).\n\n### Add indicators\n\nAdd whatever indicators and signals you want to your data.\n\n```javascript\nconst movingAverage = inputSeries\n    .deflate(bar =\u003e bar.close)          // Extract closing price series.\n    .sma(30);                           // 30 day moving average.\n\ninputSeries = inputSeries\n    .withSeries(\"sma\", movingAverage)   // Integrate moving average into data, indexed on date.\n    .skip(30)                           // Skip blank sma entries.\n```\n\n### Create a strategy\n\nThis is a very simple and very naive mean reversion strategy:\n\n```javascript\nconst strategy = {\n    entryRule: (enterPosition, args) =\u003e {\n        if (args.bar.close \u003c args.bar.sma) { // Buy when price is below average.\n            enterPosition({ direction: \"long\" }); // Long is default, pass in \"short\" to short sell.\n        }\n    },\n\n    exitRule: (exitPosition, args) =\u003e {\n        if (args.bar.close \u003e args.bar.sma) {\n            exitPosition(); // Sell when price is above average.\n        }\n    },\n\n    stopLoss: args =\u003e { // Optional intrabar stop loss.\n        return args.entryPrice * (5/100); // Stop out on 5% loss from entry price.\n    },\n};\n```\n\n### Running a backtest\n\nBacktest your strategy, then compute and print metrics:\n\n```javascript\nconst trades = backtest(strategy, inputSeries)\nconsole.log(\"Made \" + trades.length + \" trades!\");\n\nconst startingCapital = 10000;\nconst analysis = analyze(startingCapital, trades);\nconsole.log(analysis);\n```\n\n### Visualizing the results\n\nUse [Data-Forge Plot](https://github.com/data-forge/data-forge-plot) to visualize the equity curve and drawdown chart from your trading strategy:\n\n```javascript\ncomputeEquityCurve(trades)\n    .plot()\n    .renderImage(\"output/my-equity-curve.png\");\n\ncomputeDrawdown(trades)\n    .plot()\n    .renderImage(\"output/my-drawdown.png\");\n```\n\n## Advanced backtesting\n\nWe are only just getting started in this example to learn more please follow my [blog](http://www.the-data-wrangler.com/) and [YouTube channel](https://www.youtube.com/channel/UCOxw0jy384_wFRwspgq7qMQ).\n\n## Resources\n\n- [Data-Forge](http://data-forge-js.com/)\n- [Data-Forge Notebook](http://www.data-forge-notebook.com/)\n\n## Support the developer \n\n\u003ca target=\"_blank\" href=\"https://www.codecapers.com.au/about#support-my-work\"\u003eClick here to **support the developer.**\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGrademark%2Fgrademark","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGrademark%2Fgrademark","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGrademark%2Fgrademark/lists"}