Arrange Variables from high to low? like a makeshift sorting algorithm....

jphilli11

Member
Ok, I have 4 variables that I'd like to order to from lowest to highest. I can't think of anyway of doing this other than typing out all possible combinations in an if statement. Is there an easier way to figure out the variable placement order? E.g.

V1 = 1
v2 = .001
v3 = -2
v4 = 10

I want to know their position as such:
Pos1 = v3 (-2)
pos2 = v2 (.001)
pos3 = v1 (1)
pos4 = v4 (10)

Any help would be greatly appreciated!
 
Solution
Ok, I have 4 variables that I'd like to order to from lowest to highest. I can't think of anyway of doing this other than typing out all possible combinations in an if statement. Is there an easier way to figure out the variable placement order? E.g.

V1 = 1
v2 = .001
v3 = -2
v4 = 10

I want to know their position as such:
Pos1 = v3 (-2)
pos2 = v2 (.001)
pos3 = v1 (1)
pos4 = v4 (10)

Any help would be greatly appreciated!

here is a post with a couple of studies that sort data, that might give you some ideas.
https://usethinkscript.com/threads/rank-volume-1-10.9504/
Ok, I have 4 variables that I'd like to order to from lowest to highest. I can't think of anyway of doing this other than typing out all possible combinations in an if statement. Is there an easier way to figure out the variable placement order? E.g.

V1 = 1
v2 = .001
v3 = -2
v4 = 10

I want to know their position as such:
Pos1 = v3 (-2)
pos2 = v2 (.001)
pos3 = v1 (1)
pos4 = v4 (10)

Any help would be greatly appreciated!

here is a post with a couple of studies that sort data, that might give you some ideas.
https://usethinkscript.com/threads/rank-volume-1-10.9504/
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

here is a post with a couple of studies that sort data, that might give you some ideas.
https://usethinkscript.com/threads/rank-volume-1-10.9504/
this solution looks really good but I don't think it will apply to what I'm doing. The reference solution is comparing values of the same study/data across time (bars) whereas I'm setting all the variables on the same bar and trying to sort them on a single bar. So, while I can sort volume based on it's value by folding through the data (by the bar), I can't do this with 4 distinct variables on a single bar. I would need to find a way to plot them as a bar-based values on a single variable.

hmmm.....
 
Ok, I managed to figure this out though its a bit ugly. Below is the code/script for a brute force sorting algo that returns the relative position of a variable in a sequence of up to 10. I'm just using it to arrange potential Price Targets, formulated from various studies, at an alert event. E.G. bollinger upperband, the next fib retracement, SMA50, etc etc. I stack them in order of lowest to highest (from the entry price) and then evaluate for best short and long term targets.

I'm sure there are plenty of other uses for this so I figured I'd share it.


Code:
# simple sorting algorithm for a sequence of variables that cannot be contained in an array. Useful for organizing formulated Price Targets at a specific bar/event or other sequencing needs.


#
# Script by Jphilli11
# Notes:
#     1) Max variables supported (as is) is 10.
#     2) lowest position is 0, highest is 9
#     3) duplicate values will be assinged the same position value and will be the greater of the 2 potential positions. 
#        e.g. [ 1, 2, 2, -1  ]
#        [ 2 ] will be assigned position #3 in both cases and position #2 will be "empty"
#     4) Once position values are assigned they can easily be sorted/ranked for use later:
#        if v1p == 0 then ...else
#        if v2p == 0 .... etc etc.

declare upper;

def _bn = if !isnan(close) then barnumber() else _bn[1];
def _end = highestall(_bn);
def v1 = 1;
def v2 = 80;
def v3 = -4;
def v4 = 1.8;

script PainfulSort{
    input v1 = 99999.99;  #assign 9999.99 to any unspecified value
    input v2 = 99999.99;
    input v3 = 99999.99;
    input v4 = 99999.99;
    input v5 = 99999.99;
    input v6 = 99999.99;
    input v7 = 99999.99;
    input v8 = 99999.99;
    input v9 = 99999.99;
    input v0 = 99999.99;

    def r1 = v1 < v2;    #if the primary is less than next, increment 1
    def r2 = v1 < v3;
    def r3 = v1 < v4;
    def r4 = v1 < v5;
    def r5 = v1 < v6;
    def r6 = v1 < v7;
    def r7 = v1 < v8;
    def r8 = v1 < v9;
    def r9 = v1 < v0;

    #sum all increments to determine relative position in the list
    plot result = r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9;
#end script
};


#iterate through each variable as the primary for comparison. 
#first position in each script call should be the next variable in the sequence to evaluate.
#subtract from 10 as 10 is the maximum variables calculated (whether or not we're sorting 10).
def v1p = 10 - (PainfulSort(v1, v2, v3, v4)."result");
def v2p = 10 - (PainfulSort(v2, v1, v3, v4)."result");
def v3p = 10 - (PainfulSort(v3, v2, v1, v4)."result");
def v4p = 10 - (PainfulSort(v4, v2, v1, v1)."result");
#continue calling sort for each variable in the sequence. this example is only sorting 4


#display the results
addchartbubble(_bn == _end, close,
       "V1: " + v1 + " Pos: " + v1p +
    "\n V2: " + v2 + " Pos: " + v2p +
    "\n V3: " + v3 + " Pos: " + v3p +
    "\n V4: " + v4 + " Pos: " + v4p
    ,color.yellow, yes);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
377 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top