• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Improve the AlpahStochasticPivots indicator

Beltrame1

New member
VIP
Don`t know where I got this, but it was labeled WIP_AlpahStochasticPivots_v01 and would appreciate any improvements if possible, since it is a WIP

Code:
#      AlphaHisto_JQ_v2018_08_21b
 
##  r8.20.2018  activated 2nd sma for histo

##  8.21.2018  added Gaussian Filtered Trendline


declare lower;

#************************************************************************************;

#       Declare Inputs and Variables  

#************************************************************************************;


input debug = no;  # This is a Boolean Value

input Correlation_Index = "SPY";

input GaussianLength  = 5;

input GaussianLengthLong  = 20;

input betaDev = 7.0; #hint betaDev: adjust this value till the label indicates a minimum of 68.2% of closes inside the 1 St Dev bands.

input soundAlerts = { default "Silent", "Audible"};


#************************************************************************************;

#       PREPERATORY MATH FOR RS and SRatio

#************************************************************************************;

def open_index = open(Correlation_Index);

def High_index = high(Correlation_Index);

def Low_index = low(Correlation_Index);

def close_index = close(Correlation_Index);



def RStrength_open = if open_index == 0 then 0 else open / open_index;

def RStrength_high = if high_index == 0 then 0 else high / High_index;

def RStrength_low = if low_index == 0 then 0 else low / low_index;

def RStrength_close = if close_index == 0 then 0 else close / close_index;



def r_RStrength_open = CompoundValue("historical data" = RStrength_open, "visible data" = if IsNaN(r_RStrength_open[1]) then RStrength_open else r_RStrength_open[1]);

def r_RStrength_High = CompoundValue("historical data" = RStrength_High, "visible data" = if IsNaN(r_RStrength_High[1]) then RStrength_high else r_RStrength_high[1]);

def r_RStrength_low = CompoundValue("historical data" = RStrength_low, "visible data" = if IsNaN(r_RStrength_low[1]) then RStrength_low else r_RStrength_low[1]);

def r_RStrength_close = CompoundValue("historical data" = RStrength_close, "visible data" = if IsNaN(r_RStrength_close[1]) then RStrength_close else r_RStrength_close[1]);



def rRS_open =  r_RStrength_open;

def rRS_high =  r_RStrength_high;

def rRS_low =  r_RStrength_low;

def rRS_close =  r_RStrength_close;





#************************************************************************************;

#        Preparatory Math to draw Histogram Bars    

#        Plotting has been placed below to have average line drawn on top of Histogram Bars

#************************************************************************************;

def new_rs_open = (((RStrength_open / rRS_open) - 1) * 100);

def new_rs_high = (((RStrength_high / rRS_high) - 1) * 100);

def new_rs_low = (((RStrength_low / rRS_low) - 1) * 100);

def new_rs_close = (((RStrength_close / rRS_close) - 1) * 100); #...... new_rs is the histogram bars

#def delta_rs = (new_rs - new_rs[1]) / AbsValue(new_rs[1]);    #....  Changed formula to include absvalue on 2018-03-02





#************************************************************************************;

#       drawing specs for line that connects the histogram vlaues the new_rs_line

#



plot new_rs_line = new_rs_close;      #.....  this is the line that connects the histogram bars

new_rs_line.SetPaintingStrategy(PaintingStrategy.LINE);

new_rs_line.SetLineWeight(1);

new_rs_line.HideBubble();

new_rs_line.AssignValueColor(if new_rs_close > new_rs_close[1] 

                              then Color.LIGHT_GREEN

                           else if RStrength_close  < RStrength_close[1]   

                            then Color.LIGHT_RED

                            else Color.GRAY);

new_rs_line.Hide();





#************************************************************************************;

#       DRAW HORIZONTAL REFERENCE LINES ON STUDY  

#************************************************************************************;

#





plot zero_line = 0;

zero_line.SetDefaultColor (Color.WHITE);

plot plusOne = 1;

plusOne.SetDefaultColor (Color.UPTICK);

plot minusOne = -1;

minusOne.SetDefaultColor (Color.DOWNTICK);

#plot plusTwo = 2;

#plusTwo.SetDefaultColor (Color.UPTICK);

#plot minusTwo = -2;

#minusTwo.SetDefaultColor (Color.DOWNTICK);









#*******************************************************************************;



# Trying to achieve Normal Distribution

# Mobius

# V01.09.2015

#Hint: Plots a Gaussian Mean with Standard deviation Envelope. If Normal Distribution is met, then at minimum, 68.2% of the close values should be inside a One Standard Deviation Envelope and 95.4% of the close values should be inside a 2 Standard Deviation Envelope.



# Mobius

# V01.09.2015

def c2;

def w2;

def beta2;

def alpha2;

def G2;



plot GaussianMeanLong;

GaussianMeanLong.SetDefaultColor(Color.YELLOW);

GaussianMeanLong.setLineWeight(2);



c2 = new_rs_close;

w2 = (2 * Double.Pi / GaussianLengthLong);

beta2 = (1 - Cos(w2)) / (Power(1.414, 2.0 / betaDev) - 1 );

alpha2= (-beta2 + Sqrt(beta2 * beta2 + 2 * beta2));

G2 = Power(alpha2, 4) * c2 + 

                 4 * (1 – alpha2) * G2[1] – 6 * Power( 1 - alpha2, 2 ) * G2[2] + 

                 4 * Power( 1 - alpha2, 3 ) * G2[3] - Power( 1 - alpha2, 4 ) * G2[4];



GaussianMeanLong = G2;



#************************************************************************************;

#       draw histogram bars .. Code moved to bottom so that 

#        Mean Lines are more visable

#



plot Histo_Bars = new_rs_close;

Histo_Bars.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);

Histo_Bars.SetLineWeight(1);

Histo_Bars.HideBubble();

Histo_Bars.AssignValueColor(if new_rs_close > new_rs_close[1] 

                           then Color.LIGHT_GREEN

                           else if new_rs_close  < new_rs_close[1]   

                           then Color.LIGHT_RED

                           else Color.GRAY);





#************************************************************************************;

#     Add Labels denoting Daily and Weekly Performance Relative to Benchmark

#



input show_Text_Labels = yes;



AddLabel( show_Text_Labels, "Bar[0] " + AsPercent ((new_rs_close - new_rs_close[1]) / 100) + " vs " + Correlation_Index + " ",

     if new_rs_close > new_rs_close[1] * 1.02 then Color.UPTICK 

    else if new_rs_close < new_rs_close[1] * .98 then Color.DOWNTICK

        else Color.LIGHT_GRAY);



AddLabel( show_Text_Labels, Concat("3 Bars ", AsPercent ((new_rs_close - new_rs_Close[3]) / 100)),

     if new_rs_close > new_rs_Close[3] * 1.02 then Color.UPTICK 

    else if new_rs_close < new_rs_close[3] * .98 then Color.DOWNTICK

        else Color.LIGHT_GRAY);



AddLabel( show_Text_Labels, Concat("5 Bars ", AsPercent ((new_rs_close - new_rs_close[5]) / 100)),

     if new_rs_close > new_rs_close[5] * 1.02 then Color.UPTICK 

    else if new_rs_close < new_rs_close[5] * .98 then Color.DOWNTICK

        else Color.LIGHT_GRAY);



AddLabel( show_Text_Labels, Concat("10 Bars ", AsPercent ((new_rs_close - new_rs_close[10]) / 100)),

     if new_rs_close > new_rs_Close[10] * 1.02 then Color.UPTICK 

    else if new_rs_close < new_rs_close[10] * .98 then Color.DOWNTICK

        else Color.LIGHT_GRAY);



AddLabel(show_Text_Labels, Concat("20 Bars ", AsPercent ((new_rs_close - new_rs_close[20]) / 100)),

     if new_rs_close > new_rs_close[20] * 1.02 then Color.UPTICK 

    else if new_rs_close < new_rs_close[20] * .98 then Color.DOWNTICK

        else Color.LIGHT_GRAY);











# Simple Stochastic High Low Pivots

# Mobius

# 1.12.2019

#hint: Uses a stochastic model to reset the pivots. The larger the "n" value the greater the range between pivots.



# AlphaInvestor: BTW, the Stochastic Pivots and TMO work very well 

# together. Much better than RSI Pivots

#

# Mobius: I've been looking for something to replace ORB. I do that

# every now and then. I've been trading that Stochastic Pivot for a 

# bit now and do like it. Not sure I'd replace ORB with it but maybe. 

# Will take so more work. So far it has proven profitable to a high 

# degree.

#

# Mobius: This is the one I'm trading. I don't always follow the 

# signals. More times than not I'm in the trade before price crosses 

# the signal line. But I find the shaded areas very useful for 

# showing price fading a high or low. 

#

# Mobius: When the Stochastic gets to 50 both the high pivots and 

# low pivots will top each other. I find that to be a trading or 

# balance zone and not good for trading. When those areas start 

# flashing around - I know it time to go make another pot of coffee 

# and chill out. So there's a learning curve to that study too. 

# There always is. The other interesting part of that study is. 

# There's no future bars used to calculate the pivots. It's not 

# needed. So plots are always realtime

#

# Mobius: Pivots are really the key. So anything that can reset 

# looking for new pivots can be used. Pivots are long entry and 

# short entry with mean as stop. If you just trade as close to the 

# extremes as you can you don't have to lose much when wrong and if 

# you exit at the mean you don't have to wait for a quick reverse 

# that takes you back to that anyway. Just my view. Add to that the 

# notion of Risk On and a Risk Off trade and you can get pretty 

# comfortable making a profit and real brave on the runner

#

# Nube: Will have to look at using mean as an exit. The timing of 

# that tip might be perfect. Have done a fair amount of scripting 

# recently with that gaussian filter of yours to create a better mean.

#

# Mobius: Yep good choice



input n = 100; #hint n: Stochastic Calculation periods.

input RangeMult = 1.5; #hint RangeMult: Range Multiplier for Entry Line.



# Variables

def o_sshlp = new_rs_open;

def h_sshlp = new_rs_high;

def l_sshlp = new_rs_low;

def c_sshlp = new_rs_close;

def x = BarNumber();

def nan = Double.NaN;

# Stochastic

def stoch = ((c_sshlp - lowest(l_sshlp, n)) / (highest(h_sshlp, n) - lowest(l_sshlp, n)));

# Pivot High Variables

def hh = if stoch crosses above .5

         then h_sshlp

         else if h_sshlp > hh[1]

              then h_sshlp

              else hh[1];

def PHBar = if h_sshlp == hh

            then x 

            else nan;

# Pivot Low Variables

def ll = if stoch crosses below .5

         then l_sshlp

         else if l_sshlp < ll[1]

              then l_sshlp

              else ll[1];

def PLBar = if l_sshlp == ll 

                  then x 

                  else nan;

# Entry Variables

def R = Max(h_sshlp, c_sshlp[1]) - Min(l_sshlp, c_sshlp[1]);

def TS = TickSize();

def hhTR = if x == HighestAll(PHBar)

           then Round((RangeMult * R) / TS, 0) * TS

           else hhTR[1];

def llTR = if x == HighestAll(PLBar)

           then Round((RangeMult * R) / TS, 0) * TS

           else llTR[1];

# Plots

plot R1 = if x >= HighestAll(PHBar)

          then HighestAll(if IsNaN(c_sshlp[-1])

                          then hh

                          else nan)

          else nan;

     R1.SetStyle(Curve.LONG_DASH);

     R1.SetLineWeight(2);

     R1.SetDefaultColor(Color.RED);

     R1.HideTitle();

plot STO = if x >= HighestAll(PHBar)

           then HighestAll(if IsNaN(c_sshlp[-1])

                           then hh - hhTR

                           else nan)

           else nan;

     STO.SetDefaultColor(Color.RED);

     STO.HideTitle();

plot S1 = if x >= HighestAll(PLBar)

          then HighestAll(if IsNaN(c_sshlp[-1])

                          then ll

                          else nan)

          else nan;

     S1.SetStyle(Curve.LONG_DASH);

     S1.SetLineWeight(2);

     S1.SetDefaultColor(Color.GREEN);

     S1.HideTitle();

plot BTO = if x >= HighestAll(PLBar)

           then HighestAll(if IsNaN(c_sshlp[-1])

                           then ll + llTR

                           else nan)

           else nan;

     BTO.SetDefaultColor(Color.GREEN);

     BTO.HideTitle();

#plot price_ = HighestAll(if IsNaN(c_sshlp[-1])

#                         then c_sshlp

#                         else Double.NaN);

#     price_.SetStyle(Curve.LONG_DASH);

#     price_.SetDefaultColor(CreateColor(255, 178, 77));

#     price_.HideTitle();

AddCloud(STO, R1, color.light_red, color.light_red);

AddCloud(S1, BTO, color.light_green, color.light_green);

input showStochasticLabel = no;

AddLabel(showStochasticLabel, "Stochastic = " + AsPercent(Round(Stoch, 2)), if Stoch >= .5

                                     then color.green

                                     else color.red); 

alert(soundAlerts and c_sshlp crosses below STO, "STO", Alert.Bar, Sound.Ring); 

alert(soundAlerts && c_sshlp crosses above BTO, "BTO", Alert.Bar, Sound.Ring);  

# End Code Simple Fractal Pivot Study
 
Last edited by a moderator:

Similar threads

Top