Simple Moving Average Version
JavaScript strategy by sabar.
The original spec has no written description. Read the source below.
Parameters
| Name | Default | What it controls |
|---|---|---|
| Amount | 100 | Amount |
Source JavaScript
/*backtest
start: 2021-06-01 00:00:00
end: 2021-09-22 00:00:00
period: 5m
basePeriod: 1m
exchanges: [{"eid":"Futures_OKCoin","currency":"BTC_USD"}]
*/
// Define Object
var e = exchange
e.SetContractType('swap')
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_10 = TA.MA(records, 10)
MA_close_10 = MA_10[MA_10.length - 1]
var MA_20 = TA.MA(records, 20)
MA_close_20 = MA_20[MA_20.length - 1]
var MA_30 = TA.MA(records, 30)
MA_close_30 = MA_30[MA_30.length - 1]
}
// Open and close position rules
function onTick() {
if (LastBarTime !== Bar.Time) { // KExecute trades after the line ends
if (status === PD_LONG) {
if (Bar.Close < MA_close_10) {
exchange.SetDirection("closebuy")
exchange.Sell(ticker.Buy, Amount)
status = Idle
}
}
if (status === PD_SHORT) {
if (Bar.Close > MA_close_10) {
exchange.SetDirection("closesell")
exchange.Buy(ticker.Sell, Amount)
status = Idle
}
}
Sleep(1 * 1000)
if (status === Idle) {
if (Bar.Close > MA_close_20 ) {
exchange.SetDirection("buy")
exchange.Buy(ticker.Sell, Amount)
status = PD_LONG
}
if (Bar.Close < MA_close_20 ) {
exchange.SetDirection("sell")
exchange.Sell(ticker.Buy, Amount)
status = PD_SHORT
}
}
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 sabar on the FMZ strategy square · last modified 2021-09-23. 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/Simple-Moving-Average-Version.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
- StochRSI Consistent with OKCoinJavaScript · Other
- Teaching Strategy TCI Strategy Mai Language TransplantationJavaScript · Other · 1d
- DMI Test ExampleJavaScript · Other · 1h
- StochRSI Strategy Indicator FrameworkJavaScript · Other
- Price Amplitude Moving Average 330000 Times Earnings in 3 YearsJavaScript · Other · 1d
- Angle Attack Follow Line IndicatorPine Script · Other · 15m