Modify Slow Stochastic to show signals within X bars

MBF

Active member
2019 Donor
Hi there,

I am trying to have the typical TOS Slow Stoch give me the option to change "in how many bars". so say the stoch k crosses above stoch d within 2 bars, 1 bar, etc.

Here are the original study perimeters;

Code:
#
# TD Ameritrade IP Company, Inc. (c) 2008-2020
#

declare lower;

input over_bought = 80;
input over_sold = 20;
input KPeriod = 10;
input DPeriod = 10;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default "No", "On SlowK", "On SlowD", "On SlowK & SlowD"};

plot SlowK = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullK;
plot SlowD = reference StochasticFull(over_bought,over_sold,KPeriod,DPeriod,priceH,priceL,priceC,3,averageType).FullD;
plot OverBought = over_bought;
plot OverSold = over_sold;

def upK = SlowK crosses above OverSold;
def upD = SlowD crosses above OverSold;
def downK = SlowK crosses below OverBought;
def downD = SlowD crosses below OverBought;

plot UpSignal;
plot DownSignal;
switch (showBreakoutSignals) {
case "No":
    UpSignal = Double.NaN;
    DownSignal = Double.NaN;
case "On SlowK":
    UpSignal = if upK then OverSold else Double.NaN;
    DownSignal = if downK then OverBought else Double.NaN;
case "On SlowD":
    UpSignal = if upD then OverSold else Double.NaN;
    DownSignal = if downD then OverBought else Double.NaN;
case "On SlowK & SlowD":
    UpSignal = if upK or upD then OverSold else Double.NaN;
    DownSignal = if downK or downD then OverBought else Double.NaN;
}

UpSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");
DownSignal.setHiding(showBreakoutSignals == showBreakoutSignals."No");

SlowK.setDefaultColor(GetColor(5));
SlowD.setDefaultColor(GetColor(0));
OverBought.SetDefaultColor(GetColor(1));
OverSold.SetDefaultColor(GetColor(1));
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
DownSignal.SetDefaultColor(Color.DOWNTICK);
DownSignal.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
 

MBF

Active member
2019 Donor
Is this correct? Am I on the right path?

Code:
declare lower;

input AggregationPeriod = AggregationPeriod.ONE_Day;
input AggregationPeriod = AggregationPeriod.FOUR_HOUR;
input AggregationPeriod = AggregationPeriod.ONE_HOUR;
input AggregationPeriod = AggregationPeriod.THIRTY_MIN;
input  AggregationPeriod = AggregationPeriod.FIFTEEN_MIN;
 

BenTen

Administrative
Staff member
Staff
VIP
Your original question was about "change "in how many bars". so say the stoch k crosses above stoch d within 2 bars" but the snippet you just posted deals with the multi-timeframe aspect. What exactly are you trying to achieve? Turning the indicator into an MTF version?
 

MBF

Active member
2019 Donor
Hi Ben!

I wanted to scan for k crossing over d within so many bars on any timeframe. I did this but it's not working. It also doesn't allow me to change the aggregation. I looked through one note but Im not precisely sure what Im looking for so just used TOS education.

Timea ggregate or aggregation period or both?
 

MBF

Active member
2019 Donor
How about this @BenTen? Better?

Code:
declare lower;
input Time_Frame = AggregationPeriod.MIN;
input Time_Frame = AggregationPeriod.Ten_Min;
input Time_Frame = AggregationPeriod.Thirty_Min;
input Time_Frame = AggregationPeriod.One_Hour;
input Time_Frame = AggregationPeriod.Two_Hour;
input Time_Frame = AggregationPeriod.Four_Hour;

AggregationPeriod = Day;
AggregationPeriod = Four_Hour;
AggregationPeriod = Two_Hour;
AggregationPeriod = Hour;
AggregationPeriod = Thirty_Min;
AggregationPeriod = Ten_Min;
 

BenTen

Administrative
Staff member
Staff
VIP
Time aggregation has nothing to do with what you're trying to achieve. I would imagine the "within x bar" condition is more appropriate for your scanner.
 

MBF

Active member
2019 Donor
It's coming up red @BenTen

<condition> within 1 bar
<condition> within
<length> bars

declare lower;
input Time_Frame = MIN;
input Time_Frame = HOUR;
input Time_Frame = Thirty_MIN;
input Time_Frame = fifteen_Min;
input Time_Frame = TwoHour;
input Time_Frame = Four_Hour;
input Time_Frame = Day;
input Time_Frame = Week;
 

Similar threads

Top