Problems with coding local vs global variables on mql4
Results 1 to 10 of 10

Thread: Problems with coding local vs global variables on mql4

  1. #1
    Hello,
    I'm fairly new to mql4. I have been doing fine but lately I have been completely stumped by a problem I have encounter. I have coded a simple ea that works fine once I declare a local variable for buyorder
    int buyorder=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0 ,0,NULL,MagicNumber,0, Green);

    if (buyordergt;0)
    OrderModify(buyorder,OrderOpenPrice(),bsl,btp,0,CL R_NONE);

    but should I admit buyorder as a global variable (and take away the int) it doesn't work. It merely keeps modifying orders infinitum. For the life of me, I can't comprehend why this should happen as a global variable should have the ability to be used anywhere in the app and it's value should have the ability to change. I could stick with the local variable but I wish to enlarge the ea to make numerous orders and to use monitoring stops etc and I need to use global variables. I have included the ea below. The first version(local variable) functions, the second(global) doesn't. If anyone can help me, that would be so appreciated as I have literally spent days trying to work out it. PS This also applies to the variable sellorder.
    Thanks
    https://forexintuitive.com/attachmen...1727520325.mq4
    https://forexintuitive.com/attachmen...1081469781.mq4

  2. #2
    When you declare a local variable it is only available during the 1 case that start{} is called.
    Every time start{} runs it re-declares the variable.
    Looking at your code that the buyorder variable is only called if there are no open BUY orders in place, and a new order is opened.
    In case an order exists, then the buyorder variable is bypassed from the if statements so doesn't get announced and so the order change regular doesn't get invoked.
    Ie when buyorder doesnt get called, then buyorder remains undeclared, or has a zero or value, as buyorder gt; 0 is false.
    Therefore no order alteration.

    On the other hand, if you declare a global variable then buyorder will retain the values and thus the code as written will attempt to change each time, as buyorder gt; 0 is accurate.

    Hope that this helps
    Fred

  3. #3
    Thanks Fred,
    Thank You Xaphod,

    So easy to make a mistake with this coding!!
    Will definitely try and make the coding more legible.
    Thanks again for the input!

  4. #4
    Quote Originally Posted by ;
    Thanks Fred, Thanks, really simple to make a mistake with this coding!! Will definitely attempt to make the coding more legible. Thank you for the input!
    You should only call OrderModify once after the OrderSend. Then it matters not whether buyorder is global or local. Local is more preferable.

    Inserted Code int buyorder=0if (PreviousFastlt;PreviousSlow CurrentFastgt;CurrentSlow) if (OpenOrdersThisPair(Symbol())==0) buyorder=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,0 ,0,NULL,MagicNumber,0, Green); if (buyordergt;0) OrderModify(buyorder,OrderOpenPrice(),bsl,btp,0,CL R_NONE);

  5. #5
    IV
    Guest
    Quote Originally Posted by ;
    I cannot know why this should occur as a global variable ought to have the ability to be utilized anywhere
    That's the exact problem. It's a scoping issue.

    In both examples the if(lt;buyorder/sellordergt; gt; 0) statements are in the incorrect scope; together with the difference being the scope of buyorder/sellorder changes together with all the examples, inducing the ill-effect in the next example. Watch the sample code to get proper scoping of Xaphod.

    If I may ask, what language are you used to code in?

  6. #6
    Quote Originally Posted by ;
    quote That's the exact problem. It's a scoping problem. In both examples the if(lt;buyorder/sellordergt; gt; 0) statements are in the wrong extent; with the difference being the reach of buyorder/sellorder changes with the examples, inducing the ill-effect at the second case. Watch Xaphod's sample code for proper scoping. If I may ask, what language are you accustomed to code in?
    Hello rockit! Really I don't know how to program, I have been reading a lot and moved through the mql4 book. I am unsure what you mean by scoping although I understand from the illuions above where I went wrong.
    Thanks!

  7. #7
    IV
    Guest
    Quote Originally Posted by ;
    quote Hi! Really I don't understand how to program, I have been reading a lot and went via the mql4 novel. I am not sure what you mean by scoping although I know from the examples above where I went wrong. Thanks!
    I mean scope/context: https://en.wikipedia.org/wiki/Scope_(computer_science)

    The if-statements that checks for buyorder/sellorder were put in the wrong context, since they belonged from the context (scope) of the other (outer) if-statements.

  8. #8
    Hello as can observe the differences between a global variable and neighborhood is that the global variable may be used by more than EA at once to transport data between EA's and Indiors, the local variable is known just from the EA declare on it.

    That I hope you find this helpful

  9. #9
    Quote Originally Posted by ;
    Hello as can see the differences between a global variable and neighborhood is that the global variable may be used by over EA at once to transfer information between EA's and Indiors, the local variable is known just by the EA declare on it. I hope you find this useful
    The terminology is unfortunate and may be confusing to some.
    You're referring to a Global Variable of the client terminal.
    The earlier posts are speaking to some globalscope variable which can only be used in the EA etc where it is declared.

  10. #10
    Quote Originally Posted by ;
    Hi, I am pretty new to mql4. I've been doing fine but recently I've been completely stumped by a problem I've run into. For the life span of me, I cannot understand this...
    Start by doing right indentation of the code. The formatting is really poor.
    Https://en.wikipedia.org/wiki/Indent_style

    Subsequently sew all if statements/blocks in curly braces {}.

    Then you are going to see you are using an undefined/uninitialized factor in one of those apps. The compiler must not really invent it.
    Or that the factor isn't updated in another edition.

    I would also suggest defining all local variables on peak of each function and using curly braces to enclose all cubes. That would probably have provided comments via the compiler that some thing was wrong.

    TLDR:
    Poor code partitioning; Nesting of if statements without curly braces; Inline variable declaration Tricking compiler into approving bad code; Code not running as expected.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
This website uses cookies
We use cookies to store session information to facilitate remembering your login information, to allow you to save website preferences, to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners more information