need help following Janeczkoimplementing multiple time frame analysis in AmiBroker

trash

Well-Known Member
#11
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Regarding no. 1: Heres the code that requires I hit scan twice before I see trades in the scan window:

**********
Count = 0;
result = 0;

for( i = 10; i <= 300; i++ )
{
TimeFrameSet( i * in1Minute );
m = MACD(12, 26 );
TimeFrameRestore();
m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 );
result = result + m;
Count++;
}

AddToComposite( result / Count, "~MACD"+Name(), "X" );
x = Foreign("~MACD"+Name(),"C");
Buy = Cross(x,-.5);
Sell= Cross(.5,x);
BuyPrice = SellPrice = Close;
*************

However, if I substitute this code after the closing bracket of the "for" loop, I only have to hit scan once:

x=result/Count;
Buy = Cross(x,-.5);
Sell= Cross(.5,x);
BuyPrice = SellPrice = Close;

So it seems it's the extra step of creating the artificial ticker, and then importing it back into the program, that accounts for the need to hit scan twice.
The reason that you need to hit scan twice to see buy/sell stuff ... that's because AB is now completely multi-threaded app (since 5.50) www.amibroker.com/guide/whatsnew.html

So you need to add critical section to such code

Code:
#include <CriticalSec.afl>;
  
if( _TryEnterCS( "mysemaphore" ) )
{ 
	Count = 0;
	result = 0;

	for( i = 10; i <= 300; i++ )
	{
		TimeFrameSet( i * in1Minute );
		m = MACD(12, 26 );
		TimeFrameRestore();
		m = IIf( TimeFrameExpand( m, i * in1Minute ) > 0, 1, -1 );
		result = result + m;
		Count++;
	}

	AddToComposite( result / Count, "~MACD"+Name(), "X" );

	_LeaveCS();
}

else _TRACE("Unable to enter CS");  

x = Foreign("~MACD"+Name(),"C");
Buy = Cross(x,-.5);
Sell= Cross(.5,x);
BuyPrice = SellPrice = Close;

SetOption("RefreshWhenCompleted", True );

Put this code to the Include folder of Amibroker with AFL name CriticalSec.afl

Code:
function _TryEnterCS( secname )
{
  global _cursec;
    _cursec= "";

  // try obtaining semaphore for 1000 ms
  for( i = 0; i < 1000; i++ )
    if( StaticVarCompareExchange( secname, 1, 0 ) == 0 )
    {
      _cursec = secname;
      break;
    }
    else ThreadSleep( 1 ); //sleep one millisecond

  return _cursec != "";
} 


// call it ONLY when _TryEnterCS returned TRUE!
function _LeaveCS()
{    
  global _cursec;
  if( _cursec != "" )
  {
    StaticVarSet( _cursec, 0 );
    _cursec = "";
  }
}
www.amibroker.com/guide/afl/staticvarcompareexchange.html

Such critical section code is now also needed to be added to AFLs that e.g. export data to file from Amibroker.

Also read this www.amibroker.com/guide/h_multithreading.html
 
Last edited:

trash

Well-Known Member
#12
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Its called Foreign because it allows you to reference a ticker symbol different from (i.e., foreign to) the currently active one. [Personally, I think these would be far less cryptic if they were instead named "ExportArtTick" (or "CreateArtTick") and "ImportArtTick" (where ArtTick means artificial ticker). And I wish there were somewhere in the manual it had been explained in this fashion!]
The manual clearly tells what Foreign and SetForeign do.

It says "(Referencing other symbol data)" as well as "Allows referencing other (than current) tickers in the AFL formulas"

www.amibroker.com/guide/afl/

I don't understand what so difficult about it.
The function name Foreign says it all. I didn't even need to read the explanation to understand what the name does mean
 

trash

Well-Known Member
#13
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

As for Buy = 1 Buy = 0

Yes adding it then you can see the quotes listed in the Scan window.
Same with Filter = 1 for Exploration.

Well since it's a scan window for buy, sell, short, cover signals you most probably add buy, sell, short, cover to your scan code to see the output. That's what a scan is made for. In this case of creating a composite you probably don't need to add it to the code if you don't wanna see output in Scan window because signals are not relevant.
 
#14
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Thanks, trash, for generously taking the time to introduce me to this sophisticated code. I do recall reading previously about multithreading in AB and I'm curious -- what about my original code makes it multithreaded (and thus able to benefit from the CriticalSec function)? Is it because it examines multiple timeframes?

I ask because my composite indicator is based on one symbol and is displayed in one pane, and the manual states: "1 operation * 1 symbol = 1 thread. The operation is displaying single chart pane, scan, exploration, backtest, optimization. The consequences are as follows: single chart pane always uses one thread. Also a single backtest or optimization running on one symbol uses one thread only."

I also have what I hope is an interesting question about a variation on multi timeframe implementation. If you think it's too far afield from the OT I'd be happy to start another thread, but:

I'd like to try implementing a strategy that uses indicators based not on different time bars, but different range bars. For instance, in "AB pseudocode" (AB doesn't actually have this functionality), it would look something like this, where "inR(i)" means an i-tick range bar:

**************
Count = 0;
result = 0;

for( i = 10; i <= 300; i++ )
{
TimeFrameSet( inR(i) );
m = MACD(12, 26 );
TimeFrameRestore();
m = IIf( TimeFrameExpand( m, inR(i) ) > 0, 1, -1 );
result = result + m;
Count++;
}

AddToComposite( result / Count, "~MACD"+Name(), "X" );
x = Foreign("~MACD"+Name(),"C");
Buy = Cross(x,-.5);
Sell= Cross(.5,x);
BuyPrice = SellPrice = Close;
***************
It seems AB could do it in theory. After all, AB knows, to the second (based on the tick data I give it), the timestamp for the close of each range bar. It could, in principle, use that to align multiple range bar sets in TimeFrameExpand. The only caveat would be if you had, for example, more than one 3-tick range bar form in a second, and a 7-tick range bar also form during that second. In that case, Amibroker wouldn't know how to order them within that second (3,3,7 vs. 3,7,3, etc.). But for my data that's rare and, besides, that would only create localized errors within that second.

Nevertheless, I contacted AB support (who have been nice enough to help me during my trial evaluation), and was told AB doesn't have this functionality-- so I know there is no standard way to implement it in AB. But can you think of some clever non-standard way to approach this problem?

To combine 3-tick and 7-tick range bar frames, one approach might be:

TimeFrameMode(4);
TimeFrameSet(3);
//Do stuff
TimeFrameRestore;
TimeFrameSet(7);
//Do stuff
TimeFrameRestore;

But then I would need to figure out a way to use the time stamps to align them myself, since TimeFrameExpand can't expand range bars. And then in the Backtester settings, I would need to know how to set the periodicity (perhaps 1 second, if I'm aligning them based on second timestamps).

Any ideas (and I don't need to do it for 290 different range bar sets -- four or five would be enough)?
 
Last edited:

trash

Well-Known Member
#15
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Applying a scan, exploration etc on current symbol only then it should be one thread, yes. I don't know the internals of the software but that additional code does what you want that is having output of ATC signals right after first scan. In the line "else ThreadSleep( 1 ); //sleep one millisecond" you can input e.g 100 milliseconds and so on. instead of 1 if it still shouldn't do what expected. But I tested it using 1 ms and it did work.

On the other side you can still use the old Analysis environment, it's still there. There you can use that composite code without critical section since old AA is not multithreaded.

Regarding multiple range bars. I never tried it. Need to think about it.
 
#16
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

I think I'll try posting the multi-range bar question as a separate thread here, to see what ideas that might elicit; perhaps I'll also try either the AB Yahoo group or aussiestockforums, since there appears to be a good concentration of AB experts there as well. If TimeFrameGetPrice() worked for range bars I think that might solve my problem, but I tried it and it doesn't appear to (the documentation makes no mention of its use for range bars).
 
Last edited:
#17
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Hi trash. Here's an update: I posted my multi-range bar question on the AB Yahoo board, and got a reply from TJ (http://finance.groups.yahoo.com/group/amibroker/message/171249). Unfortunately, I can't understand how it answers my question :confused:, so I was wondering if you might be able to make sense of it and "translate" :D what he's saying:

****************************
Hello,

As written in TimeFrameSet docs:
http://www.amibroker.com/guide/afl/afl_view.php?timeframeset

You can also use TimeFrameSet to create N-volume bars as well as Range bars. See
TimeFrameMode() function for more details.

Then if you open TimeFrameMode
http://www.amibroker.com/guide/afl/afl_view.php?timeframemode

- you will see that it is already possible to use N-tick based intervals

TimeFrameMode( 1 );


******************************
The reason I'm confused is because, to do multi-frame analysis, I thought you need TimeFrameExpand() to align the different series. His reply only talks about using either TimeFrameSet() or TimeFrameMode() to create the range bars, which I already know how to do. This appears to leave unanswered the question of how to align them so they can be combined into a single strategy. [I'm also a bit confused by his reference to TimeFrameMode(1), which is for N-tick bars, rather than N-range bars.] But I assume he understood my question, so I was hoping you could explain what I'm missing.

[Perhaps he means that, if I switch to TimeFrameMode(4), I can use TimeFrameExpand() with range bars, just using positive numbers to indicate the tick range instead of negative ones. Seems unlikely that would work, but I can try that out tomorrow.]
 
Last edited:

sr114

Well-Known Member
#18
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

I think I'll try posting the multi-range bar question as a separate thread here, to see what ideas that might elicit; perhaps I'll also try either the AB Yahoo group or aussiestockforums, since there appears to be a good concentration of AB experts there as well. If TimeFrameGetPrice() worked for range bars I think that might solve my problem, but I tried it and it doesn't appear to (the documentation makes no mention of its use for range bars).
Hello

i am trying to use the range bars in trading. any afl is there for geting the range bars in cas eof equity intraday or commodity. can i use it (range bars) in case of eod?

if there is no afl then what settings i have to follow.
help solicited

thanx
sr
 
#19
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Hello

i am trying to use the range bars in trading. any afl is there for geting the range bars in cas eof equity intraday or commodity. can i use it (range bars) in case of eod?

if there is no afl then what settings i have to follow.
help solicited

thanx
sr
Any price chart AFL can be used for range bars. U set range bars in preferences - intraday! After that you choose set range bars by right clicking on chart and there in the context menu by going to "Intraday". In order to not use context menu as one and only option to choose range bars you can additionally create range bar buttons on the menu bar by using "Tools - Customize". You also need to set the tick size of your symbols.

Also EOD has got nothing to do with range bars. Range bars are time independent because they dependent on ticks!

You have completely wrong way of thinking
 

sr114

Well-Known Member
#20
Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok

Any price chart AFL can be used for range bars. U set range bars in preferences - intraday! After that you choose set range bars by right clicking on chart and there in the context menu by going to "Intraday". In order to not use context menu as one and only option to choose range bars you can additionally create range bar buttons on the menu bar by using "Tools - Customize". You also need to set the tick size of your symbols.

Also EOD has got nothing to do with range bars. Range bars are time independent because they dependent on ticks!

You have completely wrong way of thinking
Detwo

if in intraday we can cut out the time component from the price chart then why not from daily price chart. time independent stuff can work in any frame may be 1 min, 5 min, 30 min 4hours, or 24 hours

sr
 

Similar threads