FMZベースのオーダー同期管理システム設計 (2)

作者: リン・ハーンニナバダス作成日: 2022-04-06 17:12:00, 更新日: 2022-04-06 17:20:08

FMZベースのオーダー同期管理システム設計 (2)

オーダー同期管理システム (同期サーバー)

前回の記事で議論を継続しましょう:FMZベースのオーダー同期管理システム設計 (1), 同期オーダー監督戦略を策定する. デザインに関する次の質問について考えてみてください

  • 1.しばらくの間,同期的に注文を監視したくない場合は,停止できますか?停止すると,拡張APIから再起動を無効にし,パスワード認証を使用します. この関数を実行するには, 2 つのグローバル変数を追加します.
    var isStopFollow = false   // used to mark whether to currently supervise orders or not 
    var reStartPwd = null      // used to record the restart password 
    

img

戦略を停止/再起動するために戦略編集ページにインタラクティブなコントロールを追加します (ボットを停止するのではなく,ロジックを停止し,何もせずに命令に従わず,監視するだけです).それを停止するときは,停止パスワードを設定できます.Order Synchronous Management System Library (Single Server)命令を監督するために再起動すると,オーダー監督機能を呼び出すために事前に設定されたパスワードを入力します. 関連機能の実装コード:

...
          // 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.監督される注文の注文金額は,指定または比率によって拡大することができます.

    img

    specifiedAmount: 監督されるオーダーの金額を指定します.デフォルトは-1です.つまり指定されていません. zoomAmountRatio: 送信された信号の順序量に応じてズームします.例えば送信された信号は:ETH_USDT,swap,buy,1オーダー金額の値を zoomAmountRatio で掛けます.デフォルトは -1 です.つまり,ズームされていません.

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

    実現したズーム注文金額,または特定の値を指定する受け取った信号によると

  • 3.可能な限りシンプルなコードを書いて,注文の処理のために他のテンプレートライブラリを使用してください.

    スポットオーダーを行うために使用されるテンプレートライブラリ:https://www.fmz.com/strategy/10989フューチャー・オーダーを行うために使用されるテンプレートライブラリ: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
      }
    

    したがって,注文を行うには,ただ"つの声明が必要であることがわかります.$.Sell(amount), $.Buy(amount), $.OpenLong(exchange, action.ct, amount)など

戦略コード:

前回の一時コードはOrder Synchronous Management System (Synchronous Server)次のとおりです

img

設計してみましょうOrder Synchronous Management System (Synchronous Server)もう一度

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

テスト

このとき,Binanceのリアル・ティックテストは,オーダーを持つアカウントに使用され,OKEXアカウントは,オーダー監督ボットに使用されます.main機能についてOrder Synchronous Management System Library (Single Server)モデル)

img

取引量は0.003に変更されました (Binance USDT-margined Contracts は小数点に置くことができます). しかし,オーダーを持つ OKEX アカウントは整数でなければなりません (OKEX による注文は整数でなければなりません),したがってパラメータは,戦略パラメータを指定しますspecifiedAmount1 として

img

テスト機能のボットOrder Synchronous Management System Library (Single Server)取引を誘発しました

img

ボット戦略を監視する命令は,この信号を受け取って,監視アクションを実行します:

img

プラットフォームは対応する注文を開いた.

img

次に,閉じるポジションをテストし,メイン関数でオーダーの方向を0.003で閉じるショートポジションに変更します

img

命令を実行するボットを再起動します (Order Synchronous Management System Library (Single Server)).

img

同じ操作はオーダー監督ボットでも実行されます:

img

戦略アドレス:オーダー 同期管理システムライブラリ (シングルサーバー) オーダー同期管理システム (同期サーバー)

コミュニケーションや研究のためにのみ使われます 実際の利用のために 自分で修正し 調整し 最適化する必要があります


もっと