Strategy vault / Trend following
One Moving Average Trend Demo
JavaScript strategy by 扁豆子.
JavaScriptTrend following
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| Amount | 100 | Amount |
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.
/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- npx agenkit install engineering-kit in your project
- Paste the command into your agent and approve each phase
- 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
- Large Medium and Small Three Cycle Leap Strategy V20 Spot TestJavaScript · Trend following · 1d
- Step by Step Position Adding Trading Strategy Based on EMA Trend Filtering TutorialJavaScript · Trend following · 1h
- KingKeltner Trend Strategy Low FrequencyJavaScript · Trend following
- Statistics Candlestick Backtesting to See If There Is a TrendJavaScript · Trend following
- SuperTrend Strategy LessonJavaScript · Trend following · 15m
- Code Implementation and Feasibility Analysis of Following the Major Trend and Going Against the Minor Trend StrategyJavaScript · Trend following