Need help getting Spread to work

 

Hello everyone,

I'm trying to code a very basic EA but I'm having trouble getting the function to work that checks the spread.

I have tried calculating it manually as the code is now.

I have tried calculating it with the MarketInfo(Symbol(), MODE_SPREAD) function.

I have tried calculating it with the SymbolInfoInteger(Symbol(), SYMBOL_SPREAD) function.

Sometimes I can get the EA to trade but it doesn't stop trading when I change the MaxSpread to a very low value.

Or I can get the EA not to trade, but it won't trade when I change the MaxSpread to a very high value.

Any help is appreciated!

Thank you,

//+------------------------------------------------------------------+
//|                                                        Test1.mq4 |
//|                                     Copyright 2021, Chris Jones. |
//|                                                                  |
//+------------------------------------------------------------------+
#property strict

#define MAGICNUM  20131111

//Define our Parameters
input double Lots             = 0.1;
input int TakeProfit          = 15;
input int StopLoss            = 5;
input double MaxSpread        = 1;

double TProfit;
double SLoss;

// Get current prices and spread
double AskPrice() {
   return(Ask*Point());
   }
double BidPrice() {
   return(Bid*Point);
   }
// Check if Spread is less than the MaxSpread
bool LessThanMaxSpread() {
   double Spread = (Ask - Bid) / _Point;
   if(Spread <= MaxSpread)
     {
      return true;
     }
   else return false;
   }

// Check if an order was already opened at current price
bool OrderOpenAtCurrentPrice() {
  bool YesNo = false;
 
  for(int i = OrdersTotal() - 1; i >= 0; i--) {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
 
    if(OrderOpenPrice() == Ask) {
      YesNo = true;
      }
    }
    
  return(YesNo);
}

// Check if price is a full pip
bool FullPip(){
   bool YesNo = false;
   
    if(Ask*10000 == round(AskPrice())) {
       YesNo = true;
      }
    
    return(YesNo);
   }
   
// Normalize prices
double ND(double val)
{
return(NormalizeDouble(val, Digits));
}
//Normalize StopLoss
double SL()
{
double s = StopLoss * 0.0001;
return(Ask - s);
}
//Normalize TakeProfit
double TP()
{
double t = TakeProfit * 0.0001;
return (Ask + t);
}

// Main function
void start() {

// Set Take Profit and Stop Loss to Prices
     //if(TakeProfit > 0)
     //   {
     //    TProfit = Ask+(TakeProfit*Point);
     //   }
     //if(StopLoss > 0)
     //   {
     //    SLoss = Ask-(StopLoss*Point);
     //   }

     //if(FullPip() && OrderOpenAtCurrentPrice() && Spread() < MaxSpread)
     if(OrderOpenAtCurrentPrice() == false && LessThanMaxSpread() == true)
      {
         OrderSend(Symbol(), OP_BUY, Lots, ND(Ask), 3, SL(), TP(), "3xROI", 0, 0, Green);
      }
     else return;
      
   }

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   start();
  }
//+------------------------------------------------------------------+
SymbolInfoInteger - Market Info - MQL4 Reference
SymbolInfoInteger - Market Info - MQL4 Reference
  • docs.mql4.com
SymbolInfoInteger - Market Info - MQL4 Reference
Reason: