Scan for StochasticFast?


Category:
0
0

Hi Pete,

Please help setting up scan for Buy and Sell with following conditions:

Scan for Buy: KPeriod and DPeriod must both be below “20” (over_sold) and cross over.

Scan for Sell: KPeriod and DPeriod must both be above “80” (over_bought) and cross over.

Here’s the StochasticFast:

#
# TD Ameritrade IP Company, Inc. (c) 2008-2017
#

declare lower;

input over_bought = 80;
input over_sold = 20;
input KPeriod = 10;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = {default “No”, “On FastK”, “On FastD”, “On FastK & FastD”};

plot FastK = reference StochasticFull(over_bought, over_sold, KPeriod, DPeriod, priceH, priceL, priceC, 1, averageType).FullK;
plot FastD = reference StochasticFull(over_bought, over_sold, KPeriod, DPeriod, priceH, priceL, priceC, 1, averageType).FullD;
plot OverBought = over_bought;
plot OverSold = over_sold;

def upK = FastK crosses above OverSold;
def upD = FastD crosses above OverSold;
def downK = FastK crosses below OverBought;
def downD = FastD crosses below OverBought;

plot UpSignal;
plot DownSignal;
switch (showBreakoutSignals) {
case “No”:
UpSignal = Double.NaN;
DownSignal = Double.NaN;
case “On FastK”:
UpSignal = if upK then OverSold else Double.NaN;
DownSignal = if downK then OverBought else Double.NaN;
case “On FastD”:
UpSignal = if upD then OverSold else Double.NaN;
DownSignal = if downD then OverBought else Double.NaN;
case “On FastK & FastD”:
UpSignal = if upK or upD then OverSold else Double.NaN;
DownSignal = if downK or downD then OverBought else Double.NaN;

Marked as spam
Posted by (Questions: 23, Answers: 57)
Asked on August 18, 2017 5:19 pm
514 views
0
Private answer
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 18, 2017 9:13 pm
0

Pete, please check the code below to see if my logic correct. Thank you!
————-
declare lower;

input over_bought = 80;
input over_sold = 20;
input KPeriod = 14;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;

def FastK = reference StochasticFull(over_bought, over_sold, KPeriod, DPeriod, priceH, priceL, priceC, 1, averageType).FullK;
def FastD = reference StochasticFull(over_bought, over_sold, KPeriod, DPeriod, priceH, priceL, priceC, 1, averageType).FullD;
def OverBought = over_bought;
def OverSold = over_sold;

def pivotLowFastK = FastK[2] > FastK[1] and FastK[1] < FastK and FastK < OverSold; def pivotLowFastD = FastD[2] > FastD[1] and FastD[1] < FastD and FastD < OverSold; def pivotHighFastK = FastK[2] < FastK[1] and FastK[1] > FastK and FastK > OverBought;
def pivotHighFastD = FastD[2] < FastD[1] and FastD[1] > FastD and FastD > OverBought;

def lastPivotLowPriceFastK = if pivotLowFastK then FastK[1] else if FastK < OverSold then lastPivotLowPriceFastK[1] else FastK; def lastPivotLowPriceFastD = if pivotLowFastD then FastD[1] else if FastD < OverSold then lastPivotLowPriceFastD[1] else FastD; def lastPivotHighPriceFastK = if pivotHighFastK then FastK[1] else if FastK > OverBought then lastPivotHighPriceFastK[1] else FastK;

def lastPivotHighPriceFastD = if pivotHighFastD then FastD[1] else if FastD > OverBought then lastPivotHighPriceFastD[1] else FastD;

# use this to scan for FastK & FastD pivot low cross but still below OverSold
plot scan = lastPivotLowPriceFastK crosses above lastPivotLowPriceFastD and FastK < OverSold and FastD < OverSold; # use this to scan for FastK & FastD pivot high cross but still above OverBought #plot scan = lastPivotHighPriceFastK crosses below lastPivotHighPriceFastD and FastK > OverBought and FastD > OverBought;

( at August 19, 2017 6:42 am)
0
Private answer

In response to Kim’s reply on August 18th, here is the solution I would use. I examined Kim’s code and found it jumps through a lot of hoops that I find unnecessary. If you check if FullD of the previous bar is above overbought or below oversold, you already know that FullK is also in that condition, because it is on the other side of FullD before the cross. So just create the statements that define the crosses and then check for when a cross has occurred. The final condition is to check if FullD was in correct relation to overbought/oversold on the bar immediately prior to the cross.

I am sure this has already been demonstrated on the site. Everyone seems to find a use for this sort of thing. So I suppose adding one more solution on this topic will make it that much easier for folks to find.

declare lower;
input over_bought = 80;
input over_sold = 20;
input KPeriod = 14;
input DPeriod = 3;
input priceH = high;
input priceL = low;
input priceC = close;
input averageType = AverageType.SIMPLE;
def FullK = reference StochasticFull(over_bought, over_sold, KPeriod, DPeriod, priceH, priceL, priceC, 1, averageType).FullK;
def FullD = reference StochasticFull(over_bought, over_sold, KPeriod, DPeriod, priceH, priceL, priceC, 1, averageType).FullD;
def crossAbove = FullK[1] < FullD[1] and FullK > FullD;
def crossBelow = FullK[1] > FullD[1] and FullK < FullD;
plot scan = FullD[1] < over_sold and crossAbove;
#plot scan = FullD[1] > over_bought and crossBelow;

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on August 19, 2017 8:48 am
0

Wow! This code is much simpler. Thank you, Pete!

( at August 19, 2017 2:29 pm)