Re: need help following Janeczkoimplementing multiple time frame analysis in AmiBrok
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
Put this code to the Include folder of Amibroker with AFL name CriticalSec.afl
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
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.
**********
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.
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 = "";
}
}
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: