Hello,
Thank you very much for your e-mail.
You can remove semaphores completely if you use
StaticVarAdd instead of StaticVarGet/StaticVarSet
http://www.amibroker.com/f?staticvaradd
Best regards,
Tomasz Janeczko
amibroker.com
Hi,
I have made two changes in the afl
1. I did not find the use of "Filter" after its definition.
2. The usage of StaticVarCompareExchange in the afl will check the semaphore once. Therefore, it the semaphore is locked by another thread, the waiting threads will not update the static variables.
Try this below: i still got some out of whack running totals, as Amibroker will print values alphabetically, but the threads would be updating the static values in random order. I think the only way to get synchronized values is to run it as a single thread. If the order of updating the totals is important, you would need to run as a single thread.
Filter= InWatchListName( "List 0" );
R = H - L; // get the daily range
Zdate = DateNum(); // I want to display the start date for each symbol
TotNR7 = 0; // initialize first value
Numbars = 0; // initialize first value
GrandTot = 0; // initialize first value
TotBars = 0; // initialize first value
if( Status( "stocknum" ) == 0 )
{
StaticVarRemove( "my*" );
StaticVarSet( "mygrandNR7", 0 ); // keeps running total of NR7
StaticVarSet( "mygrandtot", 0 ); // keeps running total of all bars
}
if( Filter )
{
for( i = 7; i < BarCount; i++ )
{
NumBars = NumBars + 1;
if( R[i] < R[i - 1] AND R[i] < R[i - 2] AND R[i] < R[i - 3] AND R[i] < R[i - 4] AND R[i] < R[i - 5] AND R[i] < R[i - 6] )
TotNR7 = TotNR7 + 1;
}
//GrandTot = GrandTot + TotNR7;
for( i = 0; i < 10000; i++ )
{
if( StaticVarCompareExchange( "semaphore", 1, 0 ) == 0 )
{
Grandtot = StaticVarGet( "MygrandNR7" );
Totbars = StaticVarGet( "Mygrandtot" );
Grandtot = GrandTot + TotNR7;
Totbars = Totbars + Numbars;
StaticVarSet( "mygrandNR7", grandtot );
StaticVarSet( "mygrandtot", Totbars );
StaticVarSet( "semaphore", 0 ); // reset semaphore
break;
}
else
{
ThreadSleep( 1 );
}
}
}
AddColumn( TotNR7, "NR7", 1.0 );
AddColumn( Zdate[0], "Start Date", 1.0 );
AddColumn( GrandTot, "NR7 Cum", 1.0 );
AddColumn( Numbars, "Total Bars", 1.0 );
AddColumn( Totbars, "Total Bars", 1.0 );
I have a watchlist (end of day) with 850 symbols. I want to scan the data for calculating the number of narrow range days (narrowest range of seven days) in each symbol and then make a total of all such days for the 850 symbols together. Calculating the narrow range and number of bars for each symbol was easy, but the use of static variables is not working. The same running total is displayed in some of the symbols. This means: if symbol C had 75 narrow range days and the accumulated days till symbol C was 200, then the accumulated days after symbol C should be 200+75 = 275. The exploration will show 200 (the previous number). Also, the cumulative numbers keep on shifting from one exploration to another.
Any help will be appreciated.
Here is the code:
Filter= InWatchListName( "List 0" );
R = H - L; // get the daily range
Zdate = DateNum(); // I want to display the start date for each symbol
TotNR7 = 0; // initialize first value
Numbars = 0; // initialize first value
GrandTot = 0; // initialize first value
TotBars = 0; // initialize first value
if ( Status("stocknum") == 0 )
{
StaticVarRemove( "my*" );
StaticVarSet("mygrandNR7", 0 ); // keeps running total of NR7
StaticVarSet("mygrandtot", 0 ); // keeps running total of all bars
}
if( StaticVarCompareExchange( "semaphore", 1, 0 ) == 0 ) // obtain semaphore
{
Grandtot = StaticVarGet("MygrandNR7");
Totbars = StaticVarGet("Mygrandtot");
StaticVarSet("semaphore", 0 ); // reset semaphore
}
else
{
_TRACE("Can not obtain semaphore");
}
for(i = 7; i < BarCount; i++)
{
NumBars = NumBars + 1;
if( R[i] < R[i - 1] AND R[i] < R[i -2] AND R[i] < R[i - 3] AND R[i] < R[i - 4] AND R[i] < R[i - 5] AND R[i] < R[i - 6])
TotNR7 = TotNR7 + 1;
}
//GrandTot = GrandTot + TotNR7;
if( StaticVarCompareExchange( "semaphore", 1, 0 ) == 0 ) // obtain semaphore
{
// protected section here
// Here you have exclusive access (no other threads that check for semaphore will enter simultaneously)
/////////////////////////
Grandtot = GrandTot + TotNR7;
Totbars = Totbars + Numbars;
StaticVarSet("mygrandNR7", grandtot );
StaticVarSet("mygrandtot", Totbars );
}
else
{
_TRACE("Can not obtain semaphore");
}
AddColumn(TotNR7,"NR7",1.0);
AddColumn(Zdate[0],"Start Date",1.0);
AddColumn(GrandTot,"NR7 Cum",1.0);
AddColumn(Numbars,"Total Bars",1.0);
AddColumn(Totbars,"Total Bars",1.0);
Posted by: Tomasz Janeczko <groups@amibroker.com>
Reply via web post | • | Reply to sender | • | Reply to group | • | Start a New Topic | • | Messages in this topic (4) |
This group is for the discussion between users only.
This is *NOT* technical support channel.
TO GET TECHNICAL SUPPORT send an e-mail directly to
SUPPORT {at} amibroker.com
TO SUBMIT SUGGESTIONS please use FEEDBACK CENTER at
http://www.amibroker.com/feedback/
(submissions sent via other channels won't be considered)
For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/
EmoticonEmoticon