0bps

Strategy vault / Tools & templates

Moving Average Bot 30 lines

"Talk is cheap. Show me the code" Just for learning, Be carefully when using in real market. Only run on digital currency exchanges. Note: this strategy included template. (function with $.) Beginers can start with…

Author
小草
Complexity
Starter · 48 lines
Parameters
5
Backtest bar
1d · swing
Tested on
BTC/USDT · spot

"Talk is cheap. Show me the code"

Just for learning, Be carefully when using in real market.

Only run on digital currency exchanges.

Note: this strategy included template. (function with $.)

Beginers can start with this strategy, learn how to code, know the difference between backtesting and real market.

https://dn-filebox.qbox.me/fb4d0c7d773ca83e9d0230927705d419dc0bbeaa.png

Parameters

NameDefaultWhat it controls
Slippage0.1Slippage
FastPeriod5fast line periods
SlowPeriod15slow line periods
EnterPeriod2observe periods
Interval120Interval time(second)

Source JavaScript

/*backtest
start: 2020-10-11 00:00:00
end: 2020-10-11 23:59:00
period: 1d
basePeriod: 1h
exchanges: [{"eid":"Binance","currency":"BTC_USDT"}]
*/

start = false
function main() {
    Log('started');
    var initAccount = _C(exchange.GetAccount);
    var ticker = exchange.GetTicker();
    var InitValue = (initAccount.Stocks + initAccount.FrozenStocks)*ticker.Last + initAccount.Balance + initAccount.FrozenBalance;
    while (true) {
        var records = _C(exchange.GetRecords, PERIOD_M15);
        if(!start){
            Log(records[0], records.length)
            start = true
        }
        ticker =_C(exchange.GetTicker);
        var FastRecords = TA.MA(records,FastPeriod);
        var SlowRecords = TA.MA(records,SlowPeriod);
        var NowAccount = _C(exchange.GetAccount);
        var n = _Cross(FastRecords, SlowRecords);
        if (n >= EnterPeriod && NowAccount.Balance > 0) {
            var Price = _N(ticker.Sell+Slippage, 2);
            var Amount = _N(0.99*NowAccount.Balance/Price, 3);
            if(Amount>0.1){
               var id = exchange.Buy(Price, Amount);
               if(exchange.GetOrders(id).Status == ORDER_STATE_PENDING){exchange.CancelOrder(id);}
               LogProfit((NowAccount.Stocks + NowAccount.FrozenStocks)*ticker.Last + NowAccount.Balance + NowAccount.FrozenBalance - InitValue);
            }
        }
        if(n <= -EnterPeriod && NowAccount.Stocks > 0) {
            var Price = _N(ticker.Buy-Slippage, 2);
            var Amount = _N(NowAccount.Stocks, 3);
            if(Amount>0.1){
                var id = exchange.Sell(Price, Amount);
                if(exchange.GetOrders(id).Status == ORDER_STATE_PENDING){exchange.CancelOrder(id);}
                LogProfit((NowAccount.Stocks + NowAccount.FrozenStocks)*ticker.Last + NowAccount.Balance + NowAccount.FrozenBalance - InitValue);
            }
        }
        if(!IsVirtual()){
            Sleep(Interval*1000)
        }
    }
}

Published by 小草 on the FMZ strategy square · last modified 2020-10-13. 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 tested, production-ready module from strategies/Moving-Average-Bot-30-lines.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