I'm just looking at your last function:
int is_already_opened() { int i = 0; for ( i = 0; i < PositionsTotal(); i++) { int position_magic = 0; ulong ticket = PositionGetTicket(i); if (PositionSelectByTicket(ticket)) { position_magic = PositionGetInteger(POSITION_MAGIC)) return (position_magic); } } return 0; }
Some remarks:
- The function name is_already_opened implies that the outcome is either true or false, so I'd expect it to return a bool which is not the case.
- In Mql5 a magic number is of type long, but you use int.
- Your for loop traverses orders front to back, this can cause it to skip an order if concurrently one of the orders has been removed from the list.
- You're selecting an order twice, first with PositionGetTicket() then again with PositionSelectByTicket(). The first select will be sufficient.
- Your function exits as soon as any magic number has been found, but what if it's not the magic you were looking for? You need to pass the wanted magic to your function so it can seek for it:
if (buy_signal_flat && ! is_already_opened(flat_buy_magic))
I didn't look at the rest. Happy coding.
lippmaje:
Wow, thank you very much for the clear and detailed explanations.I'm just looking at your last function:
Some remarks:
- The function name is_already_opened implies that the outcome is either true or false, so I'd expect it to return a bool which is not the case.
- In Mql5 a magic number is of type long, but you use int.
- Your for loop traverses orders front to back, this can cause it to skip an order if concurrently one of the orders has been removed from the list.
- You're selecting an order twice, first with PositionGetTicket() then again with PositionSelectByTicket(). The first select will be sufficient.
- Your function exits as soon as any magic number has been found, but what if it's not the magic you were looking for? You need to pass the wanted magic to your function so it can seek for it:
I didn't look at the rest. Happy coding.
I see that my attempt was very sloppy and that I don't really comprehend how functions work.
Will dig deeper into it.
Best regards,
TS
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello,
I'm having a stupid hard time setting one position by signal.
Signals are both buy and sell.
They're based on 2 strategies : flat & trending (which I got inspired by one of the incredible articles on the site).
What I did is setting magic numbers to track which signal created the trade :
then depending on condition, set the magic number and open a position, for eg :
I'm using a bool to return if there's an already opened position by the sub-strategy (flat or trend). In this instance :
EA is working as expected. But I'm trying to improve my coding skills.
What I would like to do is :
for the 4 scenariis :
flat_buy // flat_sell
trend_buy // trend_sell
rather than having 4 bool functions to return if there's already an opened position with the set magic number
have a single looping kind-of function that returns the magic number to check if trade is possible....
basically what I tried is :
then the condition would be
if (buy_signal_flat && is_already_opened() != flat_buy_magic)
but I feel like the int function I made doesn't return a correct value or something beyong what I'm capable to grasp.
So it opens more than one position per signal..
What am I missing?
In any case, hope this is clear enough and thanks for your time and help.