0bps

Strategy vault / Other

Binance buy and sell statistics

Records active transaction data, reflecting the activity level of buy and sell orders.

JavaScriptOther
Author
小草
Complexity
Starter · 35 lines
Parameters
2

Records active transaction data, reflecting the activity level of buy and sell orders.

Parameters

NameDefaultWhat it controls
Interval600Log interval(second)
Pairbtcusdttrading pair

Source JavaScript

var tradeHistory = {buyData:{amount:0, money:0}, sellData:{amount:0, money:0}}
if(_G('tradeHistory')){
    tradeHistory = _G('tradeHistory')
}
function onexit(){
    Log('exit,saving data...')
    _G('tradeHistory', tradeHistory)
}
function main(){
    var client = Dial("wss://stream.binance.com:9443/ws/" + Pair.toLowerCase() + "@trade", 60)
    var updateTime = new Date().getTime()
    while(true){
        var trade = JSON.parse(client.read())
        if(trade.m){
            tradeHistory.sellData.amount += parseFloat(trade.q)
            tradeHistory.sellData.money += parseFloat(trade.q) * parseFloat(trade.p)
        }else{
            tradeHistory.buyData.amount += parseFloat(trade.q)
            tradeHistory.buyData.money += parseFloat(trade.q) * parseFloat(trade.p)
        }
        var buyNet = _N(tradeHistory.buyData.money - tradeHistory.sellData.money, 3)
        logSting = 'active buy amount:' + _N(tradeHistory.buyData.amount, 3) + '  total money: ' + _N(tradeHistory.buyData.money, 3) + '\n'
        logSting += 'active sell amount:' + _N(tradeHistory.sellData.amount, 3) + '  total money: ' + _N(tradeHistory.sellData.money, 3) + '\n'
        logSting += 'Net inflow of funds: ' + _N(tradeHistory.buyData.money - tradeHistory.sellData.money, 3)
        LogStatus(logSting)
        if(new Date().getTime() - updateTime > Interval*1000){
            updateTime = new Date().getTime()
            LogProfit(buyNet)
            $.PlotLine('buyNet', buyNet)
            $.PlotLine('buy', _N(tradeHistory.buyData.money,3))
            $.PlotLine('sell', _N(tradeHistory.sellData.money,3))
            Log(logSting)
        }
    }
}

Published by 小草 on the FMZ strategy square · last modified 2019-07-03. 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/Binance-buy-and-sell-statistics.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