Open orders question

 
I'm new to programming. I have a hedging EA I'm testing. I would like to know the number of open long and short positions and print this to the screen. I think I could branch out after a new position is opened or when the EA closes positions. Can I call the same sub routiene from different places in the code? Can any one help with a short subroutine?
 

One solution for you M.

see attached.

You can call this function from any place in your EA.

Perhaps for your purposes, you would call it at beginning of start()

Also, could call it as last statement in EA before start() exits...

imho, "as last statement" is overkill since EA called every datatick and not want to tire Client Terminal ;)

various ways to get function into your EA.

1. save the .mqh in the ...\experts\include directory and put this at start of your EA: #include <openOrdersInfo.mqh>

2. copy,paste function into your EA - outside of the start() {...} function.

3. do not forget to put a call somewhere! openOrdersInfo();

by all means - please hit <F5> to recompile

;o))


PS.

1. Buyer Beware!! tested but not rigourously tested ;)

2. usual caveats apply as Murphys law might kick in!


few test pictures...





and NO... I do not trade like above! - just kept hitting <F9> to get some varied orders going so test harnes would pick 'em up ;)


Files:
 
ukt:

One solution for you M.

see attached.

You can call this function from any place in your EA.

Perhaps for your purposes, you would call it at beginning of start()

Also, could call it as last statement in EA before start() exits...

imho, "as last statement" is overkill since EA called every datatick and not want to tire Client Terminal ;)

various ways to get function into your EA.

1. save the .mqh in the ...\experts\include directory and put this at start of your EA: #include <openOrdersInfo.mqh>

2. copy,paste function into your EA - outside of the start() {...} function.

3. do not forget to put a call somewhere! openOrdersInfo();

by all means - please hit <F5> to recompile

;o))


PS.

1. Buyer Beware!! tested but not rigourously tested ;)

2. usual caveats apply as Murphys law might kick in!


few test pictures...





and NO... I do not trade like above! - just kept hitting <F9> to get some varied orders going so test harnes would pick 'em up ;)


Thanks. I'll give this a try. Looks like it should get the job done nicely. I had some thing completely different in mind. That may be why I could not get it to work correctly. I was trying to call the information like I would in managing the trades or run a trailing stop. Then ++ to a variable all within the EA. Then print to the chart. But that would have been reprinting every tic as you said.

 

"every tic" can be thorny issue.

It can be that start of new bar is good enough for EA. eg, if M1 and 15 tics incomming in this minute, EA called upto 15 times - decision needed. intra bar tics can be misleading. All depends on strategy and SL size to cope with possible intra bar tic spikes which in reality not determine final bar status. All mainline EA code will run each data tic unless chunks are protected by decision making such as counters or flags. Like maybe: only do this every 5 tics - so have function static (or EA global) int counter ++'d each tic like you mention. Just test it's post increment value with 5 and if >= then do the 'every 5 tics code chunk' and also resetting counter to 0 ready for next count up to 5,...

You can do as you say, if I understand correctly. When open BUY you "++" buyCounter and same idea open SELL with it's sellCounter, and at other end, same for close BUY,SELL but use "--"

Then only need the Comment(...) put somewhere.

I did function as means main EA code not need concern self with your requirement - only one line to call function needed instead of having to poke around code figuring out best place to do increment/decrement - whatever floats one's boat really. One additional benefit is that function is reusable in any program just by calling it...

btw, may seem strange to do: counter >= 5

question to always ask self in conditional tests is what if counter "somehow" jumps over a value ?

You know? like Murphy's Law kicks in...

 
ukt:

"every tic" can be thorny issue.

It can be that start of new bar is good enough for EA. eg, if M1 and 15 tics incomming in this minute, EA called upto 15 times - decision needed. intra bar tics can be misleading. All depends on strategy and SL size to cope with possible intra bar tic spikes which in reality not determine final bar status. All mainline EA code will run each data tic unless chunks are protected by decision making such as counters or flags. Like maybe: only do this every 5 tics - so have function static (or EA global) int counter ++'d each tic like you mention. Just test it's post increment value with 5 and if >= then do the 'every 5 tics code chunk' and also resetting counter to 0 ready for next count up to 5,...

You can do as you say, if I understand correctly. When open BUY you "++" buyCounter and same idea open SELL with it's sellCounter, and at other end, same for close BUY,SELL but use "--"

Then only need the Comment(...) put somewhere.

I did function as means main EA code not need concern self with your requirement - only one line to call function needed instead of having to poke around code figuring out best place to do increment/decrement - whatever floats one's boat really. One additional benefit is that function is reusable in any program just by calling it...

btw, may seem strange to do: counter >= 5

question to always ask self in conditional tests is what if counter "somehow" jumps over a value ?

You know? like Murphy's Law kicks in...

"T"

I took a look at the code. I like the counter you have created. It has given me another idea for something else I'm working on. How do I call the variables in this .mqh file for use with in the EA? This is something I have never tried to do and it is of much interest to me. I have always stayed with in the confines of the EA and never used external files. If I understand correctly more than one EA can access this file at the same time? Correct me if I'm wrong but this file counts all orders and does not distinguish only the orders for the chart it is connected to. If that is true what would I need to add to use only the chart it is attached to? Will this file be able to distinguish the pair and magic number it is on. Or would this data checking "counting" routine need to be moved into the EA to process this type of information? I could have this as a seperate routine within the EA by using just a call and return keepiing it as a modular unit?

 

How do I call the variables in this .mqh file for use with in the EA?

When you "include" an mqh, the contents of the mqh file replace the "include" statement.

https://docs.mql4.com/basis/preprosessor/include

You call the variables or functions just as if they are part of the main file.

 
phy:

How do I call the variables in this .mqh file for use with in the EA?

When you "include" an mqh, the contents of the mqh file replace the "include" statement.

https://docs.mql4.com/basis/preprosessor/include

You call the variables or functions just as if they are part of the main file.

Thanks Phy. Just so I'm absolutly clear. They become global variables? Able to be called by any part of the program? Do I need to declare the variable in the program int, double and the like? Or as you say that is part of the "include" sattement? Nothing else to be done.

 

#include "TheFile.mqh"

That line is replaced by the contents of the file, just as if you copied the contents and pasted it in to the main source code mq4.

Write a test program to find your answers.

 
phy:

#include "TheFile.mqh"

That line is replaced by the contents of the file, just as if you copied the contents and pasted it in to the main source code mq4.

Write a test program to find your answers.

will do thanks

Reason: