0bps

Strategy vault / Other

Perpetual Contract One Click Market Price Closing

One-click to close all perpetual contract positions

JavaScriptOther
Author
GCC
Complexity
Starter · 34 lines
Parameters
0

One-click to close all perpetual contract positions

Source JavaScript

function main(){
        exchange.SetContractType('swap')
        exchange.SetCurrency('BNB_USDT')
        Log(exchange.GetPosition())
        acc = exchange.GetAccount()
        //Log(exchange.GetAccount().Info)
        var pos = exchange.GetAccount().Info.positions
        var positioned = false
        for(var i=0;i<pos.length;i++){
            if (pos[i].positionAmt != 0){
                positioned = true
                var symbol = pos[i].symbol.replace('USDT','_USDT')
                //var symbol = pos[i].symbol.replace('BUSD','_BUSD')
                exchange.SetCurrency(symbol)
                var amount = parseFloat(pos[i].positionAmt.replace('-',''))
                if(pos[i].positionSide == 'LONG'){
                    exchange.SetDirection('closebuy')
                    exchange.Sell(-1, amount)
                }
                if(pos[i].positionSide == 'SHORT'){
                    exchange.SetDirection('closesell')
                    exchange.Buy(-1, amount)
                }
            }
        }
        if(positioned == false){
            Log('No position')
            return ('No position')
        }
        else{
            Log('Complete position closing')
            return ('Complete position closing')
        }
}

Published by GCC on the FMZ strategy square · last modified 2022-03-25. 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/Perpetual-Contract-One-Click-Market-Price-Closing.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