Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal

# Combinatorics - library for MetaTrader 5

Views:
2934
Rating:
Published:
2012.12.27 11:56
Updated:
2016.07.11 10:09

Initial library of combinatorics functions.

Combinatorics is a branch of mathematics, that studies discrete structures, sets (combinations, permutation, arrangement and enumerative_combinatorics) and relations on them (for instance, partially_ordered_set). Combinatorics is related to many other areas of mathematics such as algebra, geometry, probability theory and has a wide spectrum of application in different areas of knowledge (for instance in genetics, computer science, statistical physics).

1) The first function:

```//+------------------------------------------------------------------+
//|                     Number factorial                             |
//+------------------------------------------------------------------+
double factorial(int x);```

Factorials are often used in combinatorics, number theory and functional analyses.

Factorial is an extremely fast growing function. It grows faster than polynomial of any degree and faster than exponential function (but slower than double exponential function).

That is why returned value must be of double type. It is done because factorial grows too fast and already the 13! does not fit in the ranges of a long type.

2) The second function:

```//+------------------------------------------------------------------+
//|                 Combination (combination)                        |
//+------------------------------------------------------------------+
double combination(int N,int k);```

Combinations allow to select unique set of elements from some set consisting of N elements. Each set will have k elements.

Suppose that there are 10 trading signals. And for the trading system only 3 are to use. Then, for the further analyses all possible sets (3 signals) from the set of 10 signals should be selected.

Hence the number of such sets will be the number of combinations:

C(k,N) = C(3,10) = 120.

I.e. there are 120 unique combinations on 3 signals on each.

3) The third function:

```//+------------------------------------------------------------------+
//|            Combination (combination) with repetitions            |
//+------------------------------------------------------------------+
double _combination(int N,int k);```

Combinations with repetitions admit that in the set some element can be presented more than one time. Suppose that there are five fruits. The set consisting of 3 elements has 2 oranges and 1 apple. Such set is a combination with repetitions. Orange repeated.

As for the set of 10 signals and possible sets (3 signals), we find a number of combinations with repetitions:

~C(k,N) = ~C(3,10) = 220.

4) The fourth function:

```//+------------------------------------------------------------------+
//|                        Arrangement                               |
//+------------------------------------------------------------------+
double arrangement(int N,int k);```

Arrangements differ from the combinations in a fact that not the kit is important but the order of elements in the set. Suppose that there are 2 sets with 3 elements: apple-orange-banana, apple-banana-orange. In terms of the combinations we have only one combination (one set of participants). But concerning arrangement there are 2 combinations (participants changed order in a set).

It is clear that there will be more arrangements than combinations. For instance, from the set of signals (10 signals) and possible sets (3 signals) we can get as many arrangements:

A(k,N) = A(3,10) = 720.

5) The fifth function:

```//+------------------------------------------------------------------+
//|                   Arrangement with repetitions                   |
//+------------------------------------------------------------------+
double _arrangement(int N,int k);```

I.e. as in case with combinations admit that elements in a set can be repeated.

Then for the set of 10 signals and possible sets (3 signals) we will get such a number of arrangements with repetitions:

~A(k,N) = ~A(3,10) = 1000.

6) The sixth function:

```//+------------------------------------------------------------------+
//|                       Permutation                                |
//+------------------------------------------------------------------+
double permutation(int N);```

Permutation allows to know how many approaches can be to change the order in the set of elements.

So for the set of signals (10 signals) we can get as many permutations:

P(N) = P(10) = 3 628 800.

And for the set of 3 signals we can get as many permutations:

P(3) = 6.

Number of permutations returns factorial. Thus such a difference between the number of permutation for 3 and 10 elements.

7) The seventh function:

```//+------------------------------------------------------------------+
//|                  Permutation with repetitions                    |
//+------------------------------------------------------------------+
double _permutation(int &nM[]);```

I.e. as in case with combinations admit that elements in a set can be repeated.

Only as a parameter passes an array in which each element indicates how many times it can be repeated.

For instance, there are 3 signals, each is repeated one time, then:

~P(N) = ~P({1,1,1}) = 6.

I.e. ~P({1,1,1}) = P(3).

Suppose that the first signal can be repeated 2 times, then:

~P({2,1,1}) = 12.

Translated from Russian by MetaQuotes Software Corp.
Original code: https://www.mql5.com/ru/code/1197

The class for drawing Moving Average using the ring buffer

The class is designed for calculation of Moving Averages (Moving Average) using the algorithm of the ring buffer.

The class to create the ring buffer

The class allows to organize the mini time series, indicator minibuffers, short sized buffers to store intermediate stream data inside the Expert Advisor or indicator.

Exp_ColorTrend_CF

The Exp_ColorTrend_CF trading system is based on change of the trend direction displayed by the ColorTrend_CF indicator

The class for drawing the ATR using the ring buffer

The class is designed for calculation of the Average True Range indicator (Average True Range, ATR) using the algorithm of the ring buffer.