Examples
First automation
Buy one share of AAPL, no logic. This is the simplest automation you can build. Every time you run this it will generate one order => buy one share of AAPL
this.buy(this.asset("AAPL"), 1);
//=> {symbol: "AAPL", units: 1, side: "long", ...}
Some logic
This automation has some logic. Calculates Simple Moving Average for the last 21 days and buys using all cash only if the asset current price is over SMA21 if not closes all positions
const asset = this.asset("MSFT"); // define assetconst sma21 = this.sma(21, "MSFT"); // calculates SMA21const closePrice = asset.close; // gets asset close price
if (closePrice > sma21) { this.buyAmount(asset, this.getCash()); // yes => Buy one share} else { this.closePositions(); // no => Close all open positions}
Multiple assets
This automation sorts a list of assets by CMR (cumulative total return) and picks top 2 to invest.
const topX = 2;const weight = 1 / topX;const stocks = [ "MSFT", "ADBE", "ADSK", "CRM", "AAPL"];
const stocksSorted = stocks.sort((a, b) => { return this.cmr(21, b) - this.cmr(21, a);});
// buy TOP topXfor (let i = 0; i < topX; i++) { const symbol = stocksSorted[i]; const amount = this.getCash() * weight; this.buyAmount(this.asset(symbol), amount, true);}