How to convert boolean to string (in order to export it to csv)?

Hi,

I have the most basic newbie question ever: how to convert boolean to string in order to write it to csv?

If I use the following code:


procedure writeSignalsToDisk(filepath, includeHeader, deletFileBoferWriting, mode) {
	//SetOption( "usecustombacktestproc", True );

	SetCustomBacktestProc("");

	if(Status("action") == actionPortfolio) {
		dt = DateTime();
		bo = GetBacktesterObject();
		bo.PreProcess();

		if (deletFileBoferWriting) fdelete(filepath);
		
		fh = fopen(filepath, mode, True);
		 
		if (fh) {
			if (includeHeader) {
				fputs("DateTime,Symbol,Price,IsEntry,IsExit,IsLong,IsScale,Reason,Type\n", fh);
			}

			for(bar = 0; bar < BarCount; bar++) {
				for(sig = bo.getfirstsignal(bar); sig; sig = bo.GetNextSignal(bar)) {
					fputs(
						  NumToStr(dt[bar], formatDateTime) + "," + 
						  sig.Symbol + "," + 
						  StrFormat("%1.5f", sig.Price) + "," + 
						  StrFormat("%g", sig.IsEntry()) + "," +
						  StrFormat("%g", sig.IsExit()) + "," +
						  StrFormat("%g", sig.IsLong()) + "," + 
						  StrFormat("%g", sig.IsScale()) + "," + 
						  StrFormat("%.0f", sig.Reason) + "," + 
						  StrFormat("%.0f", sig.Type) + 
						  "\n"
						  , fh);
				} // end For all signals

				//bo.ProcessTradeSignals(bar);
			}
			
			fclose(fh);
		} else {
			_TRACE("Failed to open the file ", filepath);
		}
		
		//bo.PostProcess();
	}
}

I get the following output:

DateTime,Symbol,Price,IsEntry,IsExit,IsLong,IsScale,Reason,Type
30/07/2002,TLT,81.52000,-1,0,-1,0,0,1
31/07/2002,TLT,82.53000,0,-1,-1,0,1,2
22/08/2002,TLT,85.06000,-1,0,-1,0,0,1
23/08/2002,TLT,85.90000,0,-1,-1,0,1,2
...

Shouldn't boolean True be 1 and False 0? What I see is -1 and 0 respectively.

Thanks.

No, in AFL like in C, zero means False, non-zero means True. Any non- zero value is True.

Certain fields in AmiBroker are using only one bit. This is to conserve RAM as there can be billions of signals in backtest. Such one bit bool gets sign extended and you will see -1 for true. If you don’t like it just use unary minus : -value

1 Like

This topic was automatically closed 100 days after the last reply. New replies are no longer allowed.