MY EA is only taking Sell orders

 

The EA I created is a beast based on MA cross and never loses if I dont use SL. but the issue I am getting in the strategy tester is that it is only opening short trades...

it also open many positions at once like 100, and when they hit tp, it looks to sell straight away. 

as well as getting it to open buy orders, can you also help me figure out how to restrict the amount of trades triggered?


I will attach the script here

//+------------------------------------------------------------------+
//|                                                   RSI Scalper.mq4|
//|                                                    Yussuf Ibrahim|
//|                                         Suggested timeframe 15min|
//+------------------------------------------------------------------+

// Profit £, D/D %

#property copyright "Yussuf Ibrahim"
#property link      "None"
#property strict

#include <stdlib.mqh>
#include <stderror.mqh>

extern int Slippage=3;
extern int MagicNumber = 45243310;
extern int TimeFrame = 0;// Optimize per currency pair. current=0 , M1=1, M5=5, M15=15,
// M30=30, H1=60, H4=240, Daily=1440
//-------------------------------
extern bool MM=true;//Select true for automatic money management
extern double RiskPercent=1;//Update per your desired risk
extern double Lots=0.01;//Optimize per account balance
extern double Stoploss=25;
extern double Profit=50;
extern int MaxTrade=1;
//----------------------
extern bool TrailingAlls = true;
extern int Trail = 29;            //Optimize per currency pair and timeframe
extern int TrailStart = 1;       //Optimize per currencypair and timeframe
//----------------------
extern bool BreakEven = true;
extern int  BreakEvenTrigger = 20;
//-----------------------------
double Poin; // global variable declaration to fix the 6 Digit Forex Quotes
//issue for MetaTrader Expert Advisors
string EA_Name = "RSI Scalper";
bool AlertOn = true;
datetime timeprev=0;//Working only after a new candle.


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+

void OnTick ()
  {
           
    //current chart,current period, 30 candles, no shift, exponential, close price
    double SlowMovingAverage = iMA (NULL,0,30,0,MODE_EMA,PRICE_CLOSE,0);
    
    //current chart,current period, 30 candles, no shift, exponential, close price
    double LastSlowMovingAverage = iMA (NULL,0,30,0,MODE_EMA,PRICE_CLOSE,1);
    
    //current chart, current period, 20 candles, no shift, exponential, close price
    double FastMovingAverage = iMA (NULL,0,20,0,MODE_EMA,PRICE_CLOSE,0);
    
    //current chart, current period, 20 candles, no shift, exponential, close price
    double LastFastMovingAverage = iMA (NULL,0,20,0,MODE_EMA,PRICE_CLOSE,1); 
                
    double Stochastics = iStochastic (NULL,0,5,3,3,MODE_SMA,0,PRICE_CLOSE,0)
    
    //-----------------------
    // SENDING ORDERS
    //------------------------
    
    //Send Sell Order
    
   ; if(OrdersTotal()==0);
    {
    if(iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_MAIN,0) < iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL,0) && 
    iMA(NULL, PERIOD_CURRENT, 20, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA(NULL, PERIOD_CURRENT, 30, 0, MODE_EMA, PRICE_CLOSE, 0))
      
    OrderSend (_Symbol,OP_SELL,0.01,Bid,0,Stoploss,Bid-500*_Point,NULL,0,0,Red);
    else 
      return;
    }
    
    //Send Buy Order
    
    if (OrdersTotal()==0)
    {
    if(iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_MAIN, 0) > iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL, 0) && 
    iMA(NULL, PERIOD_CURRENT, 20, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA(NULL, PERIOD_CURRENT, 30, 0, MODE_EMA, PRICE_CLOSE, 0))
     
    OrderSend (_Symbol,OP_BUY,0.01,Ask,0,Stoploss,Ask+500*_Point,NULL,0,0,Green);
    else 
      return;
    
  }
}

//+------------------------------------------------------------------+
//| calculating orders breakeven                                   |
//+------------------------------------------------------------------+

void Breakeven()
   {
   int totalorders = OrdersTotal();
   for(int i=totalorders-1;i>=0;i--)
   {
   
   if(BreakEven== false)
   continue;   
   
   if(BreakEvenTrigger == 0)
   continue;   
   
   if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   continue;

   if(OrderSymbol()!=Symbol())
   continue;
   
   if(OrderMagicNumber()!=MagicNumber)
   continue;
   
   if(BreakEven== true)
   
   if(BreakEvenTrigger > 0)

   if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
   
   if(OrderSymbol()==Symbol())
   
   if(OrderMagicNumber()==MagicNumber)
   {   
   
   if(OrderType() == OP_BUY)
   
   if(Ask<OrderOpenPrice())
   continue;

   if(OrderStopLoss()>OrderOpenPrice()) 
   continue;
     
   if(Ask>OrderOpenPrice())

   if(OrderStopLoss()<OrderOpenPrice() || OrderStopLoss()==0) 

   if(NormalizeDouble((Ask-OrderOpenPrice()),Digits)>=(BreakEvenTrigger*Poin))
   { 

   OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
   Print( "Order moved to breakeven : ", OrderStopLoss());
   }
   
   if(OrderType() == OP_SELL)
   
   if(Bid>OrderOpenPrice())
   continue;

   if(OrderStopLoss()<OrderOpenPrice()) 
   continue;
     
   if(Bid<OrderOpenPrice())   
   
   if(OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0) 
   
   if(NormalizeDouble((OrderOpenPrice()-Bid),Digits)>=(BreakEvenTrigger*Poin))
   { 
   
   OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Green);
   
   }
   }
   }
   return;
   }

//+------------------------------------------------------------------+
//| modifying orders trailing stop                                   |
//+------------------------------------------------------------------+
 
void Trailingalls(int start,int stop)
{
 int profit;
 double stoptrade;
 double stopcal;
 
 if(stop==0)
  return;
 
 int trade;
 for(trade=OrdersTotal()-1;trade>=0;trade--)
 {
  if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))
   continue;

  if(OrderSymbol()!=Symbol())
   continue;
  
  if(OrderMagicNumber()!=MagicNumber)
   continue;
   
  if(TrailingAlls== false)
  continue;   

  if(OrderSymbol()==Symbol())
  
  if(OrderMagicNumber()==MagicNumber)
  
  if(TrailingAlls== true)
   
  {
  if(OrderType()==OP_BUY)
  {
   profit=NormalizeDouble((Bid-OrderOpenPrice())/Poin,0);
   if(profit<start)
    continue;
    
   stoptrade=OrderStopLoss();
   stopcal=Bid-(stop*Poin);
   
   if(stoptrade==0||(stoptrade!=0 && stopcal>stoptrade))
      
      //RefreshRates();
      OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);
  }
  
  if(OrderType()==OP_SELL)
  {
   profit=NormalizeDouble((OrderOpenPrice()-Ask)*Poin,0);
   if(profit<start)
    continue;
    
   stoptrade=OrderStopLoss();
   stopcal=Ask+(stop*Poin);
   
   if(stoptrade==0||(stoptrade!=0 && stopcal<stoptrade))
       //RefreshRates();
       OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);
  }
   }
    }
     }
//+------------------------------------------------------------------+
 
TraderTect: The EA I created is a beast based on MA cross and never loses if I dont use SL. but it is only opening short trades...
  1. No SL means you have infinite risk.
    In code (MT4): Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.
    1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
                Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
    4. You must normalize lots properly and check against min and max.
    5. You must also check FreeMargin to avoid stop out

    Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

  2.    ; if(OrdersTotal()==0);
        {
        if(iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_MAIN,0) < iStochastic(NULL, PERIOD_CURRENT, 5, 3, 3, MODE_SMA, 0, MODE_SIGNAL,0) && 
        iMA(NULL, PERIOD_CURRENT, 20, 0, MODE_EMA, PRICE_CLOSE, 0) < iMA(NULL, PERIOD_CURRENT, 30, 0, MODE_EMA, PRICE_CLOSE, 0))
          
        OrderSend (_Symbol,OP_SELL,0.01,Bid,0,Stoploss,Bid-500*_Point,NULL,0,0,Red);
        else 
          return;
        }
    
    Why are you ignoring the compiler's warnings? " empty controlled statement found    testscr.mq4    5    26" Your first if does nothing, therefor if the conditions are correct, you will open one order per tick.

  3. Using OrdersTotal (or OrdersHistoryTotal) directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles
  4. Run your code through the styler so you can see the logic flow.
  5. When you find a problem, stop using the tester, Use the debugger or print out your variables, including _LastError and prices and find out why.
Reason: