Thiết kế hệ thống quản lý đồng bộ dựa trên FMZ (2)

Tác giả:Ninabadass, Tạo: 2022-04-06 17:12:00, Cập nhật: 2022-04-06 17:20:08

Thiết kế hệ thống quản lý đồng bộ dựa trên FMZ (2)

Hệ thống quản lý đồng bộ đơn hàng (Server đồng bộ)

Chúng ta hãy tiếp tục cuộc thảo luận trong bài viết trước:Thiết kế hệ thống quản lý đồng bộ dựa trên lệnh FMZ (1), để thiết kế một chiến lược giám sát lệnh đồng bộ. Vui lòng xem xét các câu hỏi thiết kế sau:

  • 1.Nếu bạn không muốn giám sát đồng bộ các đơn đặt hàng trong một thời gian, có thể dừng lại không? Để thực hiện hàm này, thêm 2 biến toàn cầu:
    var isStopFollow = false   // used to mark whether to currently supervise orders or not 
    var reStartPwd = null      // used to record the restart password 
    

img

Sau đó thêm các điều khiển tương tác trên trang chỉnh sửa chiến lược để dừng / khởi động lại chiến lược (nó không phải là để dừng bot, chỉ dừng logic, không theo dõi và giám sát lệnh, mà không làm bất cứ điều gì).Order Synchronous Management System Library (Single Server)Khi khởi động lại để giám sát lệnh, nhập mật khẩu đặt trước để gọi chức năng giám sát lệnh. Mã thực hiện của chức năng liên quan:

...
          // Judge the interactive command
          if (arr.length == 2) {
          	// Buttons with controls
          	if (arr[0] == "stop/restart") {
          		// Stop/restart to supervise orders 
          		if (!isStopFollow) {
          		    isStopFollow = true
          		    reStartPwd = arr[1]
          		    Log("stopped to supervise orders,", "the set restart password is:", reStartPwd, "#FF0000")
          		} else if (isStopFollow && arr[1] == reStartPwd) {
          			isStopFollow = false 
          			reStartPwd = null 
          			Log("restarted to supervise orders,", "clear the restart password.", "#FF0000")
          		} else if (isStopFollow && arr[1] != reStartPwd) {
          			Log("Wrong restart password!")
          		}
          	}
          	continue 
          }
  • 2.Tổng lệnh của lệnh được giám sát có thể được xác định hoặc nó có thể được phóng to theo tỷ lệ:

    img

    specifiedAmount: chỉ định số tiền của lệnh được giám sát; mặc định là -1, tức là không xác định. zoomAmountRatio: zoom theo số lượng thứ tự trong tín hiệu được gửi. Ví dụ, tín hiệu được gửi là:ETH_USDT,swap,buy,1, sau đó nhân giá trị của số tiền đặt hàng bằng zoomAmountRatio; mặc định là -1, cụ thể là không được phóng to.

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

    Ở đây chúng ta đã nhận razoomsố tiền đặt hàng hoặcchỉ định một giá trị nhất định, theo tín hiệu nhận được.

  • 3.Viết mã đơn giản nhất có thể, và sử dụng các thư viện mẫu khác để xử lý đặt hàng.

    Thư viện mẫu được sử dụng để đặt đơn đặt hàng tại chỗ:https://www.fmz.com/strategy/10989Thư viện mẫu được sử dụng để đặt lệnh tương lai:https://www.fmz.com/strategy/203258

      function trade(action) {
          // Switch the trading pair, and set contract 
          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
      }
    

    Do đó, có thể thấy rằng đặt một lệnh chỉ cần một tuyên bố:$.Sell(amount), $.Buy(amount), $.OpenLong(exchange, action.ct, amount), vv

Mã chiến lược:

Mã tạm thời trongOrder Synchronous Management System (Synchronous Server)là như sau:

img

Bây giờ, hãy thiết kếOrder Synchronous Management System (Synchronous Server)Một lần nữa:

// Global variables
var isStopFollow = false
var reStartPwd = null 

function trade(action) {
    // Switch the trading pair, and set contract
    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 = {}
	// Parse cmd, such as: 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() {
	// Clear all logs 
    LogReset(1)  

    if (isSimulateOKEX) {
    	exchange.IO("simulate", true)
    	Log("Switch to OKEX simulated bot!")
    }

    // set precision 
    exchange.SetPrecision(pricePrecision, amountPrecision)

    // Check specifiedAmount and zoomAmountRatio, for they cannot be set at the same time 
    if (specifiedAmount != -1 && zoomAmountRatio != -1) {
    	throw "cannot set specifiedAmount and zoomAmountRatio at the same time"
    }

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

            // Judge the interactive command 
            if (arr.length == 2) {
            	// Buttons with controls 
            	if (arr[0] == "stop/restart") {
            		// Stop/restart to supervise orders 
            		if (!isStopFollow) {
            		    isStopFollow = true
            		    reStartPwd = arr[1]
            		    Log("stopped to supervise orders,", "the set restart password is:", reStartPwd, "#FF0000")
            		} else if (isStopFollow && arr[1] == reStartPwd) {
            			isStopFollow = false 
            			reStartPwd = null 
            			Log("restarted to supervise orders,", "Clear the restart password", "#FF0000")
            		} else if (isStopFollow && arr[1] != reStartPwd) {
            			Log("Wrong restart password!")
            		}
            	}
            	continue 
            }
            
            // Allow to supervise orders 
            if (!isStopFollow) {
                // Parse the interactive command of the order supervising signal
                var objAction = parseCmd(cmd)
                if (objAction) {
            	    // Parse correctly 
            	    var ret = trade(objAction)
                } else {
                	Log("Wrong signal cmd:", cmd)
                }
            }
        }
        
        // Display the order supervising status 
        LogStatus(_D(), isStopFollow ? "Stop synchronization" : "Maintain synchronization", "\n")

        Sleep(1000)
    }
}

Kiểm tra

Lần này, Binance thử nghiệm tick thực được sử dụng cho tài khoản với lệnh, và tài khoản OKEX được sử dụng cho lệnh giám sát bot.mainchức năng trongOrder Synchronous Management System Library (Single Server)mẫu).

img

Nó chỉ là chúng tôi đã thay đổi hướng giao dịch để ngắn, và khối lượng giao dịch đã được thay đổi thành 0,003 (Binance USDT ký hợp đồng ký quỹ có thể được đặt theo số thập phân). Tuy nhiên, tài khoản OKEX với lệnh phải là một số nguyên (lệnh đặt bởi OKEX phải là một số nguyên), vì vậy các tham số tôi chỉ định các tham số chiến lượcspecifiedAmountlà 1.

img

Các bot của chức năng kiểm tra trongOrder Synchronous Management System Library (Single Server)đã kích hoạt giao dịch.

img

Các lệnh giám sát chiến lược bot nhận được tín hiệu, và thực hiện hành động giám sát:

img

Nền tảng đã mở lệnh tương ứng.

img

Tiếp theo, kiểm tra các vị trí đóng, và thay đổi hướng lệnh trong chức năng chính để đóng vị trí ngắn, 0,003.

img

Sau đó khởi động lại bot chịu trách nhiệm thực hiện lệnh (Order Synchronous Management System Library (Single Server)).

img

Hoạt động tương tự cũng được kích hoạt trong lệnh giám sát bot:

img

Địa chỉ chiến lược:Đặt hàng thư viện hệ thống quản lý đồng bộ (Máy chủ duy nhất) Hệ thống quản lý đồng bộ đơn hàng (Server đồng bộ)

Những chiến lược đó chỉ được sử dụng để giao tiếp và nghiên cứu; để thực sự sử dụng, bạn cần phải sửa đổi, điều chỉnh và tối ưu hóa chúng bởi chính mình.


Thêm nữa