Help with Script{}

jphilli11

Member
I'm having trouble using the Script{} functionality in a study. The code works when used inline but I can't seem to get the call to the defined script to work properly. Can someone take a look and let me know where I'm going wrong. right now I just have this closing out with plot = _c just to have a plot in place. Within the script is a variable called HL which I'll use later to change some of the parameters to determine high or low pivots. For now, everything is just set to find a high pivot.

Code:
declare upper;
#===================================================
#                INPUTS & GLOBAL VARIABLES
#===================================================
input Hide_Gap_Down = no;
input Pivot_Forward = 1;
Input Pivot_Backward = 10;
input _Forward = 1;
input _Backward = 10;
input Hide_Extreme_Deviations = yes;
#input Show__Pivot = yes;
#input Show_Price_Pivot = yes;
#input Show_Pivot_Connector = no;

#input show_price_trendline  = yes;
#input trend_price = close;
#input trend_deviations = 2.0;
#input trend_fullRange = Yes;
#input trend_length = 21;

def _BN  = BarNumber();
def _nan = Double.NaN;
def _L   = low;
def _H   = high;
def _O   = open;
def _C   = close;
def _V   = volume;


Script PivotPoints{

    input dat = high; #the data or study being evaluated
    input HL  = 0; #high or low pivot designation, 0 is low, 1 is high
    input PF  = 5; #pivot forward period
    input PB  = 10; #pivot backward period

    def _BN  = BarNumber(); #the current barnumber
    def _VStop;  #confirms that the lookforward period continues the pivot trend
    def _V;      #the Value at the actual pivot point
    def _VBar;   #the bar number at the pivot point
    def _PV;     #the previous pivot Value
    def _PVBar;  #the previous pivot bar number
    def _VDiff;  #the difference in values between last two pivot points
    def _VDist;  #the diffence in barnumbers between last two pivot points
    def _VSlope; #the Slope calculated using value and distance changes
    def _VPivot; #used for the pivot point connector line
    def _nan = double.nan;
 
    _VStop =
        fold a = 1 to PF + 1
        with b = 1 while b
        do dat > GetValue(dat, -a);

    _V =
        if _BN > PB
        and dat == Highest(dat, PB)
        and _VStop
        then dat else _NaN;

    _VBar   = if !IsNaN(_V) then _BN else _VBar[1];
    _PV     = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
    _PVBar  =
        if   _VBar != _VBar[1]
        then _PVBar[1] else
        _VBar;
    _VDiff  = AbsValue(_V) - AbsValue(_PV);
    _VDist  = _BN - _PVBar;
    _VSlope = if _V > _PV
              then 1 else
              if _V < _PV           
              then -1 else 0;
    _VPivot = _bn >= lowestAll(_PVBar);

    plot V      = _V;
    
    #plot Vstop  = _Vstop;   
    #plot VBar   = _Vbar;
    #plot PV     = _PV;
    #plot PVBar  = _PVBar;
    #plot Vdiff  = _Vdiff;
    #plot Vdist  = _Vdist;
    #plot VSlope = _Vslope;
    #plot VPivot = _Vpivot;
#end script
};
    
def PP_V = PivotPoints(     #call the to script above and sending 4 inputs
    dat = _H,
    HL = 1,
    PF = Pivot_Forward,
    PH = Pivot_Backward);



plot close = _C;
 
Solution
"PivotPoints" is already in use by a platform default study. You need to rename the script to something else. Also take out the variablename= stuff in the parameters when calling script.
"PivotPoints" is already in use by a platform default study. You need to rename the script to something else. Also take out the variablename= stuff in the parameters when calling script.
 
Solution

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

If anyone has any use for a script to find pivot points, here's the working result (thanks to Joshua) that now uses a single def line to find the price pivot low and another to find the price pivot high. You could replace the price variables with any study value to find similar results. Seems useful.


Code:
declare upper;
#===================================================
#                INPUTS & GLOBAL VARIABLES
#===================================================
input Hide_Gap_Down   = no;
input Pivot_Forward       = 1;
Input Pivot_Backward     = 10;

def _BN  = BarNumber();
def _nan = Double.NaN;
def _L     = low;
def _H    = high;
def _O    = open;
def _C    = close;
def _V    = volume;


Script FindPivots{
    input dat = high; #the data or study being evaluated
    input HL  = 0; #high or low pivot designation, -1 low, +1 high
    input PF  = 3; #pivot forward period
    input PB  = 10; #pivot backward period

    def _BN  = BarNumber(); #the current barnumber
    def _VStop;  #confirms that the lookforward period continues the pivot trend
    def _V;      #the Value at the actual pivot point
    def _VBar;   #the bar number at the pivot point
    def _PV;     #the previous pivot Value
    def _PVBar;  #the previous pivot bar number
    def _VDiff;  #the difference in values between last two pivot points
    def _VDist;  #the diffence in barnumbers between last two pivot points
    def _VSlope; #the Slope calculated using value and distance changes
    def _VPivot; #used for the pivot point connector line only
    def _nan = double.nan;

    _VStop =
        fold a = 1 to PF + 1
        with b = 1 while b
        do if HL > 0 then
            dat > GetValue(dat, -a) else
            dat < GetValue(dat, -a) ;
    if(HL > 0){
        _V =
            if _BN > PB
            and dat == Highest(dat, PB)
            and _VStop
            then dat else _NaN;
    }else{
        _V =
            if _BN > PB
            and dat == lowest(dat, PB)
            and _VStop
            then dat else _NaN;
    };

    _VBar   = if !IsNaN(_V) then _BN else _VBar[1];
    _PV     = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
    _PVBar  =
        if   _VBar != _VBar[1]
        then _PVBar[1] else
        _VBar;
    _VDiff  = AbsValue(_V) - AbsValue(_PV);
    _VDist  = _BN - _PVBar;
    _VSlope = if _V > _PV  then 1 else
              if _V < _PV  then -1 else 0;
    if(HL > 0){
         _VPivot = _bn >= highestAll(_PVBar);
    }else{
         _VPivot = _bn >= lowestAll(_PVBar);
    };   

    plot Vstop  = _Vstop;
    plot V      = _V;
    plot VBar   = _Vbar;
    plot PV     = _PV;
    plot PVBar  = _PVBar;
    plot Vdiff  = _Vdiff;
    plot Vdist  = _Vdist;
    plot VSlope = _Vslope;
    plot VPivot = _Vpivot;
    plot result = if !isNaN(_v) and _VStop then _V else _nan;
#end script
};

#examples of use for this:
def Price_Pivot_Low = FindPivots(_L, -1, Pivot_Forward, Pivot_Backward)."result"; 
def Price_Pivot_High = FindPivots(_H, 1, Pivot_Forward, Pivot_Backward)."result";
  

plot pivot_high =
    if !IsNAN(Price_Pivot_High) then Price_Pivot_High else _nan;
pivot_high.setpaintingstrategy(paintingstrategy.arrow_down);
pivot_high.setlineweight(3);


plot pivot_low =
    if !IsNAN(Price_Pivot_Low) then Price_Pivot_Low else _nan;
pivot_low.setpaintingstrategy(paintingstrategy.arrow_up);
pivot_low.setlineweight(3);
 
If anyone has any use for a script to find pivot points, here's the working result (thanks to Joshua) that now uses a single def line to find the price pivot low and another to find the price pivot high. You could replace the price variables with any study value to find similar results. Seems useful.


Code:
declare upper;
#===================================================
#                INPUTS & GLOBAL VARIABLES
#===================================================
input Hide_Gap_Down   = no;
input Pivot_Forward       = 1;
Input Pivot_Backward     = 10;

def _BN  = BarNumber();
def _nan = Double.NaN;
def _L     = low;
def _H    = high;
def _O    = open;
def _C    = close;
def _V    = volume;


Script FindPivots{
    input dat = high; #the data or study being evaluated
    input HL  = 0; #high or low pivot designation, -1 low, +1 high
    input PF  = 3; #pivot forward period
    input PB  = 10; #pivot backward period

    def _BN  = BarNumber(); #the current barnumber
    def _VStop;  #confirms that the lookforward period continues the pivot trend
    def _V;      #the Value at the actual pivot point
    def _VBar;   #the bar number at the pivot point
    def _PV;     #the previous pivot Value
    def _PVBar;  #the previous pivot bar number
    def _VDiff;  #the difference in values between last two pivot points
    def _VDist;  #the diffence in barnumbers between last two pivot points
    def _VSlope; #the Slope calculated using value and distance changes
    def _VPivot; #used for the pivot point connector line only
    def _nan = double.nan;

    _VStop =
        fold a = 1 to PF + 1
        with b = 1 while b
        do if HL > 0 then
            dat > GetValue(dat, -a) else
            dat < GetValue(dat, -a) ;
    if(HL > 0){
        _V =
            if _BN > PB
            and dat == Highest(dat, PB)
            and _VStop
            then dat else _NaN;
    }else{
        _V =
            if _BN > PB
            and dat == lowest(dat, PB)
            and _VStop
            then dat else _NaN;
    };

    _VBar   = if !IsNaN(_V) then _BN else _VBar[1];
    _PV     = if !IsNaN(_V) then GetValue(dat, _BN - _VBar[1]) else _PV[1];
    _PVBar  =
        if   _VBar != _VBar[1]
        then _PVBar[1] else
        _VBar;
    _VDiff  = AbsValue(_V) - AbsValue(_PV);
    _VDist  = _BN - _PVBar;
    _VSlope = if _V > _PV  then 1 else
              if _V < _PV  then -1 else 0;
    if(HL > 0){
         _VPivot = _bn >= highestAll(_PVBar);
    }else{
         _VPivot = _bn >= lowestAll(_PVBar);
    };  

    plot Vstop  = _Vstop;
    plot V      = _V;
    plot VBar   = _Vbar;
    plot PV     = _PV;
    plot PVBar  = _PVBar;
    plot Vdiff  = _Vdiff;
    plot Vdist  = _Vdist;
    plot VSlope = _Vslope;
    plot VPivot = _Vpivot;
    plot result = if !isNaN(_v) and _VStop then _V else _nan;
#end script
};

#examples of use for this:
def Price_Pivot_Low = FindPivots(_L, -1, Pivot_Forward, Pivot_Backward)."result";
def Price_Pivot_High = FindPivots(_H, 1, Pivot_Forward, Pivot_Backward)."result";
 

plot pivot_high =
    if !IsNAN(Price_Pivot_High) then Price_Pivot_High else _nan;
pivot_high.setpaintingstrategy(paintingstrategy.arrow_down);
pivot_high.setlineweight(3);


plot pivot_low =
    if !IsNAN(Price_Pivot_Low) then Price_Pivot_Low else _nan;
pivot_low.setpaintingstrategy(paintingstrategy.arrow_up);
pivot_low.setlineweight(3);
Can anyone help with a scanner for this?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
486 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