0bps

Strategy vault / Other

Dynamic Balance Alpha

JavaScript strategy by 中本大料.

JavaScriptOther
Author
中本大料
Complexity
Starter · 69 lines
Parameters
5

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

Parameters

NameDefaultWhat it controls
threshold0.05threshold
LoopInterval60Loop Time
Minstock0.001Minimum transaction volume
XPrecision4quantity precision
YPrecision8Price precision

Source JavaScript

var threshold = 0.05
var LoopInterval = 60
var Minstock = 0.001
var XPrecision = 4
var ZPrecision = 8

//Cancel Order Function
function CancelPendingOrders() {
    Sleep(1000);
    var ret = false;
    while (true) {
        var orders = null;
        while (!(orders = exchange.GetOrders())) {
            Sleep(1000);
        }
        if(orders.length == 0){
            return ret;
        }
        for (var j = 0; j< orders.length; j++){
            exchange.CancelOrder(orders[j].Id);
            ret = true;
            if(j<(orders.length-1)){
                Sleep(1000);
            }
        }
    }
}

//Order Function
function onTick() {
    var acc = _C(exchange.GetAccount); //Get account information
    var ticker = _C(exchange.GetTicker); //ObtainTickerData
    var spread = ticker.Sell - ticker.Buy; //ObtainTickerData bid-ask spread
    var diffAsset = (acc.Balance - (acc.Stocks * ticker.Sell)) / 2;//The difference between the account balance and the current value of the position0.5times
    var ratio = diffAsset / acc.Balance;
    LogStatus('ratio:'.ratio, _D()); //Print ratio and current time
    if (Math.abs(ratio) < threshold) { //IfratioAbsolute value is less than the specified threshold
        print("spread")
        return false; //Returnfalse
    }
    if (ratio > 0) { //IfratioGreater than0;
        var buyPrice = _N(ticker.Sell + spread, ZPrecision);
        var buyAmount = _N(diffAsset / buyPrice, XPrecision);
        if (buyAmount < Minstock) { //If the order quantity is less than the minimum trading volume
            return false;
        }
        exchange.Buy(buyPrice, buyAmount, diffAsset, ratio);//Buy Order
    }else{
        var sellPrice = _N(ticker.Buy - spread, ZPrecision);//Calculate order price
        var sellAmount = _N(-diffAsset / sellPrice, XPrecision);//Calculate order quantity
        if(sellAmount < Minstock){
            return false;
        }
        exchange.Sell(sellPrice, sellAmount, diffAsset, ratio); //Sell Order
    }
    return true;
}

//Main Function
function main() {
    //SetErrorFilter("GetRecords:|GetOrders:|GetDepth:|GetAccount|:Buy|Sell|timeout"); //Filter non-essential information
    while (true) {
        if (onTick()) {
            CancelPendingOrders();
            Log(_C(exchange.GetAccount));
        }
        Sleep(LoopInterval * 1000);
    }
}

Published by 中本大料 on the FMZ strategy square · last modified 2018-09-06. 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/Dynamic-Balance-Alpha.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