72 lines
2.1 KiB
MQL5
72 lines
2.1 KiB
MQL5
//+------------------------------------------------------------------+
| |||
//| myfirstrobot.mq5 |
| |||
//| Strategy: RSI 30/70 EURUSD M15 - Fixed Lot 0.01 |
| |||
//+------------------------------------------------------------------+
| |||
#property strict
| |||
| |||
#include <Trade/Trade.mqh>
| |||
CTrade trade;
| |||
| |||
input int RSI_Period = 14;
| |||
input int RSI_Oversold = 30;
| |||
input int RSI_Overbought = 70;
| |||
input double LotSize = 0.01;
| |||
input int StopLossPoints = 100;
| |||
input int TakeProfitPoints = 200;
| |||
input long MagicNumber = 123456;
| |||
| |||
int rsi_handle;
| |||
| |||
//+------------------------------------------------------------------+
| |||
int OnInit()
| |||
{
| |||
rsi_handle = iRSI(_Symbol, PERIOD_M15, RSI_Period, PRICE_CLOSE);
| |||
if(rsi_handle==INVALID_HANDLE) return(INIT_FAILED);
| |||
return(INIT_SUCCEEDED);
| |||
}
| |||
//+------------------------------------------------------------------+
| |||
void OnDeinit(const int reason)
| |||
{
| |||
if(rsi_handle!=INVALID_HANDLE) IndicatorRelease(rsi_handle);
| |||
}
| |||
//+------------------------------------------------------------------+
| |||
bool HasOpenPosition()
| |||
{
| |||
for(int i=0; i<PositionsTotal(); i++)
| |||
{
| |||
if(PositionGetSymbol(i)!="")
| |||
if(PositionGetInteger(POSITION_MAGIC)==MagicNumber)
| |||
return true;
| |||
}
| |||
return false;
| |||
}
| |||
//+------------------------------------------------------------------+
| |||
void OnTick()
| |||
{
| |||
if(HasOpenPosition()) return;
| |||
| |||
double rsi_buffer[];
| |||
if(CopyBuffer(rsi_handle,0,0,2,rsi_buffer)<=0) return;
| |||
double rsi_now = rsi_buffer[0];
| |||
| |||
double price = SymbolInfoDouble(_Symbol, SYMBOL_ASK);
| |||
double sl=0, tp=0;
| |||
| |||
if(rsi_now < RSI_Oversold)
| |||
{
| |||
// BUY
| |||
if(StopLossPoints>0) sl = price - StopLossPoints*_Point;
| |||
if(TakeProfitPoints>0) tp = price + TakeProfitPoints*_Point;
| |||
trade.SetExpertMagicNumber(MagicNumber);
| |||
trade.Buy(LotSize, _Symbol, 0, sl, tp);
| |||
}
| |||
else if(rsi_now > RSI_Overbought)
| |||
{
| |||
// SELL
| |||
price = SymbolInfoDouble(_Symbol, SYMBOL_BID);
| |||
if(StopLossPoints>0) sl = price + StopLossPoints*_Point;
| |||
if(TakeProfitPoints>0) tp = price - TakeProfitPoints*_Point;
| |||
trade.SetExpertMagicNumber(MagicNumber);
| |||
trade.Sell(LotSize, _Symbol, 0, sl, tp);
| |||
}
| |||
}
|