Getting Started
You can watch a video tutorial if you prefer a visual guide on how to create a project and start writing your first automation.
Otherwise, follow the steps below to get started with ZapCLI.
-
Install the CLI
Start by installing the CLI globally using your preferred package manager.
Terminal window npm install @zapcli/cli -gTerminal window yarn global add @zapcli/cliTerminal window pnpm install @zapcli/cli -g -
Create a project
Generate a new project using zapcli. This action will set up a sample project and install all necessary dependencies.
Terminal window zapcli create MyProjectAfter running the command, you will see a new directory named
MyProject
with the following structure:Directorydata/
- …
Directoryreport/
- data.json
- report.html
Directorysrc/
- hello.zp
- hello.js
- package.json
- zp.config.js
Open your project in VSCode or any other editor.
-
Run automation
ZapCLI can execute an automation once on a specific day and showcase the orders in the terminal or store them in a file (e.g.,
orders.json
). To run an automation just open it in VSCode (src/hello.zp
) and pressCtrl+Shift+B
. Or you can use the terminal:Terminal window zapcli execute ./src/hello.zpTerminal window npm execute -
Run your first backtest
Terminal window zapcli backtest ./src/hello.zpTerminal window npm backtestThis command will execute
hello.zp
for a duration of 20 days. It will display the result in the terminal and save the backtest data inbacktests/data.json
. You can change the directory where the backtest data is saved by changing thebacktestsDir
property in thezp.config.js
file. -
Generate a report
Terminal window zapcli reportTerminal window npm reportThis command will generate a visual report file
reports/report.html
based on the backtest data saved inbacktests/
directory. You can generate multiple reports by providing thename
parameter. -
Write your own automation
To write your own strategy you need to create an automation file that will be run on every bar. Currently we have been using the
hello.(zp|js)
file;; Loops a list a symbols and buys 1 share of each(def symbols ["AAPL","MSFT"])(loop symbol in symbols(buy {symbol} 1))const assets = ["AAPL", "MSFT"]const window = 1const settings = {}function run() {for (const symbol of assets) {this.buy(this.asset(symbol), 1)}}To see more automation examples check out the Examples page.
-
Configuration
ZapCLI is using a configuration file to run a backtest or execute an automation file. Your project started with the default config file:
import { analyzers } from '@zapcli/backtest'const config = {// DirectoriesdataDir: "./data",reportsDir: "./reports",backtestsDir: "./backtests",// Data Provider// ex. zapcli download -s AAPLdataProvider: "zapant",// Execute automation// ex. zapcli executeexecute: {date: "2024-05-20",inputs: {assets: ["AAPL", "MSFT"],initialCapital: 10000,cash: 5000,openPositions: [// Provide open positions to automation// {// symbol: 'AAPL',// openDate: 1682366400000,// openPrice: 200.00,// closeDate: null,// closePrice: null,// units: 5,// side: 'long',// accountType: 'paper'// }]},},// Backtest automation// ex. zapcli backtestbacktest: {startDate: "2024-05-01",endDate: "2024-05-20",inputs: {assets: []},analyzers: [new analyzers.RetursAnalyzer(),new analyzers.DrawDownAnalyzer(),new analyzers.TradesAnalyzer(),new analyzers.PositionsAnalyzer(),]}}export default config;As you can see here you can configure your backtest with a start, end date or add analyzers to run with your backtest. To learn more about Analyzers and Data Providers please read the documentaion.