Implementation of a simple digital currency cash register robot

Author: The Little Dream, Created: 2021-02-18 16:09:43, Updated: 2023-09-26 20:57:30

img

Implementation of a simple digital currency cash register robot

There are many people in the coin circle who need to follow the robot manually, but it also takes a long time to get started with programmed design when they are struggling with the basics. Based on such needs, we have designed a simple spot-following robot in this article to help 0 basic learn programmed trading.

First, let's analyze the demand, the function of this robot is to make sure that when one account is bought and sold, the other accounts follow suit. So, first of all, let's clarify two subjects:

  • Reference account: monitored account, monitor this account step by step.
  • Follow-up account: Follow-up account, when a reference account takes action, the follow-up account performs the same action.

We've already identified this need, and we'll continue to think about it. How do we identify a reference account step by step?

Monitoring a reference account is very simple for a cash account, we can simply compare the number of coins in the most recent account information data with the number of coins in the most recent account information data currently available. If the number of coins in the latest account information data obtained is more than the previous record, it proves that the reference account executed a buy operation and the purchase was successful. Conversely, if the amount of coins is less, a sell operation is executed for the reference account. After we discover this action, we can have other exchange accounts perform the same operation.

When a reference account is found to have executed a transaction, be sure to update the most recent account data record to compare with the next account information obtained to determine whether there is a new transaction activity.

The following logical strategy code describes:

        // 检测跟单
        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
        }
        
        // 执行跟单
        Log("跟单!数量:", Math.abs(amount), "#FF0000")
        for (var i = 1 ; i < exchanges.length ; i++) {    // i 等于 0 时,是参考交易所,不处理,处理其它的跟单交易所        
            func(exchanges[i], Math.abs(amount))          // 执行具体的交易函数,可能是$.Buy 或者是 $.Sell ,具体看amount 是大于0,还是小于0
        }
        
        // 执行跟单之后更新参考交易所账户信息记录
        initAcc = nowAcc                                  // 更新参考交易所最近一次的账户信息,用于下次对比

The main detection logic of the strategy is the above code. For simplicity, the strategy uses the official FMZ's cryptocurrency trading library template.$.Buy , $.SellAll of these are functions of this template, the function of which is to execute the following operation.

Adding a few status bars to the policy to help monitor the data of each account, the complete policy is as follows:

function test() { 
    // 测试函数
    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 "没有跟单的交易所"
    }
    var exName = exchange.GetName()
    // 检测参考交易所
    if (exName.includes("Futures_")) {
        throw "仅支持现货跟单"
    }
    Log("开始监控", exName, "交易所", "#FF0000")
    
    // 检测跟单交易所
    for (var i = 1 ; i < exchanges.length ; i++) {
        if (exchanges[i].GetName().includes("Futures_")) {
            throw "不支持期货交易所跟单"
        }
    }
    
    var initAcc = _C(exchange.GetAccount)
    while(1) {
        if(IsVirtual()) {
           // 测试函数
           test()  
        }  
        Sleep(5000)
        
        // 更新参考账户当前的账户信息
        var nowAcc = _C(exchange.GetAccount)
        
        // 参考交易所账户信息
        var refTbl = {
            type : "table", 
            title : "参考交易所",
            cols : ["名称", "币", "冻结币", "钱", "冻结钱"],
            rows : []
        }
        refTbl.rows.push([exName, nowAcc.Stocks, nowAcc.FrozenStocks, nowAcc.Balance, nowAcc.FrozenBalance])
        
        // 跟单交易所账户信息
        var followTbl = {
            type : "table", 
            title : "跟单交易所",
            cols : ["名称", "币", "冻结币", "钱", "冻结钱"],
            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])
        }
        
        // 状态栏显示
        LogStatus(_D(), "\n`" + JSON.stringify(refTbl) + "`", "\n`" + JSON.stringify(followTbl) + "`")
        
        // 检测跟单
        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
        }
        
        // 执行跟单
        Log("跟单!数量:", Math.abs(amount), "#FF0000")
        for (var i = 1 ; i < exchanges.length ; i++) {            
            func(exchanges[i], Math.abs(amount))
        }
        
        // 执行跟单之后更新参考交易所账户信息记录
        initAcc = nowAcc
    }
}

Let's do a real-world test, using FMZ's wexApp simulated exchange test. Here I added three wexApp accounts, all independent of each other. One of them serves as a reference exchange, the other two as a tracking exchange.

img

Then we use FMZ's trading terminal to manually order the next order and see if the robot will automatically check it.

img

You can see that the robot detects the transaction and performs the ordering operation.

img

The full strategy:https://www.fmz.com/strategy/255182

The strategy is for learning only, if you have any questions, thank you for commenting.


Related

More

The SyriansHello, can you help with the deployment, please pay for help. Thank you.

The SyriansHello, can you help with the deployment, please pay for help. Thank you.

The fat kid.Is it possible to detect the hanging list?

13169222039Hello, I'd like to ask you if you can get this person code on the coin.

The Little DreamThe policy is open source and can be found on Strategy Square, where you can view the tutorials put up by the FMZ community. The primary tutorials are for teaching use and will be available for basic deployment.

rootmeThere is a delay on the actual coin, and your price may be a lot different from theirs.