0bps

Strategy vault / Other

Cross Time Period Strategy

JavaScript strategy by 一刀.

JavaScriptOtherRSI
Author
一刀
Complexity
Starter · 80 lines
Parameters
5
Backtest bar
15m · intraday
Tested on
BTC/USD · spot

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

Parameters

NameDefaultWhat it controls
splide_price0.1slippage
slow_ma20Slow MA Period
fast_ma5Fast MA Period
rsi_period5RSItimeframe
tick_interval60Cycle Interval

Source JavaScript

/*backtest
start: 2021-02-14 00:00:00
end: 2022-02-13 23:59:00
period: 15m
basePeriod: 5m
exchanges: [{"eid":"Bitfinex","currency":"BTC_USD","stocks":0}]
args: [["rsi_period",10]]
*/

/*
* fast_ma: Fast moving average, short period
* slow_ma: Slow moving average, long period
* rsi_period: rsitimeframe
* tick_interval: Cycle Interval
* splide_price: slippage
*/

// RSIIndicates operation status
var RSI_NONE = 0;
var RSI_BUY = 1;
var RSI_SELL = 2;

// Cancel unfilled orders
function canelPendingOrders() {
    while (true) {
        let orders = _C(exchange.GetOrders);
        if (orders.length == 0) {
            break;
        }
        for (let i = 0; i < orders.length; i++) {
            exchange.CancelOrder(orders[i].Id);
        }
    }
}

// ObtainRSIOperation Status
function getRsiStatus(records) {
    let rsi = TA.RSI(records, rsi_period)[records.length - 1];
    if (rsi < 30) {
        return RSI_BUY;
    }
    else if (rsi > 70) {
        return RSI_SELL;
    }
    else {
        return RSI_NONE;
    }
}

function onTick() {
    let records = _C(exchange.GetRecords, PERIOD_M15);
    let ticker = _C(exchange.GetTicker);
    let account = _C(exchange.GetAccount);
    let fast = TA.MA(records, fast_ma);
    let slow = TA.MA(records, slow_ma);
    let n = _Cross(fast, slow);
    if (n > 0 && getRsiStatus(records) == RSI_BUY) {
        canelPendingOrders();
        let price = ticker.Last + splide_price;
        let amount = account.Balance / price - 0.1;
        if (amount > 0.1) {
            exchange.Buy(price, amount);
        }
    }
    else if (n < 0 && getRsiStatus(records) == RSI_SELL) {
        canelPendingOrders();
        let price = ticker.Last - splide_price;
        let amount = account.Stocks;
        if (amount > 0.1) {
            exchange.Sell(price, amount);
        }
    }
}

function main() {
    while (true) {
        onTick();
        Sleep(tick_interval * 1000);
    }
}

Published by 一刀 on the FMZ strategy square · last modified 2022-02-15. 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/Cross-Time-Period-Strategy.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