0bps

Strategy vault / Trend following

One Moving Average Trend Demo

JavaScript strategy by 扁豆子.

JavaScriptTrend following
Author
扁豆子
Complexity
Starter · 74 lines
Parameters
1

The original spec has no written description. Read the source below.

Parameters

NameDefaultWhat it controls
Amount100Amount

Source JavaScript

// Define Object
var e = exchange
e.SetContractType('XBTUSD')
var LastBarTime = 0
Idle = -1
status = Idle

// Connect to exchange, Get relevant information
function UpdateInfo() {
    var account = exchange.GetAccount()
    records = exchange.GetRecords()
    ticker = exchange.GetTicker()
    balance = account.Stocks
    Bar = records[records.length - 1]
}

// Indicator calculation and acquisition
function Get_MA() {
    var MA = TA.MA(records, 30)
    MA_close = MA[MA.length - 1]
}

// Open and close position rules
function onTick() {
    if (LastBarTime !== Bar.Time) { // KExecute trades after the line ends
        if (status === Idle) {
            if (Bar.Close > MA_close) {
                exchange.SetDirection("buy") 
                exchange.Buy(ticker.Sell, Amount)
                status = PD_LONG
            }
            if (Bar.Close < MA_close) {
                exchange.SetDirection("sell") 
                exchange.Sell(ticker.Buy, Amount)
                status = PD_SHORT
            }
        }
        if (status === PD_LONG) {
            if (Bar.Close < MA_close) {
                exchange.SetDirection("closebuy")
                exchange.Sell(ticker.Buy, Amount)
                exchange.SetDirection("sell") 
                exchange.Sell(ticker.Buy, Amount)
                status = PD_SHORT
            }
        }
        if (status === PD_SHORT) {
            if (Bar.Close > MA_close) {
                exchange.SetDirection("closesell") 
                exchange.Buy(ticker.Sell, Amount)
                exchange.SetDirection("buy") 
                exchange.Buy(ticker.Sell, Amount)
                status = PD_LONG
            }
        }
        LastBarTime = Bar.Time
    }
}

function main() {
    // Main Function, Continuous Loop
    while (1) {
        // Connect to exchange, Get relevant information
        UpdateInfo()
        // Indicator calculation and acquisition
        Get_MA()
        // Open and close position rules
        onTick()
        // Printbalance
        LogStatus(balance)
        // PollingsleepTime
        Sleep(5 * 1000)
    }
}

Published by 扁豆子 on the FMZ strategy square · last modified 2020-04-11. Collected in the Quant Trading Vault. Unvalidated research: re-test with your own data and costs before trusting it.

Build this with AgenKit

A spec is not a trading system. Paste this into your coding agent and AgenKit runs a specialist team — architecture, test-first build, risk gates, review — with paper trading on by default.

in Claude Code, Cursor, Codex & 4 more
/agenkit build a production-grade trading bot with a backtest engine, risk controls and paper trading by default from strategies/One-Moving-Average-Trend-Demo.md
  1. npx agenkit install engineering-kit in your project
  2. Paste the command into your agent and approve each phase
  3. Get tests, backtest, risk controls and a paper-trading switch

Understand it first

Before you run this, read the lesson behind the idea and how to backtest it honestly. Specs like this one are the survivors of an unknown number of trials; deflate their results accordingly.

Related specs

esc

Try , , or .

↑↓ move↵ open