Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, dr_panther), 821 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Fetching the most recent prices from multiple assets #484143
09/15/21 23:04
09/15/21 23:04
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline OP
Member
Grant  Offline OP
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
Hello all,

I'm working on a script which needs to keep an array up-to-date with multiple asset prices for further analysis. In short, my script performs 3 steps:

1. It prefetches the history (1999 bars) into an array during the 'FIRSTRUN'.
2. Each bar from the history gets shifted 1 bar back in time to make space for the last price.
3. The last prices are fetched and added to this array.

I can't make step #3 work correctly, because it's unable to fetch a more recent price than the last one from step #1.

Is there any way to solve this issue?

Thank you in advance!

Grant


Code
double PP1[2000][5];
int i, j;


function run()
	{
	History = ".t6";
	BarPeriod = 1;
	LookBack = 2000;
	set(TICKS);	

	if(is(TESTMODE)) 
		{
		StartDate = 20200101;
		}
	else
		{
		StartDate = NOW;
		}	
	
	if(is(INITRUN))
		{
	  //Sequence based most on complete price history
		asset("EUR/USD");
		asset("USD/JPY");
		asset("GBP/USD");
		asset("AUD/USD");
		asset("USD/CAD");
		}

	if(Bar < LookBack)
		{
		return;	
		}
	
  //#1) Prefetch history
	if(is(FIRSTRUN))
		{
		for(i = 1; i < LookBack; i++)
			{
		  //Sequence based most on complete price history		
			asset("EUR/USD");
			PP1[i][1] = priceClose(LookBack - i + 1);

			asset("USD/JPY");
			PP1[i][4] = priceClose(LookBack - i + 1);

			asset("GBP/USD");
			PP1[i][2] = priceClose(LookBack - i + 1);

			asset("AUD/USD");
			PP1[i][0] = priceClose(LookBack - i + 1);

			asset("USD/CAD");
			PP1[i][3] = priceClose(LookBack - i + 1);
			}							
	
		return;
		}	
	
  //#2) Shift history 1 bar back in time
	for(i = 0; i < (LookBack - 1); i++)
		{
		for(j = 0; j <= 4; j++)
			{
			PP1[i][j] = PP1[i + 1][j];
			}
		}

  //#3) Fetch latest price	
  //Sequence based most on complete price history	
	asset("EUR/USD");
	PP1[LookBack][1] = priceClose();

	asset("USD/JPY");
	PP1[LookBack][4] = priceClose();		

	asset("GBP/USD");
	PP1[LookBack][2] = priceClose();

	asset("AUD/USD");
	PP1[LookBack][0] = priceClose();
	
	asset("USD/CAD");
	PP1[LookBack][3] = priceClose();

  //Debugging
	for(i = 0; i < LookBack; i++)
		{
		for(j = 0; j <= 4; j++)
			{
			if(j < 4)
				{
				print(TO_CSV, "%.5f,", PP1[i][j]);
				}
			else
				{
				print(TO_CSV, "%.5f\n", PP1[i][j]);
				}
			}
		}
	
	printf("\nFinished");
	quit();
	}

Last edited by Grant; 09/15/21 23:07.
Re: Fetching the most recent prices from multiple assets [Re: Grant] #484196
09/20/21 11:05
09/20/21 11:05
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
If you have an array [2000], the maximum index is not 2000, but 1999.

Re: Fetching the most recent prices from multiple assets [Re: Grant] #484201
09/20/21 15:48
09/20/21 15:48
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline OP
Member
Grant  Offline OP
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
Dang, now I see it, so 'PP1[LookBack - 1][1] = priceClose();' etc.
Thank you JCL!

A question about the array shift procedure.
So far, most relevant code examples I've seen, suggest the same loop approach, but isn't there a more efficient approach?
I was thinking about something like:

Code
for(j = 0; j <= 4; j++)
    {
    memmove(&PP1[0][j], &PP1[1][j], 1999 * sizeof(PP1[0]));
    }



but I wasn't able to find a working string.h header file, so I'm not sure if this will do the trick more efficiently.

Last edited by Grant; 09/20/21 17:00.
Re: Fetching the most recent prices from multiple assets [Re: Grant] #484206
09/21/21 11:57
09/21/21 11:57
Joined: Apr 2008
Posts: 586
Austria
Petra Offline
Support
Petra  Offline
Support

Joined: Apr 2008
Posts: 586
Austria
Use the loop. Memmove would not work for your array because the data is not contiguous. One of the reasons why i learned to not use multidimensional arrays in C.

Re: Fetching the most recent prices from multiple assets [Re: Grant] #484207
09/21/21 12:09
09/21/21 12:09
Joined: Aug 2017
Posts: 294
Netherlands
G
Grant Offline OP
Member
Grant  Offline OP
Member
G

Joined: Aug 2017
Posts: 294
Netherlands
I could define separate array objects for every asset, but I perform a ton of data transformations as well, so I stick to loop.
Thanks for your input Petra!


Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1