Design of an order synchronization system based on FMZ quantification (2)

Author: The Little Dream, Created: 2022-02-16 12:11:36, Updated: 2023-09-15 20:46:46

img

Design of an order synchronization system based on FMZ quantification (2)

Synchronous server is a synchronous order management system.

We continue with the previous article:Design of an order synchronization system based on FMZ quantification (1)In the meantime, we're discussing how to start designing strategies for synchronizing the lists.

Consider a few design questions:

  • 1, if you do not want to synchronize the checklist for a while, can you pause. Once paused, it is forbidden to start from the extension API and use password verification. To implement this feature, two global variables are added:

    var isStopFollow = false   // 用于标记当前是否跟单
    var reStartPwd = null      // 用于记录重启密码
    

    img

    Then, on the policy edit page, an interactive control is added to pause/restart the policy (not stop this disk, just logical pause, no more lists, nothing is done).订单同步管理系统类库(Single Server)The hard drive can no longer wake you up to your policy. When restarting the list, enter the default password to wake up the list function. Implementation code for related functionality:

    ...
              // 判断交互指令
              if (arr.length == 2) {
              	// 带控件的按钮
              	if (arr[0] == "stop/restart") {
              		// 暂停/重启跟单
              		if (!isStopFollow) {
              		    isStopFollow = true
              		    reStartPwd = arr[1]
              		    Log("已经停止跟单,", "设置的重启密码为:", reStartPwd, "#FF0000")
              		} else if (isStopFollow && arr[1] == reStartPwd) {
              			isStopFollow = false 
              			reStartPwd = null 
              			Log("已经重启跟单,", "清空重启密码。", "#FF0000")
              		} else if (isStopFollow && arr[1] != reStartPwd) {
              			Log("重启密码错误!")
              		}
              	}
              	continue 
              }
    
  • 2, you can specify the order size or multiply scale Add parameters to the policy:

    img

    specifiedAmount: Specifies the number of tracks, defaulting to -1, i.e. not specified. zoomAmountRatio: The zoom ratio of the transmitted signal depends on the amount of the transmitted signal, for example:ETH_USDT,swap,buy,1, then multiply the value of the lower unit quantity by zoomAmountRatio.

        var amount = specifiedAmount == -1 ? action.amount : specifiedAmount
        amount = zoomAmountRatio == -1 ? amount : amount * zoomAmountRatio
    

    This is the order of magnitude of the required sequence in the received signal.Scaling upOrSpecify specific values

  • 3, the code is as streamlined as possible, the sub-order is handled using other template libraries.

    This is a list of all the different ways Toys for Kids is credited in the database.https://www.fmz.com/strategy/10989This is a list of all the different ways Future Subscribe is credited in the database:https://www.fmz.com/strategy/203258

      function trade(action) {
          // 切换交易对,设置合约
          exchange.SetCurrency(action.symbol)
          if (action.ct != "spot") {
              exchange.SetContractType(action.ct)        
          }        
    
          var retTrade = null 
          var amount = specifiedAmount == -1 ? action.amount : specifiedAmount
          amount = zoomAmountRatio == -1 ? amount : amount * zoomAmountRatio    
    
          if (action.direction == "buy") {
              retTrade = action.ct == "spot" ? $.Buy(amount) : $.OpenLong(exchange, action.ct, amount)
          } else if (action.direction == "sell") {
              retTrade = action.ct == "spot" ? $.Sell(amount) : $.OpenShort(exchange, action.ct, amount)
          } else if (action.direction == "closebuy") {
              retTrade = action.ct == "spot" ? $.Sell(amount) : $.CoverLong(exchange, action.ct, amount)
          } else if (action.direction == "closesell") {
              retTrade = action.ct == "spot" ? $.Buy(amount) : $.CoverShort(exchange, action.ct, amount)
          }
          return retTrade
      }
    

    So you see, the list is just one sentence:$.Sell(amount)$.Buy(amount)$.OpenLong(exchange, action.ct, amount)And so on.

This is the code of the strategy:

Previously订单同步管理系统(Synchronous Server)The temporary code is:

img

Now start redesigning.订单同步管理系统(Synchronous Server)

// 全局变量
var isStopFollow = false
var reStartPwd = null 

function trade(action) {
    // 切换交易对,设置合约
    exchange.SetCurrency(action.symbol)
    if (action.ct != "spot") {
        exchange.SetContractType(action.ct)        
    }    

    var retTrade = null 
    var amount = specifiedAmount == -1 ? action.amount : specifiedAmount
    amount = zoomAmountRatio == -1 ? amount : amount * zoomAmountRatio

    if (action.direction == "buy") {
        retTrade = action.ct == "spot" ? $.Buy(amount) : $.OpenLong(exchange, action.ct, amount)
    } else if (action.direction == "sell") {
    	retTrade = action.ct == "spot" ? $.Sell(amount) : $.OpenShort(exchange, action.ct, amount)
    } else if (action.direction == "closebuy") {
    	retTrade = action.ct == "spot" ? $.Sell(amount) : $.CoverLong(exchange, action.ct, amount)
    } else if (action.direction == "closesell") {
    	retTrade = action.ct == "spot" ? $.Buy(amount) : $.CoverShort(exchange, action.ct, amount)
    }
    return retTrade
}

function parseCmd(cmd) {
	var objAction = {}
	// 解析cmd ,例如:ETH_USDT,swap,buy,1
    var arr = cmd.split(",")
    if (arr.length != 4) {
    	return null 
    }
    objAction.symbol = arr[0]
    objAction.ct = arr[1]
    objAction.direction = arr[2]
    objAction.amount = arr[3]
    return objAction
}

function main() {
	// 清除所有日志
    LogReset(1)  

    if (isSimulateOKEX) {
    	exchange.IO("simulate", true)
    	Log("切换到OKEX模拟盘!")
    }

    // 设置精度
    exchange.SetPrecision(pricePrecision, amountPrecision)

    // 检查缩放、指定不能同时设置
    if (specifiedAmount != -1 && zoomAmountRatio != -1) {
    	throw "不能同时指定同步量和缩放量"
    }

    while (true) {
        var cmd = GetCommand()
        if (cmd) {
            Log("cmd: ", cmd)
            var arr = cmd.split(":")

            // 判断交互指令
            if (arr.length == 2) {
            	// 带控件的按钮
            	if (arr[0] == "stop/restart") {
            		// 暂停/重启跟单
            		if (!isStopFollow) {
            		    isStopFollow = true
            		    reStartPwd = arr[1]
            		    Log("已经停止跟单,", "设置的重启密码为:", reStartPwd, "#FF0000")
            		} else if (isStopFollow && arr[1] == reStartPwd) {
            			isStopFollow = false 
            			reStartPwd = null 
            			Log("已经重启跟单,", "清空重启密码。", "#FF0000")
            		} else if (isStopFollow && arr[1] != reStartPwd) {
            			Log("重启密码错误!")
            		}
            	}
            	continue 
            }
            
            // 允许跟单
            if (!isStopFollow) {
                // 解析跟单信号交互指令
                var objAction = parseCmd(cmd)
                if (objAction) {
            	    // 解析正确
            	    var ret = trade(objAction)
                } else {
                	Log("错误的信号指令 cmd:", cmd)
                }
            }
        }
        
        // 显示跟单情况
        LogStatus(_D(), isStopFollow ? "停止同步" : "保持同步", "\n")

        Sleep(1000)
    }
}

Testing

This time the bandwidth account uses the Binance disk test, while the bandwidth account uses the OKEX account.订单同步管理系统类库(Single Server)In the templatemainThe function) ⋅

img

But we changed the direction of the transaction to zero, and the volume of the transaction changed to 0.003 ((Binance USDT local contracts can be ordered in decimal numbers)). But the OKEX account ledger must be an integer ((OKEX exchange ledger must be an integer), so the parameter I specify is a policy parameter.specifiedAmountThis is 1⁄4.

img

订单同步管理系统类库(Single Server)The real disk of the test function triggers the transaction.

img

The real-time strategy receives a signal and performs the following action:

img

The exchange issued the corresponding order.

img

Next, test the flat position, changing the direction of the bottom row in the test main function to flatten the empty position 0.003.

img

And then run this disk again, which is responsible for the tape.订单同步管理系统类库(Single Server))。

img

It also triggers the same operation on a single hard drive:

img

The policy address:Order synchronization management system class library (single server) Synchronous server is a synchronous order management system.

Strategy design is only for exchange learning, the actual production needs to be modified, adjusted, optimized.


Related

More