Visualization module to build trading strategies - in-depth
Tags: visualization, trading, fmzquant, strategy
Published: 2024-05-10 02:27:35

- ### Logic module type

  - #### 1. Conditions module
    
    This module is used to combine conditional judgment, and the module can add multiple conditional branches.
    Click the small "gear" icon to add conditional branches.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d7tgbgel8vrdq5ibzemg.png)
    
The example use of conditional modules is tested in combination with the next module.
    
  - #### 2. Numerical comparison module

    This module is used to compare two numerical values (you can also think of this module and the numerical module combined into an expression module), and return to a boolean value.
    This module can determine whether the value on both sides is "greater than", "less than", "greater than or equal to", "less than or equal to", "not equal to", or "equal to".
    The tenon (concave) positions on both sides of the drop-down box option can be embedded in numerical and variable modules (as long as the modules that return numerical values ​​can be embedded).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqu4bvu9kohq99sjqj8l.png)
  
    An example of using the "conditional block" and "value comparison block" to form a judgment value:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tryf3k19jyhws24iwagy.png)

It can be seen that this example has a total of 3 branches when judging the conditions.
    
    Like the use of if statements in JavaScript strategies.
    ```
    function main () {
        var a = 1
        var b = 2
        if (a > b) {
            Log("a > b")
        } else if (a < b) {
            Log("a < b")
        } else {
            Log("a == b")
        }
    }
    ```
    
  - #### 3. logical OR, logical AND module
    
    This module is used to perform "OR operation" or "AND operation". The tenon (concave) positions on both sides of the drop-down box in the middle of the module are embedded in the module involved in the calculation (it can be a module that returns to a boolean value or numerical value).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hzg583amlbeqkar5wl9h.png)
    
    Before testing this module specifically, let's learn the module representing the boolean value "true"/"false" (set with the drop-down box), the module representing the boolean value "not", and the module representing the null value first.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/un2kx3zdzppj2cid2oo7.png)
    
    - Null modules, which represent the null value in code, are used to compare whether some variables are null.
    - Modules with boolean values ​​of "true"/"false" represent the true / false values ​​in the code, which are used to determine the boolean values ​​returned by certain modules or combinations of modules.
    - The "NO" module of boolean value represents the ! in the code, which is used for Boolean NOT calculations.

    Test example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkg47t79wrq75o79io1l.png)
    
    We can see that the "logical OR, logical AND" modules can also be nested.
    
    Nested module splicing example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56sep4at4y0cfpuz6j3j.png)
    
    Equivalent JavaScript strategy code:
    ```
    function main () {
        var a = 1 
        var b = 2
        Log((true && !false) || (a==b))
        Log(null)
    }
    ```
    
!false is not false, that is, true value, logical expression: (true && !false): two true values ​​are performed and calculated, the result is true.
    a == b is obviously not equal, so it is false.
    A true value and a false value perform a logic or calculation, and the result is true.
    
    Operation result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lanlvdlxmwvxbzws02z.png)
    
  - #### 4. Ternary operation module
  
    This module is also called the assertion module, and its function is similar to the ternary operator in some programming languages.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u6mamz1qtb6j5l1u1s26.png)

    This module can also be nested. The essence of the ternary operation module is also conditional judgment logic, and its function is similar to that of the conditional module.
    
    Use the ternary operation module to reconstruct the teaching example of the "conditional module" above.
    The following example:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uwn4g5423wmpphrpw6j6.png)
    
    As the strategy code written in JavaScript:
    ```
    function main () {
        var a = 1
        var b = 2
        Log(a < b ? b : (a > b ? a : "equal"))
    }
    ```
    
    If you are interested, you can adjust the values ​​of a and b and run the backtest.
    
- ### Mathematics module type

  > In many of the previous examples, we have used some mathematics modules to a greater or lesser extent.
  > Next we explain some mathematics modules that have not studied yet.

  - #### 1. Trigonometric module

    Note that the parameter filled in the tenon (concave) position of this module is an angle value, not a radian value.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ugvtq41qrzkwumj0w5u0.png)

  - #### 2. Circumference numerical module

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/khuujqqzka9889vbqgot.png)
    
    Backtesting prints:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nxi5bs9a4sk2elnopdof.png)     
    
  - #### 3. Get a random number module within a range of values
    
    This module takes a random number within a set range of values, and the module tenon (concave) position can directly fill in the value, or use a variable as the start and end value of the random range.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fvky2l8kf6we74y067rs.png)
    
    As the strategy code written in JavaScript:
    ```
    function main () {
        var a = 1
        var b = 9
        Log(_N(a + Math.random() * (b - a), 0))
    }
    ```
    
  - #### 4. Limited value range module

    This module will limit the variable filled in the first tenon (concave) position, and take the value according to the range set by the second and third tenon (concave) positions.
    If it is greater than the maximum value of this range, the module returns to the maximum value of this range, and if it is less than the minimum value of this range, the module returns to the minimum value.
    If it is within this range, the value of the variable itself that takes the first tenon (concave) position is returned.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/spr7znipwo1qnuqapk2c.png)

    As the strategy code written in JavaScript:
    ```
    function main () {
        var a = 9
        Log(Math.min(Math.max(2, a), 5))
    }
    ```
    
  - #### 5. Remainder module

    This module performs numerical remainder operation on the numerical module set at the tenon (concave) position.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q9lmdn90q4ra9sx92ylm.png)
    
    Divide 64 by 10 to get 6 and the remaining 4.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bm1qn51ct03lilm4pkr5.png)
 
  - #### 6. List calculation module

    This module performs calculations on a certain list module (functions such as calculating the sum of the elements in the list).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dyifxfs47z0cr77ch4ym.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8j54gz8gr1s6yb575ja6.png)
    
    As the strategy code written in JavaScript:
    ```
    function sum (arr) {
        var ret = 0
        for (var i in arr) {
            ret += arr[i]
        }
        return ret 
    }
    
    function main () {
        var b = 2
        var a = 1
        Log(sum([b,a,b,a,a]))
    }
    ```


> Visualization example strategy:

- https://www.fmz.cn/strategy/121404
- https://www.fmz.cn/strategy/129895
- https://www.fmz.cn/strategy/123904
- https://www.fmz.cn/strategy/122318

More strategies are available at: https://www.fmz.cn/square

> Other articles in the series

- [Visualization Module to Build Trading Strategy - Advanced Understanding](https://www.fmz.com/bbs-topic/9815)
- [Visualization Module to Build Trading Strategy - First Acquaintance](https://www.fmz.com/bbs-topic/9814)

### Boring programming can be easily done by building blocks, try it out, it's very interesting!

From: https://blog.mathquant.com/2022/07/11/visualization-module-to-build-trading-strategies-in-depth.html