OPEN-SOURCE SCRIPT

Statistics • Chi Square • P-value • Significance

The Statistics • Chi Square • P-value • Significance publication aims to provide a tool for combining different conditions and checking whether the outcome is significant using the Chi-Square Test and P-value.


🔶 USAGE

The basic principle is to compare two or more groups and check the results of a query test, such as asking men and women whether they want to see a romantic or non-romantic movie.



We calculate the Chi-Square Formula, which is:

Χ² = Σ ( (Observed Value − Expected Value)² / Expected Value )


In this publication, this is:



Together with the 'Degree of Freedom', which is (rows − 1) × (columns − 1), the P-value can be calculated.

In this case it is P-value: 0.02462

A P-value lower than 0.05 is considered to be significant. Statistically, women tend to choose a romantic movie more, while men prefer a non-romantic one.

Users have the option to choose a P-value, calculated from a standard table or through a math.ucla.edu - Javascript-based function (see references below).

Note that the population (10 men + 10 women = 20) is small, something to consider.

Either way, this principle is applied in the script, where conditions can be chosen like rsi, close, high, ...

🔹 CONDITION

Conditions are added to the left column ('CONDITION')

For example, previous rsi values (rsi[1]) between 0-100, divided in separate groups

snapshot

🔹 CLOSE

Then, the movement of the last close is evaluated

  • UP when close is higher then previous close (close[1])
  • DOWN when close is lower then previous close
  • EQUAL when close is equal then previous close


It is also possible to use only 2 columns by adding EQUAL to UP or DOWN

  • UP
  • DOWN/EQUAL


or

  • UP/EQUAL
  • DOWN


In other words, when previous rsi value was between 80 and 90, this resulted in:

  • 19 times a current close higher than previous close
  • 14 times a current close lower than previous close
  • 0 times a current close equal than previous close


However, the P-value tells us it is not statistical significant.

NOTE: Always keep in mind that past behaviour gives no certainty about future behaviour.

A vertical line is drawn at the beginning of the chosen population (max 4990)

snapshot

Here, the results seem significant.

🔹 GROUPS

It is important to ensure that the groups are formed correctly. All possibilities should be present, and conditions should only be part of 1 group.

snapshot

In the example above, the two top situations are acceptable; close[1] against close[2] can only be higher, lower or equal.

The two examples at the bottom, however, are very poorly constructed.

Several conditions can be placed in more than 1 group, and some conditions are not integrated into a group. Even if the results are significant, they are useless because of the group formation.

A population count is added as an aid to spot errors in group formation.

snapshot

In this example, there is a discrepancy between the population and total count due to the absence of a condition.

snapshot

The results when rsi was between 5-25 are not included, resulting in unreliable results.

🔹 PRACTICAL EXAMPLES

In this example, we have specific groups where the condition only applies to that group.
For example, the condition rsi > 55 and rsi <= 65 isn't true in another group.
Also, every possible rsi value (0 - 100) is present in 1 of the groups.

rsi > 15 and rsi <= 25 28 times UP, 19 times DOWN and 2 times EQUAL. P-value: 0.01171

When looking in detail and examining the area 15-25 RSI, we see this:

snapshot

The population is now not representative (only checking for RSI between 15-25; all other RSI values are not included), so we can ignore the P-value in this case. It is merely to check in detail. In this case, the RSI values 23 and 24 seem promising.

NOTE: We should check what the close price did without any condition.
If, for example, the close price had risen 100 times out of 100, this would make things very relative.

In this case (at least two conditions need to be present), we set 1 condition at 'always true' and another at 'always false' so we'll get only the close values without any condition:

snapshot

Changing the population or the conditions will change the P-value.

snapshot

snapshot

snapshot


In the following example, the outcome is evaluated when:

  • close value from 1 bar back is higher than the close value from 2 bars back
  • close value from 1 bar back is lower/equal than the close value from 2 bars back


snapshot

Or:
  • close value from 1 bar back is higher than the close value from 2 bars back
  • close value from 1 bar back is equal than the close value from 2 bars back
  • close value from 1 bar back is lower than the close value from 2 bars back


snapshot

In both examples, all possibilities of close[1] against close[2] are included in the calculations. close[1] can only by higher, equal or lower than close[2]

Both examples have the results without a condition included (5 = 5 and 5 < 5) so one can compare the direction of current close.


🔶 NOTES

• Always keep in mind that:
  • Past behaviour gives no certainty about future behaviour.
  • Everything depends on time, cycles, events, fundamentals, technicals, ...


• This test only works for categorical data (data in categories), such as Gender {Men, Women} or color {Red, Yellow, Green, Blue} etc., but not numerical data such as height or weight. One might argue that such tests shouldn't use rsi, close, ... values.

• Consider what you're measuring

For example rsi of the current bar will always lead to a close higher than the previous close, since this is inherent to the rsi calculations.

snapshot

• Be careful; often, there are na-values at the beginning of the series, which are not included in the calculations!

snapshot

• Always keep in mind considering what the close price did without any condition

• The numbers must be large enough. Each entry must be five or more. In other words, it is vital to make the 'population' large enough.

• The code can be developed further, for example, by splitting UP, DOWN in close UP 1-2%, close UP 2-3%, close UP 3-4%, ...

• rsi can be supplemented with stochRSI, MFI, sma, ema, ...


🔶 SETTINGS

🔹 Population

• Choose the population size; in other words, how many bars you want to go back to. If fewer bars are available than set, this will be automatically adjusted.

🔹 Inputs

At least two conditions need to be chosen.

snapshot

• Users can add up to 11 conditions, where each condition can contain two different conditions.

🔹 RSI

• Length

🔹 Levels

• Set the used levels as desired.

🔹 Levels

• P-value: P-value retrieved using a standard table method or a function.

• Used function, derived from Chi-Square Distribution Function; JavaScript




🔶 REFERENCES


chi-squareeducationalp-valuesignificancestatisticstatisticalstatistics

Script open-source

Dans le plus pur esprit TradingView, l'auteur de ce script l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par nos Règles. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.

Vous voulez utiliser ce script sur un graphique ?


LuxAlgo Dev: luxalgo.com
PineCoder: pinecoders.com

- We cannot control our emotions,
but we can control our keyboard -
Aussi sur:

Clause de non-responsabilité