Stochastic scanning using Multiple TF

Helo to all...


I'm trying to code stochastic scanning using Weekly TF as trend and trading signals from Daily TF. I coded what I understand from the amibroker forum, but not getting results as expected. It's showing some wrong trade signals on Daily TF. When Weekly stochastic not below the 80, it is showing sell signal on daily time frame.

My strategy is :
When the Weekly Stochastic above 80, all the stochastic up cross signals on daily time frame is buy.
When the weekly stochastic below 20, all the stochastic down cross signals on daily time frame is sell.

Can any body please correct this, where I'm wrong.

Thanks and regards,


WStB=StochK(12,3) > 80; 
Wsts=20 < StochK(12,3);


Buy = Wstb AND Crossbuy;
Sell = wsts AND Crosssell;

After TimeFrameRestore, where is your TimeFrameExpand ?

Hi awilson

Many thanks for the suggestion. I have added that, but not fruitful results.

I have added Timeframeexpand, and the modified AFL.

WStB=StochK(12,3) > 80; 
Wsts=20 < StochK(12,3);

Wbuy=TimeFrameExpand(wstb, inWeekly);
Wsell=TimeFrameExpand(wsts, inWeekly);


Buy = Wbuy AND Crossbuy;
Sell = wsell AND Crosssell;

Any suggestions or help highly appreciated from the experts.

Thanks and regards,

@dragon what do you mean by “not fruitful” results?

Any basic idea, before any extensive backtesting, may need some tweaking and IMHO it is a good practice to try to visualize it using both charts and explorations.

I’m no expert at developing trading systems, but, for instance, I would rewrite your code as posted below (the extra lines and verbose comments show how I will go over it in order to verify what I’m doing).

Keep on mind that this IS NOT production code (up to you to verify/fix it as needed) since it is the kind of sample code that I will only use to see what are the “moving parts” of the system and how they interact (my goal here is to get an overview of what the provided rules are supposed to achieve).

The exploration should be applied to a single (“current”) ticker or a watchlist, over a long period of time (dates from/to) since moving in the StochK from 20 to 80 and vice-versa in the weekly timeframe may take multiple weeks/months.

You may want also to “apply” it to a chart to visually see the intersections of the stochastic curves.

Sample code:


// Used to show only True (1) values in exploration 
// False (0) are displayed as empty cells
function nil(a) { 
   return iif(a > 0, a, Null);

TimeFrameSet( inWeekly );
	WeeklyStK = StochK( 12, 3 ); // used by plot
	WstB = StochK( 12, 3 ) > 80;
	WstS = StochK( 12, 3 ) < 20;

// we need to Expand previous signals taken in weekly timeframe
WeeklyStKExp = TimeFrameExpand(WeeklyStK, inWeekly);
WkStBuyExp = TimeFrameExpand(Wstb, inWeekly);
WkStSellExp = TimeFrameExpand(Wsts, inWeekly);
/* As an alternative set the signals out of the TimeFrameSet()
   block but the result will be the same */
// WkStBuyExp2 = WeeklyStKExp > 80; // same as WstbExp
// WkStSellExp2 = WeeklyStKExp < 20; // same as WstsExp

crossBuy = Cross( StochK( 12, 3 ), StochD( 12, 3, 3 ) );
crossSell = Cross( StochD( 12, 3, 3 ), StochK( 12, 3 ) );

// Try to reverse part of the logic...
BuySignal = WkStBuyExp AND crossBuy;
SellSignal = WkStSellExp AND crossSell;

// Check conditions using an exploration
Filter = 1;
AddColumn(C, "Close");
AddColumn(nil(WkStBuyExp), "Wk StK > 80", 1.0);
// AddColumn(nil(WkStBuyExp2), "2 Wk StK > 80", 1.0);
AddColumn(nil(WkStSellExp), "Wk Stk < 20", 1.0);
// AddColumn(nil(WkStSellExp2), "2 Wk Stk < 20", 1.0); 
AddColumn(nil(crossBuy), "Cross Buy", 1.0);
AddColumn(nil(crossSell), "Cross Sell", 1.0);
AddColumn(nil(buySignal), "Buy signal", 1.0);
AddColumn(nil(sellSignal), "Sell signal", 1.0);

// Optionally reduce number of Buy/Sell with ExRem
Buy = BuySignal;
Sell = SellSignal;
Buy = ExRem(Buy, Sell);
Sell = ExRem(Sell, Buy);

// Chart indicator
GraphXSpace = 10;
SetChartOptions( 1, chartShowDates, chartGridMiddle, 0, 0, 0 );
Plot(WeeklyStKExp, "Wk St K", colorYellow, styleLine|styleThick);
// As an addition idea see what happens when you smooth the above line
Plot(EMA(WeeklyStKExp, 5), "Wk St K", colorWhite, styleLine|StyleDashed);
Plot(StochK( 12, 3 ), "Day St K", colorOrange);
Plot(StochD( 12, 3, 3 ), "Day St D", colorRose);
Plot(80, "", colorBrightGreen, styleLine|styleNoTitle|styleNoLabel); 
Plot(20, "", colorRed, styleLine|styleNoTitle|styleNoLabel); 
PlotShapes(Buy * shapeUpArrow, colorAqua, 0, 80, 6);
PlotShapes(Sell * shapeDownArrow, colorAqua, 0, 20, 6);

As a part of testing the idea I reversed part of your Buy/Sell logic:

BuySignal = NOT WkStBuyExp AND crossBuy;
SellSignal = NOT WkStSellExp AND crossSell;

Here, for a certain period, this variation seemed to produce better results using the S&P500 stocks, but I did no extensive verification so it may just be another unfruitful idea…

What I want to stress here is that you should try to use all the tools that AmiBroker offerse you to test your own ideas until you reach your goal!

Happy hunting!



Thanks for the AFL.

Here I'm attaching scanning results window. On 1/22/2017 weekly stochastic > 80 but in daily time frame it's showing sell signal. Like that When the weekly stochastic in down i..e, below 80 and slipping down scan window showing buy signals.

I'm unable to figure out how to solve.


1 Like

@dragon, as I said in my previous post, the code I posted was intended to show you how to “debug” your idea. With the exploration, you have all the data points to verify when the conditions “as presently coded” are true (1) or false (0 - in my sample this are empty cells).

The column "BuySignal and “Sell Signal” are the ones that show you the result of the code

BuySignal = WkStBuyExp AND crossBuy;
SellSignal = WkStSellExp AND crossSell;

(for now ignore that later the ExRem() calls may remove some redundant signals)

If you sort the results on this columns it is easier to verify if the code is right or not (I mean is doing what you want to achieve).
In your screenshot, I do not see any problem: the BuySignal column shows 1 (True) when both the “Wkl St > 80” and the “Cross buy” are also showing 1 (both are True).

And in that screenshot, there is NO SellSignal. On 1/22/2107 you get a “Cross Sell” set to 1 (True) but since the “Wk Stk > 80” is set to 1 (True) the actual sell signal is NOT generated since the logic operation (1 AND 0) gives 0 as a result. Only (1 AND 1) will give you 1 as a result.

But maybe you wanted to achieve something else. In such a case, if the code results DO NOT correspond to your desired goal you have to fix/modify the code: the best way to actually learn how to code is to try on your own until it works!

The code is working, but sometimes it is showing wrong signals. I don’t know how to modify the logic. My idea for this :

When the weekly stochastic > 80 AFL scan/exploration will show all the up crossover signals on daily TF.
When the weekly stochastic <20 AFL scan/exploration will show all the down crossover signals on daily TF.

I request you to , with the above logic can you modify your code.