Skip to content

Commit 96f50ab

Browse files
beta lib minor retouch
1 parent 4902e5f commit 96f50ab

13 files changed

+611
-119
lines changed

beta/libs/ArrayStatistics.pinescript

+21-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// @description Statistic Functions using arrays.
77
library(title="ArrayStatistics")
88

9-
import RicardoSantos/ArrayExtension/2 as ae
9+
import RicardoSantos/ArrayExtension/1 as ae
1010
import RicardoSantos/ArrayOperationsFloat/1 as aof
1111

1212
// @function Root Mean Squared
@@ -49,6 +49,25 @@ export skewness_pearson2 (float[] sample) => //{
4949
// https://www.statisticshowto.com/pearsons-coefficient-of-skewness/
5050
//}}}
5151

52+
// @function Pearson correlation coefficient measures the linear relationship between two datasets.
53+
// @param sample_a float array, sample with data.
54+
// @param sample_b float array, sample with data.
55+
// @returns float p
56+
export pearsonr(float[] sample_a, float[] sample_b) => //{
57+
int _size_a = array.size(id=sample_a)
58+
int _size_b = array.size(id=sample_b)
59+
if _size_a > 0 and _size_b > 0
60+
float _err_sum_ab = array.covariance(id1=sample_a, id2=sample_b)
61+
float _err_sum_a2 = array.stdev(id=sample_a)
62+
float _err_sum_b2 = array.stdev(id=sample_b)
63+
float _r = _err_sum_ab / (_err_sum_a2 * _err_sum_b2)
64+
//{
65+
// usage:
66+
float[] a = array.from(1.0, 2.0, 3.0, 4.0, 5.0)
67+
float[] b = array.from(10.0, 9.0, 2.5, 6.0, 4.0)
68+
plot(pearsonr(a, b))// 0.74
69+
//}}
70+
5271
// @function Kurtosis of distribution.
5372
// @param sample float array, data sample.
5473
// @returns float
@@ -72,7 +91,7 @@ export kurtosis (float[] sample) => //{
7291
//}}}
7392

7493
// @function Get range around median containing specified percentage of values.
75-
// @param values float array, Histogram array.
94+
// @param sample int array, Histogram array.
7695
// @param percent float, Values percentage around median.
7796
// @returns tuple with [int, int], Returns the range which containes specifies percentage of values.
7897
export range_int ( int[] sample, float percent ) => //{

beta/libs/FunctionBestFitFrequency.pinescript

+43-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,48 @@
66
// @description TODO: add library description here
77
library(title='FunctionBestFitFrequency')
88

9-
import RicardoSantos/FunctionArrayMovingAverage/1 as ma
10-
import RicardoSantos/ArrayExtension/2 as ae
9+
import RicardoSantos/ArrayExtension/1 as ae
10+
11+
// @function Moving Average values for selected data.
12+
// @param sample float array, sample data values.
13+
// @param length int, length to smooth the data.
14+
// @param ommit_initial bool, default=true, ommit values at the start of the data under the length.
15+
// @param fillna string, default='na', options='na', '0', 'avg'
16+
// @returns float array
17+
// errors:
18+
// length > sample size "Canot call array methods when id of array is na."
19+
export array_moving_average (float[] sample, int length, bool ommit_initial=true, string fillna='na') => //{
20+
int _size = array.size(id=sample)
21+
if _size > 1 and _size > length
22+
float[] _ma = array.copy(id=sample)
23+
float _nav = float(na)
24+
if fillna == '0'
25+
_nav := 0
26+
if fillna == 'avg'
27+
_nav := array.avg(id=sample)
28+
float[] _ma_total = array.new_float(0)
29+
for _i = 0 to _size-1
30+
array.push(id=_ma_total, value=array.get(id=sample, index=_i))
31+
if array.size(id=_ma_total) > length
32+
array.shift(id=_ma_total)
33+
if ommit_initial and _i < length
34+
array.set(id=_ma, index=_i, value=_nav)
35+
else
36+
float _avg = array.avg(id=_ma_total)
37+
array.set(id=_ma, index=_i, value=_avg)
38+
_ma
39+
//{
40+
// usage:
41+
int length = input.int(defval=6)
42+
bool ommit_init = input.bool(true)
43+
string fill_na = input.string(defval='na', options=['na', '0', 'avg'])
44+
rngs0 = array.from(79, 47, 5, 82, 31, 77, 58, 45, 15, 76, 76, 33, 47, 8, 1, 14, 65, 16, 16, 10, 60)
45+
string tex = str.format('S: {0}', rngs0)
46+
for _i = 2 to length
47+
tex += str.format('\n{0}: {1}', _i, str.tostring(array_moving_average(rngs0, _i, ommit_init, fill_na), '#.##'))
48+
if barstate.islast
49+
label.new(bar_index, 0.0, tex)
50+
//}}
1151

1252
// @function Search a frequency range for the fairest moving average frequency.
1353
// @param sample float array, sample data to based the moving averages.
@@ -20,7 +60,7 @@ export best_fit_frequency (float[] sample, int start, int end) => //{
2060
int _best = 0
2161
float _percent = -999
2262
for _freq = start to end
23-
float[] _avg = ma.array_moving_average(sample=sample, length=_freq, ommit_initial=false, fillna='avg')
63+
float[] _avg = array_moving_average(sample=sample, length=_freq, ommit_initial=false, fillna='avg')
2464
float _esum = 0.0
2565
for _i = 0 to _size-1
2666
float _erri = array.get(id=sample, index=_i) - array.get(id=_avg, index=_i)

beta/libs/FunctionDatestring.pinescript

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22
// © RicardoSantos
33

44
//@version=5
5+
// @description Methods to stringify date/time, altho there is already builtin support for it.
56
library(title='FunctionDatestring')
67

7-
import RicardoSantos/DebugConsole/2 as console
8+
import RicardoSantos/DebugConsole/1 as console
89
[__T, __C] = console.init(20)
910

1011
// @function check if string has 1 digits and prefixes a "0" if true.
1112
prefixdigit (string str) => (str.length(str) == 1 ? '0' : '') + str
1213

1314
// @function a stringified date stamp at specified unix time.
15+
// @param unixtime int unix timestamp.
16+
// @returns string
1417
export datetime (int unixtime=timenow) => //{
1518
_year = str.tostring(year(unixtime))
1619
_month = prefixdigit(str.tostring(month(unixtime)))
@@ -22,6 +25,8 @@ export datetime (int unixtime=timenow) => //{
2225
//}
2326

2427
// @function a stringified date stamp at specified unix time.
28+
// @param unixtime int unix timestamp.
29+
// @returns string
2530
export date_ (int unixtime=timenow) => //{
2631
_year = str.tostring(year(unixtime))
2732
_month = prefixdigit(str.tostring(month(unixtime)))
@@ -30,6 +35,8 @@ export date_ (int unixtime=timenow) => //{
3035
//}
3136

3237
// @function a stringified date stamp at specified unix time.
38+
// @param unixtime int unix timestamp.
39+
// @returns string
3340
export time_ (int unixtime=timenow) => //{
3441
_hour = prefixdigit(str.tostring(hour(unixtime)))
3542
_min = prefixdigit(str.tostring(minute(unixtime)))

beta/libs/HarmonicPattern.pinescript

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
//@version=5
55

6-
// @description Functions to detect harmonic patterns
6+
// @description Functions to detect/check harmonic patterns from provided values.
77
library("HarmonicPattern")
88

99
// @function Compute the price rate of the line AB divided by the the line BC

beta/libs/Math/MathSpecialFunctionsTestFunctions.pinescript

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ library(title='MathSpecialFunctionsTestFunctions')
1010
// https://github.com/mathnet/mathnet-numerics/blob/a50d68d52def605a53d129cc60ea3371a2e97548/src/Numerics/SpecialFunctions/TestFunctions.cs
1111

1212
// imports:
13-
import RicardoSantos/DebugConsole/2 as console
13+
import RicardoSantos/DebugConsole/1 as console
1414
[__T, __C] = console.init(25)
1515
import RicardoSantos/MathConstants/1 as constants
1616
// constants.Pi2() -> ackley()

0 commit comments

Comments
 (0)