There are a lot of friends in the cryptocurrency circle who need an order supervising bot, but they are suffering from the long time to get started with programmatic design when they have zero basic. Based on such needs, in this article, we will design a simple spot order supervising bot to help those friends with zero basic to learn program trading.

First of all, let's analyze the requirement. The function of this bot is to realize that when an account has a buy or sell operation, other accounts of order supervising will follow and execute the operation too.
So, we first define 2 subjects:

  • Reference account: the monitored account, each of whose actions is monitored.
  • Order supervising account: the order supervising account; when the reference account has an action, the order supervising account will perform the same action.

After this requirement has been initially clarified, we continue to think about the next step. How can we identify every action of the reference account?

Monitor the reference account. It is very simple for the spot trading accounts. We only need to compare the symbol amount in the latest account information data with the current symbol amount in the latest account information data obtained.
If the symbol amount in the latest account information data currently obtained is more than the previous record, it proves that the reference account has performed a buy operation and the execution was successful. Conversely, if there are fewer symbols, a sell operation is performed for the reference account. After we detect the action, let other platform accounts perform the same operation.

When it is found that the reference account has performed a trade, the latest account data record must be updated to compare it with the account information obtained next time to determine whether there is a new trading action.

The strategy code description of the logic above:

        // detect order supervising
        var amount = (nowAcc.Stocks + nowAcc.FrozenStocks) - (initAcc.Stocks + initAcc.FrozenStocks)  // detect the currency amount changes 
        var func = null 
        if (amount > 0) {   // the amount increased 
            func = $.Buy    // buy
        } else if (amount < 0) {  // the amount decreased 
            func = $.Sell         // sell 
        } else {
            continue
        }

        // execute order supervising 
        Log("Order supervising! Amount:", Math.abs(amount), "#FF0000")
        for (var i = 1 ; i < exchanges.length ; i++) {    // when i equals 0, it indicates the reference platform, not to be processed; process other order supervising platforms 
            func(exchanges[i], Math.abs(amount))          // execute the specified trading function, and it could be $.Buy or $.Sell, which needs to be determined by observing whether amount is larger than 0 or less than 0
        }

        // update the information record of the reference account after order supervising 
        initAcc = nowAcc                                  // update the latest account information of the reference platform, to compare it with the next one 

The main detection logic of the strategy is the above code. In order to simplify the design, the strategy uses the official FMZ [Digital Currency Trading Library] template, and $.Buy and $.Sell are all functions of the template, the function is to execute the order operations.

Add some status bar displays to the strategy to facilitate monitoring the data of each account. The complete strategy is as follows:

function test() { 
    // test function 
    var ts = new Date().getTime()    
    if (ts % (1000 * 60 * 60 * 6) > 1000 * 60 * 60 * 5.5) {
        Sleep(1000 * 60 * 10)
        var x = Math.random()
        if (x > 0.5) {
            $.Buy(exchange, x / 10)    
        } else {
            $.Sell(exchange, x / 10)    
        }        
    }
}

function main() {
    LogReset(1)
    if (exchanges.length < 2) {
        throw "no platform of order supervising"
    }
    var exName = exchange.GetName()
    // detect the reference platform 
    if (exName.includes("Futures_")) {
        throw "only support sport order supervising"
    }
    Log("start monitoring", exName, "platform", "#FF0000")

    // detect the order supervising platforms 
    for (var i = 1 ; i < exchanges.length ; i++) {
        if (exchanges[i].GetName().includes("Futures_")) {
            throw "Do not support the order supervising of futures platfroms"
        }
    }

    var initAcc = _C(exchange.GetAccount)
    while(1) {
        if(IsVirtual()) {
           // test function
           test()  
        }  
        Sleep(5000)

        // update the current information of the reference account 
        var nowAcc = _C(exchange.GetAccount)

        // the account information of the reference platform
        var refTbl = {
            type : "table", 
            title : "reference platform",
            cols : ["name", "symbol", "frozen symbol", "assets", "frozen assets"],
            rows : []
        }
        refTbl.rows.push([exName, nowAcc.Stocks, nowAcc.FrozenStocks, nowAcc.Balance, nowAcc.FrozenBalance])

        // the account information of the order supervising platform
        var followTbl = {
            type : "table", 
            title : "order supervising platform",
            cols : ["name", "symbol", "frozen symbol", "assets", "frozen assets"],
            rows : []        
        }
        for (var i = 1 ; i < exchanges.length ; i++) {
            var acc = _C(exchanges[i].GetAccount)
            var name = exchanges[i].GetName()
            followTbl.rows.push([name, acc.Stocks, acc.FrozenStocks, acc.Balance, acc.FrozenBalance])
        }

        // status bar display 
        LogStatus(_D(), "\n`" + JSON.stringify(refTbl) + "`", "\n`" + JSON.stringify(followTbl) + "`")

        // detect order supervising 
        var amount = (nowAcc.Stocks + nowAcc.FrozenStocks) - (initAcc.Stocks + initAcc.FrozenStocks)
        var func = null 
        if (amount > 0) {
            func = $.Buy
        } else if (amount < 0) {
            func = $.Sell
        } else {
            continue
        }

        // execute order supervising 
        Log("Order supervising! Amount:", Math.abs(amount), "#FF0000")
        for (var i = 1 ; i < exchanges.length ; i++) {            
            func(exchanges[i], Math.abs(amount))
        }

        // update the information record of the reference account after order supervising 
        initAcc = nowAcc
    }
}

Let's test it on the real bot, by FMZ wexApp simulation platfrorm to test. Here I have added three wexApp accounts, all of which are independent accounts. One of them serves as a reference platform, and the other two serve as order supervising platforms.

/upload/asset/16286a598cc40038b802.png

Then, we manually placed an order by FMZ trading terminal to see if the bot could automatically supervise the order.

/upload/asset/16c0ee0f39d082411d16.png

You can see that the bot detected the trade and performed the order supervising operation.

/upload/asset/1708d6512b1fb4fd3a76.png

Complete Strategy: https://www.fmz.com/strategy/255182

The strategy is only for study. When there is any question, we would appreciate it if you leave comments about it.

Leave a Reply

Your email address will not be published. Required fields are marked *