@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:
// http://forum.amibroker.com/t/stochastic-scanning-using-multiple-tf/3790
// 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;
TimeFrameRestore();
// 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!