expert advisor is not placing buys

 

Hey all,

Metatrader seems to be really finnicky and sometimes when I launch the strategy tester it will place buys and sometimes it doesn't I dont understand whats wrong with the code i've written. any help would be greatly appreciated.

#include<Trade\Trade.mqh>
CTrade trade;
bool Cross_Below = false;
bool Cross_Above = false;
bool in_agreement = false;
input int TrailingStop = 30;
string dumb = "asdbadakbv";
double PositionOpenPrice(string pSymbol = NULL)
   {
      if(pSymbol == NULL) pSymbol = _Symbol;
      bool select = PositionSelect(pSymbol);
      if(select == true) return(PositionGetDouble(POSITION_PRICE_OPEN));
      else return(WRONG_VALUE);
      
   }
  
void OnTick()

  {
//---
    // get current time
    // save time  when crossed below
    // minus current time from timev saved equals our shift bar back to check if any crossed below 100n or
    double close_current = iClose(_Symbol,PERIOD_CURRENT,1);
    MqlTick price;
    SymbolInfoTick(_Symbol, price);
   
    double ask = price.ask;
    double bid = price.bid;
   
    // price array with previous price info
    MqlRates PriceInfo[];
   
    // sorts the array from the current candle as 0 to past being n
    ArraySetAsSeries(PriceInfo, true);
   
    //this fills the array with data from the current candle back 3 candles
    int PriceData = CopyRates(_Symbol,_Period,  0,3, PriceInfo);
   
    string signal = "";
   
    double EMA50Array[], EMA100Array[], EMA150Array[];
   
    int EMA50Definition = iMA(_Symbol, _Period, 50,0,MODE_EMA,PRICE_CLOSE);
   
    int EMA100Definition = iMA(_Symbol, _Period, 100,0,MODE_EMA,PRICE_CLOSE);
   
    int EMA150Definition = iMA(_Symbol, _Period, 150,0,MODE_EMA,PRICE_CLOSE);
   
    ArraySetAsSeries(EMA50Array, true);
   
    ArraySetAsSeries(EMA100Array, true);
   
    ArraySetAsSeries(EMA150Array, true);
   
    CopyBuffer(EMA50Definition, 0,0, 50, EMA50Array);
   
    CopyBuffer(EMA100Definition, 0,0, 100, EMA100Array);
   
    CopyBuffer(EMA150Definition, 0,0, 150, EMA150Array);
    
    double ema_slope50 = (EMA50Array[0]-EMA50Array[10])/10;
    double ema_slope100 = (EMA100Array[0]-EMA100Array[10])/10;
    double ema_slope150 = (EMA150Array[0]-EMA150Array[10])/10;
   if (close_current > EMA50Array[0] && Cross_Below == true)
   {
   Cross_Above= true;
   Cross_Below = false;
   
   }
       if (close_current < EMA50Array[0])
    {
     Cross_Below = true;
    }
    
    if (close_current < EMA150Array[0])
    {
     Cross_Below = false;
     
    }
   
    if(Cross_Above == true)  //&& ema_slope100>0 && ema_slope150 >0 &&ema_slope50>0)
    
    {
      Comment(dumb);
      //trade.Buy(0.10, NULL, bid,0,0,NULL);
      MqlTradeRequest request;
      MqlTradeResult result;
      request.action = TRADE_ACTION_DEAL;
      request.type = ORDER_TYPE_BUY;
      request.symbol = _Symbol;
      request.volume = 1;
      request.type_filling = ORDER_FILLING_FOK;
      //request.price = NormalizeDouble(ask,_Digits);
      request.price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
      request.sl = price.last-0.0002;
      request.tp = price.last+0.0002;
      request.deviation = 50;
      request.magic = 43;
      Cross_Above = false;
      Cross_Below = false;
   
      OrderSend(request, result);
    }
    if(PositionSelect(_Symbol) == true && TrailingStop >0)
    {
      MqlTradeRequest request;
      MqlTradeResult result;
      request.action = TRADE_ACTION_SLTP;
      request.symbol = _Symbol;
      
      long posType = PositionGetInteger(POSITION_TYPE);
      double currentStop = PositionGetDouble(POSITION_SL);
      
      double trailStop = TrailingStop * _Point;
      double traiStopPrice;
      
      if(posType == POSITION_TYPE_BUY)
      {
         double trailStopPrice = SymbolInfoDouble(_Symbol,SYMBOL_BID) - trailStop;
         if(trailStopPrice > currentStop)
         {
            request.sl = trailStopPrice;
            OrderSend(request,result);
            
         }
      }
      else if(posType == POSITION_TYPE_SELL)
      {
         double trailStopPrice = SymbolInfoDouble(_Symbol,SYMBOL_ASK) + trailStop;
         if(trailStopPrice < currentStop)
         {
            request.sl = trailStopPrice;
            OrderSend(request,result);
         }
      }
     }
       
    //datetime time = price.time;
   double close = iClose(_Symbol,PERIOD_CURRENT,5);
   //Comment(PositionOpenPrice());
   
 
   
}
 
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 (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.03.08
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 2020.07.05
          How to call indicators in MQL5 - MQL5 Articles 12 March 2010

 
Tim G. :

Hey all,

Metatrader seems to be really finnicky and sometimes when I launch the strategy tester it will place buys and sometimes it doesn't I dont understand whats wrong with the code i've written. any help would be greatly appreciated.

Correct your main mistake first.

Forum on trading, automated trading systems and testing trading strategies

How to start with MQL5

Vladimir Karputov, 2020.11.13 06:01

Receiving data from an indicator in an MQL5.

Scheme:

Getting data from indicators in an MQL5 Expert Advisor

An example for the iMA indicator:


Reason: