![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
it does not open any trades
//+------------------------------------------------------------------+
//| Hedgegrid.mq4 |
//| Copyright © 2009, MetaQuotes Software Corp. |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link "https://www.metaquotes.net/"
//---- input parameters
extern double Lot1=0.1;
extern double Lot2=0.1;
extern string Sym1="EURUSD";
extern string Sym2="USDCHF";
extern string Operation1="buy";
extern string Operation2="buy";
extern double Commission1=0.0;
extern double Commission2=0.0;
extern double Profit=50;
extern double multiply= 1.6;
extern int MaxTrades= 4; // Maximum number of orders to open
extern int Pips= 50; // Distance in Pips from one order to another
extern int StopLoss = 300; // StopLoss
extern int TrailingStop = 15;// Pips to trail the StopLoss
extern bool BuyStopOrders=True;
extern bool UseMM=true;
extern string Bollinger_Symbol = "EURCHF";
int OP1=-1, OP2=-1;
double Ilo1=0, Ilo2=0;
extern double Lots = 0.1;
extern double GridSize = 50;
extern double GridSteps = 2;
extern double TakeProfit = 50;
extern string Data5 = " * * * Money Management";
extern bool AccountIsMicro = false;
extern double ProfitTarget = 50;
extern double Risk = 6;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----
//init global variables
if (!GlobalVariableCheck("_CanClose")) {
GlobalVariableSet("_CanClose",0);
}
//if (!GlobalVariableCheck("_CanSet")) {
//GlobalVariableSet("_CanSet",0);
//}
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
Ilo1=Lot1; Ilo2=Lot2;
if (UseMM) {
Ilo1=TradeLot(AccountBalance());
Ilo2=TradeLot(AccountBalance());
}
//----
double Commissions=0;
if (Operation1=="buy" || Operation1=="BUY") OP1=OP_BUY;
if (Operation2=="buy" || Operation2=="BUY") OP2=OP_BUY;
if (Operation1=="sell" || Operation1=="SELL") OP1=OP_SELL;
if (Operation2=="sell" || Operation2=="SELL") OP2=OP_SELL;
if (OP1<0 || OP2<0) {
Comment("Wrong operation selected, aborted...");
return;
}
if (GlobalVariableGet("_CanClose")==1 && CntOrd(OP1,0,Sym1)==0 && CntOrd(OP2,0,Sym2)==0) {
GlobalVariableSet("_CanClose",0);
}
if (GlobalVariableGet("_CanClose")==0) {
//Set intitial orders
SetOrders();
}
Comment("Balance=",AccountBalance(),"\n",Sym1," Lot=",Ilo1," ",Sym2," Lot=",Ilo2,"\nFloating profit=",CalcProfit()," Expected profit=",Profit*Ilo1*10);
//Check for profit
Commissions=Commission1*Ilo1+Commission2*Ilo1;
if ( (CalcProfit()-Commissions) >= (Profit*Ilo1*10) ) {
GlobalVariableSet("_CanClose",1);
}
CloseAll();
//----
return(0);
}
//+------------------------------------------------------------------+
double CalcProfit() {
//Calculating profit for opened positions
int cnt;
double _Profit;
_Profit=0;
for(cnt=0; cnt<OrdersTotal(); cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Sym1 || OrderSymbol()==Sym2) {
_Profit=_Profit+OrderProfit();
}
}
return(_Profit);
}
void CloseAll() {
int _total=OrdersTotal(); // number of orders
int _ordertype;// order type
if (_total==0) {return;}
int _ticket; // ticket number
double _priceClose;// price to close orders;
//Closing all opened positions
if (GlobalVariableGet("_CanClose")==1) {
for(int _i=_total-1;_i>=0;_i--)
{
if (OrderSelect(_i,SELECT_BY_POS))
{
_ordertype=OrderType();
_ticket=OrderTicket();
switch(_ordertype)
{
case 0:
// close buy
_priceClose=MarketInfo(OrderSymbol(),MODE_BID);
Print("Close on ",_i," position order with ticket ¹",_ticket);
OrderClose(_ticket,OrderLots(),_priceClose,0,Red);
break;
case 1:
// close sell
_priceClose=MarketInfo(OrderSymbol(),MODE_ASK);
Print("Close on ",_i," position order with ticket ¹",_ticket);
OrderClose(_ticket,OrderLots(),_priceClose,0,Red);
break;
default:
// values from 1 to 5, deleting pending orders
Print("Delete on ",_i," position order with ticket ¹",_ticket);
OrderDelete(_ticket);
break;
}
}
}
}
return;
}
void SetOrders() {
//Setting initial orders
double OpenPrice=0;
if (CntOrd(OP1,0,Sym1)==0) {
if (OP1==OP_BUY) OpenPrice=MarketInfo(Sym1,MODE_ASK);
if (OP1==OP_SELL) OpenPrice=MarketInfo(Sym1,MODE_BID);
OrderSend(Sym1,OP1,Ilo1,OpenPrice,0,0,0,"HedgeTrader",0,0,Red);
//return;
}
if (CntOrd(OP2,0,Sym2)==0) {
if (OP2==OP_BUY) OpenPrice=MarketInfo(Sym2,MODE_ASK);
if (OP2==OP_SELL) OpenPrice=MarketInfo(Sym2,MODE_BID);
OrderSend(Sym2,OP2,Ilo2,OpenPrice,0,0,0,"HedgeTrader",0,0,Green);
//return;
}
}
int CntOrd(int Type, int Magic, string Symb) {
//return number of orders with specific parameters
int _CntOrd;
_CntOrd=0;
for(int i=0;i<OrdersTotal();i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symb) {
if ( (OrderType()==Type && (OrderMagicNumber()==Magic) || Magic==0)) _CntOrd++;
}
}
return(_CntOrd);
}
double TradeLot(double MyBalance) {
double _Ilo=0;
//AccountEquity()
_Ilo=MathFloor(MyBalance/Delta)/10;
if (_Ilo<0.1) _Ilo=0.1;
return (_Ilo);
}