0bps

Strategy vault / Tools & templates

Liquidity Exit Tool

JavaScript strategy by kongbai979.

Author
kongbai979
Complexity
Advanced · 358 lines
Parameters
5

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

Parameters

NameDefaultWhat it controls
a0.002Minimum order quantity per transaction, how many unitsBTC
k11Risk coefficient k, the recommended value is 10-50. The smaller the value, the higher the risk and the greater the return.
g2100Leverage multiple
xxfalseThis is a counter
pc50Close position value

Source JavaScript

function xiadan() { //Order Function

    exchange.SetPrecision(2, 3)  
    Sleep(51)
    exchange.SetContractType("swap")  
    Sleep(51)
    exchange.SetMarginLevel(g2)  
    Sleep(51)
    var fff000 = exchange.GetPosition()  
    if (fff000.length == 0) {
        var records = exchange.GetRecords(PERIOD_D1)
        var records1 = records[records.length - 1].High - records[records.length - 1].Low
        var records2 = records[records.length - 2].High - records[records.length - 2].Low
        var records3 = records[records.length - 3].High - records[records.length - 3].Low
        var records4 = records[records.length - 4].High - records[records.length - 4].Low
        var records5 = records[records.length - 5].High - records[records.length - 5].Low
        var recordsn = [records1, records2, records3, records4, records5]
        yh = 0
        for (var i = 0; i < recordsn.length; i++) {
            if (recordsn[i] > 5) {
                yh = yh + 1  
            }
        }
        if (records1 > 20) {
            Sleep(51)
            exchange.SetDirection("buy")        
            Sleep(60)
            var id = exchange.Buy(-1, a)    
            Sleep(500)
            Log("Open position, direction is long, intelligent recognition, my choice won't be wrong! Go for it!")
        } else {
            exchange.SetDirection("sell")      
            Sleep(51)
            var id = exchange.Sell(-1, a)   
            Sleep(500)
            Log("Open a position, with a short direction. Smart recognition-my choice is right! Keep it up!")
        }


        var oder = exchange.GetOrder(id)  
        var j = oder.AvgPrice  
        var ying = j + k
        var sun = j - k



    } else {
        var jjjj = fff000[0].Price  
        var ying = jjjj + k
        var sun = jjjj - k
    }
    var t = 0
    Sleep(60)



    Sleep(100)
    /*↓ Secondary loop ↓*/
    while (true) {


        Sleep(60)
        zhanghu2 = _C(exchange.GetAccount) 

        Sleep(80)
        m100 = exchange.GetAccount()
        v1 = m100.Info
        v2 = v1.assets
        v3 = v2[1].walletBalance         

        LogProfit(_N(v3 - (_G("v4")), 2), 'USDT')

        var yu2 = zhanghu2.Balance  
        Sleep(70)
        /*↓ The following determines whether there is a position, if not, the loop will jump out. ↓*/
        var fff = exchange.GetPosition()    
        var eee = 66
        if (fff.length != 0) {                        
            var cc = fff[0].Amount  
            var pp = fff[0].Price  
            var ww = fff[0].Type    
            t = t + 1  
            if (t > 2) {
                var tx = ((t * 0.0008 * a) / cc) * pp
                var td = (t * k * a) / cc
            } else {
                var tx = 0
                var td = 0
            }
            var lirun = (parseInt(cc / a)) * k / 2
            if (yu2 > (cc / g2)) {
                /*↓ Up and down pending orders in the case of long positions ↓*/
                if (ww == 0) { //If the position is long



                    while (true) {
                        var recordss = exchange.GetRecords(60)
                        var records11 = recordss[recordss.length - 1].High - recordss[recordss.length - 1].Low
                        Sleep(60)

                        if (records11 < 0) {
                            Sleep(3000)
                        } else {


                            if (cc > pc * a) {
                                Sleep(100)
                                exchange.SetDirection("closebuy")    
                                var sellid = exchange.Sell(-1, cc)   
                                Log("Haiyan, you should be more careful; risk control triggered, reopen position")
                                Sleep(10000)
                                break
                            } else if (cc > a) {

                                Sleep(100)
                                exchange.SetDirection("closebuy")  
                                Sleep(100)
                                var sellid = exchange.Sell(pp + k + tx + lirun, cc - a)  
                                Sleep(100)
                                exchange.SetDirection("buy")  
                                Sleep(100)
                                var buyid = exchange.Buy(sun, a)    
                                Sleep(100)
                                if (cc > 2 * a) {
                                    exchange.SetDirection("closebuy")  
                                    Sleep(100)
                                    var abc = exchange.Sell(ying, a)    
                                    var eee = 88
                                    Sleep(100)
                                }
                                /*else if ((pp + k + tx + lirun) > ying + k) {
                                    exchange.SetDirection("closebuy")  
                                    Sleep(100)
                                    var abc = exchange.Sell(ying, a)     
                                    var eee = 88
                                    Sleep(100)
                                }*/
                                xx = (xx + 1)
                                var zye = _N(v3 - 0, 2)
                                Log("This is the", xx, "Times to place an order", "Total account balance:", zye, "USDT", "Increase risk coefficient to", _N(tx, 2), "Decrease risk coefficient to", _N(td, 2), "Number of transactions in this position building", t, "Time:", _D())

                            } else {
                                Sleep(100)
                                exchange.SetDirection("closebuy")  
                                Sleep(100)
                                var sellid = exchange.Sell(pp + k, cc) 
                                Sleep(100)
                                exchange.SetDirection("buy")  
                                Sleep(100)
                                var buyid = exchange.Buy(sun, a)    
                                Sleep(100)
                                xx = (xx + 1)
                                var zye = _N(v3 - 0, 2)
                                Log("This is the", xx, "Times to place an order", "Total account balance:", zye, "USDT", "Increase risk coefficient to", _N(tx, 2), "Decrease risk coefficient to", _N(td, 2), "Number of transactions in this position building", t, "Time:", _D())
                            }


                            break
                        }
                        Sleep(2000)
                    }






                    /*↓ Up and down orders in case of short position ↓*/
                } else if (ww == 1) {  






                    while (true) {
                        var recordsss = exchange.GetRecords(60)
                        var records111 = recordsss[recordsss.length - 1].High - recordsss[recordsss.length - 1].Low
                        Sleep(60)
                        if (records111 > 0) {
                            Sleep(3000)
                        } else {


                            if (cc > pc * a) {
                                Sleep(100)
                                exchange.SetDirection("closesell")  
                                Sleep(100)
                                var buyid = exchange.Buy(-1, cc)  
                                Log("Haiyan, you should be more careful; risk control triggered, reopen position")
                                Sleep(10000)
                                break

                            } else if (cc > a) {

                                Sleep(100)
                                exchange.SetDirection("closesell")  
                                Sleep(100)
                                var buyid = exchange.Buy(pp - k - tx - lirun, cc) 
                                Sleep(100)
                                exchange.SetDirection("sell")
                                Sleep(100)
                                var sellid = exchange.Sell(ying, a)   
                                Sleep(100)

                                if (cc > 2 * a) {
                                    exchange.SetDirection("closesell")  
                                    Sleep(100)
                                    var abc = exchange.Buy(sun, a)    
                                    Sleep(100)
                                    var eee = 88
                                }
                                /*else if ((pp - k - tx - lirun) < sun - k) {
                                    exchange.SetDirection("closesell") 
                                    Sleep(100)
                                    var abc = exchange.Buy(sun, a)    
                                    Sleep(100)
                                    var eee = 88
                                }*/
                                xx = (xx + 1)
                                var zye = _N(v3 - 0, 2)
                                Log("This is the", xx, "Times to place an order", "Total account balance:", zye, "USDT", "Increase risk coefficient to", _N(tx, 2), "Decrease risk coefficient to", _N(td, 2), "Number of transactions in this position building", t, "Time:", _D())
                            } else {
                                Sleep(100)
                                exchange.SetDirection("closesell")  
                                Sleep(100)
                                var buyid = exchange.Buy(pp - k, cc)  
                                Sleep(100)
                                exchange.SetDirection("sell")
                                Sleep(100)
                                var sellid = exchange.Sell(ying, a)   
                                Sleep(100)
                                xx = (xx + 1)
                                var zye = _N(v3 - 0, 2)
                                Log("This is the", xx, "Times to place an order", "Total account balance:", zye, "USDT", "Increase risk coefficient to", _N(tx, 2), "Decrease risk coefficient to", _N(td, 2), "Number of transactions in this position building", t, "Time:", _D())
                            }

                            break
                        }
                        Sleep(2000)
                    }


                }
            } else {
                Log("Insufficient balance, remaining is", yu2, "USDT", "@")
                Sleep(1000000)
                break
            }
            /*↓ Three-level loop ↓*/
            while (true) {

         
                Sleep(1500)
                var m1 = exchange.GetOrder(sellid) 
                var m2 = m1.Status 
                var jg1 = m1.AvgPrice  
                Sleep(1500)
                var m3 = exchange.GetOrder(buyid)  
                var m4 = m3.Status 
                var jg2 = m3.AvgPrice
                Sleep(1500)

                if (eee == 88) {
                    Sleep(100)
                    var m5 = exchange.GetOrder(abc) 
                    var m6 = m5.Status  
                    var jg3 = m5.AvgPrice
                    Sleep(60)
                }
                if (m2 == 1) {  
                    if (m4 == 0) {
                        exchange.CancelOrder(buyid)
                    }  
                    if (m6 == 0) {
                        exchange.CancelOrder(abc)
                    }
                    ying = jg1 + k
                    sun = jg1 - k
                    m2 = 8
                    m4 = 8


                                       
                    break




                } else if (m4 == 1) { 
                    exchange.CancelOrder(sellid)   
                    if (m6 == 0) {
                        exchange.CancelOrder(abc)
                    }
                    sun = jg2 - k
                    ying = jg2 + k
                    m2 = 8
                    m4 = 8


                  
                    break
                } else if (m6 == 1 && eee == 88) { //If this newly added order is completed
                    if (m2 == 0) {
                        exchange.CancelOrder(sellid)
                       


                    }

                    if (m4 == 0) {
                        exchange.CancelOrder(buyid)
                
                    }  
                    sun = jg3 - k
                    ying = jg3 + k
                    m2 = 8
                    m4 = 8
                    break
                }



            }
        } else {
            break
        }
    }
}

function main() {
    LogReset()
    LogProfitReset()
    var m100 = exchange.GetAccount()
    var v1 = m100.Info
    var v2 = v1.assets
    var v3 = v2[1].walletBalance  
    //var v4 =v3
    _G("v4", v3)



    while (true) {
        var zhanghu = _C(exchange.GetAccount)  
        var yu = zhanghu.Balance // USDTAvailable balance
        var yu1 = zhanghu.Stocks //BTCHolding balance
        Sleep(1000)
        var hang = exchange.GetTicker()
        var zui = hang.Last //Get current latest price  
        Sleep(500)
        if (yu > (zui * a) / g2) { //Conditional check
            xiadan()         
        } else {
            Log("Insufficient balance, balance is", yu, "Required balance is", zui * a, "@")
        }
    }
}

Published by kongbai979 on the FMZ strategy square · last modified 2020-03-26. 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 tested, production-ready module from strategies/Liquidity-Exit-Tool.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