Help with a bit of code

 

Hello, 


I'm new to MQL5, coming from pro-realtime. Currently using online apps to generate code. I have a simple one I'll paste below. It's a simple follow the 6 period LWMA and buy when trending up and close on a lower low. I've added a stop loss, take profit and trailing stop. The problem I have is that when, say it hits my stop or take profit, it immediately opens a new position in the same candle, sometimes leading to several trades in one candle. 


So could someone out there help to add the bit of code to make this EA do just one round trade per candle at the most, regardless of what time interval I'm using. 


Many thanks to anyone who can help. The code, which compiles just fine is;




 

When you post code please use the CODE button (Alt-S)!

Use the CODE button

 

If your program compiles without any error or warning but does not do what was intended use the debugger:

https://www.metatrader5.com/en/metaeditor/help/development/debug // Code debugging
https://www.mql5.com/en/articles/2041 // Error Handling and Logging in MQL5
https://www.mql5.com/en/articles/272 // Tracing, Debugging and Structural Analysis of Source Code
https://www.mql5.com/en/articles/35 // scrol down to: "Launching and Debuggin"

Code debugging - Developing programs - MetaEditor Help
Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
              Messages Editor

  2. //                   Copyright ©2020, ForexEAdvisor.com

    EA builder, EA Builder Pro, EATree, Etasoft forex generator, Forex Strategy Builder, ForexEAdvisor STRATEGY BUILDER, ForexRobotAcademy.com, FX EA Builder, fxDreema, Forex Generator, FxPro, Molanis, Octa-FX Meta Editor, Online Forex Expert Advisor Generator, Strategy Builder FX, Strategy Quant, Visual Trader Studio, MQL5 Wizard, etc., are all the same. You will get something quick, but then you will spend a much longer time trying to get it right, than if you learned the language up front, and then just wrote it.

    1. Since you haven't learned MQL4/5, therefor there is no common language for us to communicate.
      If we tell you what you need, you can't code it.
      If we give you the code, you don't know how to integrate it into yours.
      We are willing to HELP you when you post your attempt (using Code button) and state the nature of your problem, but we are not going to debug your hundreds of lines of code. You are essentially going to be on your own.

    2. EA builder makes bad code counting up while closing multiple orders.
      EA builder makes bad code Bars is unreliable (Max bars in chart), volume is unreliable (miss ticks.) Always use time.
      EA builder makes bad code, not adjusting for 4/5 digit brokers, TP/SL and slippage.
      EA builder makes bad code, not adjusting for ECN brokers. pre-Build 500)
      EA builder makes bad code, not checking return codes.

    3. EATree uses objects on chart to save values — not persistent storage (files or GV+Flush.) No recovery (crash/power failure.)

    4. FX EA Builder makes bad code, not checking return codes.
      FX EA Builder makes bad code, loosing open tickets on terminal restart. No recovery (crash/power failure.)
      FX EA Builder makes bad code, not adjusting stops for the spread.
      FX EA Builder makes bad code, using OrdersTotal directly.

    5. FOREXEADVISOR STRATEGY BUILDER makes bad code, non-updateing global variables.
      FOREXEADVISOR STRATEGY BUILDER makes bad code, compilation errors.
      FOREXEADVISOR STRATEGY BUILDER makes bad code, not checking return codes.

    Learn to code it, or pay someone (Freelance) someone to code it.
              Hiring to write script - General - MQL5 programming forum #1 (2019)

  3.         if(StopLoss>0) TheStopLoss=SymbolInfoDouble(_Symbol,SYMBOL_ASK)-StopLoss*MyPoint;
            if(TakeProfit>0) TheTakeProfit=SymbolInfoDouble(_Symbol,SYMBOL_ASK)+TakeProfit*MyPoint;

    You buy at the Ask and sell at the Bid. Pending Buy Stop orders become market orders when hit by the Ask.

    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid / OrderClosePrice reaches it. Using Ask±n, makes your SL shorter and your TP longer, by the spread. Don't you want the specified amount used in either direction?

    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask / OrderClosePrice reaches it. To trigger close to a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25

    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (control+O) → charts → Show ask line.)

      Most brokers with variable spreads widen considerably at end of day (5 PM ET) ± 30 minutes.
      My GBPJPY shows average spread = 26 points, average maximum spread = 134.
      My EURCHF shows average spread = 18 points, average maximum spread = 106.
      (your broker will be similar).
                Is it reasonable to have such a huge spreads (20 pip spreads) in EURCHF? - General - MQL5 programming forum (2022)

  4. Your code
    double CopyBufferMQL4(int handle,int index,int shift)
      {
       double buf[];
       switch(index)
         {
          case 0: if(CopyBuffer(handle,0,shift,1,buf)>0)
             return(buf[0]); break;
          case 1: if(CopyBuffer(handle,1,shift,1,buf)>0)
             return(buf[0]); break;
          case 2: if(CopyBuffer(handle,2,shift,1,buf)>0)
             return(buf[0]); break;
          case 3: if(CopyBuffer(handle,3,shift,1,buf)>0)
             return(buf[0]); break;
          case 4: if(CopyBuffer(handle,4,shift,1,buf)>0)
             return(buf[0]); break;
    Simplified
    double CopyBufferMQL4(int handle,int index,int shift)
      {
       double buf[];
       if(CopyBuffer(handle,index,shift,1,buf)>0) return(buf[0]);
    

  5. double iACMQL4(string symbol, int tf, int shift){
    
       ENUM_TIMEFRAMES timeframe=TFMigrate(tf);
       int handle=iAC(symbol,timeframe);

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)