Buy or Sell only once when reaching the average

 

In mql5 , I need help from everyone, I am in need of a code that when reaching the moving average of 20 periods it sell or buy as required, this only once when reaching the 20-period averages and don't buy or sell all the time, you understand, this sale or purchase is just one time when reaching the averages and then waits until reaching the averages again.



Here's the code I have...



fastmedium= iMA (_Symbol,mm_time_graph,mm_Rapid_period,displacementmm,methodmm,mm_price);

if (rates[0].low>handlingspeed[0] && PositionsTotal()==0)
    {
     trade.Buy(lote,NULL,Ask,(Ask-SL1),(Ask+SL2),"Buy");
     Print ( "Open Buy Order" ); 
    }

if (rates[0].high<handlingspeed[0] && PositionsTotal()==0)
    {
    trade.Sell(lote,NULL,Bid,(Bid+SL1),(Bid-SL2),"Sell");
    Print ( "Open Sell Order" ); 
    }
 
PR: Here's the code I have..

You have no code, just a useless snippet.

  1. Your posted code is without context. Are those lines together, or is the iMA/iCustom call in OnInit (where it should be)? Always post all relevant code (using Code button) or attach the source file.
         How To Ask Questions The Smart Way. (2004)
              Be precise and informative about your problem

  2. You save the iMA handle somewhere, but where do you get the values into handlingspeed[]?

    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)

  3. You reference a structure called rates, but where do you declare it and set values into it?

  4. trade.Buy(lote,NULL,Ask,(Ask-SL1),(Ask+SL2),"Buy");

    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 at 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)

Reason: