Forex Zone - Forex Forum

Help with Coding please

Discussion started on MT4 / MT5 EAs

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Hi
I am trying my hand at coding, and wrote a simple EA.
I am having a problem with this 1 issue.

 
 bool OneOrderAtATime=false;
  if(OrdersTotal()==0 || !inOneOrderAtATime)
      OneOrderAtATime=true;
      else OrdersTotal()=MaxOrders;
     

I give the option to have more that 1 order open, I get the "OneOrderAtATime" = True right, so it doesn't open any more orders.
But if it returns false,  When I want more than 1 open order, it opens up to 20 orders, I want to set the Max.
(If I set it to false, so the false statement is what I want,) I want to set a MaxOrders value. 
(Maxorders is in my Variables), I keep on getting this error when I compile:
'OrdersTotal' - l-value required Optimizing RS.mq4 132 12

Linkback: https://www.forex.zone/mt4-mt5-eas/7/help-with-coding-please/198/
#1 - August 26, 2018, 06:58:22 PM

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
I noticed a few things wrong.

1) You cannot set a value to OrdersTotal(). OrdersTotal() is a function that returns a value, but not a variable that can have a value set to it.

2) You should not even use OrdersTotal() to begin with. OrdersTotal() gives the total number of orders open in the platform, but not from your specific EA. So orders from other EA's or even manual orders will be counted. It would be better to know how many orders this specific EA has made and manage only your EA's orders.

3) You have OneOrderAtATime set to false and the very next line has a check that sees if it is false (which it will be) and then sets it to true. I'm not sure what purpose that would serve and I do not think it is right.
#2 - August 26, 2018, 07:48:36 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
I thought OrdersTotal will not be right
What can I call there to set a max nr of trades?
#3 - August 26, 2018, 07:55:24 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
I set MaxTrades as a variable in the global scope.
The EA must check how many open orders I have, (hopefully excluding pending orders, ie, SL and TP, else it will count those as open orders too. Or that is how my mind is seeing it)

I just want it to limit the amount of open trading orders.

Sorry for all the stupid posts, maybe 1 day I will code without help, then I can be like you and CP ;)
#4 - August 26, 2018, 08:00:49 PM

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
What can I call there to set a max nr of trades?
You have to write your own code to count and limit orders since there is nothing you can call to do this for you.
#5 - August 26, 2018, 08:26:15 PM

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
I set MaxTrades as a variable in the global scope.
The EA must check how many open orders I have, (hopefully excluding pending orders, ie, SL and TP, else it will count those as open orders too. Or that is how my mind is seeing it)

I just want it to limit the amount of open trading orders.
MaxTrades can be global or even a user input, but it doesn't matter in this case since it cannot be assigned to OrdersTotal().

If you want to exclude pending orders then OrdersTotal() definitely is not what you need here since that will return pending orders too.

I understand already what you want to do, but you are going about the wrong way of doing it. First, you need to have your EA count how many orders it itself has made. It needs to recognize and count only it's own orders, but not orders from other EA's or even manual orders. Next, you need to simply write code to prevent orders if the MaxOrders # has already been met. There are many ways to do this. For example, you can add a check before the OrderSend() function. So if current order count >= MaxOrders then no new trade. 
#6 - August 26, 2018, 08:35:27 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Thanks
Trial and error is the way to go ;)
#7 - August 26, 2018, 08:42:57 PM

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
No problem. Yes, that is probably the best way to learn.
#8 - August 26, 2018, 08:47:59 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
One more question please
I have both MT4 and MT5,
How can I compile a MT5 EA or Indo to ex4?
Obviously what I can gather is ex5 doesn't work on ex4.
#9 - August 27, 2018, 03:26:15 PM

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
How can I compile a MT5 EA or Indo to ex4?
Obviously what I can gather is ex5 doesn't work on ex4.
The two platforms work differently and the languages are also different. So you need to convert the code to from MQL5 to MQL4. After this, compile using the MT4's MetaEditor which will produce an EX4.
#10 - August 27, 2018, 03:43:53 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
IS there software or something I can use to do the conversion, Google gives many answers but not anything I can use
Thanks again
#11 - August 27, 2018, 04:38:13 PM

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
No, there is nothing I know about, but I also haven't looked for one too. Even if there is a way to automatically convert the code, I don't think it would work for everything. In my opinion, something like this needs done by hand by a real person.
#12 - August 27, 2018, 05:04:29 PM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Hi all,
I've been very busy lately trying to master this EA coding thing.
I am getting there slowly...
A little coding help again please.
How do I ensure the EA only opens a trade on the close of the bar.
There a.re a couple of instances when an indicator reaches a "true" level, but the bar closes false, (due to price decrease/increase)
Closing the trade obviously depends on SL and TP (or other parameters/functions)
I tried assigning an index value to the indi, but that doesn't work
Thanks
I will be able to post EA's soon/ or even help with easy EA's ;)
#13 - September 25, 2018, 12:42:35 AM
« Last Edit: September 25, 2018, 12:46:52 AM by Francoisvs »

Administrator
  • Hero Member
  • Posts: 4764
  • Points: 34666
  • Likes Received: 4863
  • Reputation: +220/-14
You have to create a simple function to check when a new bar forms. Then have the EA trade the instant the new bar opens. When a new bar opens, it can then check the previous bar's indicator values, which are now closed and confirmed for many indicators.

Here is a sample code:

Code: [Select]
bool NewBar()
 {
  if (Time[0]!=time)
   {
    time=Time[0];
    return(true);
   }
  
  return(false);
 }

You would have a global variable 'time' declared (not shown above). When the current bar's time is different than the last recorded time, the time variable is updated and the function returns true. When this happens, trading is allowed. If the function is false, trading is not allowed. Let me know if that makes sense.
#14 - September 25, 2018, 01:05:07 AM

  • Sr. Member
  • Posts: 352
  • Points: 71
  • Likes Received: 150
  • Reputation: +11/-0
Awesome. 
Thank you very much
#15 - September 25, 2018, 07:45:08 AM

Members:

0 Members and 1 Guest are viewing this topic.