this mt4 ea open trade in 14 candles low and high range.
this ea good for 1 hour and biggest chart.
//+------------------------------------------------------------------+
//| range_trading.mq4 |
//| Copyright 2022, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
extern int MagicNumber=10002;
extern double Lots =0.01;
extern int TrailingStop=100;
extern int Slippage=3;
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
int start()
{
string signal="";
double MyPoint=Point;
if(Digits==3 || Digits==5) MyPoint=Point*10;
double speared = DoubleToStr(Ask-Bid,Digits);
datetime ctime = Time[0];
datetime lastclose = 0;
double sellstoploss = DoubleToStr(High[1]+(High[1]-Low[1]),Digits);
double selltp = DoubleToStr(Low[1]-2*(High[1]-Low[1]),Digits);
double buystoploss = DoubleToStr(Low[1]-(High[1]-Low[1]),Digits);
double buytp = DoubleToStr(High[1]+2*(High[1]-Low[1]),Digits);
//double rsi = iRSI(NULL,0,14,PRICE_CLOSE,0);
int c = OrdersHistoryTotal();
int a = OrdersHistoryTotal() - 10;
int b = OrdersHistoryTotal();
for (c = a;c < b ;c++)
{
if (OrderSelect(c,SELECT_BY_POS, MODE_HISTORY) == true && OrderMagicNumber()== MagicNumber)
{
// Alert("orders found"+ OrderTicket() + " " + OrderCloseTime());
lastclose = OrderCloseTime();
}
}
int tllow = iLowest(_Symbol,_Period,MODE_LOW,14,0);
int tlhigh = iHighest(_Symbol,_Period,MODE_HIGH,14,0);
double lowa = Low[tllow];
double higha = High[tlhigh];
if ((Low[2] > lowa) && (Low[1] > lowa) && (Open[2] < Close[2]) && (Open[1] < Close[1]) && (Close[1] > Close[2]) && (Close[0] > High[1]))
{
//set the signal variable to buy
signal="buy";
}
//if the moving average above the current price
if ((High[2] < higha) && (High[1] < higha) && (Open[2] > Close[2]) && (Open[1] > Close[1]) && (Close[1] < Close[2]) && (Close[0] < Low[1]))
{
//set the signal variable to sell
signal="sell";
}
Comment("ASK ",Ask, " BID ",Bid, " SPEARD ", speared , "\n CURRENT TIME ",ctime, " LAST ORDER CLOSED TIME ",lastclose,
"\n The current signal is : ",signal);
if( TotalOrdersCount()==0)
{
int result=0;
if ((signal == "buy" && TotalOrdersCount() ==0))//Here is your open Buy rule
{
result=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,buystoploss,0,"range_trading EA https://forexwithcandlestick.blogspot.com",MagicNumber,0,Green);
if(result>0)
{
OrderSelect(result,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(buystoploss,Digits),0,0,Green);
}
return(0);
}
//if the fast SMA is now above
if ((signal == "sell" && TotalOrdersCount() ==0)) //Here is your open Sell rule
{
result=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sellstoploss,0,"range_trading EA https://forexwithcandlestick.blogspot.com ",MagicNumber,0,Red);
if(result>0)
{
OrderSelect(result,SELECT_BY_TICKET);
OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(sellstoploss,Digits),0,0,Red);
}
return(0);
}
}
ObjectDelete("tpl");
ObjectCreate(0,"tpl",OBJ_HLINE,0,Time[20],
OrderTakeProfit(),Time[1],OrderTakeProfit());
ObjectSetInteger(0,"tpl",OBJPROP_COLOR,Blue);
ObjectSetInteger(0,"tpl",OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,"tpl",OBJPROP_WIDTH,1);
//object pivot
ObjectDelete("SL");
ObjectCreate(0,"SL",OBJ_HLINE ,0,Time[20],
OrderStopLoss(),Time[1],OrderStopLoss());
ObjectSetInteger(0,"SL",OBJPROP_COLOR,Yellow);
ObjectSetInteger(0,"SL",OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,"SL",OBJPROP_WIDTH,3);
ObjectDelete("trendlow");
ObjectCreate(0,"trendlow",OBJ_TREND,0,Time[14],
lowa,Time[0],lowa);
ObjectSetInteger(0,"trendlow",OBJPROP_COLOR,White);
ObjectSetInteger(0,"trendlow",OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,"trendlow",OBJPROP_WIDTH,3);
ObjectDelete("trendhigh");
ObjectCreate(0,"trendhigh",OBJ_TREND,0,Time[14],
higha,Time[0],higha);
ObjectSetInteger(0,"trendhigh",OBJPROP_COLOR,Green);
ObjectSetInteger(0,"trendhigh",OBJPROP_STYLE,STYLE_SOLID);
ObjectSetInteger(0,"trendhigh",OBJPROP_WIDTH,3);
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL &&
OrderSymbol()==Symbol() &&
OrderMagicNumber()==MagicNumber
)
{
if(OrderType()==OP_BUY)
{
if((OrderProfit() > 0 && Low[1]>Bid)) //here is your close buy rule
{ Alert("close buy",Bid);
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Blue);
}
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>MyPoint*TrailingStop)
{
if(OrderStopLoss()<Bid-MyPoint*TrailingStop && OrderStopLoss()<Bid-buystoploss)
{
OrderModify(OrderTicket(),OrderOpenPrice(),buystoploss,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else
{
if((OrderProfit() > 0 && High[1]<Ask)) // here is your close sell rule
{
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Yellow);
}
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(MyPoint*TrailingStop))
{
if((OrderStopLoss()>(Ask+MyPoint*TrailingStop && OrderStopLoss()> sellstoploss)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),sellstoploss,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
int TotalOrdersCount()
{
int result=0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i,SELECT_BY_POS ,MODE_TRADES);
if (OrderMagicNumber()==MagicNumber) result++;
}
return (result);
}