MTF rsi_sar.mq4 INVERSE fonctionnant sur MT4 ou MQL 4
Code
//+------------------------------------------------------------------+
//| MTF rsi_sar.mq4 INVERSE |
//| Copyright © 2011, StarLimit Software Corp. |
//| EURUSD 1 MN [email protected] |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2011, StarLimit Software Corp."
#property link "[email protected]"
//+-------------------------------------------------
extern double LotFactor=0.4; //lotsize factor
extern bool Long_Short= true;
extern bool Long_Only=false;
extern bool Short_Only=false;
extern bool Auto_Disable_EA=true;
extern double TakeProfit = 120;
extern bool use_bb=true,use_rsi = false,use_sar=false;
extern double step=0.44,maximum=0.44;
extern double InitialStop = 0;
extern bool trail=true;
extern double TrailingStop=22;
extern int MaxTrades = 5;
extern int Pips = 130;
extern int SecureProfit = 38;
extern double EURUSDPipValue = 10;
extern double GBPUSDPipValue = 10;
extern double USDCHFPipValue = 10;
extern double USDJPYPipValue = 9.715;
extern int magic = 1955;
extern int bb_period=22;
extern int bb_deviation=32;
extern int bb_shift=35;
extern int StartHour = 0;
extern int EndHour = 24;
//+------------------------------------------------------------------+
int PRICE_TYPE=5;
int OpenOrders = 0, cnt = 0;
int slippage = 3;
double sl = 0, tp = 0;
double BuyPrice = 0, SellPrice = 0;
double lotsi = 0, mylotsi = 0;
int mode = 0, myOrderType = 0;
bool ContinueOpening = True;
double LastPrice = 0;
int PreviousOpenOrders = 0;
double Profit = 0;
int LastTicket = 0, LastType = 0,res;
double LastClosePrice = 0, LastLots = 0;
double PipValue = 0;
string text = "", text2 = "";
int ActiveOrders=0;
double AllTP=0;
int rsi_per5=0;
int rsi_per15=0;
int rsi_per30=0;
int rsi_per60=0;
//+------------------------------------------------------------------+
//| Trailing Stop |
//+------------------------------------------------------------------+
void trail()
{
for(int i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(),MODE_POINT))
{
if(OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT))
{
bool modify1=OrderModify(OrderTicket(),OrderOpenPrice(),Bid-TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),SpringGreen);
}
}
} else if(OrderType()==OP_SELL) {
if(OrderOpenPrice()-Ask>TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT))
{
if((OrderStopLoss()>Ask+TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT)) ||
(OrderStopLoss()==0))
{
bool modify2=OrderModify(OrderTicket(),OrderOpenPrice(),
Ask+TrailingStop*MarketInfo(OrderSymbol(),MODE_POINT),OrderTakeProfit(),Magenta);
}
}
}
}
}
}
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- To assign a unique Magic number for each Pair.
MathSrand(MathCeil(Ask*100));
magic= MathRand();
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(trail) trail();
//----
// lotsi = Lots;
lotsi = AccountBalance() * 0.0001 /LotFactor;
//----
if(lotsi > 100)
lotsi = 100;
OpenOrders = 0;
//----
for(cnt = 0; cnt < OrdersTotal(); cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
//----
if(OrderSymbol() == Symbol())
OpenOrders++;
}
//----
if(OpenOrders < 1)
{
ActiveOrders=0;
if(TimeHour(TimeCurrent()) < StartHour)
return(0);
//----
if(TimeHour(TimeCurrent()) > EndHour)
return(0);
}
//----
if(Symbol() == "EURUSD")
PipValue = EURUSDPipValue;
//----
if(Symbol() == "GBPUSD")
PipValue = GBPUSDPipValue;
//----
if(Symbol() == "USDJPY")
PipValue = USDJPYPipValue;
//----
if(Symbol() == "USDCHF")
PipValue = USDCHFPipValue;
//----
if(PipValue == 0)
{
PipValue = 5;
}
//----
if(PreviousOpenOrders > OpenOrders)
for(cnt = OrdersTotal(); cnt >= 0; cnt--)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
mode = OrderType();
//----
if(OrderSymbol() == Symbol())
{
if(mode == OP_BUY)
if(OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slippage, Green))
//----
if(mode == OP_SELL)
if(OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), slippage, Red))
return(0);
}
}
PreviousOpenOrders = OpenOrders;
//----
if(OpenOrders >= MaxTrades)
ContinueOpening = False;
else
ContinueOpening = True;
//----
if(LastPrice == 0)
for(cnt = 0; cnt < OrdersTotal(); cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
mode = OrderType();
//----
if(OrderSymbol() == Symbol())
{
LastPrice = OrderOpenPrice();
//----
if(mode == OP_BUY)
myOrderType = 1;
//----
if(mode == OP_SELL)
myOrderType = 2;
}
}
//----
if(OpenOrders < 1)
{
myOrderType = 3;
//----
//BOLLINGER ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double upBB=iBands(Symbol(),5,bb_period,bb_deviation,0,PRICE_OPEN,MODE_UPPER,bb_shift);
double loBB=iBands(Symbol(),5,bb_period,bb_deviation,0,PRICE_OPEN,MODE_LOWER,bb_shift);
int bar1 =iBars(Symbol(),5);
int bar2 =iBars(Symbol(),15);
int bar3 =iBars(Symbol(),30);
int bar4 =iBars(Symbol(),60);
datetime time=iTime(Symbol(),5,0);
int shift1=iBarShift(Symbol(),5,time,true);
int shift2=iBarShift(Symbol(),15,time,true);
int shift3=iBarShift(Symbol(),30,time,true);
int shift4=iBarShift(Symbol(),60,time,true);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
double RSI1 =iRSI(Symbol(),5,rsi_per5,PRICE_TYPE,shift1);
double RSI2 =iRSI(Symbol(),15,rsi_per15,PRICE_TYPE,shift2);
double RSI3 =iRSI(Symbol(),30,rsi_per30,PRICE_TYPE,shift3);
double RSI4 =iRSI(Symbol(),60,rsi_per60,PRICE_TYPE,shift4);
double mabela = iSAR(Symbol(), 5,step,maximum,shift1);
double mabelb = iSAR(Symbol(), 15,step,maximum,shift2);
double mabelc = iSAR(Symbol(), 30,step,maximum,shift3);
//use all three signals
if(use_rsi && use_bb && use_sar) // use all three signals
{
if(RSI1>50 && RSI2>50 && RSI3>50 && RSI4>50 && High[bb_shift]>=upBB && mabela< Low[0] && mabelb < Low[0] && mabelc < Low[0] ) myOrderType = 2;
if(RSI1<50 && RSI2<50 && RSI3<50 && RSI4<50 && Low[bb_shift]<=loBB && mabela> High[0]&& mabelb > High[0]&& mabelc > High[0]) myOrderType = 1;
}
if(!use_rsi && use_bb && use_sar) // use both bba and sar
{
if( High[bb_shift]>=upBB && mabela< Low[0] && mabelb < Low[0] && mabelc < Low[0] ) myOrderType = 2;
if( Low[bb_shift]<=loBB && mabela> High[0]&& mabelb > High[0]&& mabelc > High[0]) myOrderType = 1;
}
if(use_rsi && !use_bb && use_sar) // use both rsi and sar
{
if(RSI1>50 && RSI2>50 && RSI3>50 && RSI4>50 && mabela< Low[0] && mabelb < Low[0] && mabelc < Low[0] ) myOrderType = 2;
if(RSI1<50 && RSI2<50 && RSI3<50 && RSI4<50 && mabela> High[0]&& mabelb > High[0]&& mabelc > High[0]) myOrderType = 1;
}
if(use_rsi && use_bb && !use_sar) // use both rsi and bb
{
if(RSI1>50 && RSI2>50 && RSI3>50 && RSI4>50 && High[bb_shift]>=upBB ) myOrderType = 2;
if(RSI1<50 && RSI2<50 && RSI3<50 && RSI4<50 && Low[bb_shift]<=loBB ) myOrderType = 1;
}
else if(use_rsi && !use_bb && !use_sar) // use only rsi
{
if(RSI1>50 && RSI2>50 && RSI3>50 && RSI4>50) myOrderType = 2;
if(RSI1<50 && RSI2<50 && RSI3<50 && RSI4<50) myOrderType = 1;
}
else if(!use_rsi && use_bb && !use_sar) // use only bb
{
if(High[bb_shift]>=upBB )myOrderType = 2;
if(Low[bb_shift]<=loBB ) myOrderType = 1;
}
else if(!use_rsi && !use_bb && use_sar) // use only sar
{
if( mabela< Low[0] && mabelb < Low[0] && mabelc < Low[0] ) myOrderType = 2;
if( mabela> High[0]&& mabelb > High[0]&& mabelc > High[0]) myOrderType = 1;
}
}
Profit = 0;
LastTicket = 0;
LastType = 0;
LastClosePrice = 0;
LastLots = 0;
for(cnt = 0; cnt < OrdersTotal(); cnt++)
{
if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
//----
if(OrderSymbol() == Symbol())
{
LastTicket = OrderTicket();
//----
if(OrderType() == OP_BUY)
LastType = OP_BUY;
//----
if(OrderType() == OP_SELL)
LastType = OP_SELL;
LastClosePrice = OrderClosePrice();
LastLots = OrderLots();
//----
if(LastType == OP_BUY)
{
if(OrderClosePrice() < OrderOpenPrice())
Profit = Profit - (OrderOpenPrice() - OrderClosePrice())*OrderLots() /
Point;
//----
if(OrderClosePrice() > OrderOpenPrice())
Profit = Profit + (OrderClosePrice() - OrderOpenPrice())*OrderLots() /
Point;
}
//----
if(LastType==OP_SELL)
{
if(OrderClosePrice() > OrderOpenPrice())
Profit = Profit - (OrderClosePrice() - OrderOpenPrice())*OrderLots() /
Point;
//----
if(OrderClosePrice() < OrderOpenPrice())
Profit = Profit + (OrderOpenPrice() - OrderClosePrice())*OrderLots() /
Point;
}
}
}
Profit = Profit*PipValue;
text2 = "Profit: $" + DoubleToStr(Profit,2) + " +/-";
//----
if(Profit >= SecureProfit)
{
if(OrderClose(LastTicket, LastLots, LastClosePrice, slippage, Yellow))
ContinueOpening = False;
return(0);
}
//----
if(myOrderType == 1 && ContinueOpening && (Long_Short|| Short_Only))
{
if((Bid - LastPrice) >= Pips*Point || OpenOrders < 1)
{
SellPrice = Bid;
LastPrice = 0;
//----
if(TakeProfit == 0)
tp = 0;
else
tp = SellPrice - TakeProfit*Point;
//----
if(InitialStop == 0)
sl = 0;
else
sl = SellPrice + InitialStop*Point;
//----
if(OpenOrders >=2)
{
mylotsi = 2*mylotsi;
//----
}
else
mylotsi=lotsi;
//----
if (!IsTradeContextBusy() && IsTradeAllowed())
res=OrderSend(Symbol(), OP_SELL, mylotsi, SellPrice, slippage, sl, tp,"MTF EA Sell",magic, 0, OrangeRed);
if (res < 0)
mylotsi=mylotsi/2; // i.e if opening of order failed.
else
{
ActiveOrders++; // increament open orders.
if(ActiveOrders==1) AllTP=tp;
else
ModifyTP();
}
return(0);
}
}
if(myOrderType == 2 && ContinueOpening && (Long_Short|| Long_Only))
{
if((LastPrice-Ask) >= Pips*Point || OpenOrders < 1)
{
BuyPrice = Ask;
LastPrice = 0;
//----
if(TakeProfit == 0)
tp = 0;
else
tp = BuyPrice + TakeProfit*Point;
//----
if(InitialStop==0)
sl = 0;
else
sl = BuyPrice - InitialStop*Point;
//----
if(OpenOrders >=2)
{
mylotsi = 2*mylotsi;
}
else
mylotsi = lotsi;
if (!IsTradeContextBusy() && IsTradeAllowed())
res=OrderSend(Symbol(), OP_BUY, mylotsi, BuyPrice, slippage, sl, tp, "MTF EA Buy", magic, 0,GreenYellow);
if (res < 0)
mylotsi=mylotsi/2;
else
{
ActiveOrders++;
if(ActiveOrders==1) AllTP=tp;
else ModifyTP();
}
return(0);
}
}
//----
return(0);
}
void ModifyTP()
{
for(cnt =0;cnt <=OrdersTotal() ; cnt++)
{
if( OrderSelect(cnt, SELECT_BY_POS,MODE_TRADES))
//----
if(OrderSymbol() == Symbol() && OrderMagicNumber()==magic)
{
if(OrderType() == OP_BUY)
if( OrderModify(OrderTicket(),OrderOpenPrice(), OrderStopLoss(),tp, 1000,PaleGreen)) Comment("\n\n\n\n MODIFYING ORDER........");
}
if(OrderType() == OP_SELL)
{
if( OrderModify(OrderTicket(), OrderOpenPrice(),OrderStopLoss(), tp, 1000,HotPink))Comment("\n\n\n\n MODIFYING ORDER........");
}
}
}
double dailyprofit()
{
int day=Day(); double res1=0,res2=0;
for(int i=0; i<OrdersHistoryTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
if(OrderMagicNumber()!=magic) continue;
if(TimeDay(OrderOpenTime())==day) res1+=OrderProfit();
}
for(i=0; i<OrdersTotal(); i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=magic) continue;
if(TimeDay(OrderOpenTime())==day) res2+=OrderProfit();
}
return(res1+res2);
}