yy_rsi_ima_v2

Author: it-yy.site eurweb@gmail.com copyright 2024
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open orders
Indicators Used
Relative strength indexMoving average indicator
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
yy_rsi_ima_v2
ÿþ//+------------------------------------------------------------------+

//|                                          Copyright © 2024 Yuriy  |

//|                                                      it-yy.site  |

//|                                                eurweb@gmail.com  |

//+------------------------------------------------------------------+



#property copyright "it-yy.site eurweb@gmail.com copyright 2024"

#property link      "it-yy.site"

#property version   "2.00"

#property description   "EA based on RSI and MA with trading time control"

#property description   "WARNING: Use this software at your own risk."

#property description    " "

#property strict





input group  " Settings"

input double         lot = 0.01;

input int            ext_profit_ptk = 300; //profit 

input int            ext_loss_ptk   = 150; //stop loss

input int            ext_slip       = 10;  //slip page

input int            ext_maxspred   = 30; //max spred

input int            Magic = 22;

input string         orderComment = "it-yy.site";



input group  "MA Settings"

input int ShortMA = 20;

input int LongMA = 50;





input group         "RSI Settings"

input int RSIPeriod = 14;

input int RSIOverboughtLevel = 70;

input int RSIOversoldLevel = 30; 









input group         "Time control"

input bool           useTimeControl = true;//use time control

input string         timeStart  = "15:00:00";//start trade

input string         timeEnd    = "18:00:00";//end trade







double RSI;

// system vars

double STOPLEVEL;



// run time vars

int countorders = 0;

int countorderssell = 0;

int countordersbuy = 0;



string error_msg = "";

 

//+------------------------------------------------------------------+

//| Expert initialization function                                   |

//+------------------------------------------------------------------+

int OnInit()

{



   // Initializing the relative strength indicator

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

   

   /// get STOPLEVEL

   STOPLEVEL = MarketInfo(Symbol(),MODE_STOPLEVEL);

   

   return(INIT_SUCCEEDED);

}







//+------------------------------------------------------------------+

//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

  {



   if (!IsTradeAllowed()) 

   {

      Alert("Trade not Allowed");

      return;

   }

   

   if (IsNewBar() == false)

     return;

   

   if (useTimeControl && tradingTime() == false)

   {

      return;

   }



   if (MarketInfo(Symbol(),MODE_SPREAD) >=  ext_maxspred)

      return;

      

   //Getting the current RSI value

   double rsi = iRSI(Symbol(),0,RSIPeriod,PRICE_CLOSE,0);

   

   // Getting current moving average values

   double shortMA = iMA(Symbol(),0,ShortMA,0,MODE_SMA,PRICE_CLOSE,0);

   double longMA = iMA(Symbol(),0,LongMA,0,MODE_SMA,PRICE_CLOSE,0);

   

   getCountOpenOrders();

   

   // Checking the conditions for entering a position

   if(countordersbuy == 0 && shortMA > longMA && rsi > RSIOversoldLevel) // 30

     {

      // Entering a long position

      myOrderSend(OP_BUY);

     

     }

   

   // Checking the conditions for entering a position

   if(countorderssell == 0 && shortMA < longMA && rsi < RSIOverboughtLevel)

     {

      // Entering a short position

      myOrderSend(OP_SELL);

     }

  }

 



int myOrderSend(int ordertype)

{

   double  sl = 0;

   double  tp = 0;

   double  price = 0;



   if (ext_loss_ptk < STOPLEVEL)

   {

      Print("Error OrderSend wrong StopLoss: ", ext_loss_ptk, " STOPLEVEL ", STOPLEVEL);

   }



   if (ext_profit_ptk < STOPLEVEL)

   {

      Print("Error OrderSend wrong TakeProfit: ", ext_profit_ptk, " STOPLEVEL ", STOPLEVEL);

   }



   if (!checkLotVolume(lot, error_msg))

   {

      Print("Error ",error_msg);

      return -2;

   }



   if(!isNewOrderAllowed()) 

   {

      Print("Error OrderSend: New Order not allowed");

      return -3;

   }





   if (AccountFreeMarginCheck(Symbol(),ordertype,lot)<0)

   {

      Print("Error OrderSend: AccountFreeMarginCheck");

      return -4;

   }

   

  if (ordertype == OP_SELL)

  {

    price = Bid;

    tp = Bid - ext_profit_ptk * _Point;

    sl = Ask + ext_loss_ptk * _Point;

  }

  if (ordertype == OP_BUY)

  {

    price = Ask;

    tp = Ask + ext_profit_ptk * _Point;

    sl = Bid - ext_loss_ptk * _Point;

  }



   sl = NormalizeDouble(sl,_Digits);

   tp = NormalizeDouble(tp,_Digits);

   int ticket = OrderSend(_Symbol, ordertype, lot, price, ext_slip, sl, tp, orderComment, Magic);

 

   if(ticket<0)

   {

      Print("Error OrderSend: ",GetLastError());

   }

   else

   {

      Print("Order price ", price, " tp ",  tp, " sl ", sl , " lot ", lot );

   }

      

   return ticket;

}

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+

//|  is New Order Allowed                                            |

//+------------------------------------------------------------------+

bool isNewOrderAllowed()

{

   int max_allowed_orders=(int)AccountInfoInteger(ACCOUNT_LIMIT_ORDERS);

   if(max_allowed_orders==0) return(true);

   if(OrdersTotal()<max_allowed_orders) return(true);

   return(false);

}





//+------------------------------------------------------------------+

//|  Checks the order volume                                         |

//+------------------------------------------------------------------+

bool checkLotVolume(double volume,string &description)

{

//--- minimum acceptable volume for trading operations

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume)

     {

      description=StringFormat("Lot volume is less than the minimum allowable SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }



//--- maximum allowed volume for trading operations

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume)

     {

      description=StringFormat("Lot volume exceeds maximum permissibleSYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }



//--- we get the minimum volume gradation

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);



   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

   {

      description=StringFormat("The volume is not a multiple of the minimum gradation SYMBOL_VOLUME_STEP=%.2f, nearest correct volume %.2f",

                               volume_step,ratio*volume_step);

      return(false);

   }



   return(true);

}

 

 

//+------------------------------------------------------------------+

//|  new bar opened ?                                                |

//+------------------------------------------------------------------+

bool IsNewBar()   

{        

   static datetime RegBarTime=0;

   datetime ThisBarTime = Time[0];

 

   if (ThisBarTime == RegBarTime)

   {

      return(false);

   }

   else

   {

      RegBarTime = ThisBarTime;

      return(true);

   }

}

//+------------------------------------------------------------------+







//+------------------------------------------------------------------+

//|  Trading  Time                                                   |

//+------------------------------------------------------------------+

bool tradingTime()

{



   bool result = false;

   datetime timeCurrent = TimeCurrent();

   datetime starttime = StringToTime( TimeToString( TimeCurrent(),TIME_DATE)+" "+timeStart);

   datetime endtime =   StringToTime( TimeToString( TimeCurrent(),TIME_DATE)+" "+timeEnd);

   if (starttime <= endtime) {

      if (timeCurrent >= starttime && timeCurrent <= endtime)

         result = true;

   }

   else {

      if (timeCurrent >= starttime || timeCurrent <= endtime)

         result = true;

   }

   return(result);

}  

//+------------------------------------------------------------------+





 

//+------------------------------------------------------------------+

//|  Returning the number of open orders                             |

//+------------------------------------------------------------------+

bool getCountOpenOrders()

{



   countorders = 0;

   countorderssell = 0;

   countordersbuy = 0;

 

   for(int order = 0; order < OrdersTotal(); order++) 

   {

      if(OrderSelect(order,SELECT_BY_POS,MODE_TRADES)==false) continue;

      

      if(OrderSymbol() != _Symbol) continue;

 

      if(OrderMagicNumber() == Magic)

      {

          if (OrderType() == OP_BUY)

          {

             countordersbuy++;

          }

          

          if (OrderType() == OP_SELL)

          {

             countorderssell++;

          }

      }

   }

   

   

   countorders = countorderssell + countordersbuy;

 

   return true;

}

//+------------------------------------------------------------------+







//+------------------------------------------------------------------+

//| Expert deinitialization function                                 |

//+------------------------------------------------------------------+

void OnDeinit(const int reason)

{

}

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---