TradingView
Duyck
15 Th02 2021 23:04

ma sorter ('sort by array' example)- JD 

Tesla, Inc.NASDAQ

Mô tả

////////////////////////////////////////////////////////////////
//////////////// ma sorter / Array sorting function //////////////////
////////////////////////////////////////////////////////////////

This script gives an example how you can sort the items in one array, by using the values in another array.



In the example here, we want to sort moving averages of different lengths by there values
as we don't have mixed arrays (yet) in Pinescript, one solution could be to store this in 2 arrays:

- one (string) array to store the ma names
- one (float) array to store the ma values


The "f_sort_by_array( )" function in this script allows you to sort the ma names by using the ma values
and sorts the two arrays together, as if the were one 2D matrix, where the rows are sorted.



With the "f_sort_by_array" function, both the (string) array1 and array2 are sorted by the values of array2, the function returns the two sorted arrays.


IMPORTANT NOTE: the output has to be stored in TWO NEW (sorted) ARRAYS, Pinescript doesn't allow function arguments to be modified by the function itself
this is why the output goes to ma_names_sorted and not ma_names !!!!



Feel free to use this function in your scripts to get more contorl over your arrays.

Gr, JD.


Bình luận
moneycat8
hey duyck
i have a heatmap that tracks similar information:

//@version=2
study("ma heatmap","ma htmap",overlay=false)

len1=input(10,"sma", integer)
//len2=input(50,"EMA B", integer)
//len3=input(100,"EMA C", integer)

out1= security(tickerid, "60", sma(close, len1))
out2= security(tickerid, "240", sma(close, len1))
out3= security(tickerid, "D", sma(close, len1))
out4= security(tickerid, "W", sma(close, len1))
out5= security(tickerid, "M", sma(close, len1))

h1 = hline(0)
h2 = hline(1)
h3 = hline(2)
h4 = hline(3)
h5 = hline(4)
h6 = hline(5)

fill(h1, h2, color=close > out5 ? lime : red, transp=25)
fill(h2, h3, color=close > out4 ? lime : red, transp=25)
fill(h3, h4, color=close > out3 ? lime : red, transp=25)
fill(h4, h5, color=close > out2 ? lime : red, transp=25)
fill(h5, h6, color=close > out1 ? lime : red, transp=25)

however i can seem to add labels to show the moving average name. could you help guide me in getting this complete?
ChrisMoody
Nice Script...
Duyck
@ChrisMoody,
Thanks, (like most people) I started scripting in Pine learning on your scripts.
It's nice to be able to give something back!!

Gr, JD.
andr_eeew
Amazing!
syrinxflunki
cheers mr !
NightCommando
Great script - what type of condition would you use to trigger buy/sell signals based on the position of the EMAs?
Basically, if the EMAs are in one specific order, how and where could we trigger buy/sell signals?
Any opinion and idea is highly welcome.
xel_arjona
Nice implementation...! Was looking to implement a method to make array.sort := output{indexex} instead of :=output{values}. This do the trick already with a related array... Cheers!
Thêm nữa