Buy and Sell Trades for indicator conditions.

 

Hello I'm a newbie and i need help in creating Buy and sell trades for my EA. All the indicators show fine on the strategy tester but no trades are placed, please Help?


Here's code;

 


 #include<Trade\Trade.mqh>


   // Creat an instance of Ctrade
   CTrade trade;

void OnTick()
  {
   // We create a string for the signal
   string signal ="";

   // We calculate the Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);

   // We calculate the Bid price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);
   
   //create an array for several prices
   double myPriceArray[];
   double ADX_plus_arr[]; 
   double ADX_min_arr[]; 
   double ADX_arr[];
   MqlRates PriceArray[];
   double KArray[];
   double DArray[];
   double mySARArray[];
   
   // sort the array from the current candle downwards
   ArraySetAsSeries(PriceArray,true);
   ArraySetAsSeries(KArray,true);
   ArraySetAsSeries(DArray,true);
   ArraySetAsSeries(mySARArray,true);
   
  
   
   //Define Indicator with a handle
   int ADX_handle = iADX(_Symbol,_Period, 12);
   int StochasticDefinition=iStochastic(_Symbol,_Period,10,7,3,MODE_EMA, STO_LOWHIGH);
   int SARDefinition = iSAR (_Symbol,_Period,0.02,0.2);
   int MacDDefinition = iMACD(_Symbol,_Period,12,26,9,PRICE_CLOSE);
   int FastMovingAverage = iMA(_Symbol,_Period,12,0,MODE_EMA,PRICE_CLOSE);
   int SlowMovingAverage = iMA(_Symbol,_Period,24,0,MODE_EMA,PRICE_CLOSE);
   
   //WE FILL the array with price data
   CopyBuffer(StochasticDefinition,0,0,3,KArray);
   CopyBuffer(StochasticDefinition,1,0,3,DArray);
   CopyBuffer(SARDefinition,0,0,3,mySARArray);
   CopyBuffer(MacDDefinition, 0,0,3,myPriceArray);
   int Data =CopyRates(_Symbol,_Period,0,3,PriceArray);
   CopyBuffer(ADX_handle,0,0,1,ADX_arr);
   CopyBuffer(ADX_handle,0,1,1,ADX_plus_arr);
   CopyBuffer(ADX_handle,0,2,1,ADX_min_arr);
   
   double KValue0=KArray[0];
   double DValue0=DArray[0];
   double KValue1=KArray[1];
   double DValue1=DArray[1];
   double ADXmin=ADX_min_arr[0];
   double ADXplus=ADX_plus_arr[0];
   
   double LastSARValue=NormalizeDouble(mySARArray[1],5);
   double MacDValue=(myPriceArray[0]);
   
   if (ADX_handle>25)
   if (ADXplus<ADXmin)
   if (LastSARValue > PriceArray[1].high)
   if (MacDValue<0)
   if (KValue0>80&&DValue0>80)
   if (KValue0 < DValue0)
   if (FastMovingAverage < SlowMovingAverage)
   
   {
   signal="sell";
   if (signal =="sell" && PositionsTotal()<1)
      trade.Sell(0.10,NULL,Bid,(Bid+200*_Point),(Bid-150 * _Point),NULL);
              
              
   }
              
              
    
   
   if (ADX_handle>25)
   if (ADXplus>ADXmin)
   if (LastSARValue < PriceArray[1].low)
   if (MacDValue>0)
   if (KValue0>20&&DValue0<20)
   if (KValue0 > DValue0)
   if (FastMovingAverage > SlowMovingAverage)
   
   {
   signal="buy";
   if (signal =="Buy" && PositionsTotal()<1)
      trade.Buy(0.10,NULL,Ask,(Ask-200*_Point),(Ask+150 * _Point),NULL);
   
           
   }
   
  }
   
  
The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
 

Forum on trading, automated trading systems and testing trading strategies

Some operator expected

Fernando Carreiro, 2023.02.26 14:10

Please edit your post (don't create a new post) and replace your code properly (with "</>" or Alt-S), or attach the original file directly with the "+ Attach file" button below the text box.

NB! Very important! DO NOT create a new post. EDIT your original post.


 
  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. 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. double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);
    

    You used NormalizeDouble, It's use is usually wrong, as it is in your case.

    1. Floating point has an infinite number of decimals, it's you were not understanding floating point and that some numbers can't be represented exactly. (like 1/10.)
                Double-precision floating-point format - Wikipedia, the free encyclopedia

      See also The == operand. - MQL4 programming forum (2013)

    2. Print out your values to the precision you want with DoubleToString - Conversion Functions - MQL4 Reference.

    3. SL/TP (stops) need to be normalized to tick size (not Point) — code fails on non-currencies.
                On 5Digit Broker Stops are only allowed to be placed on full pip values. How to find out in mql? - MQL4 programming forum (2011)

      And abide by the limits Requirements and Limitations in Making Trades - Appendixes - MQL4 Tutorial and that requires understanding floating point equality Can price != price ? - MQL4 programming forum (2012)

    4. Open price for pending orders need to be adjusted. On Currencies, Point == TickSize, so you will get the same answer, but it won't work on non-currencies. So do it right.
                Trailing Bar Entry EA - MQL4 programming forum (2013)
                Bid/Ask: (No Need) to use NormalizeDouble in OrderSend - MQL4 programming forum (2012)

    5. Lot size must also be adjusted to a multiple of LotStep and check against min and max. If that is not a power of 1/10 then NormalizeDouble is wrong. Do it right.
                (MT4 2013)) (MT5 2022))

    6. MathRound() and NormalizeDouble() are rounding in a different way. Make it explicit.
                MT4:NormalizeDouble - MQL5 programming forum (2017)
                How to Normalize - Expert Advisors and Automated Trading - MQL5 programming forum (2017)

    7. Prices you get from the terminal are already correct (normalized).

    8. PIP, Point, or Tick are all different in general.
                What is a TICK? - MQL4 programming forum (2014)

  4. trade.Buy(0.10,NULL,Ask,(Ask-200*_Point),(Ask+150 * _Point),NULL); 

    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)

 
 Sergey Golubev #:

Done, can you see it?
Reason: