Visualization Module to Build Trading Strategy - Advanced Understanding (2)

in fmz •  last year 

6. Cancel order module

23.png

This module is used to cancel the order.

There are many scenarios that require such operations when writing strategies:

Cancel all current pending orders.

There is no doubt that the "Cancel order module" must be used. While learning the cancel order module, we can use [5] to get pending orders of the current trading pair module, and combine to achieve this function.

First of all, in order to test the cancellation of all orders, it is not obvious to place an order. We start to place 2 orders, their prices and quantities are different to distinguish the two orders.

24.png

Use the "Traverse every element in the list" module in the "Loop" module to traverse the orders in the current list of pending orders.

25.png

During traversal, each order retrieved is assigned a value to the variable module order (created in the variable module type, as shown below:)

26.png

Use the "Util" module:

27.png

Take out the order ID, pass it to the tenon (concave) position of the "Cancel order" module, and the "Cancel order" module executes the order cancellation.

Backtest operation:

28.png

Use the JavaScript strategy description:

function main () {
    var id = exchange.Buy(_C(exchange.GetTicker).Last - 10, 0.1)
    Log(id)
    var id2 = exchange.Buy(_C(exchange.GetTicker).Last - 12, 0.2)
    Log(id2)
    var orders = exchange.GetOrders()
    Log(orders)
    for (var i in orders) {
        var order = orders[i]
        Log(exchange.CancelOrder(order.Id))
    }
}

7. Module to get the details of an order based on its order ID

29.png

The tenon (concave) position of the module is connected with an order ID variable module, and the order details can be returned.

30.png

Note the order returned after running:

31.png

Compared with the running results in the example [5], it can be found that the printed order is a separate order information without [] brackets.
Because the example [5] returns a list, but this example returns a separate order information (obtained based on the ID variable module on the tenon position passed in by the module).

The above example is similar to executing JavaScript strategy:

function main () {
    var id = exchange.Buy(_C(exchange.GetTicker).Last - 10, 0.1)
    Log(exchange.GetOrder(id))
}

8. Futures trading module
We will learn the above modules one by one and we set the test exchange as commodity futures.

Backtesting settings:

32.png

The following example performs backtest based on the settings.

  • Judge the connection status module between CTP commodity futures and futures company server

33.png

Commodity futures have opening time and closing time. When the market is closed, it cannot be connected.

  • Set contract module

34.png

When the object of the exchange is configured as a futures exchange, if the exchange does not set up a contract and obtains the market information directly, an error will be reported.

We set the contract as MA909, the main contract of methanol at present.

In this way, the latest price value in the current tick market of the MA909 contract is obtained.

  • Set the order direction module for futures trading
    In the execute orders module

35.png

The order direction needs to be specified, because the futures have:
buy: open long positions
sell: open short positions
closebuy: close long positions
closesell: close short positions
Four directions (there are two more directions for commodity futures: closebuy_today for closing long positions today and closesell_today for closing short positions today).

For example, if the order module is set as "buy", there are two meanings of opening long positions and closing short positions, which is ambiguous.
Therefore, the "Set the order direction module for futures trading" module is required to set a clear order direction.

36.png

Backtesting display:

37.png

Like the JavaScript strategy code:

function main () {
    while (true) {
        if (exchange.IO("status")) {
            exchange.SetContractType("MA909")
            Log(exchange.GetTicker().Last)
            exchange.SetDirection("buy")
            Log(exchange.Buy(1000, 1))
            throw "stop"
        } else {
            Log("The commodity futures front-end processor is not connected")
        }
        Sleep(1000)
    }
}

9. Digital currency futures trading module
The use of digital currency futures is basically the same as that of commodity futures in [8] above

  • Taking OKEX as an example, the contract code can be:
    this_week: this week
    next_week: next week
    quarter: quarter
    swap: perpetual

  • BitMEX :
    XBTUSD
    ETHUSD

  • Set leverage module

38.png

It is used to set the leverage of digital currency futures.

#Note: Backtesting is not supported.

Like JavaScript strategy:

function main () {
    exchange.SetMarginLevel(10)
}

Examples of visualization strategies:

https://www.fmz.com/strategy/121404
https://www.fmz.com/strategy/129895
https://www.fmz.com/strategy/123904
https://www.fmz.com/strategy/122318
For more strategies, please refer to: https://www.fmz.com/square

Other articles in this series

The boring programming can be easily completed by building blocks. It's very interesting to try!

From: https://blog.mathquant.com/2022/12/13/visualization-module-to-build-trading-strategy-advanced-understanding.html

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!