How to do it?

 

Hi there,


I need to know how can I program a code to find if a position for a currency pair is open or closed? In fact, I do not want to find if it is open. What I need is to find if it is closed...

For instance. The following code returns TRUE if a position is opened:


void CheckPosition()
{ for (int i=0; i<OrdersTotal(); i++)
{ if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{ if (OrderSymbol()==Symbol()) //IF POSITION OPEN

Print("Position is open...:");


...in this case it will return the text "Position is open".


How can I check if it closed??? I have tried to use:


if (OrderSymbol()!=Symbol()) //using the != signal


...but it seems not to be working!!!


Can anyone help me?


Regards


Paulo

 

You need to find closed position in the Trade History pool. See example https://docs.mql4.com/trading/OrdersHistoryTotal

 

Yes. Thank you.

This is my problem. I am a newbie on mql programing and I mostly use already made code with some changes.


What I need is to use on the same routine both conditions. That is, I need to verify both if a position for a currency pair is open, and on the same time verify if a trade for that currency pair was made.


Can you please send me some code for this situation?


Thank you very much


Regards

Paulo

 

This is what I need this code to.

I am working on an expert advisor that is based on a profitable strategy (at least for me, and that's the most important at this time).

For several times I have tried to find someone that could develop an expert advisor for this strategy but it's always the same... is it profitable?, can you proof it?, have you got forward/back tests?... so I have decided to do it myself.


It is not a simple task since I do not understand about programming and every code I wrote was based on samples and lots of reading (and a great help from some people on this forum like TheEconomist, Ferru, and you stringo... thanks a lot).


I have almost every code ready and I am on a optmizing phase of some routines.

I have setup the EA last week on a demo account I am following the results.

Here are the first results:



Note that all trades that closed at about 1,00 have reached at least 25pips, trades closed at about 10,00 have reached 35pips.

This ea has a trailing functionality that I am working on, that is setup to:

- After 25 pips move to break even 1

- After 35pips move to be 10,

- After 55pips move to be 20...not already programmed

- After that it must trail the position on every specifc number of pips (to be enetered by trader). Also not programmed.

- At the moment I have setup the EA to take profit at 100pips but is only for testing purposes because when I have the traling functionality fully working, I will let it run and managed by the trailing...


About the 2 losing trades?

- The first one (below you will find a chart that refers to GBP-USD), was not a bad trade at all. For some reason the profit trailing functionality that moves to break even 1 after reach the first 25 pips did not worked. Unfortunetly I was not in front of the computer at the time, but when I look at the charts I see that from the open price, there was a pip move of about 27-28 pips and the platform did not triggered the trailing functionality...perhaps a disconection problem, or a very fast move that did not allowed the trailing to work.


This trade was manually closed at the other day, since the EA that was working on the demo does not have a stop loss nor an exit strategy programmed, since I was testing the open trades and trailing functionality and not worried about exits...

Now the EA is programmed to use a "hiden" stop loss besides an exit strategy. If one of them is reached than it closes position. I am testing it with a 100pips hiden stop loss, that was not already programmed on this GBP-USD loss.


The -86,25 loss on USD-CHF was a loss. I let it go to see how would it work if reached the hiden stop... and it worked.

If I had enabled the exit trade functionality based on strategy, this trade would be closed at about 40-50 pips loss.




At the moment, besides working on the trailing functionality, I am hard working on the number of trades that must be opened on a day.

I want it to open as many trades as possible (one at a time for the same currency pair) but on different moments.

That's why I need the code requested, since I have tested many samples I none of them worked.


I need a code that allows me to:

- Test if a trade is working for a specific currency pair;

I can use the following sample:

"

void CheckPosition()
{ for (int i=0; i<OrdersTotal(); i++)
{ if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{ if (OrderSymbol()==Symbol()) //IF POSITION OPEN

Print("Position is open...:");

"


- Test if for that currency pair we had some trade made;

- Or the best option is to detect when a position was closed (by trailing, stop loss or take profit).


With this code I will be able to finish the EA including the trailing functionality for which I will need a similar code.


Thank you

Paulo

Reason: