The resource loading... loading...

Exchange configuration details for digital currency quantification trading strategy

Author: Inventors quantify - small dreams, Created: 2019-09-02 09:39:59, Updated: 2024-12-17 20:42:42

img

Exchange configuration details for digital currency quantification trading strategy

There are often a variety of strategy needs when designing a beginner digital currency quantification trading strategy, and there are a variety of strategy design needs in different situations, regardless of the language used, the platform. For example, sometimes it requires multiple rotations, sometimes it requires multiple platform hedges, sometimes it requires different types of transactions, etc. Below we share some design experiences when implementing the strategy needs. Learning platforms still use inventors' quantified trading platformshttps://www.fmz.comThe market has chosen the digital currency market.

  • How to design a multi-currency strategy

    Such demands are often due to the need to write a multi-variety trend strategy, multi-variety grid strategy, etc., which needs to be executed against strategy logic and market iteration with different trades. It's usually designed like this:

    function Process (symbol) {
        exchange.IO("currency", symbol)
        var ticker = _C(exchange.GetTicker)
        Log("已经切换交易对,按照策略逻辑处理交易对 :", symbol, "行情:", ticker)
        // ...
        // ..
        // .
    }  
    
    function main(){
        var symbols = ["BTC_USDT", "LTC_USDT", "ETH_USDT"]
        while (true) {
            for (var i = 0 ; i < symbols.length; i++) {
                Process(symbols[i])
                Sleep(500)
            }
        }
    }
    

    We configure the robot:img

    img

    As you can see, this is achieved by configuring an exchange object on the robot, switching pairs, acquiring different pairs of markets, performing a variety of markets, and executing under one strategic logic. As you can see, the three pairs we define: BTC_USDT, LTC_USDT, ETH_USDT, in the loop, the market is acquired in a sequential iteration, after the market is acquired, the market can be specifically detected, triggering the strategy designed trading logic.

    Some of my classmates may say: I don't like switching pairs, it's a bit of a hassle and the strategy is not very clear. There are other ways of designing, which we will show below.

  • The same exchange account configures multiple exchange objects for the robot

    Market data for different trading pairs is obtained through multiple exchange objects and executed in iterative strategy logic. For example, configure a robot like this: Configure three exchange objects for the robot, with the trading pair set to BTC_USDT, LTC_USDT, and ETH_USDT, respectively.img

    The name of the exchange object for the "OKEX Live V3 Test" is displayed in the Control Center, Exchange Configuration page:imgI've already configured it.

    Modify the code, because this time we have added multiple exchange objects to the bot, the exchange objects for BTC_USDT, LTC_USDT, ETH_USDT respectively.

    function Process (e) {
        var ticker = _C(e.GetTicker)
        Log("交易所", e.GetName(), "按照策略逻辑处理交易对 :", e.GetCurrency(), "行情:", ticker)
        // ...
        // ..
        // .
    }  
    
    function main(){
        while (true) {
            for (var i = 0 ; i < exchanges.length; i++) {
                Process(exchanges[i])
                Sleep(500)
            }
        }
    }
    

    The robot is running:img

    The above examples are either swapping pairs or adding a configuration account to multiple different pairs of exchange objects. All you have to do is configure one exchange account (using a well-configured exchange). So how can you use multiple exchange accounts in one strategy?

  • Strategies for using multiple exchange accounts

    Some strategies include multi-exchange cross-market hedging, multi-account strategy within a single exchange, etc.

    • Many exchanges are configured, but different exchanges.imgFor example, I configure 2 exchanges in Control Center - > Exchange - > Add Exchange page. I can access the asset information of the accounts configured by these two exchanges in the policy.

      img

      function main(){
          Log(exchanges[0].GetAccount())    // 打印第一个 交易所对象的账户资产信息,即火币期货 这个交易所的资产信息。
          Log(exchanges[1].GetAccount())    // ... 打印Bit-Z这个交易所的资产信息
      }
      

      Of course, I can also configure a second or third account for an exchange.

    • Many exchanges are configured to be the same exchange.

      For example, we add an account for a token futures.img

      As you can see, this configures the accounts of the two "token futures" exchanges.

      img

      During the creation of the policy, the bot's "Modify Configuration" option will also display a token futures exchange object to choose from.

      img

      For example, this allows two accounts to have a first-sell-first-buy grid strategy (up) and a first-sell-first-buy grid strategy (down).

      I'm going to use the two examples above.

      Here's the difference between configuring multiple exchange objects on a robot and "the same exchange account configures multiple exchange objects for a robot":

      This example is somewhat similar to the one described above: "The same exchange account configures multiple exchange objects for the robot", but there is a difference. The difference is that the above example is an exchange configuration, i.e.:

      img

      In the case of a robot, the following objects are always used when configuring the exchange:imgThis configuration.

      It's just that when you add an exchange object, the transaction settings are different. If the GetAccount function is called, all the asset information for the same account is always accessed.

      However:imgThe two tokens in this configuration are futures exchange objects, although both are tokens futures, represented by different exchange accounts.

  • It is a simple strategy to design a digital currency futures strategy with the help of the exchange configuration.

    Sometimes, in the strategy of hedging a digital currency contract, many scenarios require simultaneous ordering in order to seize a trading opportunity that is fleeting. But because contracts are different, it is necessary to switch to the corresponding contract when acquiring a market, when the order is operated. When using the exchange.Go function, simultaneous execution of a single function or acquiring a market is not fast due to synchronization problems. And the design of such a switch contract also makes the logic not so simple. Is there a better way?

    Of course there is a way! We can add two exchange objects to the robot as described above: "The same exchange account configures multiple exchange objects for the robot".imgThen use this exchange configuration to add an exchange object. This will pop up a prompt box!imgAn exchange account configuration cannot add the same currency, exchange object or transaction pair.

    How does this work? It seems that it is not possible to have a tactical robot with two exchange objects and have an exchange account code tied to the exchange object? There is a way!

    We added an OKEX futures exchange configuration in the Control Center - Exchange.

    img

    Click Save to configure it later.

    img

    This gives us two exchange configurations, but using the same API KEY configuration information.

    img

    What are the benefits of this? Of course, when it comes to writing strategies, design is simple!

    function main(){
        exchanges[0].SetContractType("quarter")        // 设置第一个添加的交易所对象 当前的合约为季度合约
        exchanges[1].SetContractType("this_week")      // 设置第二个添加的交易所对象,当前的合约为当周合约
        
        while (true) {
            var beginTime = new Date().getTime()       // 记录这次获取行情时起始的时间戳。
            var rA = exchanges[0].Go("GetTicker")      // 创建并发 线程去获取 第一个交易所对象,也就是季度合约的行情数据。
            var rB = exchanges[1].Go("GetTicker")      // 创建并发 线程去获取 第二个交易所对象,也就是当周合约的行情数据。
            
            var tickerA = rA.wait()                    // 并发的两个线程各自执行自己的任务,这里等待获取数据,A 等待时,B任务也在执行。
            var tickerB = rB.wait()                    // 所以这里看似是顺序执行,实际在底层是并发的。只不过获取的时候是顺序先获取A,在获取B。
            var endTime = new Date().getTime()         // 记录并发获取两个合约行情结束时的时间戳。
            
            if (tickerA && tickerB) {                  // 如果获取的数据没有问题,执行以下逻辑。
                var diff = tickerA.Last - tickerB.Last // 计算差价
                $.PlotLine("diff", diff)               // 使用画线类库把差价画在图表上。
                if (diff > 500) {                      // 如果差价大于500, 对冲套利(当然设置500 的差价是比较大的,很少见。)
                    // 对冲
                    rA = exchanges[0].Go("Sell", tickerA.Buy, 1)     // 并发线程创建 季度合约下卖单
                    rB = exchanges[1].Go("Buy", tickerB.Sell, 1)     // 并发线程创建 当周合约下买单
                    
                    var idA = rA.wait()           // 等待 返回下单结果,返回的是订单ID
                    var idB = rB.wait()           // ...
                }
                
                // ...
            }
            
            LogStatus(_D(), "并发获取两个合约行情耗时:", endTime - beginTime, "毫秒。")    // 显示在状态栏上时间,以便知道程序在执行。
            Sleep(500)
        }
    }
    

    The design strategy is so simple that it feels a lot simpler and the idea is clearer.

    This is a real-time drive:img

    As you can see, it only takes about 50 milliseconds for two contracts to be acquired at the same time.


Related

More

Difficult to quantifyThe last method is good.

bwxiaokWow, that's helpful!