How Do Store Just SwingHighs or Lows in Variable w/o the 0’s or Double.Nans?


0
0

In ThinkScript,

I want to put the actual SwingHigh or SwingLows in individual Arrays such that they are right next to one another so I can compare them in finding a 3 times tested Support or Resistance Level.

 

This is my code so far. As you can see I have tried a few things, nothing is working. How do you make it work if it can or not? Am I even close?

 

 

########### Straight Channel Boundaries##########################

####Definition of SwingHigh and Low############3

 

def swinghigh = if high > high[1] and high > high[2] && high> high[3]&& high> high[4] && high> high[5] and high> high[-1] && high > high[-2] && high> high[-3] && high> high[-4] then 1 else 0;

 

def swinglow = if low <= low[1] and low < low[2] && low<low[3] && low<low[4] && low<low[5] and low <= low[-1] and low < low[-2] && low <low[-3]&& low < low[-4] then 1 else 0;

 

def SwingHighArr= if Swinghigh==1 then high else SwingHigharr[1];

def SwingLowArr= if Swinglow==1 then low else SwingLowarr[1];

 

#########Assigned then Bar# of last SwingHigh or Low, and the How Far Back######3

 

def HBarNo = if SwingHigh==1 then BarNumber() else HBarNo[1];

def HBarNoDiff = BarNumber() – HBarNo;

 

def LBarNo = if SwingLow==1 then BarNumber() else LBarNo[1];

def LBarNoDiff = BarNumber() – LBarNo;

 

def LastSwingHigh= High[HBarNo];     # !!! ILLEGAL , MUST BE CONSTANT                 # +4

def Last SwingLow= Low[LBarNo];     # !!!ILLEGAL, MUST BE CONSTANT

 

def PenSwingHigh=High[HBarNoDiff+1];               # +4?

def PenSwingLow=Low[LBarNoDiff+1 ];

 

def ThirdSwingHigh=High[HBarNoDiff+HBarNoDiff[1]+1];

def ThirdSwingLow=Low[LBarNoDiff+LBarNoDiff[1]+1];

 

def SameHighRange=   LastSwingHigh< 1.02* PenSwingHigh && LastSwingHigh > 0.98* PenSwingHigh

&& PenSwingHigh< 1.02*ThirdSwingHigh && PenSwingHigh< 0.98*ThirdSwingHigh;

 

def SameLowRange=   LastSwingLow < 1.02* PenSwingLow   && LastSwingLow > 0.98* PenSwingLow ;

&& PenSwingLow< 1.02*ThirdSwingLow && PenSwingLow< 0.98*ThirdSwingLow;

 

def Consolidation= SameHighRange or SameLowRange;

 

plot VMaxHigh=if(Consolidation ,MaxHigh[0],Double.Nan);

VMaxHigh.setDefaultColor(getColor(6)); # Blue

VMaxHigh.setLineWeight(2);

 

 

plot VMaxLow= if(Consolidation,MaxLow[0],Double.Nan);

VMaxLow.setDefaultColor(getColor(5));   # Red

VMaxLow.setLineWeight(2);

 

RESOLVED
Marked as spam
Posted by (Questions: 13, Answers: 18)
Asked on June 7, 2018 9:30 pm
632 views
0
Private answer

Well I’m probably not going to spend enough time on this to address all the items required to get this working. So I’ll just point to the bits and pieces I found while reading through the code.

I suggest you take a look at the solution I provided here: https://www.hahn-tech.com/ans/flat-base-breakout-scans/

It is a much less complicated approach to a very similar goal.

First, a note on style. From all the examples we find in the Thinkscript language reference we find that variable names always begin with lower case letters. Methods (provided by the thinkscript language) begin with upper case letters. Following this style makes the code much easier to read. Fundamental data types for open, high, low, close and volume always start with lower case. You will find this style guide matches the majority of what is recommended for C based languages.

Now to the code:

def LastSwingHigh= High[HBarNo]; # !!! ILLEGAL , MUST BE CONSTANT # +4
def Last SwingLow= Low[LBarNo]; # !!!ILLEGAL, MUST BE CONSTANT
def PenSwingHigh=High[HBarNoDiff+1]; # +4?
def PenSwingLow=Low[LBarNoDiff+1 ];
def ThirdSwingHigh=High[HBarNoDiff+HBarNoDiff[1]+1];
def ThirdSwingLow=Low[LBarNoDiff+LBarNoDiff[1]+1];

Since a constant is required as the index for the high and low data, use the GetValue() method: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue.html

But I don’t see that I have addressed the question posed in the title of your question:

How Do Store Just SwingHighs or Lows in Variable w/o the 0’s or Double.Nans?

And I only see this coming into play in your plot statements. But perhaps I missed what you intended for me to find. The short answer is, you don’t. Not possible in Thinkscript. All variables are arrays (excepted enumerated variables and inputs). Every bar on the chart is an index value. Our code runs every statement from top to bottom for every bar (index value) on the chart. It is the Grand Loop run by the platform over which we have no control.

I’ll give an example to explain. Let’s take an array like we might find in Java or C++:

Note: I’m not taking any time to properly initialize this for Java or C++

myArray = {0, 1, 2, 3, 4, 5, 6};

Let’s say we wanted to loop through that array and create a new one that contained only the even number values:

evenArray = {0, 2, 4, 6};

While this is commonly done in Java and C++, we can NEVER do this with Thinkscript. NEVER. Best we can do is:

evenArray = {0, Double.NaN, 2, Double.NaN, 4, Double.NaN, 6};

And we simply have to work with that.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 8, 2018 8:02 am
0

I appreciate the answer that we can not loop thru arrays to edit them of unwanted content or to access them. I will stop trying anything like that in the future.

I still have not got GetValue() going the way I want it so that I can access the 3 spots for the in my variable array, swinghigharr or high or HBarNo.

I am getting stuck here.

lastswinghigh=swinghigharr[0]; # Solid
# now the 2nd back array
def i=GetValue(SwHChBarDiff,SwHChBarDiff);
def secswingHigh= SwingHighArr[i];
with big warning about the index in the variable array must be a constant.

Again, my concept is to acess the last 3 Swing Highs which are deposited in both High or SwingHighArr at 3 different barnumber()’s or in the first cell of the array[0], the 2nd cell of the array some i or HBarNoDiff[0] away and the 3rd Swing High some HBarNoDiff[0] +HBarNoDiff[1] away.

How do I get around this index must be constant problem. I thot the GetValue was going to do it, but apparently I was wrong.

def i=GetValue(SwHChBarDiff,SwHChBarDiff+1);
def PenSwingHigh= SwingHighArr[i];

( at June 10, 2018 3:54 pm)
0

I am not at the computer today so I can offer limited feedback. From your example it seems you are not employing the GetValue() method in the correct manner. Let me provide a very simplistic example to get you pointed in the right direction.

def varyingIndexValue = ;
def closeAtVaryingIndexValue = GetValue(close, varyingIndexValue);

The problem here is that the second argument of the GetValue() method is how many bars back (to the left) of the current bar. So if the varying index value is 10 for the current bar, this example will return the close from 10 bars back of the current bar. Ok? It does not return the close for bar number 10. Everything is referenced from whatever bar is currently being evaluated.

In your example, you are applying the same index value for both arguments. Which is nonsense. Forgive my bluntness. The first argument must be the data stream you are trying to read. The second argument is how many bars to the left in that data steam.

These two statements are indentical:
def myValue = close[10];
def myValue = GetValue(close, 10);

This should clear things up entirely.

( at June 10, 2018 4:15 pm)
0

Well, I completed it. It took awhile, but I have tested it on a few different charts. Yes, your explanation of GetValue was key. My first response before it being deleted explained that I could not really grasp the definition of GetValue() from their manual, as well as the comparable script you asked me to look at the first time- I finally did with more concentration. Glad you picked that up anyway.

In the script, just used the static offset parameter and was able to get by . I do not understand the dynamic offset parameter except in a loop. If you can share anything else about GetValue() that might improve on their brief definition, I would appreciate it. I will mark it resolved tomorrow.

( at June 12, 2018 7:51 pm)
0

Glad you got things working. As to the third argument of the GetValue() method. I have never had a need to employ that so I don’t have much to say. I will point out this portion of the description given in the Thinkscript language reference:

max offset: “Defines the maximum value of the dynamic offset…”

So if max offset has a value of 12 then your dynamic offset is capped at 12.

( at June 12, 2018 8:17 pm)