Orders Execution
Indicators Used
0
Views
0
Downloads
0
Favorites
Xp_EMAs
//+------------------------------------------------------------------+
//| Xp_EMAs.mq4 |
//| Coders Guru |
//| http://www.forex-tsd.com |
//+------------------------------------------------------------------+
#property copyright "Coders Guru"
#property link "http://www.forex-tsd.com"
//---- Includes
#include <stdlib.mqh>
//---- Trades limits
extern double TakeProfit = 150;
extern double TrailingStop = 50;
extern double StopLoss = 45;
extern double Lots = 0.1;
extern int Slippage = 5;
//--- External options
extern int CurrentBar = 0;
extern bool UseClose = true;
//2/3 2/2 1/3 4/3
extern int ema_medium_period = 20; //22 21 19 16 23 24
extern int ema_high_period = 2;
extern int ema_low_period = 3;
//--- Global variables
int MagicNumber = 10072006;
string ExpertComment = "Xp_EMAs";
extern bool LimitPairs = true;
extern bool LimitFrame = true;
extern int TimeFrame = 60;
string LP[] = {"EURUSD","USDCHF","GBPUSD","USDJPY","AUDUSD","USDCAD"}; // add/remove the paris you want to limit.
int NumberOfTries = 5;
//+------------------------------------------------------------------
int init()
{
return(0);
}
int deinit()
{
return(0);
}
//+------------------------------------------------------------------
bool isNewSymbol(string current_symbol)
{
//loop through all the opened order and compare the symbols
int total = OrdersTotal();
for(int cnt = 0 ; cnt < total ; cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
string selected_symbol = OrderSymbol();
if (current_symbol == selected_symbol && OrderMagicNumber()==MagicNumber)
return (False);
}
return (True);
}
//+------------------------------------------------------------------+
int start()
{
int cnt, ticket, total,n;
double ema_high , ema_low , ema_med ;
double pre_ema_high , pre_ema_low , pre_ema_med;
if(Bars<100) {Print("bars less than 100"); return(0);}
if(LimitFrame)
{
if(Period()!=TimeFrame) {Print("This EA is not working with this TimeFrame!"); return(0);}
}
if(LimitPairs)
{
if(AllowedPair(Symbol()) == false) {Print("This EA is not working with this Currency!"); return(0);}
}
ema_high = iMA(NULL,0,ema_high_period,0,MODE_EMA,PRICE_HIGH,CurrentBar);
ema_low = iMA(NULL,0,ema_low_period,0,MODE_EMA,PRICE_LOW,CurrentBar);
ema_med = iMA(NULL,0,ema_medium_period,0,MODE_EMA,PRICE_MEDIAN,CurrentBar);
pre_ema_high = iMA(NULL,0,ema_high_period,0,MODE_EMA,PRICE_HIGH,CurrentBar+1);
pre_ema_low = iMA(NULL,0,ema_low_period,0,MODE_EMA,PRICE_LOW,CurrentBar+1);
pre_ema_med = iMA(NULL,0,ema_medium_period,0,MODE_EMA,PRICE_MEDIAN,CurrentBar+1);
//--- Trading conditions
bool BuyCondition = false , SellCondition = false , CloseBuyCondition = false , CloseSellCondition = false ;
if (ema_low > ema_med && pre_ema_low < pre_ema_med)
BuyCondition = true;
if (ema_high < ema_med && pre_ema_high > pre_ema_med)
SellCondition = true;
if (SellCondition)
CloseBuyCondition = true;
if (BuyCondition)
CloseSellCondition = true;
total = OrdersTotal();
if(total < 1 || isNewSymbol(Symbol()))
{
if(BuyCondition) //<-- BUY condition
{
ticket = OpenOrder(OP_BUY); //<-- Open BUY order
CheckError(ticket,"BUY");
}
if(SellCondition) //<-- SELL condition
{
ticket = OpenOrder(OP_SELL); //<-- Open SELL order
CheckError(ticket,"SELL");
}
return(0);
}
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY) //<-- Long position is opened
{
if(UseClose)
{
if(CloseBuyCondition) //<-- Close the order and exit!
{
CloseOrder(OrderType()); return(0);
}
}
TrailOrder(OrderType()); return(0); //<-- Trailling the order
}
if(OrderType()==OP_SELL) //<-- Go to short position
{
if(UseClose)
{
if(CloseSellCondition) //<-- Close the order and exit!
{
CloseOrder(OP_SELL); return(0);
}
}
TrailOrder(OrderType()); return(0); //<-- Trailling the order
}
}
}
return(0);
}
//+------------------------------------------------------------------+
int OpenOrder(int type)
{
int ticket=0;
int err=0;
int c = 0;
if(type==OP_BUY)
{
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,Ask-StopLoss*Point,Ask+TakeProfit*Point,ExpertComment,MagicNumber,0,Yellow);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
}
if(type==OP_SELL)
{
for(c = 0 ; c < NumberOfTries ; c++)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,Bid+StopLoss*Point,Bid-TakeProfit*Point,ExpertComment,MagicNumber,0,Blue);
err=GetLastError();
if(err==0)
{
break;
}
else
{
if(err==4 || err==137 ||err==146 || err==136) //Busy errors
{
Sleep(5000);
continue;
}
else //normal error
{
break;
}
}
}
}
return(ticket);
}
bool CloseOrder(int type)
{
if(OrderMagicNumber() == MagicNumber)
{
if(type==OP_BUY)
return (OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet));
if(type==OP_SELL)
return (OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,Violet));
}
}
void TrailOrder(int type)
{
if(TrailingStop>0)
{
if(OrderMagicNumber() == MagicNumber)
{
if(type==OP_BUY)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
}
}
}
if(type==OP_SELL)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
}
}
}
}
}
}
void CheckError(int ticket, string Type)
{
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print(Type + " order opened : ",OrderOpenPrice());
}
else Print("Error opening " + Type + " order : ", ErrorDescription(GetLastError()));
}
bool AllowedPair(string pair)
{
bool result=false;
for (int n = 0 ; n < ArraySize(LP); n++)
{
if(Symbol() == LP[n])
{
result = true;
}
}
return (result);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---