Simple Question and Request RE Multiple Time Frames

I have a very simple question that I seem stuck on. I've searched the site, but i'm confused about multiple time frames and I haven't found an example that I can use.

Can I use a buy/sell format with timeframeset, or do I need to have the multiple time frames separate and then a buy/sell decision made later on?

For example, I would like to make a buy decision based on a MA both on the daily and then weekly close.

Buy =
Close > 5
AND MA( Close , 15 ) > Ref( MA( Close , 15 ) , -1 );

Okay, this is fine, but if I want to expand to weekly and have BOTH weekly and daily MA > Ref -1, would I then insert TIMEFRAMESET(inweekly) after the last part and included in the entire BUY? I tried that and it doesn't work, as the semi-colon stops the program running all the way (as I believe I need the semi-colon after TimeFrameSet as well).

Does this mean I need to separate the code so that the daily section is Part 1, weekly section is Part 2, then Buy = if Part 1 and Part 2 are true?

If so, how would I equate factors of Part 1 & 2 (if this, AND this, AND this) all are true, then Buy = Part 1 is true and Part 2 is true etc...

Even if someone could even just provide the code for this simple example, it would help me greatly, as I learn much better from examples.

Thanks so much!

There isn't any timeframeset code you tried to be seen in your post.
Multi timeframe support is explained here.
You would need TimeFrameSet(), TimeFrameRestore() and TimeFrameExpand().
KB provides examples also. As well as this forum.

Yes.

Example is for EOD interval. If being at intraday interval then you would have to add another TimeFrameSet procedure for daily TF.

/// Example of MA daily and MA Weekly conditions being combined to entry signal
/// Example is for EOD interval
/// If being at intraday interval then you would have
/// to add another TimeFrameSet procedure for daily TF
/// @link https://forum.amibroker.com/t/simple-question-and-request-re-multiple-time-frames/19463/2
tmfrm = inWeekly;
TimeFrameSet( tmfrm );
	weekly_MA = MA(C, 15);
	weekly_roc = ROC(weekly_MA, 1);
TimeFrameRestore();
expand_mode = expandPoint;
weekly_roc = TimeFrameExpand(weekly_roc, tmfrm, expand_mode);

daily_MA = MA(C, 15);

Buy = Close > 5 AND ROC(daily_MA, 1) > 0 AND Ref(weekly_roc, -1) > 0;
//Sell = ...;
4 Likes

Thanks so much fxshrat!!

I really do appreciate the help, this makes perfect sense!

Btw - one follow-up question. ROC only works properly with positive numbers, correct? (as I see from the Amibroker notes that ROC " Calculates the periods rate-of-change of ARRAY expressed as percentage.").

For negative numbers, could ROC work (ie this period in negative territory is lower than last period) or should I use REF?

I tried using similar outline you noted above for weekly with MACD and using REF but that didn't work (ie AND weekly_MACD < Ref (weekly_MACD, -1) , after defining weekly_MACD within inweekly part of the code.

Thanks again

We can't see what you tried (in full glory). Don't be shy.

/// Example of daily and weekly MA/MACD conditions being combined to entry signal
/// Example is for EOD interval
/// If being at intraday interval then you would have
/// to add another TimeFrameSet procedure for daily TF
/// @link https://forum.amibroker.com/t/simple-question-and-request-re-multiple-time-frames/19463/4
tmfrm = inWeekly;
TimeFrameSet( tmfrm );
	wSMA = MA(C, 15);
	wROC = ROC(wSMA, 1);
	wMACD = MACD();
	wMACD_smaller = wMACD < Ref(wMACD,-1);
TimeFrameRestore();
expand_mode = expandPoint;
wROC = TimeFrameExpand(wROC, tmfrm, expand_mode);
wMACD_smaller = TimeFrameExpand(wMACD_smaller, tmfrm, expand_mode);

dMA = MA(C, 15);
dMACD = MACD();

Buy = Close > 5 AND 
	ROC(dMA, 1) > 0 AND wROC > 0 AND // MA ROC
	dMACD < Ref(dMACD,-1) AND wMACD_smaller;// MACD	
Buy = Ref(Buy, -1);
BuyPrice = Open;
//
//Sell = ...;

Ah I see, thanks so much!

Yes, I agree with your point, it would be easier to see the entire code, but at this point I'm just trying to understand the syntax and logic of multiple time periods. I did read other threads and notes, but as a non-programmer it wasn't sinking it.

Thanks again, you have really helped me tremendously!