Newbie with MQL4 need a little help

Hi everyone.

I am learning the MQL Language hope there alot of programmer here might help me, I have this very simple EA I created for learning purpose so my problem is I wanted to implement martingale in this strategy where a trade is opened after three consecutive bear or bull, but I was not succeeded I am looking for help if anyone who is competent, below is the code I tried to upload the EA with no success.

#property copyright “”
#property link “”
#property version “1.02”
#property strict
//— input parameters
input int TakeProfit=500;
input int StopLoss=500;
input double LotSize=0.1;
input int Slippage=30;
input int MagicNumber=5555;
//±----------------------------------- ------------------------------+
//| Expert initialization function |
//±----------------------------------- ------------------------------+
int OnInit()
{
//—

//—
return(INIT_SUCCEEDED);
}
//±----------------------------------- ------------------------------+
//| Expert deinitialization function |
//±----------------------------------- ------------------------------+
void OnDeinit(const int reason)
{
//—

}
//±----------------------------------- ------------------------------+
//| Expert tick function |
//±----------------------------------- ------------------------------+
void OnTick()
{
//—

if(TotalOpenOrders() == 0 && IsNewBar() == true)
{
if(Close[1] > Open[1] && Close[2] > Open[2] && Close[3] > Open[3])
{
//Open Buy Order
OrderSend(_Symbol,OP_BUY,LotSize,As k,Slippage,Ask-StopLoss*_Point,Ask+TakeProfit*_Poi nt,“BUY”,MagicNumber);
}
// Sell Logic
if(Close[1] < Open[1] && Close[2] < Open[2] && Close[3] < Open[3])
{
OrderSend(_Symbol,OP_SELL,LotSize,B id,Slippage,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,“SELL”,MagicNumbe r);

}
}

}
//±----------------------------------- ------------------------------+

// Check if there is a new bar
bool IsNewBar()
{
static datetime RegBarTime=0;
datetime ThisBarTime = Time[0];

if (ThisBarTime == RegBarTime)
{
return(false);
}
else
{
RegBarTime = ThisBarTime;
return(true);
}
}

// Returns the number of total open orders for this Symbol and MagicNumber
int TotalOpenOrders()
{
int total_orders = 0;

for(int order = 0; order < OrdersTotal(); order++)
{
if(OrderSelect(order,SELECT_BY_POS, MODE_TRADES)==false) break;

if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
{
total_orders++;
}
}

return(total_orders);
}

Any help Traders and Programmers??

Hi!

I have tested your code, and it is working as you described above.
(Buys after three bull candle, and sells after three bear candle.)


It is possible, although, that I have not understood your problem correctly.

Hi Gorraf thanks for your reply, my problem is I want to implement Martingale after a loss of a first trade of a call or buy then a martingale should repeat until a loss has recovered and start over again hope you got my point now.

Sorry, my bad.
Your explanation was clear, but my gambling knowledge was not up to date. :slight_smile:

I have added a few extra lines to your code:

#property copyright ""
#property link ""
#property version "1.02"
#property strict
//--- input parameters
input int TakeProfit=500;
input int StopLoss=500;
input double LotSize=0.1;
input int Slippage=30;
input int MagicNumber=5555;
int OT;
double Martingale=LotSize;
//+------------------------------------ ------------------------------+
//| Expert initialization function |
//+------------------------------------ ------------------------------+
int OnInit()
{
//---

//---
return(INIT_SUCCEEDED);
}
//+------------------------------------ ------------------------------+
//| Expert deinitialization function |
//+------------------------------------ ------------------------------+
void OnDeinit(const int reason)
{
//---

}
//+------------------------------------ ------------------------------+
//| Expert tick function |
//+------------------------------------ ------------------------------+
void OnTick()
{
//---

if (OT!=NULL)
   {OrderSelect(OT,SELECT_BY_TICKET);
    if (OrderCloseTime()!=NULL && OrderProfit()<0)
       {Martingale=Martingale*2;};
    if (OrderCloseTime()!=NULL && OrderProfit()>0)
       {Martingale=LotSize;};};

if(TotalOpenOrders() == 0 && IsNewBar() == true)
{
if(Close[1] > Open[1] && Close[2] > Open[2] && Close[3] > Open[3])
{
//Open Buy Order
OrderSend(_Symbol,OP_BUY,Martingale,Ask,Slippage,Ask-StopLoss*_Point,Ask+TakeProfit*_Point,"BUY",MagicNumber);
}
// Sell Logic
if(Close[1] < Open[1] && Close[2] < Open[2] && Close[3] < Open[3])
{
OrderSend(_Symbol,OP_SELL,Martingale,Bid,Slippage,Bid+StopLoss*_Point,Bid-TakeProfit*_Point,"SELL",MagicNumber);

}
}
OrderSelect(0,SELECT_BY_POS,MODE_TRADES);
OT=OrderTicket();

}
//+------------------------------------ ------------------------------+

// Check if there is a new bar
bool IsNewBar()
{
static datetime RegBarTime=0;
datetime ThisBarTime = Time[0];

if (ThisBarTime == RegBarTime)
{
return(false);
}
else
{
RegBarTime = ThisBarTime;
return(true);
}
}

// Returns the number of total open orders for this Symbol and MagicNumber
int TotalOpenOrders()
{
int total_orders = 0;

for(int order = 0; order < OrdersTotal(); order++)
{
if(OrderSelect(order,SELECT_BY_POS, MODE_TRADES)==false) break;

if(OrderMagicNumber() == MagicNumber && OrderSymbol() == _Symbol)
{
total_orders++;
}
}

return(total_orders);
}


I hope this is what you had in mind.

Thanks for your help Gorrat, am far from my computer I will test it and if I will have any problem I will ask you.