Anda di halaman 1dari 13

//+------------------------------------------------------------------+

//| ReverseEA.mq4 |
//| Copyright 2014, tidicofx |
//| tidicofx@yahoo.com |
//| developed for loocoom |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, tidicofx"
#property link "tidicofx@yahoo.com"
#property version "1.52"
#property strict
//+------------------------------------------------------------------+
#include <stdlib.mqh>
#include <stderror.mqh>
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Input Parameters Definition |
//+------------------------------------------------------------------+
input string hint1 = "===== trade entry settings =====";
input int MaxOrdersPerCandle = 0;
input double InitialLot = 0.01;
input bool IncrementBuy = true;
input double IncrementBuyBy = 0.01;
input int IncrementBuyMaxLevels = 10;
input bool IncrementSell = true;
input double IncrementSellBy = 0.01;
input int IncrementSellMaxLevels = 10;
input double RealSL = 20;
input double TakeProfit = 50;
input double ProfitLevel1 = 10.0;
input double StopLevel1 = 2.0;
input double ProfitLevel2 = 20.0;
input double StopLevel2 = 5.0;
input double ProfitLevel3 = 30.0;
input double StopLevel3 = 10.0;
input double ProfitLevel4 = 40.0;
input double StopLevel4 = 20.0;
input double MinCandleSize = 5;

input double MaxCandleSize = 50;


input int MaxBuyOrders = 20;
input int MaxSellOrders = 20;
input string hint3 = "===== other setting =====";
input bool IsEcn = true;
input int MagicNumber = 22138;
input ENUM_LINE_STYLE SLLineStyle = STYLE_DASHDOT;
input color SLLineTextColor = clrWhite;
input string SLLineTextFontName = "Tahoma";
input int SLLineTextFontSize = 7;
//+------------------------------------------------------------------+
//| Local Parameters Definition |
//+------------------------------------------------------------------+
string lbl = "ReverseEA.", gbl;
int DecimalPoints;
bool _res;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//-----------------------------------------------------------------DecimalPoints = 1;
double x = MarketInfo(Symbol(), MODE_LOTSTEP) * 10;
while (x < 1)
{ x *= 10; DecimalPoints += 1; }
//-----------------------------------------------------------------gbl = lbl+Symbol()+"."+IntegerToString(MagicNumber)+".";
if (IsTesting()) { gbl = "B."+gbl; GlobalVariablesDeleteAll(gbl); }
//-----------------------------------------------------------------return (INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)

{
//-----------------------------------------------------------------if (reason == REASON_REMOVE) GlobalVariablesDeleteAll(gbl);
if (!IsTesting())
for (int i = ObjectsTotal(0, -1, -1)-1; i >= 0; i--)
if (StringFind(ObjectName(0, i), lbl) != -1)
ObjectDelete(0, ObjectName(0, i));
Comment("");
//-----------------------------------------------------------------}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
void OnTick()
{
//-----------------------------------------------------------------if (!IsConnected() || AccountNumber() == 0 || Bars <= 30 || ArraySize(Time) == 0) return;
//-----------------------------------------------------------------if (MaxCandleSize == 0 || NormalizeDouble(High[1]-Low[1],
_Digits)<=NormalizeDouble(PipsToPoints(MaxCandleSize), _Digits))
if (MinCandleSize == 0 || NormalizeDouble(High[1]-Low[1],
_Digits)>=NormalizeDouble(PipsToPoints(MinCandleSize), _Digits))
{
if (NormalizeDouble(High[1], _Digits)>NormalizeDouble(High[2], _Digits) &&
NormalizeDouble(Low[1], _Digits)>=NormalizeDouble(Low[2], _Digits))
if (NormalizeDouble(Close[1], _Digits)<=NormalizeDouble((High[1]+Low[1])/2, _Digits))
if (DoesSignalCatched(OP_SELL, Time[0]) == -1 && (GetOrderCount(OP_SELL)<MaxSellOrders ||
MaxSellOrders==0))
SetOrder(OP_SELL, Time[1]);
if (NormalizeDouble(Low[1], _Digits)<NormalizeDouble(Low[2], _Digits) &&
NormalizeDouble(High[1], _Digits)<=NormalizeDouble(High[2], _Digits))
if (NormalizeDouble(Close[1], _Digits)>=NormalizeDouble((High[1]+Low[1])/2, _Digits))
if (DoesSignalCatched(OP_BUY, Time[0]) == -1 && (GetOrderCount(OP_BUY)<MaxBuyOrders ||
MaxBuyOrders==0))
SetOrder(OP_BUY, Time[1]);
if (NormalizeDouble(High[0], _Digits)>NormalizeDouble(High[1], _Digits) &&
NormalizeDouble(Low[0], _Digits)<NormalizeDouble(Low[1], _Digits))

if (NormalizeDouble(Close[0], _Digits)<NormalizeDouble(Low[1], _Digits))


if (DoesSignalCatched(OP_SELL, Time[0]) == -1 && (GetOrderCount(OP_SELL)<MaxSellOrders ||
MaxSellOrders==0))
SetOrder(OP_SELL, Time[0]);
if (NormalizeDouble(Low[0], _Digits)<NormalizeDouble(Low[1], _Digits) &&
NormalizeDouble(High[0], _Digits)>NormalizeDouble(High[1], _Digits))
if (NormalizeDouble(Close[0], _Digits)>NormalizeDouble(High[1], _Digits))
if (DoesSignalCatched(OP_BUY, Time[0]) == -1 && (GetOrderCount(OP_BUY)<MaxBuyOrders ||
MaxBuyOrders==0))
SetOrder(OP_BUY, Time[0]);
}
//-----------------------------------------------------------------ManageSLTP();
//-----------------------------------------------------------------}
//+------------------------------------------------------------------+
int SetOrder(int type, datetime fore)
{
double sl = 0, sl2 = 0, tp = 0, sl3, tp3;
double price = 0;
if (type == OP_BUY)
{
price = Ask;
sl = Low[1] - _Point;
tp = price + PipsToPoints(TakeProfit);
sl2 = sl - PipsToPoints(RealSL);
}
if (type == OP_SELL)
{
price = Bid;
sl = High[1] + _Point;
tp = price - PipsToPoints(TakeProfit);
sl2 = sl + PipsToPoints(RealSL);
}
sl3 = sl2; tp3 = tp;
if (IsEcn) { sl3 = 0; tp3 = 0; }
double lot = InitialLot;

int last = GetLastClosedOrder(type);


if (last != -1)
if ((int)GlobalVariableGet(gbl+IntegerToString(last)+".ClosedByHiddenSL") == true)
{
if (type == OP_BUY && IncrementBuy)
{
lot = OrderLots()+IncrementBuyBy;
if (NormalizeDouble(lot,
DecimalPoints)>NormalizeDouble(InitialLot+IncrementBuyMaxLevels*IncrementBuyBy,
DecimalPoints))
lot = InitialLot+IncrementBuyMaxLevels*IncrementBuyBy;
}
if (type == OP_SELL && IncrementSell)
{
lot = OrderLots()+IncrementSellBy;
if (NormalizeDouble(lot,
DecimalPoints)>NormalizeDouble(InitialLot+IncrementSellMaxLevels*IncrementSellBy,
DecimalPoints))
lot = InitialLot+IncrementSellMaxLevels*IncrementSellBy;
}
}
int ticket = OrderSend(Symbol(), type, lot, price, 0, sl3, tp3, TimeToStr(fore), MagicNumber, 0);
int err = GetLastError();
if (ticket == -1 && err > 0)
Print("err=", IntegerToString(err), ", type=" + IntegerToString(type), ", price=", DoubleToStr(price,
_Digits), ", lot=", DoubleToStr(lot, DecimalPoints), ", ", ErrorDescription(err));
else if (ticket != -1)
{
GlobalVariableSet(gbl+IntegerToString(ticket)+".SL", sl);
GlobalVariableSet(gbl+IntegerToString(ticket)+".TP", tp);
if (IsEcn) ManageSLTP();
}
return (ticket);
}
//+------------------------------------------------------------------+
void ManageSLTP()
{

double isl, sl, tp;


for (int i = OrdersTotal() - 1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
{
if (IsEcn && OrderStopLoss() == 0 && OrderTakeProfit() == 0)
{
sl = 0; tp = 0;
if (OrderType() == OP_BUY)
{
tp = OrderOpenPrice() + PipsToPoints(TakeProfit);
sl = GlobalVariableGet(gbl+IntegerToString(OrderTicket())+".SL") - PipsToPoints(RealSL);
}
else if (OrderType() == OP_SELL)
{
tp = OrderOpenPrice() - PipsToPoints(TakeProfit);
sl = GlobalVariableGet(gbl+IntegerToString(OrderTicket())+".SL") + PipsToPoints(RealSL);
}
if (sl != 0 || tp != 0) _res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, tp, 0);
continue;
}
sl = OrderStopLoss();
isl = GlobalVariableGet(gbl+IntegerToString(OrderTicket())+".SL");
int index = (int)ChartGetInteger(0, CHART_VISIBLE_BARS)-3;
datetime time2 = Time[0]; if (index >= 0) time2 = Time[index];
if (ObjectFind(0, lbl+IntegerToString(OrderTicket())+".SL") == -1)
DrawLine(lbl+IntegerToString(OrderTicket())+".SL", Time[0], isl, clrRed, SLLineStyle, 1);
else
isl = ObjectGetDouble(0, lbl+IntegerToString(OrderTicket())+".SL", OBJPROP_PRICE, 0);
if (sl != 0) DrawText(lbl+IntegerToString(OrderTicket())+".TSL", "#"+IntegerToString(OrderTicket())+"
sl", time2, sl, SLLineTextColor);
int ticket = OrderTicket();
if (OrderType() == OP_BUY)
{
if (isl != 0 && NormalizeDouble(Bid, _Digits)<=NormalizeDouble(isl, _Digits))
if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0))
{

GlobalVariableSet(gbl+IntegerToString(ticket)+".ClosedByHiddenSL", true);
Print("order closed on virtual sl");
continue;
}
if (ProfitLevel4 != 0 && NormalizeDouble(Bid-OrderOpenPrice(),
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel4), _Digits))
{
if (NormalizeDouble(sl, _Digits)<NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel4),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel4), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel4");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
else if (ProfitLevel3 != 0 && NormalizeDouble(Bid-OrderOpenPrice(),
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel3), _Digits))
{
if (NormalizeDouble(sl, _Digits)<NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel3),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel3), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel3");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
else if (ProfitLevel2 != 0 && NormalizeDouble(Bid-OrderOpenPrice(),
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel2), _Digits))
{
if (NormalizeDouble(sl, _Digits)<NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel2),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel2), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel2");

_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);


}
}
else if (ProfitLevel1 != 0 && NormalizeDouble(Bid-OrderOpenPrice(),
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel1), _Digits))
{
if (NormalizeDouble(sl, _Digits)<NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel1),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()+PipsToPoints(StopLevel1), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel1");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
}
else if (OrderType() == OP_SELL)
{
if (isl != 0 && NormalizeDouble(Bid, _Digits)>=NormalizeDouble(isl, _Digits))
if (OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0))
{
GlobalVariableSet(gbl+IntegerToString(ticket)+".ClosedByHiddenSL", true);
Print("order closed on virtual sl");
continue;
}
if (ProfitLevel4 != 0 && NormalizeDouble(OrderOpenPrice()-Ask,
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel4), _Digits))
{
if (NormalizeDouble(sl, _Digits)>NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel4),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel4), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel4");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}

else if (ProfitLevel3 != 0 && NormalizeDouble(OrderOpenPrice()-Ask,


_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel3), _Digits))
{
if (NormalizeDouble(sl, _Digits)>NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel3),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel3), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel3");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
else if (ProfitLevel2 != 0 && NormalizeDouble(OrderOpenPrice()-Ask,
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel2), _Digits))
{
if (NormalizeDouble(sl, _Digits)>NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel2),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel2), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel2");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
else if (ProfitLevel1 != 0 && NormalizeDouble(OrderOpenPrice()-Ask,
_Digits)>=NormalizeDouble(PipsToPoints(ProfitLevel1), _Digits))
{
if (NormalizeDouble(sl, _Digits)>NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel1),
_Digits))
{
sl = NormalizeDouble(OrderOpenPrice()-PipsToPoints(StopLevel1), _Digits);
Print("trail sl of order #"+IntegerToString(OrderTicket())+" to "+DoubleToStr(sl, _Digits)+" on
ProfitLevel1");
_res = OrderModify(OrderTicket(), OrderOpenPrice(), sl, OrderTakeProfit(), 0);
}
}
}

}
//-----------------------------------------------------------------for (int i = ObjectsTotal()-1; i >= 0; i--)
if (StringFind(ObjectName(0, i), lbl) != -1)
if (ObjectType(ObjectName(0, i)) == OBJ_HLINE || ObjectType(ObjectName(0, i)) == OBJ_TEXT)
{
int index = StringFind(ObjectName(0, i), ".", StringLen(lbl));
int ticket = StrToInteger(StringSubstr(ObjectName(0, i), StringLen(lbl), index-StringLen(lbl)));
if (OrderSelect(ticket, SELECT_BY_TICKET))
if (OrderCloseTime() != 0)
{
GlobalVariableDel(gbl+IntegerToString(OrderTicket())+".SL");
GlobalVariableDel(gbl+IntegerToString(OrderTicket())+".TP");
ObjectDelete(ObjectName(0, i));
}
}
//-----------------------------------------------------------------}
//+------------------------------------------------------------------+
int DoesSignalCatched(int type, datetime from)
{
for (int i = OrdersTotal() - 1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type
&& OrderOpenTime()>=from)
return (OrderTicket());
for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type
&& OrderOpenTime()>=from)
return (OrderTicket());
return (-1);
}
//+------------------------------------------------------------------+
int GetOrderCount(int type)
{
int count = 0;

for (int i = OrdersTotal() - 1; i >= 0; i--)


if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type)
count ++;
return (count);
}
//+------------------------------------------------------------------+
int GetLastClosedOrder(int type)
{
for (int i = OrdersHistoryTotal() - 1; i >= 0; i--)
if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY))
if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber && OrderType() == type)
return (OrderTicket());
return (-1);
}
//+------------------------------------------------------------------+
double PipsToPoints(double pips)
{
if (_Digits == 5 || _Digits == 3) pips *= 10;
return (pips * _Point);
}
//+------------------------------------------------------------------+
string GetSignalName(int sig)
{
if (sig == OP_BUY) return ("Long");
else if (sig == OP_SELL) return ("Short");
else return ("None");
}
//+------------------------------------------------------------------+
string GetTimeFrameName(int TimeFrame)
{
switch (TimeFrame)
{
case PERIOD_M1: return("M1");
case PERIOD_M5: return("M5");
case PERIOD_M15: return("M15");
case PERIOD_M30: return("M30");

case PERIOD_H1: return("H1");


case PERIOD_H4: return("H4");
case PERIOD_D1: return("D1");
case PERIOD_W1: return("W1");
case PERIOD_MN1: return("MN1");
case 0: return(GetTimeFrameName(Period()));
}
return ("");
}
//+------------------------------------------------------------------+
void DrawText(string name, string text, datetime time, double price, color col)
{
if (ObjectFind(name) == -1)
{
ObjectCreate(name, OBJ_TEXT, 0, time, price);
ObjectSet(name, OBJPROP_BACK, false);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, false);
ObjectSetInteger(0, name, OBJPROP_HIDDEN, true);
}
ObjectSet(name, OBJPROP_ANCHOR, ANCHOR_LEFT_LOWER);
if (ObjectGet(name, OBJPROP_COLOR)!=col) ObjectSet(name, OBJPROP_COLOR, col);
if (ObjectGet(name, OBJPROP_TIME1)!=time) ObjectSet(name, OBJPROP_TIME1, time);
if (ObjectGet(name, OBJPROP_PRICE1)!=price) ObjectSet(name, OBJPROP_PRICE1, price);
if (ObjectDescription(name)!=text || ObjectGet(name, OBJPROP_FONTSIZE)!=SLLineTextFontSize)
ObjectSetText(name, text, SLLineTextFontSize, SLLineTextFontName);
}
//+------------------------------------------------------------------+
void SetText(string name, string text, int _size, string _font, color col = clrNONE)
{
ObjectSetInteger(0, name, OBJPROP_FONTSIZE, _size);
if (col != clrNONE) ObjectSetInteger(0, name, OBJPROP_COLOR, col);
ObjectSetString(0, name, OBJPROP_FONT, _font);
ObjectSetString(0, name, OBJPROP_TEXT, text);
}
//+------------------------------------------------------------------+
void DrawLine(string name, datetime time, double price, color col, int style, int width)
{

if (ObjectFind(0, name) == -1)


{
ObjectCreate(0, name, OBJ_HLINE, 0, time, price);
ObjectSetInteger(0, name, OBJPROP_RAY, false);
ObjectSetInteger(0, name, OBJPROP_BACK, false);
ObjectSetInteger(0, name, OBJPROP_SELECTED, false);
ObjectSetInteger(0, name, OBJPROP_SELECTABLE, true);
ObjectSetInteger(0, name, OBJPROP_HIDDEN, false);
}
if (ObjectGetInteger(0, name, OBJPROP_COLOR) != col) ObjectSetInteger(0, name,
OBJPROP_COLOR, col);
if (ObjectGetInteger(0, name, OBJPROP_WIDTH) != width) ObjectSetInteger(0, name,
OBJPROP_WIDTH, width);
if (ObjectGetInteger(0, name, OBJPROP_STYLE) != style) ObjectSetInteger(0, name,
OBJPROP_STYLE, style);
if (ObjectGetInteger(0, name, OBJPROP_TIME, 0) != time) ObjectSetInteger(0, name,
OBJPROP_TIME, 0, time);
if (ObjectGetInteger(0, name, OBJPROP_TIME, 1) != time) ObjectSetInteger(0, name,
OBJPROP_TIME, 1, time);
if (NormalizeDouble(ObjectGetDouble(0, name, OBJPROP_PRICE, 0), _Digits) != price)
ObjectSetDouble(0, name, OBJPROP_PRICE, 0, price);
if (NormalizeDouble(ObjectGetDouble(0, name, OBJPROP_PRICE, 1), _Digits) != price)
ObjectSetDouble(0, name, OBJPROP_PRICE, 1, price);
}
//+------------------------------------------------------------------+
bang power,copy ini dah namai mq4..ni EA trend reverse untuk sell limit/buy limit
untuk indi ketik di google " MTF_Fractal_638.ex4 site:mql5.com "
cuma 2 ini barang bagus yang ku punya..yang lain masih dalam proses