Konstruktion eines FMZ-basierten Auftragsmanagementsystems (2)

Schriftsteller:- Ich bin ein Idiot., Erstellt: 2022-04-06 17:12:00, Aktualisiert: 2022-04-06 17:20:08

Konstruktion eines FMZ-basierten Auftragsmanagementsystems (2)

Synchrone Verwaltung von Bestellungen (synchrone Server)

Lassen Sie uns die Diskussion im letzten Artikel fortsetzen:Gestaltung eines FMZ-basierten Auftragssynchronmanagementsystems (1), um eine synchrone Auftragsüberwachungsstrategie zu entwickeln. Bitte erwägen Sie folgende Fragen:

  • 1.Wenn Sie eine Weile nicht synchron Aufträge überwachen möchten, kann dies dann gestoppt werden? Um diese Funktion zu implementieren, fügen Sie 2 globale Variablen hinzu:
    var isStopFollow = false   // used to mark whether to currently supervise orders or not 
    var reStartPwd = null      // used to record the restart password 
    

img

Dann fügen Sie interaktive Steuerelemente auf der Strategiebearbeitungsseite hinzu, um die Strategie zu stoppen / neu zu starten (es ist nicht, um den Bot zu stoppen, sondern einfach die Logik zu stoppen, um keine Befehle zu befolgen und zu überwachen, ohne etwas zu tun).Order Synchronous Management System Library (Single Server)Wenn Sie Ihre erweiterte API KEY verwenden, kann die Strategie nicht aufgerufen werden. Beim Neustart, um Bestellungen zu überwachen, geben Sie das voreingestellte Passwort ein, um die Bestellungsüberwachungsfunktion aufzurufen. Implementierungscode der zugehörigen Funktion:

...
          // 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.Der Auftragsbetrag des beaufsichtigten Auftrags kann angegeben oder durch ein Verhältnis vergrößert werden:

    img

    specifiedAmount: Angabe des Betrags des beaufsichtigten Auftrags; Standardwert ist -1, d. h. nicht angegeben. zoomAmountRatio: Zoom nach der Reihenfolge des gesendeten Signals. Zum Beispiel ist das gesendete Signal:ETH_USDT,swap,buy,1, dann multiplizieren Sie den Wert des Bestellbetrags mit zoomAmountRatio; Standardwert ist -1, also nicht zoomed.

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

    Hier haben wir erkannt, daßVergrößernder Bestellbetrag odereinen bestimmten Wert angeben, entsprechend dem empfangenen Signal.

  • 3.Schreiben Sie möglichst einfache Codes und verwenden Sie andere Vorlagenbibliotheken, um mit der Auftragsvergabe umzugehen.

    Vorlagebibliothek für Spotorder:https://www.fmz.com/strategy/10989Für Futures-Orders verwendete Vorlagenbibliothek: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
      }
    

    Daher ist festzustellen, dass eine Bestellung nur eine Aussage benötigt:$.Sell(amount), $.Buy(amount), $.OpenLong(exchange, action.ct, amount), usw.

Strategiecode:

Der vorübergehende Code in der vorherigenOrder Synchronous Management System (Synchronous Server)ist wie folgt:

img

Nun, lassen Sie uns entwerfen dieOrder Synchronous Management System (Synchronous Server)Ich wiederhole:

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

Prüfung

Diesmal wird der Binance-Real-Tick-Test für das Konto mit Aufträgen verwendet, und das OKEX-Konto wird für den Auftragsüberwachungsbot verwendet.mainFunktion imOrder Synchronous Management System Library (Single Server)Vorlage).

img

Es ist nur, dass wir die Handelsrichtung zu kurz geändert, und das Handelsvolumen wurde auf 0,003 geändert (Binance USDT-Margin-Kontrakte können in Dezimalstellen platziert werden).specifiedAmountals 1.

img

Der Bot der Testfunktion inOrder Synchronous Management System Library (Single Server)hat den Austausch ausgelöst.

img

Der Auftrag, der die Bot-Strategie überwacht, erhält das Signal und führt die Überwachungsaktion aus:

img

Die Plattform eröffnete die entsprechende Bestellung.

img

Als nächstes testen Sie die Schließpositionen und ändern Sie die Bestellrichtung in der Hauptfunktion, um die Shortposition zu schließen, 0,003.

img

Dann starten Sie den Bot neu, der für die Ausführung von Aufträgen verantwortlich ist (Order Synchronous Management System Library (Single Server)).

img

Die gleiche Operation wird auch im Auftrag überwachen bot ausgelöst:

img

Strategieadresse:Bestellen Sie die Synchronous Management System Library (Single Server) Synchrone Verwaltung von Bestellungen (synchrone Server)

Diese Strategien werden nur für Kommunikation und Studium verwendet; für den tatsächlichen Gebrauch müssen Sie sie selbst ändern, anpassen und optimieren.


Mehr