EA Minion1(Trades based on RSI)

 

Hey there Folks,

just posting a simple EA in Hope for some Code Review, Tips and Advices  ;-)

Feel free to test or use parts of the Code, just some playing around for learning purposes anyways


//+------------------------------------------------------------------+
//|                                                      minion1.mq4 |
//|           This Minion trades Sell and Buy Positions based on RSI |
//|                               Feel Free to test, use and develop |
//+------------------------------------------------------------------+
#property copyright "GNU/GPL"
#property link      "https://www.mql5.com/en/users/digitalalchemist"
#property version   "1.00"
#property strict

input int magicno=331177; //no explanation 
input int RSIPeriod=14;  //Period of RSI indicator
input int RSILow=30;    //trigger for opening sell Positions
input int RSIHigh=70;   // trigger for opening buy Positions
input int maxOpenBuyOrders=1; // specifies open buy orders at the same time
input int maxOpenSellOrders=1; //specifies open sell orders at the same time
input double lotsize=0.1;  // size of opened Positions

//all following values are factors of ASK/BID or  OrderStopLos(Adjust values)

input double sellSL=1.005; //initial SL when opening a sell Position
input double sellTP=0.997; //TP for a sell Position
input double buySL=0.9975; //initial SL when opening a buy Position
input double buyTP=1.01;   //TP for a buy Position
input double adjustSellSL=0.9995; //adjusts sl when the OrderProfit turned positive
input double adjustBuySL=1.0005; //adjusts sl when the OrderProfit turned positive

int OnInit(){
   Print("Minion 1 has joined the battlefield on "+Symbol());
   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){
   Print("Minion 1 has left the battlefield on "+Symbol());   
}

void OnTick(){
   adjustSL();
   
   int signal=calculateRSI();
   
   if(signal==0  && countOpenSellOrders()<maxOpenSellOrders){
      executeShort();
   }
   else if(signal==2 && countOpenBuyOrders()<maxOpenBuyOrders){
      executeLong();
   }

   
   
   
   

}
//+------------------------------------------------------------------+

//returns an int 0 means sell 1 means do nothing 2 means buy
int calculateRSI(){
   int signal=1;
   double rsi0=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,0);
   double rsi3=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,3);
   double rsi6=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,6);
   double rsi9=iRSI(NULL,0,RSIPeriod,PRICE_TYPICAL,9);
   
   if(rsi0>rsi9+7 && rsi0>rsi6+5 && rsi0>rsi3+3 && rsi0>RSIHigh){
      signal=2;
   }
   else if(rsi0<rsi9-7 && rsi0<rsi6-5 && rsi0<rsi3-3 && rsi0<RSILow){
      signal=0;
   }   
   
   return signal;
}


//opens a short position
int executeShort(){
   int ticket=OrderSend(Symbol(),OP_SELL,lotsize,Bid,9999,Bid*sellSL,Bid*sellTP,"execute sell order",magicno,0,clrRed);
   if(ticket<0){
      Print("Sell Order Exception: "+GetLastError());
   }
   else{
      Print("Sell Order opened at :"+Bid);
   }
   
   return ticket;
}

//opens a long position
int executeLong(){
   int ticket=OrderSend(Symbol(),OP_BUY,lotsize,Ask,9999,Ask*buySL,Ask*buyTP,"execute buy order",magicno,0,clrGreen);
   if(ticket<0){
      Print("Sell Order Exception: "+GetLastError());
   }
   else{
      Print("Sell Order opened at :"+Bid);
   }

   return ticket;
}

//counts the actual opened buy positions
int countOpenBuyOrders(){
   int counter=0;
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()==OP_BUY && OrderMagicNumber()==magicno){
            counter++;
         }
      }   
   }
   return counter;
}

//counts the actual opened sell positions
int countOpenSellOrders(){
   int counter=0;
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()==OP_SELL && OrderMagicNumber()==magicno){
            counter++;
         }
      }   
   }
   return counter;
}

//iterates through open Position and adjust the sl every tick
void adjustSL(){
   for(int i=0;i<OrdersTotal();i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){
         if(OrderType()==OP_SELL && OrderMagicNumber()==magicno){
            if(OrderProfit()>15 && OrderStopLoss()>OrderOpenPrice()){
               bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrAqua);
               if(!modorder){
                  Print("OrderModify Exception: "+GetLastError());
               }
               else{
                  Print("Order Modified successfully");
               }
            }
            else if(OrderStopLoss()<=OrderOpenPrice()){
               bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()*adjustSellSL,OrderTakeProfit(),0,clrAqua);
               if(!modorder){
                  Print("OrderModify Exception: "+GetLastError());
               }
               else{
                  Print("Order Modified successfully");
               }   
            }
         }
         else if(OrderType()==OP_BUY && OrderMagicNumber()==magicno){
            if(OrderProfit()>15 && OrderStopLoss()<OrderOpenPrice()){
               bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,clrAqua);
               if(!modorder){
                  Print("OrderModify Exception: "+GetLastError());
               }
               else{
                  Print("Order Modified successfully");
               }
            }
            else if(OrderStopLoss()>=OrderOpenPrice()){
               bool modorder=OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss()*adjustBuySL,OrderTakeProfit(),0,clrAqua);
               if(!modorder){
                  Print("OrderModify Exception: "+GetLastError());
               }
               else{
                  Print("Order Modified successfully");
               } 
            }   
         }
      }
   }
}