Gamestudio Links
Zorro Links
Newest Posts
Data from CSV not parsed correctly
by EternallyCurious. 04/18/24 10:45
StartWeek not working as it should
by Zheka. 04/18/24 10:11
folder management functions
by VoroneTZ. 04/17/24 06:52
lookback setting performance issue
by 7th_zorro. 04/16/24 03:08
zorro 64bit command line support
by 7th_zorro. 04/15/24 09:36
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:48
Zorro FIX plugin - Experimental
by flink. 04/14/24 07:46
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (flink, AndrewAMD), 656 guests, and 1 spider.
Key: Admin, Global Mod, Mod
Newest Members
EternallyCurious, howardR, 11honza11, ccorrea, sakolin
19047 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Array Manipulation #436412
01/25/14 09:49
01/25/14 09:49
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline OP
Newbie
Radar  Offline OP
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
Hey Gang,

I'm wondering if there is a group of commands for manipulating arrays similar to what's in mql4, e.g. ArrayResize(ArrayName, NewSize), ArraySort(ArrayName, Ascend/Descend) etc.

I've searched the forum, and the help file, but was unable to find any examples, and I'm a slow learner, and need to be told where to go (in the nicest possible way).

The reason I want to be able to manipulate arrays is in this post... http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=436005#Post436005

In short, I want to create a multi-timeframe system, and to group trades by timeframe, so that I can calculate total protected profit per timeframe, and to be able to move individual trades from one group to another as their protected profit increases.

Thanks for your help.


Have fun!
Radar =8^)
Re: Array Manipulation [Re: Radar] #436441
01/27/14 07:36
01/27/14 07:36
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
C has standard array manipulation functions such as qsort for sorting and bsearch for a binary search. However for complex data management you normally use a linked list, not an array.

When you describe in words or pseudo code for what data structure you need the array and the operations you want to do with it, I can probably suggest how to solve it in C.

Re: Array Manipulation [Re: jcl] #436443
01/27/14 08:52
01/27/14 08:52
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline OP
Newbie
Radar  Offline OP
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
Hey JCL,

Thanks for that laugh

I'm trying to create a multi-timeframe system, and to manage trades according to their entry timeframe's settings, eventually pushing them up through the higher timeframes.

I was able to create that structure in mql4 by using arrays, so I thought I'd use the same system in lite-C, but it's going to be a little more difficult, I think.

I have to admit that whilst most of mql4 is a major pain in the butt, their array manipulation was pretty straight-forward for the user.

I'm currently attempting to build functions to do the same thing, but am coming across syntax errors, and I can't see what's causing them frown

Here's what I've got so far...

Code:
#include <default.c>
#include <intArraySort.c>

var counter[10] = {10, 1, 9, 2, 8, 3, 7, 4, 6, 5};
int countersize = 10;
int content = 0;

function start()
{
	for (int index = 0; index < countersize; index++)
	{
		content = counter[index];
		printf("n\ Element %d contains %d." index, content);
	}

	intArraySort(counter, countersize, "Ascend");

	for (int index = 0; index < countersize; index++)
	{
		content = counter[index];
		printf("n\ Element %d contains %d." index, content);
	}
	
	intArraySort(counter, countersize, "Descend");

	for (int index = 0; index < countersize; index++)
	{
		content = counter[index];
		printf("n\ Element %d contains %d." index, content);
	}
}



...and the intArraySort function...

Code:
function intArraySort(int *MyArray, int MyArraySize, string Method)
{
		
	if (Method == "Ascend")
	{
		for(int x=0; x<MyArraySize; x++)  // I get syntax errors for this line :(
		{
			int index_of_min = x;
			for(int y=x; y<MyArraySize; y++)
			{
				if(MyArray[index_of_min]>array[y])
				{
					index_of_min = y;
				}
			}
			int temp = MyArray[x];
			MyArray[x] = MyArray[index_of_min];
			MyArray[index_of_min] = temp;
		}
	}
	else
	if (Method == "Descend")
	{
		for(int x=0; x<MyArraySize; x++)
		{
			int index_of_min = x;
			for(int y=x; y<MyArraySize; y++)
			{
				if(MyArray[index_of_min] < array[y])
				{
					index_of_min = y;
				}
			}
			int temp = MyArray[x];
			MyArray[x] = MyArray[index_of_min];
			MyArray[index_of_min] = temp;
		}
		
	}
} // End _intArraySort



If you can tell me why I get the syntax error, and how to fix it, I'll wrestle with any other errors in the code... It's the only way I learn this stuff... Copy, paste, & edit until it does what I want... I can't learn from books frown

As I said, I want to be able to enter trades in different timeframes of the same instrument, and manage them separately, in order to calculate total protected profit for each timeframe. If my protected profit for a given timeframe is enough to cover the potential loss of a scale-in trade, then I'll scale-in, add that to the list of trades to be managed, then check the next timeframe up.

The idea is to build a pyramid of trades, with the pointy end down wink Check out my "My Weird Trade & MM System" (link is in first post) to see where I'm heading with this.


Have fun!
Radar =8^)
Re: Array Manipulation [Re: Radar] #436448
01/27/14 09:47
01/27/14 09:47
Joined: Mar 2010
Posts: 57
Germany, Niedersachsen
LemmyTheSlayer Offline
Junior Member
LemmyTheSlayer  Offline
Junior Member

Joined: Mar 2010
Posts: 57
Germany, Niedersachsen
1. in lite-c you have to declare x outside the for loop
2. you can not compare strings using "==". use string comparison functions instead: http://manual.zorro-trader.com/str_.htm


SCHLEIFE SCHLEIFE SCHLEIFE SCHLEIFE SCHLEIFE SCHLEIFE
Re: Array Manipulation [Re: LemmyTheSlayer] #436450
01/27/14 10:20
01/27/14 10:20
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline OP
Newbie
Radar  Offline OP
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
Thanks, Lemmy laugh That hit the spot laugh


Have fun!
Radar =8^)
Re: Array Manipulation [Re: Radar] #436693
01/31/14 13:01
01/31/14 13:01
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline OP
Newbie
Radar  Offline OP
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
Back again...

It seems we can't pass a dynamic array to a function with lite-c frown The script I posted above passes a pointer to an array to the function instead of the whole array:( There goes my plan of writing a collection of array manipulation functions to share.

I've been reading about how to create linked-lists in C, as JCL suggested, so I just need to find a tutorial on using and manipulating those. The idea is to try and make a set of tools to simplify the creation/manipulation/deletion of LL's, so that I can concentrate on the main goal, which is to create solid trading & management systems, and hopefully, make it easier for the next n00b wink

Anyroad, I'm off to google to see what I can learn wink


Have fun!
Radar =8^)
Re: Array Manipulation [Re: Radar] #436704
01/31/14 15:30
01/31/14 15:30
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
In C you normally don't pass an array to a function. That would be very ineffective and would not work for array manipulation anyway. Instead, you pass the array pointer to the function. Just look into the script of your example how it's done. C makes this easy, as an array in C is identical with a pointer to its first element.

If you need a linked list depends on what you want to do. If you want to dynamically add or remove elements, such as trades, a linked list is certainly better than an array.


Re: Array Manipulation [Re: jcl] #436741
01/31/14 23:23
01/31/14 23:23
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline OP
Newbie
Radar  Offline OP
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
Hey JCL,

Before I typed the above code, I googled "Array manipulation C programming" and found many examples of both passing by pointer (function intArraySort(int *MyArray, blah, blah), and passing the the whole array (intArraySort(int &MyArray[], blah, blah) on forums and in C tutorials.

The function intArraySort(int *MyArray, int MyArraySize, string Method) doesn't work:( When I print the contents of each element in the array, I get random numbers:(

I have 2 problems with arrays in lite-c that cannot be resolved...

1. When passing an array by pointer, I need to know the memory size of a single element, then multiply that by the number of elements, so that I can tell the function the full size of the array, but we can only get the size of the pointer, so that won't work.

2. Any changes made to an array within a function has to be seen globally, so the array has to be both global in scope, and dynamic, which doesn't appear to be possible in lite-c frown

I have attached an mql4 script that I wrote to help me understand how to use & manipulate arrays in mql4... The functions ArraySort, ArrayRange, ArrayResize, FileReadArray, & FileWriteArray are built-in to mql4.

The functions StoreTrades, ClearClosedTrades, and TrimDoubleArrays in the script will show you what I wanted to do in lite-c.

So, it looks like I'll be using linked lists wink

I've had memory fragmentation errors when experimenting with a combination of pattern recognition & lowpass filter, so I guess the best way to avoid that when removing nodes from an LL is to allocate enough contiguous memory to contain the resized LL, then copy the nodes that I want to keep into a new LL in that space before freeing the original LL's memory. I'll have to figure out how to get the size of a single node first, though wink

Attached Files

Have fun!
Radar =8^)
Re: Array Manipulation [Re: Radar] #436754
02/01/14 08:51
02/01/14 08:51
Joined: Jul 2000
Posts: 27,978
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,978
Frankfurt
If you need the memory size of an element, you use the sizeof() macro in C. For instance, sizeof(var) will return 8. But I can not see for what purpose you need that here?

And no, an array needs not be "global" for being changed by a function. There are other reasons why often global arrays are used.

- From your remarks I have the impression that your first programming experiment is too ambitious. Arrays, linked lists, element sizes, etc. is not what someone ususally has to deal with in his first C programs. But if you want to do that, definitely read a C introductory course. C is one of the best languages for handling arrays or linked lists, but the basic concepts must be understood. Otherwise you will just stumble from one silly "lite-C cannot do" misconception to the next.

I looked through the begin of your text - if I understood it right, you just want to store a stochastic and a time together. This is quite easy, but certainly not with a multidimensional array. In C you use a struct for that, like this:

Code:
typedef struct Radar
{
	struct Radar *next; // for a linked list
	int Time;
	var Stochastic;	
} Radar;



and you can then easily sort those structs in arrays or linked lists.

If you run into a coding problem, just post your code here and you'll get help.

Re: Array Manipulation [Re: jcl] #436758
02/01/14 11:57
02/01/14 11:57
Joined: Jan 2014
Posts: 28
Adelaide, South Oz
R
Radar Offline OP
Newbie
Radar  Offline OP
Newbie
R

Joined: Jan 2014
Posts: 28
Adelaide, South Oz
Hey JCL,

I'll explain what I want to do, without using "Array" or "Linked-List"...

I have a trade and money management theory that I want to put into action. It is based on multiple timeframes of a single asset (though I will expand it to other assets some time down the line).

For this example, I want to trade on the M5, M15, H1, H4, and D1 timeframes.

When I enter a trade on the M5, I want to put that trade's ID into one of two groups - M5Buy, or M5Sell. These groups will have their own trailing stop settings.

Once an M5 trade has a certain amount of profit protected by a stop, I want to remove that trade from its current group, and add it to another group (M5Export).

When there is an entry signal in the same direction on the M15 as one of the M5 trades, I will enter a trade on the M15 timeframe, and when its stop is moved to break-even, I want to import the trade from the M5Export group into the M15Buy or M15Sell group, and move its stop accordingly. When those trades have had their stops moved a certain distance in accordance with the M15 trailing stop, I want to move them from the M15Buy or M15Sell group into the M15Export group.

Continue moving trades as above for the other timeframes used in the system.

=========

Now, onto what I've read about Arrays in C, Lite-C, and C++...

1.i. Unless an array is global in scope, if you pass an array to a function, and manipulate (Resize or Sort) it in that function, the effects are only local to that function. Correct?
1.ii. Or can you declare dynamic arrays anywhere and have any changes made within functions visible to other functions (without passing directly from one function to another)?

2. If you try and get the size of an array in Lite-C, you only get the size of the pointer. Correct?

3. Another way to get the size of an array is to get the size of a single element (on further reading, I found that I can get sizeof(*DataType) in C), then multiply that by the number of elements (as long as I keep count of the elements within each function, and can access that count within every function, I should have no problems). Correct?

4. Arrays are allocated to contiguous blocks of memory. Correct?

As for the mql4 example I posted, the main point was to show how easy it was to manipulate arrays in mql4 (that script was the first thing I wrote from scratch in mql4). The Stoch stuff was for an EA I was modifying, but the main functions that I wanted to be able to use in this particular case are StoreTrades, ClearClosedTrades, and TrimDoubleArrays (or TrimArrays), as well as a couple of others for exporting/importing trades from one group to another.

If that cannot be done purely in Lite-C, then I now have an alternative...

===============

What I've read about Linked Lists...

1. They are quick and simple to create. Correct?

2. Nodes are allocated memory in any free space on the heap, which has the potential of causing memory fragmentation (though they'd have to be ridiculously large to cause that). Correct?

3. You always have to traverse an LL from the head (unless you create a doubly-linked list, so that you can traverse from the tail also) Correct?

4. Passing LL's to functions is easier, and any changes made within those functions are carried through to the rest of the program. Correct?

======================

Back to my trade & money management theory...

All changes to the groups must be both permanent, and visible to any other part of the program that needs access to them, regardless of whether they are accessed & changed within Main(), whether functions that change them are called from within Main(), or they are called from within another function.

I'm not flexible about the system, but I am flexible about the method(s) used to achieve that system wink

Thanks for your help laugh


Have fun!
Radar =8^)
Page 1 of 2 1 2

Moderated by  Petra 

Powered by UBB.threads™ PHP Forum Software 7.7.1