file_append, file_appendfront

Posted By: MatPed

file_append, file_appendfront - 01/30/19 09:34

Goodmorning community,
I am struggling using zorro's file_append and file_appendfront functions.
I have prepared a script to test the 2 functions:

Code:
void storeTrade();
/* Stores all the trades closed this bar
	Use after the algo/Asset loop. Not in the loop	*/

void printTrade(int counter);
int createReRArray(var* ReRarray, int Counter);

#define _storedRER "Data\storedRER.txt"
#define _storedTradeSize 50000 //1.000 trades (1.000 char [50])
#define _sTradeSize 50

char sRow[_storedTradeSize]; 

void storeTrade(){
	
	char sTrade[_sTradeSize];
	string ls, bo = "{", bc = "}";
	
	for(closed_trades){		// Check only phantom trades closed this Bar
		if ((int) TradeBarClose == Bar) {
			if (TradeIsPhantom && TradeIsClosed){
				if(TradeIsShort) ls = "S"; else ls = "L";		
				sprintf(sTrade, "n{%s:%s:%s%i}:%.2f", TradeAsset, TradeAlgo, ls, TradeID, TradeProfit );	
//				file_appendfront(_storedRER, sTrade, strlen(sTrade)+1 ); //<<---------	1			
//				file_appendfront(_storedRER, sTrade, strlen(sTrade) ); 	//<<---------	2			
//				file_appendfront(_storedRER, sTrade, 0); 						//<<---------	3			
//				file_append(_storedRER, sTrade, 0); 							//<<---------	4
//				file_append(_storedRER, sTrade, strlen(sTrade) ); 			//<<---------	5	
//				file_append(_storedRER, sTrade, strlen(sTrade) ); 			//<<---------	6	
				printf("%s", sTrade);
//				printf("Bar:%i - %s", Bar, sTrade);
			}
//	
		}
		else break_trades;
		
	}
}

void run()
{
//	set(LOGFILE); 	
	MaxLong	= 5;
	MaxShort	= 5;
	
	StartDate = 20180101;
	EndDate = 20180102;
	BarPeriod =1;
	Hedge = 5;
	algo("TREND"); 
	LifeTime = (int) random(10);
	if (random(6) > 3) enterLong(); else enterShort();
	enterLong(); enterShort();
	storeTrade();
}



The idea behind the script is to print the same windows output in a file in normal and reverse order.
I have tried all the 6 version listed uncommenting one of the numbered row at the time (remember to delete the file before each run).
My finding is that file_append works, but does not write the EOL so instead of a file of strings a long sequence of char is written.
file_appendfront does not work at all.

I have involved the support and they confirmed that file_appendfront() with length=0 does not work as expected.

Does anybody got some experience using this functions?
Any idea?

Ciao
Posted By: jcl

Re: file_append, file_appendfront - 01/31/19 19:14

AFAIK we have no bug confirmed for file_appendfront, so it should work as described. It's normally used to add records in front, and zero length will most likely add nothing.

I am not sure what you mean with the EOL issue. Your string has no EOL. For this, let it end with \r\n, or 0x0a 0x0d. The function just adds what you pass as argument. It does not care about EOL or other character sequences.
Posted By: MatPed

Re: file_append, file_appendfront - 02/01/19 00:20

Hi,
thank you for your reply, this is the email from support:
"Hi,

you appended data with zero length. Pass the string length to the size parameter, otherwise nothing is appended. But we'll change this in the next version so that 0 is also accepted for the length, as with file_append. "


file_append with 0 as lenght. works as stated in the manual. file_appendfront does not.


The append_front does not append any character in front of the file with any of the variants. Just run my script and you can check it. Maybe I am not using the right sintax. Can you tell me how to modify the script? I just want to print the window output in the reverse order.

I guess that n was enough as in normal printf. I will update my string accordingly with your suggestion.

Thank you in advance
Posted By: jcl

Re: file_append, file_appendfront - 02/02/19 11:46

Yes, I see that file_appendfront was changed in the current beta version so that it also accepts strings, similar to file_append.
Posted By: MatPed

Re: file_append, file_appendfront - 02/02/19 14:12

Ok,
do you have a walkaround for the previous version?
Thank You
Posted By: MatPed

Re: file_append, file_appendfront - 02/02/19 14:26

I do not see any note in the bug list. From which version the file_appendfront have been fixed?

Thank you
Posted By: jcl

Re: file_append, file_appendfront - 02/02/19 14:46

I have no fix in my list, only the note of string support. So I'm not sure if that fixes your issue - but just try!
Posted By: MatPed

Re: file_append, file_appendfront - 02/06/19 13:49

The file_appenfront in 2.016 now works as a charme. Thank you

Now I need to read all the strings written in the file and extract the info stored. Shold I use the standard C file functions or there is smarter way using Zorro, predefined functions?

In C I was looking for something like:
Code:
main(){
   char buf[80];
   FILE *fp;fp=fopen("testo.txt", "r");
   while (fscanf(fp,"%s",buf)>0)
      printf("%s", buf);
   fclose(fp);
}

Posted By: jcl

Re: file_append, file_appendfront - 02/09/19 07:18

I would simply use dataParse for reading back data from a file.
Posted By: MatPed

Re: file_append, file_appendfront - 02/09/19 22:03

Interesting, never dig into that set of function seems powerfull.
Thank You
Posted By: MatPed

Re: file_append, file_appendfront - 02/10/19 09:59

Trying to parse a simple csv:

1371,EUR/USD,TRD,79701,-0.055
1371,EUR/USD,TRD,79703,-0.055
1371,EUR/USD,TRD,82601,-0.102
1371,EUR/USD,TRD,86602,1.117
1371,EUR/USD,TRD,86802,1.373

The format I use:
string sFormat = "0,i,sss,sss,i,f";

this is the testing code:
Code:
#define _storedRER "Data\storedRER.csv"
#define _storedTradeSize 50000 //1.000 trades (1.000 char [50])
#define _sTradeSize 50

void storeTrade(){
	int iCounter = 0;
	char sTrade[_sTradeSize];
	string sDel=",", ls;
	string sFormat =  "%i%s%s%s%s%s%i%s%.3f";

	for(closed_trades){
		if (TradeIsPhantom && TradeIsClosed && TradeBarClose == Bar){
			if(TradeIsShort) ls = "S"; else ls = "L";		
			sprintf(sTrade, sFormat, TradeBarClose, sDel, TradeAsset, sDel, TradeAlgo, sDel, TradeID, sDel, TradeProfit );	
			strcat (sTrade, "rn");
			file_appendfront(_storedRER, sTrade, 0); 	
//			printf("n");
//			print(TO_WINDOW ,"n%s", sTrade);
		}
	}
}

void readTrade(){
//	string sFormat = "%i%s%s%s%s%s%i%s%.3f";
	string sFormat = "0,i,sss,sss,i,f";

	int iHandle, iCounter;
	iCounter = dataParse(iHandle, sFormat, _storedRER); //, sFilter
	
	printf("n%i", iCounter);
	dataNew(iHandle,0,0); 
}

void main() {
	set(LOGFILE); 	
	Verbose = 7|DIAG;
	
	readTrade();
}



No info is generated in the log file.
The output is always 0.

2 questions:
- Why no data are parsed?
- If I want to filter the data set only EUR/USD is the filter string ",EUR/USD,,," correct?

Thank you
Posted By: MatPed

Re: file_append, file_appendfront - 02/15/19 09:39

Any idea? Thx
Posted By: jcl

Re: file_append, file_appendfront - 02/15/19 09:43

For debugging your parsing, you can find instructions and advices under "remarks" on the dataParse manual page. Your filter string makes no sense. You can only filter strings that really appear in your csv, like "EUR/USD", but not ",EUR/USD,,,".
Posted By: MatPed

Re: file_append, file_appendfront - 02/15/19 10:27

I have seen the remarks, but I do not understand it.
With verbose = 7 I have no message in the log list and I have not seen many examples of the filter.
Are the dataparse functions standard C maybe I can use standard documentations? If not maybe some more hints or examples could be useful, not only for me.
Anyway I will keep on trying
Posted By: jcl

Re: file_append, file_appendfront - 02/15/19 12:23

C has no standard CSV parsing function. I've reworded the format string description - check the online version, maybe it's more clear now. There are many examples. Still, for some reason people have often problems to understand parsing CSV files - you're not the only one. We provide a conversion service if nothing else helps.
Posted By: MatPed

Re: file_append, file_appendfront - 02/15/19 13:02

Thank you, I will give it a looks. I am not converting I'd like to use it in order to store trade results in order to keep tracks after zorro restarts. I have only to read it, but i do not find the way how to do it...
© 2024 lite-C Forums