0bps

Strategy vault / Arbitrage

Cross Platform Arbitrage Beginner Test

JavaScript strategy by cnxzcxy.

JavaScriptArbitrage
Author
cnxzcxy
Complexity
Starter · 47 lines
Parameters
1
Backtest bar
1h · intraday
Tested on
ETH · spot

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

Parameters

NameDefaultWhat it controls
minRate1.02Price difference ratio

Source JavaScript

/*backtest
start: 2018-01-01 00:00:00
end: 2018-08-01 14:00:00
period: 1h
exchanges: [{"eid":"Bitfinex","currency":"ETH","balance":5000,"stocks":10},{"eid":"OKCoin_EN","currency":"ETH","balance":5000,"stocks":10}]
*/
var buyExchange;
var sellExchange;

function calc(sellExchange, buyExchange){
        var date = _D();
        sellDepth = sellExchange.GetDepth();
        sellAccount = sellExchange.GetAccount();
        sellPrice = sellDepth.Bids[1].Price;
        sellAmount = sellDepth.Bids[1].Amount;
        
        buyDepth = buyExchange.GetDepth();
        buyAccount = buyExchange.GetAccount();
        buyPrice = buyDepth.Asks[1].Price;
        buyAmount = buyDepth.Asks[1].Amount;
        
        amount = (sellAmount > buyAmount) ? buyAmount : sellAmount;
        amount = (amount > sellAccount.Stocks) ? sellAccount.Stocks : amount;
        amount = (amount > buyAccount.Balance / buyPrice) ? buyAccount.Balance / buyPrice : amount;
        priceRate = sellPrice / buyPrice;
        if(amount > 0){
            if(priceRate >= minRate){
                buyExchange.Buy(-1, amount);
                sellExchange.Sell(-1, amount);
                Log(date);
                Log("sell:", sellExchange.GetName(), "Selling price:", sellPrice, "buy:", buyExchange.GetName(), " Buy price:", buyPrice, " Proportion:", priceRate, " Total:", amount);
                Log('Statistics assets...');
                Log('exchange:', exchanges[0].GetName(), ' Account:', exchanges[0].GetAccount());
                Log('exchange:', exchanges[1].GetName(), ' Account:', exchanges[1].GetAccount());
            }
        }
    
}

function main(){
    while(true){
        calc(exchanges[0], exchanges[1]);
        calc(exchanges[1], exchanges[0]);
    }

}

Published by cnxzcxy on the FMZ strategy square · last modified 2018-09-12. 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 spread arbitrage engine with WebSocket order routing and leg-risk controls from strategies/Cross-Platform-Arbitrage-Beginner-Test.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