project on rsi

 
this is how it should work,

there are 2 MA´s, one with Period 5 and the larger one with Period 50

if them are crossing each other i want to open a long or a short Position.



that were my thoughts about the entry

now the close of the positions

i want to use the Rsi for example. If we have a open long position i want it to be closed if the rsi hits the 70 level

if i have a short position, i want it to be closed if the rsi hits the 30 level.
 
#include <Trade\Trade.mqh>
CTrade trade;

int handle_rsi=0;
int handle_ma1=0;
int handle_ma2=0;

input int magic_number=1234;
input int ma_period1=5;
input int ma_period2=50;
input int rsi_period=14;
input double lot=0.01;


int OnInit()
  {
//---
   trade.SetExpertMagicNumber(magic_number);
   handle_ma1=iMA(_Symbol, PERIOD_CURRENT, ma_period1, 0, MODE_EMA, PRICE_CLOSE);
   handle_ma2=iMA(_Symbol, PERIOD_CURRENT, ma_period2, 0, MODE_EMA, PRICE_CLOSE);
   handle_rsi=iRSI(_Symbol, PERIOD_CURRENT, rsi_period, PRICE_CLOSE);
//---
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
//---
   IndicatorRelease(handle_ma1);   
   IndicatorRelease(handle_ma2);
   IndicatorRelease(handle_rsi);
  }

void OnTick()
  {
//---
   if(!isNewBar()) return;

   double Ask=SymbolInfoDouble(_Symbol, SYMBOL_ASK);   
   double Bid=SymbolInfoDouble(_Symbol, SYMBOL_BID);
   
   if(BuyCount()>0 && RSI(1)>70)
      CloseBuy();

   if(SellCount()>0 && RSI(1)<30)
      CloseSell();

   if(BuyCount()==0 && crossover(1) && RSI(1)<70)
      trade.Buy(lot, _Symbol, Ask, 0,0,NULL);   
   if(SellCount()==0 && crossunder(1) && RSI(1)>30)
      trade.Sell(lot, _Symbol, Bid, 0,0,NULL);   
  }
//+------------------------------------------------------------------+

double RSI(int i)
{
   double array[];
   ArraySetAsSeries(array, true);
   CopyBuffer(handle_rsi, 0, i, 1, array);
   return array[0];
}

double MA1(int i)
{
   double array[];
   ArraySetAsSeries(array, true);
   CopyBuffer(handle_ma1, 0, i, 1, array);
   return array[0];
}

double MA2(int i)
{
   double array[];
   ArraySetAsSeries(array, true);
   CopyBuffer(handle_ma2, 0, i, 1, array);
   return array[0];
}

int BuyCount()
{
   int buy=0;
   for(int i=0;i<PositionsTotal();i++)
   {
      ulong ticket=  PositionGetTicket(i);
      if(ticket==0) continue;
      if(PositionGetInteger(POSITION_TYPE)!=POSITION_TYPE_BUY) continue;
      if(PositionGetInteger(POSITION_MAGIC)!=magic_number) continue;
      if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;
      buy++;
   }  
   return buy;
}

int SellCount()
{
   int sell=0;
   for(int i=0;i<PositionsTotal();i++)
   {
      ulong ticket=  PositionGetTicket(i);
      if(ticket==0) continue;
      if(PositionGetInteger(POSITION_TYPE)!=POSITION_TYPE_SELL) continue;
      if(PositionGetInteger(POSITION_MAGIC)!=magic_number) continue;
      if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;
      sell++;
   }  
   return sell;
}

bool crossover(int index)
{
   return MA1(index)>MA2(index) &&
      MA1(index+1)<MA2(index+1);
}

bool crossunder(int index)
{
   return MA1(index)<MA2(index) &&
      MA1(index+1)>MA2(index+1);
}

datetime timer=NULL;
bool isNewBar()
{
   if(timer==NULL) {}
   else if(timer==iTime(_Symbol, PERIOD_CURRENT, 0)) return false;
   timer=iTime(_Symbol, PERIOD_CURRENT, 0);
   return true;
}

void CloseBuy()
{
   for(int i=PositionsTotal()-1;i>=0;i--)
   {
      ulong ticket=  PositionGetTicket(i);
      if(ticket==0) continue;
      if(PositionGetInteger(POSITION_MAGIC)!=magic_number) continue;
      if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;
      if(PositionGetInteger(POSITION_TYPE) != POSITION_TYPE_BUY) continue;
      trade.PositionClose(ticket);
   }  
}

void CloseSell()
{
   for(int i=PositionsTotal()-1;i>=0;i--)
   {
      ulong ticket=  PositionGetTicket(i);
      if(ticket==0) continue;
      if(PositionGetInteger(POSITION_MAGIC)!=magic_number) continue;
      if(PositionGetString(POSITION_SYMBOL)!=_Symbol) continue;
      if(PositionGetInteger(POSITION_TYPE) != POSITION_TYPE_SELL) continue;
      trade.PositionClose(ticket);
   }  
}
 

@Graham Campbell

Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893

  • Usually people who can't code don't receive free help on this forum.
  • If you show your attempts and describe your problem clearly, you will most probably receive an answer from the community. Use the CODE button (Alt-S) when inserting code.
  • To learn MQL programming, you can research the many available Articles on the subject, or examples in the Codebase, as well as reference the online Documentation.
  • If you do not want to learn to code, that is not a problem. You can either look at the Codebase if something free already exists, or in the Market for paid products (also sometimes free). However, recommendations or suggestions for Market products are not allowed on the forum, so you will have to do your own research.
  • Finally, you also have the option to hire a programmer in the Freelance section.
 
Yashar Seyyedin #:
thanks buddy
Reason: