Code
//+------------------------------------------------------------------+
//| SVE RSI I FISH SYMPHO_v4.mq4 |
//| Copyright 2019, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//#property strict
extern int MagicNumber=20190815;
double lot;
extern double LotFactor =50; //lotsize factor
extern double StopLoss=0;
extern double TakeProfit=10;
extern int TrailingStop=10;
extern int Slippage=3;
extern int RsiPeriod=72;
extern int EmaPeriod=144;
extern int LevelUp=60;
extern int LevelDown=20;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
// expert start function
//+------------------------------------------------------------------+
int start()
{
int MyPoint=1;
if(Digits==3 || Digits==5) MyPoint=10;
double SVE_T1 = iCustom(NULL,0,"SVE_RSI_I-Fish",RsiPeriod,EmaPeriod,0,1);
double SVE_T2 = iCustom(NULL,0,"SVE_RSI_I-Fish",RsiPeriod,EmaPeriod,0,2);
double TheStopLoss=0;
double TheTakeProfit=0;
if( TotalOrdersCount()==0 )
{
int result=0;
if((SVE_T2<LevelDown)&&(SVE_T1>LevelDown)&&(SVE_T1<LevelUp)) // Here is your open buy rule
{
if(CheckMoneyForTrade(NULL,NR(Lot_Volume()),OP_BUY))
result=OrderSend(Symbol(),OP_BUY, NR(Lot_Volume()),Ask,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Blue);
if(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
if(TakeProfit>0) TheTakeProfit=Ask+TakeProfit*MyPoint*_Point;
if(StopLoss>0) TheStopLoss=Ask-StopLoss*MyPoint*_Point;
if(OrderSelect(result, SELECT_BY_TICKET))
bool modif1= OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
}
return(0);
}
if((SVE_T2>LevelUp)&&(SVE_T1<LevelUp)&&(SVE_T1>LevelDown)) // Here is your open Sell rule
{
if(CheckMoneyForTrade(NULL,NR(Lot_Volume()),OP_SELL))
result=OrderSend(Symbol(),OP_SELL, NR(Lot_Volume()),Bid,Slippage,0,0,"EA Generator www.ForexEAdvisor.com",MagicNumber,0,Red);
if(result>0)
{
TheStopLoss=0;
TheTakeProfit=0;
if(TakeProfit>0) TheTakeProfit=Bid-TakeProfit*MyPoint*_Point;
if(StopLoss>0) TheStopLoss=Bid+StopLoss*MyPoint*_Point;
if(OrderSelect(result, SELECT_BY_TICKET))
bool modif2=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(TheStopLoss,Digits),NormalizeDouble(TheTakeProfit,Digits),0,Green);
}
return(0);
}
}
for(int cnt=0;cnt<OrdersTotal();cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
if(OrderType()==OP_BUY)
{
if((SVE_T2<LevelUp)&&(SVE_T1>LevelUp)) //here is your close buy rule
{
bool modif3= OrderClose(OrderTicket(),OrderLots(),MarketInfo(_Symbol,MODE_BID),Slippage,Red);
}
if(TrailingStop>0)
{
TrailingAlls(TrailingStop, MyPoint);
}
}
else if(OrderType()==OP_SELL)
{
if((SVE_T2>LevelDown)&&(SVE_T1<LevelDown)) // here is your close sell rule
{
bool modif5= OrderClose(OrderTicket(),OrderLots(),MarketInfo(_Symbol,MODE_ASK),Slippage,Red);
}
if(TrailingStop>0)
{
TrailingAlls(TrailingStop, MyPoint);
}
}
}
}
return(0);
}
int TotalOrdersCount()
{
int result=0;
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS ,MODE_TRADES) == True)
if(OrderMagicNumber()==MagicNumber)
result++;
}
return (result);
}
void TrailingAlls(double trail, int t)
{
if(trail==0)
return;
//----
double stopcal=0;
int trade=0;
int trades=OrdersTotal();
double profitcalc=0;
bool Order_Modif=False;
for(trade=0;trade<trades;trade++)
{
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==False)
{
Print("Select Failed");
continue;
}
if(OrderSelect(trade,SELECT_BY_POS,MODE_TRADES)==True)
{
if(OrderSymbol()==Symbol())
{
//continue;
//LONG
if(OrderType()==OP_BUY && (OrderMagicNumber()==MagicNumber))// && OrderStopLoss()>=OrderOpenPrice())
{
//Print("Number = " + trade);
if(OrderStopLoss()<OrderOpenPrice() && Bid>OrderOpenPrice()+(trail*t*_Point) && CheckStopLoss_Takeprofit(OP_BUY,OrderOpenPrice(),OrderTakeProfit()))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
if(Order_Modif==True)
Print("BE BUY");
continue;
}
stopcal=NormalizeDouble((OrderStopLoss()+(trail*t*_Point)), _Digits);
profitcalc=OrderTakeProfit();
if(OrderStopLoss()>=OrderOpenPrice() && stopcal<Bid && CheckStopLoss_Takeprofit(OP_BUY,NormalizeDouble(OrderStopLoss()+(trail/2*t*Point), _Digits),profitcalc))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss()+(trail/2*t*Point), _Digits),profitcalc,0,Blue);
if(Order_Modif==False)
Print("Modif failed");
//continue;
}
}//LONG
//Shrt
if(OrderType()==OP_SELL && (OrderMagicNumber()==MagicNumber))// && OrderStopLoss()<=OrderOpenPrice())
{
//Print("Number = " + trade);
if(OrderStopLoss()>OrderOpenPrice() && Ask<OrderOpenPrice()-(trail*t*_Point) && CheckStopLoss_Takeprofit(OP_SELL,OrderOpenPrice(),OrderTakeProfit()))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice(),OrderTakeProfit(),0,Blue);
if(Order_Modif==True)
Print("BE SELL");
continue;
}
stopcal=NormalizeDouble((OrderStopLoss()-(trail*t*_Point)), _Digits);
profitcalc=OrderTakeProfit();
if(OrderStopLoss()<=OrderOpenPrice() && stopcal>Ask && CheckStopLoss_Takeprofit(OP_SELL,NormalizeDouble(OrderStopLoss()-(trail/2*t*Point), _Digits),profitcalc))
{
Order_Modif=OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderStopLoss()-(trail/2*t*Point), _Digits),profitcalc,0,Red);
if(Order_Modif==False)
Print("Modif failed");
//continue;
}
}
}
}
}//Shrt
}
bool CheckStopLoss_Takeprofit(ENUM_ORDER_TYPE type, double SL, double TP)
{
//--- get the SYMBOL_TRADE_STOPS_LEVEL level
int stops_level=(int)SymbolInfoInteger(_Symbol,SYMBOL_TRADE_STOPS_LEVEL);
//---
bool SL_check=false,TP_check=false;
//--- check only two order types
switch(type)
{
//--- Buy operation
case ORDER_TYPE_BUY:
{
//--- check the StopLoss
SL_check=(Bid-SL>stops_level*10*_Point);
if(SL==0)
SL_check=True;
//--- check the TakeProfit
TP_check=(TP-Bid>stops_level*10*_Point);
if(TP==0)
TP_check=True;
//--- return the result of checking
return(SL_check&&TP_check);
}
//--- Sell operation
case ORDER_TYPE_SELL:
{
//--- check the StopLoss
SL_check=(SL-Ask>stops_level*10*_Point);
if(SL==0)
SL_check=True;
//--- check the TakeProfit
TP_check=(Ask-TP>stops_level*10*_Point);
if(TP==0)
TP_check=True;
//--- return the result of checking
return(TP_check&&SL_check);
}
break;
}
//--- a slightly different function is required for pending orders
return false;
}
bool CheckMoneyForTrade(string symb,double lots,int type)
{
double free_margin=AccountFreeMarginCheck(symb,type, lots);
//-- if there is not enough money
if(free_margin<0)
{
string oper=(type==OP_BUY)? "Buy":"Sell";
//fPrint("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
return(false);
}
//--- checking successful
return(true);
}
//
//+------------------------------------------------------------------+
//Calculates Lot Size based on balance and factor
//+------------------------------------------------------------------+
double NR(double thelot)
{
double maxlots=MarketInfo(Symbol(),MODE_MAXLOT),
minilot=MarketInfo(Symbol(),MODE_MINLOT),
lstep=MarketInfo(Symbol(),MODE_LOTSTEP);
double lots=lstep*NormalizeDouble(thelot/lstep,0);
lots=MathMax(MathMin(maxlots,lots),minilot);
return (lots);
}
//+------------------------------------------------------------------+
double Lot_Volume()
{
lot=AccountBalance() * 0.01 /LotFactor ;
return(lot);
}
//+------------------------------------------------------------------+
//|
//+------------------------------------------------------------------+