0bps

Strategy vault / Other

Multi Threaded Batch Tasks

- $.go() Batch ExecutionGo Parameter Type Two-dimensional array Return value cmdList array - $.wait(cmdList) Get Results - $.gowait() Execute Go and get the result

JavaScriptOther
Author
一拳男孩
Complexity
Starter · 55 lines
Parameters
1
    var tasks = $.go([
        [exchange, 'GetDepth'],
        [exchange, 'GetTicker'],
    ])
    var retList = $.wait(tasks)
    Log(retList[0])
    Log(retList[1])
  • $.go()

Batch ExecutionGo Parameter Type Two-dimensional array

   [
        [exchange, 'GetDepth'],
        [exchange, 'GetTicker'],
    ]

Return value cmdList array

  • $.wait(cmdList)

Get Results

  • $.gowait()

Execute Go and get the result

Parameters

NameDefaultWhat it controls
RETRY_INTERVALtrueRetry interval (seconds)

Source JavaScript

$.go = function(tasks) {
    return _.map(tasks, function(args) {
        return {
            result: null,
            args: args,
            defer: args[0].Go.apply(args[0], args.slice(1))
        }
    })
}

$.gowait = function(tasks, maxConcurrent, retry) {
    return $.wait($.go(tasks), maxConcurrent, retry)
}

$.wait = function(cmdList, maxConcurrent, retry) {
    retry = retry || true
    maxConcurrent = Math.min(cmdList.length, maxConcurrent || 5)
    var total = Math.ceil(cmdList.length / maxConcurrent)
    return _.reduce(_.range(total), function(beforeResultList, currentPage) {
        var lastCmdList = cmdList.slice(currentPage * maxConcurrent, currentPage * maxConcurrent + maxConcurrent)
        while (1) {
            lastCmdList = _.map(lastCmdList, function(cmd) {
                var args = cmd.args
                if (cmd.result == null) {
                    var ret = cmd.defer.wait()
                    if (ret != null) {
                        return _.extend(cmd, {
                            result: ret
                        })
                    } else if (retry === true) {
                        return _.extend(cmd, {
                            defer: args[0].Go.apply(args[0], args.slice(1)),
                            result: null
                        })
                    }
                }
                return cmd
            })
            var isFinished = _.every(lastCmdList, function(cmd) {
                return cmd.result
            })
            if (isFinished) {
                break
            }
            Sleep(RETRY_INTERVAL * 1000)
        }
        var newResultList = lastCmdList.map(function(cmd) {
            return cmd.result
        })
        if (currentPage + 1 < total) {
            Sleep(1000)
        }
        return beforeResultList.concat(newResultList)
    }, [])
}

Published by 一拳男孩 on the FMZ strategy square · last modified 2019-08-24. 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/Multi-Threaded-Batch-Tasks.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