Two-way contract mesh trading v1.0.2

Author: wind, Date: 2021-06-24 15:37:17
Tags: Grid

Two-way contract mesh trading v1.0.2

Function

Contract Grid Trading And it's also about making a difference by doing more time off. This is because the chances of a two-way strategy are very low.

  • Buy one over time
  • The Double
  • Automated check-in
  • The trend is open ((to be developed, paid version))
  • Dynamic change in the number of orders (development, paid version)
  • Gold forks joined the trend (in development, paid version)

Repeat the data

img

img

doubled in the first half of 2000 The benefits are obvious, and the ups and downs are inevitable.

Maintenance

Continuous optimization



/*backtest
start: 2021-01-01 00:00:00
end: 2021-06-21 23:59:00
period: 1m
basePeriod: 1m
exchanges: [{"eid":"Futures_Binance","currency":"BTC_USDT","balance":2000}]
*/

// 首次买入
let FIRST_BUY = true;
// 已存在买涨订单
let MANY_BUYING = false;
// 已存在做空订单
let SHORT_BUYING = false;
// 买涨订单创建时间
let MANY_BUY_TIME = null;
// 做空订单创建时间
let SHORT_BUY_TIME = null;
// 买涨空仓时间
let MANY_EMPTY_STEP_TIME = null;
// 做空空仓时间
let SHORT_EMPTY_STEP_TIME = null;
// 校验空仓时间
let CHECK_TIME = null;

let QUANTITY = [0.001, 0.002, 0.004, 0.008, 0.016, 0.032, 0.064];
// 下次购买价格(多仓)
let MANY_NEXT_BUY_PRICE = 0;
// 下次购买价格(空仓)
let SHORT_NEXT_BUY_PRICE = 0;
// 当前仓位(多仓)
let MANY_STEP = 0;
// 当前仓位(空仓)
let SHORT_STEP = 0;
// 止盈比率
let PROFIT_RATIO = 1;
// 补仓比率
let DOUBLE_THROW_RATIO = 1.5;
// 卖出后下次购买金额下浮比率
let BUY_PRICE_RATIO = 1;
// 交易订单列表(多仓)
let MANY_ORDER_LIST = [];
// 交易订单列表(空仓)
let SHORT_ORDER_LIST = [];

function getManyQuantity() {
    if (MANY_STEP < QUANTITY.length) {
        return QUANTITY[MANY_STEP]
    }
    return QUANTITY[0]
}

function getShortQuantity() {
    if (SHORT_STEP < QUANTITY.length) {
        return QUANTITY[SHORT_STEP]
    }
    return QUANTITY[0]
}

function firstManyBuy(ticker) {
    if (MANY_BUYING) {
        return
    }
    exchange.SetDirection("buy")
    let orderId = exchange.Buy(ticker.Last, getManyQuantity())
    if (!orderId) {
        return
    }
    MANY_BUYING = true
    while (true) {
        exchange.SetDirection("buy") 
        let order = exchange.GetOrder(orderId)
        if (null === order) {
            continue
        }
        if (1 === order.Status || 2 === order.Status) {
            MANY_NEXT_BUY_PRICE = order.Price * ((100 - DOUBLE_THROW_RATIO) / 100)
            MANY_STEP = MANY_STEP + 1
            MANY_BUYING = false
            MANY_EMPTY_STEP_TIME = null
            let sellPrice = order.Price * ((100 + PROFIT_RATIO) / 100)
            MANY_ORDER_LIST.push({
                buyPrice: order.Price,
                sellPrice: sellPrice,
                quantity: order.Amount,
                isSell: false,
            })
            break
        }
    }
}

function firstShortBuy(ticker) {
    if (SHORT_BUYING) {
        return
    }
    exchange.SetDirection("sell")
    let orderId = exchange.Sell(ticker.Last, getShortQuantity())
    if (!orderId) {
        return
    }
    SHORT_BUYING = true
    while (true) {
        let order = exchange.GetOrder(orderId)
        if (null === order) {
            continue
        }
        if (1 === order.Status || 2 === order.Status) {
            SHORT_NEXT_BUY_PRICE = order.Price * ((100 + DOUBLE_THROW_RATIO) / 100)
            SHORT_STEP = SHORT_STEP + 1
            SHORT_BUYING = false
            SHORT_EMPTY_STEP_TIME = null
            let sellPrice = order.Price * ((100 - PROFIT_RATIO) / 100)
            SHORT_ORDER_LIST.push({
                buyPrice: order.Price,
                sellPrice: sellPrice,
                quantity: order.Amount,
                isSell: false,
            })
            break
        }
    }
}

function manyBuy(ticker) {
    if (MANY_BUYING) {
        return
    }
    Log('ticker: ' + ticker.Last + ' MANY_NEXT_BUY_PRICE: ' + MANY_NEXT_BUY_PRICE)
    if (ticker.Last > MANY_NEXT_BUY_PRICE) {
        return
    }
    exchange.SetDirection("buy")
    let orderId = exchange.Buy(ticker.Last, getManyQuantity())
    if (!orderId) {
        return
    }
    MANY_BUYING = true
    MANY_BUY_TIME = Unix()
    while (true) {
        let now = Unix()
        let order = exchange.GetOrder(orderId)
        let expire = MANY_BUY_TIME + (60 * 30)
        if (null === order) {
            continue
        }
        // 买入成功处理
        if (1 === order.Status || 2 === order.Status) {
            MANY_NEXT_BUY_PRICE = order.Price * ((100 - DOUBLE_THROW_RATIO) / 100)
            MANY_STEP = MANY_STEP + 1
            MANY_BUYING = false
            MANY_EMPTY_STEP_TIME = null
            let sellPrice = order.Price * ((100 + PROFIT_RATIO) / 100)
            MANY_ORDER_LIST.push({
                buyPrice: order.Price,
                sellPrice: sellPrice,
                quantity: order.Amount,
                isSell: false,
            })
            break
        }
        // 买入超时处理
        if (now >= expire) {
            exchange.CancelOrder(orderId)
            MANY_BUYING = false
            MANY_BUY_TIME = null
            MANY_NEXT_BUY_PRICE = ticker.Last * ((100 - DOUBLE_THROW_RATIO) / 100)
            return
        }
    }
}

function shortBuy(ticker) {
    if (SHORT_BUYING) {
        return
    }
    Log('ticker: ' + ticker.Last + ' SHORT_NEXT_BUY_PRICE: ' + SHORT_NEXT_BUY_PRICE)
    if (ticker.Last < SHORT_NEXT_BUY_PRICE) {
        return
    }
    exchange.SetDirection("sell")
    let orderId = exchange.Sell(ticker.Last, getShortQuantity())
    if (!orderId) {
        return
    }
    SHORT_BUYING = true
    SHORT_BUY_TIME = Unix()
    while (true) {
        let now = Unix()
        let expire = SHORT_BUY_TIME + (60 * 30)
        let order = exchange.GetOrder(orderId)
        if (null === order) {
            continue
        }
        // 买入成功处理
        if (1 === order.Status || 2 === order.Status) {
            SHORT_NEXT_BUY_PRICE = order.Price * ((100 + DOUBLE_THROW_RATIO) / 100)
            SHORT_STEP = SHORT_STEP + 1
            SHORT_BUYING = false
            SHORT_EMPTY_STEP_TIME = null
            let sellPrice = order.Price * ((100 - PROFIT_RATIO) / 100)
            SHORT_ORDER_LIST.push({
                buyPrice: order.Price,
                sellPrice: sellPrice,
                quantity: order.Amount,
                isSell: false,
            })
            break
        }
        // 买入超时处理
        if (now >= expire) {
            exchange.CancelOrder(orderId)
            SHORT_BUYING = false
            SHORT_BUY_TIME = null
            SHORT_NEXT_BUY_PRICE = ticker.Last * ((100 + DOUBLE_THROW_RATIO) / 100)
            return
        }
    }
}


function manySell(ticker) {
    // 遍历卖出订单
    for (let item of MANY_ORDER_LIST) {
        if (item.isSell) {
            continue
        }
        if (ticker.Last >= item.sellPrice) {
            item.isSell = true;
            exchange.SetDirection("closebuy")
            let orderId = exchange.Sell(ticker.Last, item.quantity)
            if (!orderId) {
                return
            }
            while (true) {
                let order = exchange.GetOrder(orderId)
                if (null === order) {
                    continue
                }
                if (1 === order.Status || 2 === order.Status) {
                    MANY_NEXT_BUY_PRICE = ticker.Last * ((100 - BUY_PRICE_RATIO) / 100)
                    MANY_STEP = MANY_STEP - 1
                    if (0 === MANY_STEP) {
                        MANY_EMPTY_STEP_TIME = Unix()
                    }
                    break
                }
            }
        }
    }
}

function shortSell(ticker) {
    // 遍历卖出订单
    for (let item of SHORT_ORDER_LIST) {
        if (item.isSell) {
            continue
        }
        if (ticker.Last <= item.sellPrice) {
            item.isSell = true;
            exchange.SetDirection("closesell")
            let orderId = exchange.Buy(ticker.Last, item.quantity)
            if (!orderId) {
                return
            }
            while (true) {
                let order = exchange.GetOrder(orderId)
                if (null === order) {
                    continue
                }
                if (1 === order.Status || 2 === order.Status) {
                    SHORT_NEXT_BUY_PRICE = ticker.Last * ((100 + BUY_PRICE_RATIO) / 100)
                    SHORT_STEP = SHORT_STEP - 1
                    if (0 === SHORT_STEP) {
                        SHORT_EMPTY_STEP_TIME = Unix()
                    }
                    break
                }
            }
        }
    }
}

function check(ticker) {
    let now = Unix()
    if (null !== CHECK_TIME) {
        let expire = CHECK_TIME + (60 * 10)
        if (now < expire) {
            return
        }
    }
    CHECK_TIME = now

    if (null !== MANY_EMPTY_STEP_TIME) {
        let expire = MANY_EMPTY_STEP_TIME + (60 * 30)
        if (now >= expire) {
            MANY_NEXT_BUY_PRICE = ticker.Last * ((100 - DOUBLE_THROW_RATIO) / 100)
            Log('没有买涨持仓, 调整买入价: ' + MANY_NEXT_BUY_PRICE)
        }
    }
    
    if (null !== SHORT_EMPTY_STEP_TIME) {
        let expire = SHORT_EMPTY_STEP_TIME + (60 * 30)
        if (now >= expire) {
            SHORT_NEXT_BUY_PRICE = ticker.Last * ((100 + DOUBLE_THROW_RATIO) / 100)
            Log('没有做空持仓, 调整买入价: ' + SHORT_NEXT_BUY_PRICE)
        }
    }
}

function onTick() {
    // 在这里写策略逻辑,将会不断调用,例如打印行情信息
    let ticker = exchange.GetTicker()
    if (!ticker) {
        return
    }
    if (FIRST_BUY) {
        // 首次做多购买
        firstManyBuy(ticker)
        // 首次做空购买
        firstShortBuy(ticker)
        FIRST_BUY = false
        return
    }
    
    // 做多买入
    manyBuy(ticker)
    // 做空买入
    shortBuy(ticker)
    // 做多卖出
    manySell(ticker)
    // 做空卖出
    shortSell(ticker)
    // 空仓检测
    check(ticker)
}

function main() {
    // 开合约
    exchange.SetContractType("swap")

    while(true){
        onTick()
        // Sleep函数主要用于数字货币策略的轮询频率控制,防止访问交易所API接口过于频繁
        Sleep(60000)
    }
}


Related

More

hexie8Can you have multiple currencies?

rich_roryWhat does check mean? What does it mean?

artronWhy isn't this set to policy parameters?

[Translated from Chinese]The problem was also discovered, when retesting from 2020-1-1 to 2020-6 the whole robot stopped at 3-30 and also when testing ETH it stopped at 1-2 during testing from 2021-1-1 to 201-6-21.

evanIt can't be a real multi-space hedge, like a contract grid.

[Translated from Chinese]WeChat has not responded, I can only come here and say a few words, it has been running for 7 days, 200 knives and 70 knives. 1. is it normal to have a two-way open single function? see the strategy says that the two-way open single, the probability of a boom is very small, but in seven days, except for the robot just started to open a two-way single, there is almost no two-way open single, after my research, it was found that 10 minutes to update a single position, which leads to a slow change, it makes no sense, and not open a single, for example, today's btc 5% drop is not open. I have 200 knives today if I don't wake up suddenly I'm already in the stock market, temporary additional guarantee money. If the funds are not enough, you can also manually adjust the amount of stock market, here remind me, so as not to see the code of the stock market. 3. Please ask me, because there is never a stop loss, so if there is enough capital, will there never be a bust, but why will there be a loss in the review? I like this strategy, you can make a profit regardless of the direction, adjust the leverage and the risk of a bullish position is not high, the feeling is that adjusting the open and the overbought price can be optimized, depending on the capital added, adjust the leverage and so on.

YesXMy brother tweeted.

[Translated from Chinese]The problem is solved, and the exchange is done.

[Translated from Chinese]I'm trying to find a way to use it on the real disk. I'm trying to find a way to use it on the real disk. I'm trying to find a way to use it on the real disk.

wbsyThere is no real one.

[Translated from Chinese]This is a terrible gain curve, losing over 120%.

Drunk picked up a lamp and looked at a swordLaughing at me.

[Translated from Chinese]I'm sorry, I lost money today, I'm looking forward to 2.0, please tell me how I can experience the new strategy.

windThese problems were discovered in development 2.0. The reason you can't buy a short is because the percentage of openings is fixed, that is, up one percent, short, down one percent, buy up one percent. Since the decline is a percentage, the decline in the percentage of purchases is consistent, from 1000 tonnes to 1500 tonnes, and the 1000 percent and 1500 percent are not the same, the vacancy is more difficult to buy, and the price change is too frequent, which may lead to the vacancy not being bought. Stop-loss problem, in version 2.0, a certain processing is done, a very simple stop-loss processing, but from the feedback data, the effect can also be, when you buy a stock or empty a position when it reaches a certain threshold, the entire stock starts to sell, 1.0 is not processed so there will be some orders that can never be dealt, such as in BTC 65000 buyers number 0.001, then BTC falls this list can never go out /upload/asset/2034c4ec56c423120b9c6.png /upload/asset/203032c94e60a3915cc9f.png /upload/asset/2030b880b030476977f4b.png /upload/asset/2030d89e9fd59f528be4c.png /upload/asset/2030d89e9fd59f528be4c.png /upload/asset/2030b880b030476977f4b.png /upload/asset/2030d89e9fd59f528be4c.png /upload/asset/

windI'm using Binance to test the hard drive running, other platforms might report some errors.

windThe real disk is still running, it takes time

windLook at the most recent curve, the previous one just got bugs.