Database to remember trades? - page 2

 
cloudbreaker wrote >>

SanMiguel. You need to listen to the good advice from BarrowBoy and fbj.

Remember that MQL4 is a programming FRAMEWORK not just a programming language.

Good programmers attempt first to reuse rather than reinvent. Sorry for sounding patronizing here.

You will find yourself in a world of pain which is all your own making if you continue what I think you are doing which is to attempt to reinvent the wheel without first taking the time to appraise yourself a) that the wheel exists b) how it behaves and c) how to use it.

Use the inbuilt order history to access persistent order data without the overhead of having to manage it.

Use your own flat files when you need to persist state information which is specific to your own EA - eg. to provide unattended recovery from sudden outage of your PC.

Have a look at some of the code snippets on this forum to learn how to leverage magic numbers. Only use magic numbers if necessary to identify orders. Do not add the management overhead in the situation, for example, where an EA only ever 'owns' one open order.

Make your code modular and make those modules reusable.

Keep an open mind. There are folks on here who know a great deal about MQL4 and program design.

I tried looking for info on unique magic numbers but couldn't find any.

If I search through the history by the time I have hundreds of orders, the code will be processing very slowly?

How do we generate a unique magic order number each time without using a database?

Code outline:

1. So I create an order through a script assigning a magic number, which also takes the ticket number, e.g 0.2 lots ticket number 123456, magicnumber 1.

2. Half the trade is closed at a certain point.

3. A new ticket number is created with half the lot, but the same magic number by the broker, eg 0.1 lots ticket number 123457, magicn umber 1.

4. I can now search the history for the magic number 1, find the lot size of 0.2 and leave any new orders with magic number 1 and lot size 0.1 alone - the script will now ignore them.

5. I create another new order for 0.2 lots ticket number 654321, magicnumber - how do I get the magicnumber here? Do I have to scroll through all the history and find the highest magic number and add 1 - time consuming. Wouldn't a database with a unique ID create this much more quickly and efficiently for me? It can also store the ticket numbers.

 
SanMiguel:

I tried looking for info on unique magic numbers but couldn't find any.

If I search through the history by the time I have hundreds of orders, the code will be processing very slowly?

How do we generate a unique magic order number each time without using a database?

Code outline:

1. So I create an order through a script assigning a magic number, which also takes the ticket number, e.g 0.2 lots ticket number 123456, magicnumber 1.

2. Half the trade is closed at a certain point.

3. A new ticket number is created with half the lot, but the same magic number by the broker, eg 0.1 lots ticket number 123457, magicn umber 1.

4. I can now search the history for the magic number 1, find the lot size of 0.2 and leave any new orders with magic number 1 and lot size 0.1 alone - the script will now ignore them.

5. I create another new order for 0.2 lots ticket number 654321, magicnumber - how do I get the magicnumber here? Do I have to scroll through all the history and find the highest magic number and add 1 - time consuming. Wouldn't a database with a unique ID create this much more quickly and efficiently for me? It can also store the ticket numbers.

The magic number only needs to be unique within the context of your account number - you'll be running one account per platform instance.

So let's assume you have a number of accounts. Within each account you have a number of charts, each containing one EA. Some of these charts have the same EA.

So code your EA to create the magic number by concatenating the following:

- A number specific to the EA

- A number specific to the Symbol

- A number specific to the Period

- A number specific to the Order (a sequence number)

 
cloudbreaker wrote >>

The magic number only needs to be unique within the context of your account number - you'll be running one account per platform instance.

So let's assume you have a number of accounts. Within each account you have a number of charts, each containing one EA. Some of these charts have the same EA.

So code your EA to create the magic number by concatenating the following:

- A number specific to the EA

- A number specific to the Symbol

- A number specific to the Period

- A number specific to the Order (a sequence number)

The problem is that when half a trade is closed, a new ticket is created. I need the EA to ignore this new ticket.

Now, it can do it by finding the original order & magic number and ignoring any other tickets with that magic number.

However, the next time I create an order, I need a new unique magic number otherwise the EA won't know what to do.

I can go, 1 2 3 4 5 6 7 8 etc. but the EA needs to create the new magic number each time.

The only way I can think of doing this is by searching through the history for the highest magic number and adding 1. This is a loop - it will be very slow after a while.

A database will be much quicker.

I have only 1 EA running, I only operate on one symbol, and I only operate on 4 hour charts, so how do I get unique magic numbers without using a loop code? :)

 
SanMiguel:

The only way I can think of doing this is by searching through the history for the highest magic number and adding 1. This is a loop - it will be very slow after a while.

A database will be much quicker.

You're right that a simple loop will, ultimately, be very slow.


However, having just done a brief test, I can scan through 3,000 orders to find the highest magic number in under a millisecond. And that becomes colossally improvable if you can make safe assumptions about only needing to scan the last n orders - on the basis that the magic numbers will be sequential - rather than having to check the entire history.


I haven't tried it myself, but I'd be surprised if you can reliably and consistently do a round trip from MT4 to MySQL, via a DLL and all the necessary translation, in under a millisecond. In fact, "surprised" isn't really the word for what I'd be.

 
SanMiguel wrote >>

The problem is that when half a trade is closed, a new ticket is created. I need the EA to ignore this new ticket.

Now, it can do it by finding the original order & magic number and ignoring any other tickets with that magic number.

However, the next time I create an order, I need a new unique magic number otherwise the EA won't know what to do.

I can go, 1 2 3 4 5 6 7 8 etc. but the EA needs to create the new magic number each time.

The only way I can think of doing this is by searching through the history for the highest magic number and adding 1. This is a loop - it will be very slow after a while.

A database will be much quicker.

I have only 1 EA running, I only operate on one symbol, and I only operate on 4 hour charts, so how do I get unique magic numbers without using a loop code? :)

Use OrderComment()

"The problem is that when half a trade is closed, a new ticket is created. "

to ignore this new ticket, this ticket has such an ordercomment "from #12345" where 12345 is the ticketno_of_prev_fullorder

so just detect this comment ie dont process orders with such ordercomment string

 
SanMiguel:

The problem is that when half a trade is closed, a new ticket is created. I need the EA to ignore this new ticket.

Now, it can do it by finding the original order & magic number and ignoring any other tickets with that magic number.

However, the next time I create an order, I need a new unique magic number otherwise the EA won't know what to do.

I can go, 1 2 3 4 5 6 7 8 etc. but the EA needs to create the new magic number each time.

The only way I can think of doing this is by searching through the history for the highest magic number and adding 1. This is a loop - it will be very slow after a while.

A database will be much quicker.

I have only 1 EA running, I only operate on one symbol, and I only operate on 4 hour charts, so how do I get unique magic numbers without using a loop code? :)

Why not just write the last highest sequence number that you've used to a flat file.

Then when you need a new sequence number to append to a magic number, just read the value from the flat file and add 1.

Reason: