FMZ 퀀트를 기반으로 하는 주문 동기화 관리 시스템의 설계 (2)

저자:리디아, 창작: 2022-11-08 11:34:20, 업데이트: 2023-09-15 20:48:46

img

FMZ 퀀트를 기반으로 하는 주문 동기화 관리 시스템의 설계 (2)

주문 동기화 관리 시스템 (동시 서버)

이전 기사에서 토론을 계속해 봅시다: FMZ 정량화 기반의 주문 동기화 관리 시스템의 설계 (1) (https://www.fmz.com/digest-topic/9729) 를 통해 순서 순환을 위한 전략 수립을 시작해야 합니다.

몇 가지 디자인 문제를 생각해보세요.

    1. 만약 당신이 일시적으로 동기화 명령어-추천을 수행하고 싶지 않다면, 중지 할 수 있습니까? 일단 중단되면 확장 된 API에서 시작하는 것은 금지되어 있으며 암호로 인증되어야합니다. 이 함수를 2개의 글로벌 변수를 더하여 구현합니다.
var isStopFollow = false   // Used to mark whether the current order is being followed
var reStartPwd = null      // Used to record the restart password

img

그 다음 전략 편집 페이지에 전략 중지 / 재시작에 대한 인터랙티브 컨트롤을 추가합니다 (실제 봇을 중지하지 않고 논리 중지, 더 이상 명령어를 따르지 않습니다). 우리는 중지 할 때 일시 중지 암호를 설정할 수 있습니다.Order Synchronization Management System Class Library (Single Server)끝, 그것은 당신의 전략을 깨울 수 없습니다. 명령어-따르는 기능을 다시 시작할 때, 명령어-따르는 기능을 깨우치기 위해 미리 설정된 암호를 입력합니다. 관련 기능의 실행 코드:

...
          // Judge the interaction command
          if (arr.length == 2) {
          	// Buttons with controls
          	if (arr[0] == "stop/restart") {
          		// Pause/restart order-following
          		if (!isStopFollow) {
          		    isStopFollow = true
          		    reStartPwd = arr[1]
          		    Log("it has stopped the order-following,", "Set the restart password as:", reStartPwd, "#FF0000")
          		} else if (isStopFollow && arr[1] == reStartPwd) {
          			isStopFollow = false 
          			reStartPwd = null 
          			Log("it has restarted the order-following, ", "Clear the restart password.", "#FF0000")
          		} else if (isStopFollow && arr[1] != reStartPwd) {
          			Log("Restart password error!")
          		}
          	}
          	continue 
          }
    1. 당신은 따라야 하는 명령의 양을 지정하거나 배수로 확장할 수 있습니다 전략에 매개 변수를 추가합니다:

img

specifiedAmount: 순서를 따르는 수를 지정합니다. 기본값은 -1입니다. 즉, 지정되지 않습니다. zoomAmountRatio: 전송된 주문의 양에 따라 스케일링, 예를 들어, 전송 신호가: ETH_USDT,swap,buy,1, zoomAmountRatio로 주문 금액의 값을 곱하면. 기본값은 -1, 즉 스케일링이 없습니다.

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

여기서는 수신 신호에서 따라야 할 명령의 양에 대한 특정 값을 스케일하거나 지정하기 위해 구현됩니다.

    1. 코드는 가능한 한 간단해야 합니다. 주문을 하기 위해 다른 템플릿 라이브러리를 사용하세요.

스팟 주문에 의해 사용되는 클래스 라이브러리:https://www.fmz.com/strategy/10989미래의 주문에 의해 사용되는 클래스 라이브러리:https://www.fmz.com/strategy/203258

  function trade(action) {
      // Switch trading pairs and set up contracts
      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
  }

그래서 우리는 주문을 할 때 단 하나의 문장이 필요하다는 것을 볼 수 있습니다.$.Sell(amount), $.Buy(amount), $.OpenLong(exchange, action.ct, amount). 등등

전략 코드:

이전의 임시 코드Order Synchronous Management System (Synchronous Server)다음과 같습니다.

img

이제 순서 동기화 관리 시스템 (동시 서버) 의 재설계를 시작합니다:

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

function trade(action) {
    // Switch trading pairs and set up contracts
    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 demo!")
    }

    // Set accuracy
    exchange.SetPrecision(pricePrecision, amountPrecision)

    // Check zoom and specify it cannot be set at the same time
    if (specifiedAmount != -1 && zoomAmountRatio != -1) {
    	throw "it cannot specify simultaneous volume and scaling volume at the same time"
    }

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

            // Judge interaction commands
            if (arr.length == 2) {
            	// Buttons with controls
            	if (arr[0] == "stop/restart") {
            		// Pause/restart order-following
            		if (!isStopFollow) {
            		    isStopFollow = true
            		    reStartPwd = arr[1]
            		    Log("it has stopped the order-following.", "Set the restart password as.", reStartPwd, "#FF0000")
            		} else if (isStopFollow && arr[1] == reStartPwd) {
            			isStopFollow = false 
            			reStartPwd = null 
            			Log("it has restarted the order-following", "Clear the restart password.", "#FF0000")
            		} else if (isStopFollow && arr[1] != reStartPwd) {
            			Log("Restart password error!")
            		}
            	}
            	continue 
            }
            
            // Permission to follow orders
            if (!isStopFollow) {
                // Resolve the interaction instructions of order-following signal
                var objAction = parseCmd(cmd)
                if (objAction) {
            	    // The analysis is correct
            	    var ret = trade(objAction)
                } else {
                	Log("Wrong signal command cmd:", cmd)
                }
            }
        }
        
        // Display order-following status
        LogStatus(_D(), isStopFollow ? "Stop Synchronization" : "Keep Synchronization", "\n")

        Sleep(1000)
    }
}

테스트

우리는 이 시간 동안 Binance 실제 봇을 사용하여 주문을 주도하는 계정을 테스트하고, 우리는 주문을 따르는 실제 봇을 위해 OKEX 계정을 사용합니다.main테스트 기능의 함수(Order Synchronization Management System Class Library (Single Server)전 기사에서 사용된 템플릿)

img

여기서 우리는 거래 방향을 sell로 변경하고 거래 부피를 0.003로 변경합니다. (Binance USDT 표준 계약에 대한 주문은 소수로 배치 될 수 있습니다.) 그러나 OKEX 계정에 대한 주문 후속은 정수 숫자가어야합니다. (OKEX 교환 주문은 정수여야합니다.), 따라서 specifiedAmount의 전략 매개 변수를 1로 지정합니다.

img

다음으로, 0.003로 짧은 위치를 닫기 위해 테스트 주 함수에서 순서 방향을 변경하여 포지션을 닫는 테스트를 해 봅시다.

img

다음으로 다시 실행합니다. 명령어 리딩 (오더 동기화 관리 시스템 클래스 라이브러리 (싱글 서버)) 에 책임이 있습니다.

같은 작업은 명령에 따라 실제 로봇에 의해 발동되었습니다.

전략 주소: 주문 동기화 관리 시스템 클래스 라이브러리 (단독 서버) (https://www.fmz.com/strategy/345171) 주문 동기화 관리 시스템 (동시 서버) (https://www.fmz.com/strategy/345172)

이 전략은 의사소통과 학습을 위해만 설계되어 있습니다. 실제 필요에 따라 조정하고 최적화하십시오.


관련

더 많은