[amibroker] fputs() misses some strings

 

Hi --
I'm trying to output a string containing Symbol, Fullname, Firstdate, Lastdate, and Numberofbars for all symbols in a watchlist to a file using fputs().  With watchlists longer than only a few symbols, one or more strings are always missing.  For example for a watchlist containing 83 symbols, only 73 are output (not always the same 73 though).  This happens using either Explore and Scan.

I believe the problem is related to Analysis multithreading.  And tried to fix it using semaphore (identical to that of Example 2 in the AB manual).  Please see code snippets below for the relevant parts:

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(10);//sleep one millisecond
   }
   return _cursec !="";
}

// call it ONLY when _TryEnterCS returned TRUE !
function _LeaveCS(){
   global _cursec;
   if(_cursec !=""){
      StaticVarSet(_cursec,0);
      _cursec ="";
   }
}

if(stocknumber == 0){
   StaticVarRemove("*");
   StaticVarSet("DelistedListActive", False);
   fdelete(f1filename);
   text1 = "SYM,NAME,FIRST,LAST,BARS\n";
   fh1 = fopen(f1filename, "a");
   if(fh1){
      fputs(text1, fh1);
      fclose(fh1);
      StaticVarSet("DelistedListActive", True);
   }else _trace("Opening fh1 failed, stocknum0\n");
}

if(StaticVarGet("DelistedListActive") == True){
   datearray = DateNum();
   first = int(datearray[0]);
   last = int(datearray[BarCount - 1]);
   firststr = DateNumToStr(first);
   laststr = DateNumToStr(last);
   barsstr = NumToStr(BarCount, 1.0, False);
   
   for(i=10; i>0; --i){ // for large data, need to try multiple times
      if(_TryEnterCS("semifh1")) break; // successful
   }if(i>0){  // in 10 or less tries
      for(j=10; j>0; --j){  // fopen may need multiple tries
         fh1 = fopen(f1filename, "a");
         if(fh1){
            fputs(Name() + "," + FullName() + "," + firststr + "," + laststr + "," + barsstr + "\n", fh1);
            fclose(fh1);
            _LeaveCS();
            break;  // out of for()
         }else{ // if(j<=0){
            _TRACE("Missed fh1 for stocknum: " + NumToStr(Status("Stocknum")) + "\n");
         }
      }if(j<=0) PopupWindow("Failed to EnterCS for " + Name(), "AB Warning", 0);
   }else PopupWindow("Failed to EnterCS for " + Name(), "AB Warning", 0);
}

BTW, the _TRACE() AND PopupWindow() functions for trouble shooting.  But, they never display.

Any and all help would be appreciated.
Thank you.
-- Keith


__._,_.___

Posted by: Keith McCombs <kmccombs@engineer.com>
Reply via web post Reply to sender Reply to group Start a New Topic Messages in this topic (1)

Have you tried the highest rated email app?
With 4.5 stars in iTunes, the Yahoo Mail app is the highest rated email app on the market. What are you waiting for? Now you can access all your inboxes (Gmail, Outlook, AOL and more) in one place. Never delete an email again with 1000GB of free cloud storage.

**** IMPORTANT PLEASE READ ****
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