Gamestudio Links
Zorro Links
Newest Posts
Blobsculptor tools and objects download here
by NeoDumont. 03/28/24 03:01
Issue with Multi-Core WFO Training
by aliswee. 03/24/24 20:20
Why Zorro supports up to 72 cores?
by Edgar_Herrera. 03/23/24 21:41
Zorro Trader GPT
by TipmyPip. 03/06/24 09:27
VSCode instead of SED
by 3run. 03/01/24 19:06
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (degenerate_762), 1,114 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
sakolin, rajesh7827, juergen_wue, NITRO_FOREVER, jack0roses
19043 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
TRADE struct encourages precision loss #467830
09/03/17 18:59
09/03/17 18:59
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Hello.

Can you please change the member types of struct TRADE from float to double? The precision loss is a problem.

Code:
typedef struct TRADE
{
	float	fEntryPrice;	// buy price, or premium without multiplicator
	float	fExitPrice;	// sell price per unit, without spread
	float fResult;		// current profit of the trade
	float	fEntryLimit;	// buy entry limit
	float	fProfitLimit;	// profit limit price
// ...
} TRADE;



Here is an actual example where the conversion to float and back causes an issue. It became apparent when I tried to abuse a double to transfer a pointer to a TMF.

Code:
int callback(double v)
{
	printf("ncallback v: %f", v); // v = 134550672.0
	return 0;
}

void run()
{
	if (Bar == StartBar)
	{
		enterLong(callback, 134550669.0);
	}
}


Last edited by pascalx; 09/03/17 19:01.
Re: TRADE struct encourages precision loss [Re: pascalx] #467836
09/04/17 03:39
09/04/17 03:39
Joined: Dec 2016
Posts: 71
F
firecrest Offline
Junior Member
firecrest  Offline
Junior Member
F

Joined: Dec 2016
Posts: 71
I think you point out a good point but is such precision needed in trading?

Re: TRADE struct encourages precision loss [Re: firecrest] #467841
09/04/17 09:42
09/04/17 09:42
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
Yeah maybe float is just fine for most of the variables in that struct. I didn't think that through yet. I just ran into above practical issue, so I suggest to have another look.

double and float are not the same thing. double is 8 bytes and float is 4 bytes. double can present significantly more numbers than float. If there comes a precision loss depends on the intended use.

The only solid reason to use float over double here (afaik) is to decrease the amount of memory needed for 1 TRADE instance, effectively halving it. Depending on your strategy code you will have different amount of trades. So decreasing its memory footprint is generally appreciated, especially if the application keeps all opened and closed trades in memory.

Conversion to float will definitely break all code where you want to use the 8 bytes of a double as pure storage. A more elegant solution for this case would be to add a void* member to the TRADE struct reserved for user data. Since a TRADE struct does not seem to provide a unique ID, you also cannot accompany it with a map of trade user data.

What are these variables for? Are these free for the user? Maybe the comment is just misleading.
Code:
var	Skill[NUM_SKILLS];	// general purpose variables for TMF



For prices, please correct me if my test is wrong, we could check if the storage precision is a problem like so.
I assume a 5 digit price precision for this test.

Code:
void main()
{	
	float f1 = 0.0;
	float f2 = 0.0;
	double d1 = 0.0;
	double d2 = 0.0;
	int i;
	
	printf("nBusy...");
	
	for (i=0; i<0x7fffffff; ++i)
	{
		d1 += 0.00001;
		f1 = d1;
		d2 = f1;
		if (d1 != d2)
		{
			printf("nMismatch %.5f vs %.5f", d1, d2);
			break;
		}
	}
	
	printf("nDone.");
}



Result:

Quote:
Busy...
Mismatch 0.00007 vs 0.00007
Done.


Ok. There is a mismatch quite early but that has to be expected. It will be a very insignificant small digit.
So lets round the double after we took it from float into our desired precision.

Code:
void main()
{	
	double pip = 0.00001;
	float f1 = 0.0;
	float f2 = 0.0;
	double d1 = 0.0;
	double d2 = 0.0;
	int i;
	
	printf("nBusy...");
	
	for (i=0; i<0x7fffffff; ++i)
	{
		d1 += pip;
		f1 = d1;
		d2 = f1;
		if (round(d1,pip) != round(d2,pip))
		{
			printf("nMismatch %.5f vs %.5f", d1, d2);
			break;
		}
	}
	
	printf("nDone.");
}



Quote:
Busy...
Mismatch 128.00001 vs 128.00000
Done.


So the first mismatch with 0.00001 precision occurs at a relatively high value. Is that good enough? I think so yes.

Re: TRADE struct encourages precision loss [Re: pascalx] #467845
09/04/17 13:37
09/04/17 13:37
Joined: Jul 2000
Posts: 27,977
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,977
Frankfurt
Floats are generally used for all structs that have a memory impact.

Re: TRADE struct encourages precision loss [Re: jcl] #467850
09/04/17 14:03
09/04/17 14:03
Joined: Aug 2017
Posts: 58
P
pascalx Offline OP
Junior Member
pascalx  Offline OP
Junior Member
P

Joined: Aug 2017
Posts: 58
For above issue I use this workaround now. You can't use the parameters from enterLong/enterShort directly due the hidden conversion.

Code:
#define TMF_PTR_ARG 7

void* getTmfPointer(TRADE* pTrade)
{
	void* p = NULL;
	
	if (pTrade)
	{
		memcpy(&p, &pTrade->fArg[TMF_PTR_ARG], sizeof(float));
	}
	
	return p;
}

void setTmfPointer(TRADE* pTrade, void* p)
{
	if (pTrade)
	{
		memcpy(&pTrade->fArg[TMF_PTR_ARG], &p, sizeof(float));
	}
}


struct MyData
{
	int member1;
	int member2;
};

int tmf()
{
	struct MyData* pMyData = getTmfPointer(ThisTrade);
	return 0;
}

struct MyData g_myData;

void run()
{	
	if (Bar == StartBar)
	{
		TRADE* pTrade = enterLong(tmf);
		setTmfPointer(pTrade, &g_myData);
	}
}



Moderated by  Petra 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1