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
ÿþ//+------------------------------------------------------------------+

//|                                          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   "1.00"

#property description   "EA based on RSI and MA"

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

#property description    " "

#property strict





// 0@0<5B@K A:>;L7OI8E A@54=8E

input int ShortMA = 20;

input int LongMA = 50;



// 0@0<5B@K RSI

input int RSIPeriod = 14;

input int RSIOverboughtLevel = 70;

input int RSIOversoldLevel = 30; 



input double         lot = 0.01;

input int            ext_profit_ptk = 100; // profit 

input int            ext_loss_ptk   = 100; // stop los

input int            ext_slip       = 10;    // slip

input int            ext_maxspred   = 30; // max spred

input int            Magic = 243545;





//--- 8=48:0B>@K

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()

{



   // =8F80;870F8O 8=48:0B>@0 >B=>A8B5;L=>9 A8;K

   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 (MarketInfo(Symbol(),MODE_SPREAD) >=  ext_maxspred)

      return;

      

   // >;CG5=85 B5:CI53> 7=0G5=8O RSI

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

   

   // >;CG5=85 B5:CI8E 7=0G5=89 A:>;L7OI8E A@54=8E

   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();

   

   // @>25@:0 CA;>289 2E>40 2 ?>78F8N

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

     {

      // E>4 2 4;8==CN ?>78F8N

      myOrderSend(OP_BUY);

     

     }

   

   // @>25@:0 CA;>289 2E>40 2 ?>78F8N

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

     {

      // E>4 2 :>@>B:CN ?>78F8N

      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, "open Order", Magic);

 

   if(ticket<0)

   {

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

   }

   else

   {

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

   }

      

   return ticket;

}









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

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);

   }

}   

 

 

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

//|  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 ---