The resource loading... loading...

High-frequency strategy design from the Magnetic Cabbage Harvester

Author: Inventors quantify - small dreams, Created: 2021-03-09 13:41:54, Updated: 2023-09-26 20:53:41

img

High-frequency strategy design from the Magnetic Cabbage Harvester

In the previous articles, we analyzed the ideas behind the original, live version of the high-frequency strategy of the cabbage harvester and the code implementation.

How to dissect cabbage harvesters (1) How to analyze cabbage harvesters (2)

Many users are more concerned about the quantity of coin circles.print moneyThe strategy of the monkeyprint moneyThe strategy of the Daisuke is to trade in binary USDT contracts. From observation and analysis of many observers, the high-frequency strategy is similar to the principle of a cabbage harvester (Grassin has also said that the principles of the high-frequency strategy are more similar).

So the little boy who is skilled in the technique can't help but make a change, although the tactical effect of the magic change was crushed into scum by the great gods' tactics. But it is also a learning practice for high-frequency tactics, interested FM Zer classmates can come together to explore and learn.

The Magic of the Cabbage Harvester

var TickInterval = 100

function LeeksReaper() {
    var self = {}
    self.numTick = 0
    self.lastTradeId = 0
    self.vol = 0
    self.askPrice = 0
    self.bidPrice = 0
    self.orderBook = {
        Asks: [],
        Bids: []
    }
    self.prices = []
    self.tradeOrderId = 0
    self.account = null
    self.buyPrice = 0
    self.sellPrice = 0
    self.state = 0
    self.depth = null

    self.updateTrades = function() {
        var trades = _C(exchange.GetTrades)
        if (self.prices.length == 0) {
            while (trades.length == 0) {
                trades = trades.concat(_C(exchange.GetTrades))
            }
            for (var i = 0; i < 15; i++) {
                self.prices[i] = trades[trades.length - 1].Price
            }
        }
        self.vol = 0.7 * self.vol + 0.3 * _.reduce(trades, function(mem, trade) {
            // Huobi not support trade.Id
            if ((trade.Id > self.lastTradeId) || (trade.Id == 0 && trade.Time > self.lastTradeId)) {
                self.lastTradeId = Math.max(trade.Id == 0 ? trade.Time : trade.Id, self.lastTradeId)
                mem += trade.Amount
            }
            return mem
        }, 0)

    }
    self.updateOrderBook = function() {
        var orderBook = _C(exchange.GetDepth)
        self.depth = orderBook
        self.buyPrice = orderBook.Bids[pendingLevel].Price
        self.sellPrice = orderBook.Asks[pendingLevel].Price
        self.orderBook = orderBook
        if (orderBook.Bids.length < 3 || orderBook.Asks.length < 3) {
            return
        }
        self.bidPrice = orderBook.Bids[0].Price * 0.618 + orderBook.Asks[0].Price * 0.382 + 0.01
        self.askPrice = orderBook.Bids[0].Price * 0.382 + orderBook.Asks[0].Price * 0.618 - 0.01
        self.prices.shift()
        self.prices.push(_N((orderBook.Bids[0].Price + orderBook.Asks[0].Price) * 0.15 +
            (orderBook.Bids[1].Price + orderBook.Asks[1].Price) * 0.1 +
            (orderBook.Bids[2].Price + orderBook.Asks[2].Price) * 0.1 +
            (orderBook.Bids[3].Price + orderBook.Asks[3].Price) * 0.075 +
            (orderBook.Bids[4].Price + orderBook.Asks[4].Price) * 0.05 +
            (orderBook.Bids[5].Price + orderBook.Asks[5].Price) * 0.025))
    }

    self.updateAccount = function() {
        var account = exchange.GetAccount()
        if (!account) {
            return
        }
        self.account = account
        LogProfit(parseFloat(account.Info.totalWalletBalance), account)
    }

    self.CancelAll = function() {
        while (1) {
            var orders = _C(exchange.GetOrders)
            if (orders.length == 0) {
                break
            }
            for (var i = 0; i < orders.length; i++) {
                exchange.CancelOrder(orders[i].Id)
            }
            Sleep(100)
        }
    }

    self.poll = function() {
        self.numTick++
        self.updateTrades()
        self.updateOrderBook()
        var pos = _C(exchange.GetPosition)

        var burstPrice = self.prices[self.prices.length - 1] * burstThresholdPct
        var bull = false
        var bear = false
        LogStatus(_D(), "\n", 'Tick:', self.numTick, 'self.vol:', self.vol, ', lastPrice:', self.prices[self.prices.length - 1], ', burstPrice: ', burstPrice)

        if (self.numTick > 2 && (
                self.prices[self.prices.length - 1] - _.max(self.prices.slice(-6, -1)) > burstPrice ||
                self.prices[self.prices.length - 1] - _.max(self.prices.slice(-6, -2)) > burstPrice && self.prices[self.prices.length - 1] > self.prices[self.prices.length - 2]
            )) {
            bull = true
        } else if (self.numTick > 2 && (
                self.prices[self.prices.length - 1] - _.min(self.prices.slice(-6, -1)) < -burstPrice ||
                self.prices[self.prices.length - 1] - _.min(self.prices.slice(-6, -2)) < -burstPrice && self.prices[self.prices.length - 1] < self.prices[self.prices.length - 2]
            )) {
            bear = true            
        }

        if (pos.length != 0) {
            if (pos[0].Type == PD_LONG) {
                self.state = 1
            } else {
                self.state = 2
            }
        } else {
            self.state = 0
        }


        if ((!bull && !bear)) {
            return
        }

        if (bull) {
            var price = (self.state == 0 || self.state == 1) ? self.buyPrice : self.depth.Bids[coverPendingLevel].Price
            var amount = (self.state == 0 || self.state == 1) ? pendingAmount : pos[0].Amount
            exchange.SetDirection("buy")
            exchange.Buy(price, amount)
        } else if (bear) {
            var price = (self.state == 0 || self.state == 2) ? self.sellPrice : self.depth.Asks[coverPendingLevel].Price
            var amount = (self.state == 0 || self.state == 2) ? pendingAmount : pos[0].Amount
            exchange.SetDirection("sell")
            exchange.Sell(price, amount)                    
        }
        self.numTick = 0
        Sleep(TickInterval)
        self.CancelAll()
        self.updateAccount()
    }

    while (!self.account) {
        self.updateAccount()
        Sleep(500)
    }
    Log("self.account:", self.account)

    return self
}

function main() {
    LogProfitReset()
    exchange.SetPrecision(pricePrecision, amountPrecision)
    exchange.SetContractType("swap")
    var reaper = LeeksReaper()  
    while (true) {
        reaper.poll()
        Sleep(100)
    }
}

img

Strategic change of mind

The strategy is planned to be used for trading in the Binance USDT contract market, Binance contracts support one-way holding. So the strategy is designed to modify the design according to the characteristics of one-way holding (one-way holding is more convenient to modify the strategy), not considering the flat, only consider buying and selling.

The strategy basically retains the original short-term price trend breakthrough determination criteria, the short-term price breakthrough magnitude is determined by the parametersburstThresholdPctThe price of a stock is the price at which the short-term price of the stock is determined.bull(cow) orbear(Bear)

The policy eliminates some modules from the original version, such as the balance module. A major change is to change the sub-order to be placed on the order sheet, waiting for the transaction. Expect to open a position at a lower cost in a chaotic market, follow the short-term trend, and continue to hold the position in reverse when the short-term trend reverses.

The strategy eliminates other useless code so it is very short and simple. Although the strategy is a strategy that does not make money or even lose money, as FMZer, it is a model that can be used to learn high-frequency strategies, observe the behavior of high-frequency strategies, observe the microscopic laws of the market, etc. Programmatic trading, quantitative trading requires a lot of practice, experience and theory as a basis.

Let's run for a while.

img

As you can see, it is more difficult to open a trade when the market is not active.

Optimizing the strategy

At the moment, there are no good optimization directions. Students who are interested can speak and discuss together.

The policy address:https://www.fmz.com/strategy/260806

This strategy is only for learning purposes, and the market may lose money if the market is flat.


Related

More

Roasted cabbageHow does the contract set the leverage multiplier here?

🏆BensonDr. Dreams, your strategy has appeared on another platform, please be aware of copyright.

gillbates2In the case of a one-sided transaction, how should it be handled?

The trajectory of lifeThis can be used to determine whether or not to add a transaction volume, or to determine the automatic currency choice.

qslllCan you share the data?

Quantitative measurement of the carotenoidsIn updateOrderBook, the price after weighting should be close to the median of the buy-sell-n bid, and in this article, the weighting is twice the price after calculation (close to the median of the buy-sell bid).

168Can DreamWorks make a version that can be reviewed and used on the spot?

168main:68:43 - TypeError: Cannot read property 'totalWalletBalance' of undefined Please ask Little Dream, this can't be retested, must it be on a real disk? How to adjust?

bengbanThe picture of a real runner is too skinny

sadfkj8iThe trades of the contract seem to be impossible to get, the strategy is not working, how did the bull run.

Inventors quantify - small dreamsIt is possible to lower the single-speed, but there are advantages and disadvantages. This high-frequency strategy requires a good depth of field, a lot of space and intense scenes.

Roasted cabbageI've run a night (almost 5 hours) without ordering at all and I've done 5 orders, how can I change the parameters?

Inventors quantify - small dreamsIn addition to the above, you can set up leverage directly on the exchange.

Inventors quantify - small dreamsHa ha, good, thanks for the reminder.

Inventors quantify - small dreamsThis strategy is a teaching strategy, mainly based on ideas, but only teaching.

m0606I also think the price limit is pretty good...

Inventors quantify - small dreamsINFO is in Macanese.

Passing byRe-testing does not support INFO information

bengbanThe tick is a good design.

Inventors quantify - small dreams!>_>! This is just a prototype, it's a waste of money, it's a learning tool, it's just a research object.