Konstruktion eines auf FMZ Quant basierenden Systems zur Verwaltung der Auftragssynchronisierung (2)

Schriftsteller:Lydia., Erstellt: 2022-11-08 11:34:20, Aktualisiert: 2023-09-15 20:48:46

img

Konstruktion eines auf FMZ Quant basierenden Systems zur Verwaltung der Auftragssynchronisierung (2)

System zur Verwaltung der Auftragssynchronisierung (synchroner Server)

Lassen Sie uns die Diskussion aus dem vorherigen Artikel fortsetzen: Entwurf eines Auftrags-Synchronisierungsmanagementsystems auf Basis der FMZ-Quantifizierung (1) (https://www.fmz.com/digest-topic/9729) und eine Strategie für eine synchronisierte Auftragsabwicklung zu entwickeln.

Betrachten wir einige derartige Designprobleme:

    1. Wenn Sie die synchrone Auftragserfüllung vorübergehend nicht durchführen möchten, ist es möglich, sie zu pausieren? Diese Funktion wird durch Hinzufügen von 2 globalen Variablen implementiert:
var isStopFollow = false   // Used to mark whether the current order is being followed
var reStartPwd = null      // Used to record the restart password

img

Dann fügen wir interaktive Steuerelemente auf der Strategie-Bearbeitungsseite für Strategie Pause / Neustart hinzu (nicht den echten Bot zu stoppen, nur Logik Pause, nicht mehr Befehl-Folgen).Order Synchronization Management System Class Library (Single Server)Wenn Sie die Reihenfolge wieder starten, geben Sie das voreingestellte Passwort ein, um die Reihenfolge zu wecken. Code für die Durchführung der einschlägigen Funktionen:

...
          // 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. Sie können die Anzahl der zu verfolgenden Bestellungen angeben oder in Vielfachen skaliert werden Die Strategie wird mit folgenden Parametern versehen:

img

specifiedAmount: Geben Sie die Anzahl der Bestellungen an, die folgen, der Standardwert ist -1, d. h. nicht angegeben. zoomAmountRatio: Skalierung nach der Anzahl der gesendeten Aufträge, z. B. wenn das gesendete Signal lautet: ETH_USDT,swap,buy,1, multiplizieren Sie den Wert der Anzahl der Aufträge mit zoomAmountRatio.

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

Hier wird es implementiert, um einen bestimmten Wert für die Menge der in dem empfangenen Signal zu verfolgenden Aufträge zu skalieren oder anzugeben.

    1. Der Code muss so einfach wie möglich sein, andere Vorlagenbibliotheken zum Bestellen verwenden.

Die Klassenbibliothek, die von Spot-Bestellungen verwendet wird:https://www.fmz.com/strategy/10989Die Klassenbibliothek, die von zukünftigen Aufträgen genutzt wird: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
  }

Wir können also sehen, dass eine Bestellung nur einen Satz benötigt:$.Sell(amount), $.Buy(amount), $.OpenLong(exchange, action.ct, amount). usw.

Strategiecode:

Der vorübergehende Code der vorangegangenenOrder Synchronous Management System (Synchronous Server)war wie folgt:

img

Jetzt beginnen wir mit der Neugestaltung des Order-Synchronisations-Management-Systems (Synchronous Server):

// 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)
    }
}

Prüfung

Wir testen das Auftragsführende Konto mit dem Binance-Bot für diese Zeit, und wir verwenden das OKEX-Konto für den Auftragsverfolgungs-Bot.mainFunktion in der Prüffunktion(Order Synchronization Management System Class Library (Single Server)in der Vorlage) im vorherigen Artikel verwendet.

img

Hier ändern wir die Richtung der Transaktion auf sell und das Volumen der Transaktion auf 0,003 (Orders können in Dezimalzahlen für Binance USDT Standardkontrakte platziert werden).

img

Als nächstes prüfen wir den Schließprozess der Position, indem wir die Richtung der Bestellung in der Test-Hauptfunktion ändern, um die Short-Position um 0,003 zu schließen.

img

Dann führen wir es erneut aus, das für die Bestellführung (Order Synchronization Management System Class Library (Single Server)) verantwortlich ist.

Die gleiche Operation wurde von einem richtigen Bot ausgelöst.

Die Strategieadresse: Klassenbibliothek für das System zur Verwaltung von Auftragssynchronisierungen (Single Server) (https://www.fmz.com/strategy/345171) System zur Verwaltung der Auftragssynchronisierung (synchroner Server) (https://www.fmz.com/strategy/345172)

Die Strategie ist nur für Kommunikation und Lernen gedacht, bitte passen Sie sie an und optimieren Sie sie entsprechend den tatsächlichen Bedürfnissen.


Verwandt

Mehr