need help in "simple" MQ4 coding.... how can I know the opening/closing candle in D1 TF ??

 

Hi all senior member....


I'm realy new in MQ4 programming but I have php background. I like to learn more about MQ4 coding.


There are some question in my head about MQ4 coding for creating an EA:

1. if I use D1 TF, How can I detect the closing/opening price ?

2. How can I order the BUY/SELL STOP position (pending position) with or without TP and SL ?

3. How can I do the number 2 question in spesific time ?

4. How can I do the number 2 question in the D1 TF opening candle ?

3. How can I close all position (floating or pending) in spesific time ?

4. How can I close all position (floating or pending) when the D1 TF candle closing ?


can some one help me for thats "newbie" questions ?? :)


I realy appreciate your attention here...


Thanks


note: sorry for my poor english :p

 

so.. no one here can help me??

please...

 
Have you read the documentation and tried writing some code to do what you want?
 
phy:
Have you read the documentation and tried writing some code to do what you want?

hi phy... I'm glad have a respon from you :)


can you direct me where can I find the complete MQ44 documentation ??


right now I just learn MQL4 from indicator or EA that found from many forum. I just read the source (coding) and try to copy-paste the code one to another just to know the MQL4 logic :)


but I don't have any collection that meet my question above, so I asked them here...


thanks

 
Documentation link is on this page.
 
phy wrote >>
Documentation link is on this page.

phy.... (and anyone here...)

I already search all documents with all my question and I found some answer that match with my question, but I still cann't find the answer for the questions below ...

3. How can I do the number 2 question in spesific time ?

4. How can I do the number 2 question in the D1 TF opening candle ?

I realy need help here... please...

thanks

 

Ok, I found this for my number 4 question :

int start()
{
  BeginHandleStopLoss();
  if (newbar()==Period()) {
  HandleOpenPositions();
  if (CheckOpenPositions() > 0) return(0);
  lotMM = GetLots();
  if(BuySignal())  
  {
      OpenBuyOrder();
     return(0);
  }
  if(SellSignal()) 
  {
     OpenSellOrder();
  }
  return(0);
}
}
 
int newbar()
{
   double g;
   int m,s,k;
   m=Time[0]+Period()*60-TimeCurrent();
   g=m/60.0;
   s=m%60;
   m=(m-m%60)/60;
   return(m);
}

in this thread 'Completed Bar'

is it what I need ??

 

hi phy...


here the code that I tried in my sample EA:


extern double TakeProfit = 10;
extern double StopLoss = 200;
extern double Lots = 0.1;


int Slippage=2;
int Magic=18072008;
int OrdersOpened=0;
int cnt=0;
string Komentar;
int PreviousOrders=0;
color Color1=Aqua;
double LastPrice= 0;
double Profit = 0;
double LastProfit= 0;
double LastLots= 0;
double TotalLot,LastClsPrice,ProfitPip,PipValue;
int LastTicket,PairOrder,TPBaru;
bool SdhProfit=false;
double rates[1][6],buystop,sellstop,buystoptp,sellstoptp,todayopen,haritrading;

int init()
{
return(0);
}

int deinit()
{
return(0);
}

int start()
{

LastPrice=0;
LastProfit=0;
LastTicket=0;
LastLots=Lots;
Profit=0;
ProfitPip=0;
PairOrder=0;
OrdersOpened = 0;

PreviousOrders = OrdersOpened;

double Spread = MarketInfo(Symbol(),MODE_SPREAD);
PipValue=MarketInfo(Symbol(),MODE_TICKVALUE);
ArrayCopyRates(rates, Symbol(), PERIOD_D1);

if(DayOfWeek()==0 || DayOfWeek()==6)
{
//no trade on holiday;
return(0);
}
else
{
if (newbar()==Period())
{
for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt --)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Color1);
}
}

OpenBuy();
OpenSell();
}
}


}

int newbar()
{
double g;
int m,s,k;
m=Time[0]+Period()*60-TimeCurrent();
g=m/60.0;
s=m%60;
m=(m-m%60)/60;
return(m);
}

void OpenBuy()
{
double ldLot, ldStop, ldTake;
string lsComm;

ldLot = GetSizeLot();
ldStop = Ask-Point*StopLoss;
ldTake = NormalizeDouble(GetTakeProfitBuy(),Digits);
OrderSend(Symbol(),OP_BUY,ldLot,NormalizeDouble(Ask,Digits),Slippage,ldStop,ldTake,"BUY",Magic,0,Color1);
}

void OpenSell()
{
double ldLot, ldStop, ldTake;
string lsComm;

ldLot = GetSizeLot();
ldStop = Bid+Point*StopLoss;
ldTake = NormalizeDouble(GetTakeProfitSell(),Digits);
OrderSend(Symbol(),OP_SELL,ldLot,NormalizeDouble(Bid,Digits),Slippage,ldStop,ldTake,"SELL",Magic,0,Color1);
}

double GetSizeLot()
{
return(Lots);
}

double GetTakeProfitBuy()
{
return(Ask+TakeProfit*Point);
}

double GetTakeProfitSell()
{
return(Bid-TakeProfit*Point);
}


but it's doesn't work. I tried it but my EA doesn't open any trade today.


what is wrong with my code ??

 
extern double TakeProfit = 10;
extern double StopLoss = 200;
extern double Lots = 0.1;


int Slippage=2;
int Magic=18072008;
int OrdersOpened=0;
int cnt=0;
string Komentar;
int PreviousOrders=0;
color Color1=Aqua;
double LastPrice= 0;
double Profit = 0;
double LastProfit= 0;
double LastLots= 0;
double TotalLot,LastClsPrice,ProfitPip,PipValue;
int LastTicket,PairOrder,TPBaru;
bool SdhProfit=false;
double rates[1][6],buystop,sellstop,buystoptp,sellstoptp,todayopen,haritrading;

int init()
{
    return(0);
}

int deinit()
{
    return(0);
}

int start() 
{
    
    LastPrice=0;
    LastProfit=0;
    LastTicket=0;
    LastLots=Lots;
    Profit=0;
    ProfitPip=0;
    PairOrder=0; 
    OrdersOpened = 0;
    
    PreviousOrders = OrdersOpened;
    
    double Spread = MarketInfo(Symbol(),MODE_SPREAD);
    PipValue=MarketInfo(Symbol(),MODE_TICKVALUE);
    ArrayCopyRates(rates, Symbol(), PERIOD_D1);
    
    if(DayOfWeek()==0 || DayOfWeek()==6)
    {
        //no trade on holiday;
        return(0);
    }
    else
    {
        if (newbar()==Period()) 
        {
            for (int cnt = OrdersTotal() - 1; cnt >= 0; cnt --)
            {
                OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
                if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
                {
                    OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), Slippage, Color1);
                }
            }
            
            OpenBuy();
            OpenSell();
        }
    }
    
    
}

int newbar()
{
    double g;
    int m,s,k;
    m=Time[0]+Period()*60-TimeCurrent();
    g=m/60.0;
    s=m%60;
    m=(m-m%60)/60;
    return(m);
}

void OpenBuy() 
{ 
    double ldLot, ldStop, ldTake; 
    string lsComm; 
    
    ldLot = GetSizeLot(); 
    ldStop = Ask-Point*StopLoss; 
    ldTake = NormalizeDouble(GetTakeProfitBuy(),Digits); 
    OrderSend(Symbol(),OP_BUY,ldLot,NormalizeDouble(Ask,Digits),Slippage,ldStop,ldTake,"BUY",Magic,0,Color1); 
}

void OpenSell() 
{ 
    double ldLot, ldStop, ldTake; 
    string lsComm; 
    
    ldLot = GetSizeLot(); 
    ldStop = Bid+Point*StopLoss; 
    ldTake = NormalizeDouble(GetTakeProfitSell(),Digits); 
    OrderSend(Symbol(),OP_SELL,ldLot,NormalizeDouble(Bid,Digits),Slippage,ldStop,ldTake,"SELL",Magic,0,Color1); 
}

double GetSizeLot() 
{ 
    return(Lots); 
} 

double GetTakeProfitBuy() 
{ 
    return(Ask+TakeProfit*Point); 
}

double GetTakeProfitSell() 
{ 
    return(Bid-TakeProfit*Point); 
}
 

Does it give errors?

Does it do nothing?

 

hi phy,


there is no error. I already checked it using metaeditor with F5 (compiled) and attached it on the chart metatrader and no error. But it doesn't do nothing... :(


fyi, the logic is:

- Open chart in D1 TF

- open buy and open sell when the new candle started with TP=10 and SL=200 for each position

- when the candle closed, close all position


is it something wrong with my code ??

Reason: