FMZ Quant (2) をベースにした注文同期管理システムの設計

作者: リン・ハーンリディア, 作成日:2022-11-08 11:34:20, 更新日:2023-09-15 20:48:46

img

FMZ Quant (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)

この戦略は コミュニケーションと学習のみのために設計されています 実際のニーズに応じて調整し最適化してください


関連性

もっと