Pivots scripts

Have a QScript to share, or need help with programming? Post your comment here.
Simon.B
Posts: 60
Joined: Thu Jul 23, 2015 8:26 pm
Contact:

Pivots scripts

Post by Simon.B » Thu Mar 14, 2019 2:48 pm

Hi everyone,

There are quite a few Pivot scripts available for Wve59, but all of them connect horizontal pivot levels between days with vertical line.
Since my scripting abilities are bad, I wanted to ask if someone knows how to get rid of connecting vertical lines and draw actual pivot levels only.

Thanks,
Simon

User avatar
earik
Site Admin
Posts: 322
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: Pivots scripts

Post by earik » Thu Mar 14, 2019 6:47 pm

Hi Simon,

If you have a script you like, but just want to alter it, that could be done. There's a plot style called "ps_clear", which is basically invisible. At the bar at the end of the day, you can change your plot style from "ps_solid" (the default) to ps_clear, and then change it back to ps_solid on the first bar of the next day. The effect will be that the lines aren't connecting, and will look more like floating trendlines. It would look like this:

Code: Select all

my_pivot_level = <whatever you calculate in your script>;
plot1 = my_pivot_level;
if (day != day[-1]) {
  style1 = ps_clear;
}
else {
   style1 = ps_solid;
}
So what I did was check to see if the day on the very next bar is the same as the day on the current bar (day != day[-1]), and if it was different, switch to ps_clear, otherwise just go with the regular ps_solid;

The other workaround is to use trendlines instead of plot1, plot2, etc. That's a little trickier because you have to figure out the right numbers of bar in the day for your chart, but a helpful keyword in that case is bars_per_day. So if you compute your pivots on the first bar of the day, you can draw trendlines in right then that stretch the entire length of the day. That would be like:

Code: Select all

if (day != day[1]) # this is first bar of the day
{
   pivotlevel = <do your calculation here>;
   ref = trendline(barnum, pivotlevel, barnum + bars_per_day, pivotlevel, red);
}
Either way should work. If you get stuck, you can always post your code to the qscript forum and we'll help. If you're concerned about your calculation method, just replace it with something basic to get the plotting solved, then put it back in once that's fixed.

Earik

Simon.B
Posts: 60
Joined: Thu Jul 23, 2015 8:26 pm
Contact:

Re: Pivots scripts

Post by Simon.B » Thu Mar 14, 2019 8:15 pm

Hi Earik,

Thanks for quick reply. I tried first option, but it is stil printing vertical lines. Please take a look at the script below.

input:piv_color(black),s1_color(red),r1_color(lime),s2_color(fuchsia),r2_color(aqua),
s3_color(silver),r3_color(silver),thickness(1);

if (barnum==barsback) {
dayhi,daylo=0;
fullday=false;
}

if (day!=day[1]) {
piv=(dayhi+daylo+close[1])/3;
diff=dayhi-daylo;
s1=2*piv-dayhi;
r1=2*piv-daylo;
s2=piv-diff;
r2=piv+diff;
s3=s2-piv-s1;
r3=r1-piv+r2;

dayhi=h;
daylo=l;
fullday=true;
}

if (h>dayhi) dayhi=h;
if (l<daylo) daylo=l;

if (fullday==true) {
plot1=piv;
if (day != day[-1]) {
style1 = ps_clear;
}
else {
style1 = ps_solid;
}
color1=piv_color;
plot2=s1;
color2=s1_color;
plot3=r1;
color3=r1_color;
plot4=s2;
color4=s2_color;
plot5=r2;
color5=r2_color;
plot6=s3;
color6=s3_color;
plot7=r3;
color7=r3_color;
thickness1, thickness2, thickness3, thickness4,
thickness5, thickness6, thickness7=thickness;
}

Thanks
Simon

User avatar
earik
Site Admin
Posts: 322
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: Pivots scripts

Post by earik » Fri Mar 15, 2019 7:33 pm

Hi Simon,

Thanks for sharing the code. I made a mistake writing that last section off the top of my head, which is part of the problem. Should be day!=day[1], rather than day[-1]. :oops: The other part is that you have to set the style for all the plots, in your case 7 of them. So if you change it to this...

Code: Select all

if (day!=day[1]) {
   style1, style2, style3, style4, style5, style6, style7 = ps_clear;
}
else {
   style1, style2, style3, style4, style5, style6, style7 = ps_solid;
}
...then it should work. I've built it on my end just to make sure, and it looks good. :D Check it out and let me know if that doesn't get you going.

Earik

Simon.B
Posts: 60
Joined: Thu Jul 23, 2015 8:26 pm
Contact:

Re: Pivots scripts

Post by Simon.B » Fri Mar 15, 2019 8:49 pm

Hi Earik,

That did the job, looks good !!!

Thanks a lot,
Simon

abacaba
Posts: 80
Joined: Sat Aug 15, 2015 5:02 pm
Contact:

Re: Pivots scripts

Post by abacaba » Sun Mar 17, 2019 10:57 pm

Here's another template for plotting pivot levels that lets the user select the open as a term also. It's flexible--you can add all sorts of things, don't have to use those canned pivot calcs, can develop modular transforms if you are so inclined, stuff like that.

Earik, how do get those nice looking box formats for posting code that you used here?

Todd

#Flexible script for plotting pivot levels.
Input:pivot_color(white),s1_color(aqua),r1_color(black), thickness(1);
IF (barnum == barsback)
{
startbar,endbar=barnum;
bpd= bars_per_day;
}
IF (day[-1]!= day )
{
myclose=close;myhigh=high;mylow=low;myopen=open[bars_per_day-1];
startbar=barnum+1;endbar=barnum+bpd+1;
For (i=-1 to bpd-1 by 1)
{
IF(low<mylow)mylow=low;
IF (high>myhigh)myhigh=high;
}
pivot=(myclose+mylow+myhigh)/3;
s1=(2*pivot)-myhigh;
r1=(2*pivot)-mylow;
# etc.
}
If ((startbar>=10) and (endbar>startbar))
{
ref1=trendline(startbar,pivot,endbar,pivot,pivot_color);
ref2=trendline(startbar,s1,endbar,s1,s1_color);
ref3=trendline(startbar,r1,endbar,r1,r1_color);
#etc.
}

User avatar
earik
Site Admin
Posts: 322
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: Pivots scripts

Post by earik » Mon Mar 18, 2019 5:32 pm

Hey Todd,

Thanks for sharing the script. To get the code formatting, click the button just above the box where you type your replies:
todd_code.png
code button circled!
todd_code.png (11.11 KiB) Viewed 65 times
Earik

abacaba
Posts: 80
Joined: Sat Aug 15, 2015 5:02 pm
Contact:

Re: Pivots scripts

Post by abacaba » Mon Mar 18, 2019 7:30 pm

Thanks, Earik.

Since we're talking about pivots, I've been stuck with a more generalized pivot scripting question. Is there a way to convert absolute price (such as measured by say, the W59 price counter) to bar numbers for charting time projections? I'm not referring to the actual techniques, just an open ended scripting solution with two hotspots for a selected H/L. Time to time, price to price, no problem. Price to barcount, a problem.

Todd

User avatar
earik
Site Admin
Posts: 322
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: Pivots scripts

Post by earik » Tue Mar 19, 2019 5:43 pm

Hi Todd,

You'd have to write one that performed the calculation. If you are talking about converting a high-low range to bars, you could do it with trendlines. So:

Code: Select all

if (barnum == barsback) 
{
   # get the prices, and a starting point
   price1 = hotspot_to_price(1);
   price2 = hotspot_to_price(2);
   bar1 = hotspot_to_bar(1);
   bar2 = hotspot_to_bar(2);

  #connect the price values
  ref = trendline(bar1,price1,bar2,price2,red);

  #convert to time
  time_count = abs(price1-price2); # this is straight points-to-bars
  
  #plot time, from bar2 as starting point
  ref = trendline(bar2,price2,bar2+time_count,price2,red);
}
Is that what you meant?

Full disclosure: I wrote this off the top of my head, so there may be typos in there. ;) Hopefully you get the gist of it though! Obviously, there are ways to clean this up, like using vertical trendlines for time projections, etc. I didn't want to get too fancy before understanding what you were trying to do though.

Earik

abacaba
Posts: 80
Joined: Sat Aug 15, 2015 5:02 pm
Contact:

Re: Pivots scripts

Post by abacaba » Wed Mar 20, 2019 12:24 am

Hi Earik,

Yes, that does the trick. I was stuck on that "plot time" routine. Just a matter of putting things together now. Thanks!

Todd

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests