Hi. I'm trying to tell Amibroker that when market conditions change according to certain indicators, it should switch from the weekly frame it's usually applying, sell all stocks and buy new ones in a monthly frame according to a different rule, until the market conditions change again. than it should sell the stocks it has and restore original weekly frame. i don't succeed in writing this without mistakes. can you help?
You should be using TimeFrame functions that don't "switch" but allow to combine rules from different timeframes when needed.
thanks. however i couldn't find how to conditionally use TimeFrame functions , such as if certain condition apply, then use monthly buy/sell rules, but on other conditions use weekly rules. i'll write an examplein plain english:
Amibroker is set to a weekly time frame. if rsi>70 then i want it to sell all stocks, switch to monthly time frame, and buy/sell according to another condition, say buy=sar()<c , until rsi<70/ then sell all stocks and return to a weekly time frame and to the usual buy/sell signals.
could you help? thanks!
There is NO way to switch timeframe conditionally. That is possible only if you use barcount-1 loop but that will make your code slow as hell. So all you can do is compute whatever timeframes you need in advance and use them whereever you need.
For example
myIndicator_Monthly=timeframecompress(myIndicator, inMonthly);
myIndicator_Weekly=timeframecompress(myIndicator, inWeekly);
myIndicator_Daily=timeframecompress(myIndicator, inDaily);
myIndicator_Hourly=timeframecompress(myIndicator, inHourly);
if (rsi>70)
use myIndicator_Monthly;
else
use myIndicator_Weekly;
It is funny when people say "there is no way".
There is always a way. There is a way when there is skill and effort. The simplest method is to generate multiple arrays in many time-frames using, expand them to common and use IIF function to switch Conditionally signals coming from different time-frames. IIF is an array function and it will be as fast as anything else. No looping at all.
TimeFrame functions is what you should learn about by reading carefully the docs
https://www.amibroker.com/guide/h_timeframe.html
All you need is there.
There is "EXAMPLE 3" in that Users Guide page that implements Elders Triple Screen system and shows how to combine weekly and daily signals in single formula. You can do exactly the same. You can expand it to any number of timeframes and combine / switch from signals generated in one time frame to the other very simply using IIF function
// Assuming that current interval is daily
daily_signals = ... generate whatever you want in daily timeframe...
TimeFrameSet( inWeekly );
weekly_signals = ... generate whatever you want in weekly timeframe...
TimeFrameRestore();
/// PLEASE DO READ the manual that explains what different expand modes do!
// Don't copy paste code until you UNDERSTAND what functions are doing!
weekly_signals = TimeFrameExpand( weekly_signals, inWeekly, expandLast );
// decide when to switch
Condition = ...the code that decides when you want to use weekly signals...
// now you can switch
switched_signals = IIF( Condition, weekly_signals, daily_signals );