균형 잡힌 대기 순서 전략 (교육 전략)

저자:리디아, 창작: 2022-12-19 17:19:44, 업데이트: 2023-09-20 10:01:27

img

균형 잡힌 대기 순서 전략 (교육 전략)

이 문서에서 설명한 전략의 본질은 동적 균형 전략, 즉 균형 통화의 값은 항상 평가 통화의 값과 같습니다. 그러나 전략 논리는 선행 주문으로 설계되었을 때 매우 간단합니다. 이 전략을 작성하는 주된 목적은 전략 설계의 모든 측면을 보여주는 것입니다.

  • 전략 논리 캡슐화 런타임에서 일부 데이터와 태그 변수 (물체로 캡슐화) 로 전략 논리를 캡슐화합니다.

  • 전략 처리 초기화 코드 초기 계정 정보는 이윤 계산을 위한 초기 실행에 기록됩니다. 초기 실행시에는 매개 변수에 따라 데이터를 복원할지 여부를 선택할 수 있습니다.

  • 전략 상호 작용 처리 코드 간단한 인터랙티브 처리 중지 및 재개가 설계되었습니다.

  • 전략 수익 계산 코드 이윤을 계산하기 위해 통화 표준 계산 방법을 사용합니다.

  • 전략에서 주요 데이터의 지속성 메커니즘 데이터 복구 메커니즘 설계

  • 전략 처리 정보를 표시하는 코드 상태 표시 표시

전략 코드

var Shannon = {
    // member
    e : exchanges[0],
    arrPlanOrders : [],
    distance : BalanceDistance,
    account : null,
    ticker : null, 
    initAccount : null,
    isAskPending : false,
    isBidPending : false,

    // function 
    CancelAllOrders : function (e) {
        while(true) {
            var orders = _C(e.GetOrders)
            if(orders.length == 0) {
                return 
            }
            Sleep(500)
            for(var i = 0; i < orders.length; i++) {
                e.CancelOrder(orders[i].Id, orders[i])
                Sleep(500)
            }
        }
    },

    Balance : function () {
        if (this.arrPlanOrders.length == 0) {
            this.CancelAllOrders(this.e)
            var acc = _C(this.e.GetAccount)
            this.account = acc
            var askPendingPrice = (this.distance + acc.Balance) / acc.Stocks
            var bidPendingPrice = (acc.Balance - this.distance) / acc.Stocks
            var askPendingAmount = this.distance / 2 / askPendingPrice
            var bidPendingAmount = this.distance / 2 / bidPendingPrice

            this.arrPlanOrders.push({tradeType : "ask", price : askPendingPrice, amount : askPendingAmount}) 
            this.arrPlanOrders.push({tradeType : "bid", price : bidPendingPrice, amount : bidPendingAmount})
        } else if(this.isAskPending == false && this.isBidPending == false) {
            for(var i = 0; i < this.arrPlanOrders.length; i++) {
                var tradeFun = this.arrPlanOrders[i].tradeType == "ask" ? this.e.Sell : this.e.Buy
                var id = tradeFun(this.arrPlanOrders[i].price, this.arrPlanOrders[i].amount)
                if(id) {
                    this.isAskPending = this.arrPlanOrders[i].tradeType == "ask" ? true : this.isAskPending
                    this.isBidPending = this.arrPlanOrders[i].tradeType == "bid" ? true : this.isBidPending
                } else {
                    Log("Pending order failed, clear!")
                    this.CancelAllOrders(this.e)
                    return 
                }
            }
        }

        if(this.isBidPending || this.isAskPending) {
            var orders = _C(this.e.GetOrders)
            Sleep(1000)
            var ticker = _C(this.e.GetTicker)
            this.ticker = ticker
            if(this.isAskPending) {
                var isFoundAsk = false 
                for (var i = 0; i < orders.length; i++) {
                    if(orders[i].Type == ORDER_TYPE_SELL) {
                        isFoundAsk = true
                    }
                }
                if(!isFoundAsk) {
                    Log("Selling order filled, cancel the order, reset")
                    this.CancelAllOrders(this.e)
                    this.arrPlanOrders = []
                    this.isAskPending = false 
                    this.isBidPending = false 
                    LogProfit(this.CalcProfit(ticker))
                    return 
                }
            }
            if(this.isBidPending) {
                var isFoundBid = false 
                for(var i = 0; i < orders.length; i++) {
                    if(orders[i].Type == ORDER_TYPE_BUY) {
                        isFoundBid = true
                    }
                }
                if(!isFoundBid) {
                    Log("Buying order filled, cancel the order, reset")
                    this.CancelAllOrders(this.e)
                    this.arrPlanOrders = []
                    this.isAskPending = false 
                    this.isBidPending = false 
                    LogProfit(this.CalcProfit(ticker))
                    return 
                }
            }        
        }
    }, 
    ShowTab : function() {
        var tblPlanOrders = {
            type : "table", 
            title : "Plan pending orders", 
            cols : ["direction", "price", "amount"], 
            rows : []
        }
        for(var i = 0; i < this.arrPlanOrders.length; i++) {
            tblPlanOrders.rows.push([this.arrPlanOrders[i].tradeType, this.arrPlanOrders[i].price, this.arrPlanOrders[i].amount])
        }

        var tblAcc = {
            type : "table", 
            title : "Account information", 
            cols : ["type", "Stocks", "FrozenStocks", "Balance", "FrozenBalance"], 
            rows : []            
        }
        tblAcc.rows.push(["Initial", this.initAccount.Stocks, this.initAccount.FrozenStocks, this.initAccount.Balance, this.initAccount.FrozenBalance])
        tblAcc.rows.push(["This", this.account.Stocks, this.account.FrozenStocks, this.account.Balance, this.account.FrozenBalance])
        
        return "Time:" + _D() + "\n `" + JSON.stringify([tblPlanOrders, tblAcc]) + "`" + "\n" + "ticker:" + JSON.stringify(this.ticker)
    },
    CalcProfit : function(ticker) {
        var acc = _C(this.e.GetAccount)
        this.account = acc
        return (this.account.Balance - this.initAccount.Balance) + (this.account.Stocks - this.initAccount.Stocks) * ticker.Last
    },
    Init : function() {
        this.initAccount = _C(this.e.GetAccount)
        if(IsReset) {
            var acc = _G("account")
            if(acc) {
                this.initAccount = acc 
            } else {
                Log("Failed to restore initial account information! Running in initial state!")
                _G("account", this.initAccount)
            }
        } else {
            _G("account", this.initAccount)
            LogReset(1)
            LogProfitReset()
        }
    },
    Exit : function() {
        Log("Cancel all pending orders before stopping...")
        this.CancelAllOrders(this.e)
    }
}

function main() {
    // Initialization
    Shannon.Init()

    // Main loop
    while(true) {
        Shannon.Balance()        
        LogStatus(Shannon.ShowTab())
        // Interaction
        var cmd = GetCommand()
        if(cmd) {
            if(cmd == "stop") {
                while(true) {
                    LogStatus("Pause", Shannon.ShowTab())
                    cmd = GetCommand()
                    if(cmd) {
                        if(cmd == "continue") {
                            break
                        }
                    }
                    Sleep(1000)
                }
            }
        }
        Sleep(5000)
    }
}

function onexit() {
    Shannon.Exit()
}

백테스팅

img img img img

최적화 및 확장

  • 가상 대기 주문 메커니즘을 추가 할 수 있습니다. 일부 거래소는 대기 주문에 대한 제한된 가격을 가지고 있으므로 실제로 주문이 이루어지지 않을 수 있습니다. 가격이 실제 대기 주문에 가까울 때까지 기다려야합니다.
  • 미래에셋거래를 추가합니다.
  • 여러 종과 여러 교환 버전을 확장합니다.

전략은 교육 목적으로만 사용되며 실제 봇 거래에서 조심스럽게 사용해야합니다. 전략 주소:https://www.fmz.com/strategy/225746


관련

더 많은