MQL4 Learning - page 110

 

infinite loops in ea init() ?

Is it possible to create infinite loops in the init function of an ea? I have an ea which allows through a socket a user to change timeframes on metatrader. However, the start function is only called when a new tick comes in, i need the socket to come back up immediately, so i decided to create an infinite loop in the init function and the start function.

Will the infinite loop in the init function cause any problems???

 

Need help to change EA Indicator

I have an EA based on indicator "LSMA" that buy when (red== Empty value && green !=0)

double green = iCustom(Symbol(),0,"LSMA",LSMA_Period,LSMA_Bars,2,1);

double red=...........................

I will now replace LSMA by "SHI_SilverTrendSig1" when red dot is under close.

How can I write that ?

Thanks in advance.

 

This EA is trading only on demo. Who know why?

Thanks

extern double GapRange = 10;

extern double SL_Factor = 2;

extern double TP_Factor = 3;

extern double MM_Risk = 2;

extern int ExpertID=844478;

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

//| expert initialization function |

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

int init()

{

//----

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

//---- ONE TRADE PER BAR

static bool ToTrade;

if(NewBar() == true)

{

if(COT(1, 101187) == 0 && COT(2, 201187) == 0) ToTrade = true;

}

//---- GAP

bool GAP;

double CurrOpen = iOpen(NULL, 0, 0);

double PrevClose = iClose(NULL, 0, 1);

double Range = MathAbs(PrevClose - CurrOpen);

if(Range >= GapRange * Point * 10) GAP = true;

//---- TP / SL

double ATR = iATR(NULL, 0, 13, 1);

double Spread = MarketInfo(Symbol(), MODE_SPREAD) * Point;

double TakeProfit = ATR * TP_Factor;

double StopLoss = (ATR * SL_Factor) + Spread;

//---- TRADE

int Ticket;

if(ToTrade == true && GAP == true)

{

if(CurrOpen < PrevClose)

{

Ticket = OrderSend(Symbol(), OP_BUY, LotSize(MM_Risk, StopLoss), Ask, 3, Ask - StopLoss, Ask + TakeProfit, "Gap_Trader.B", 101187, 0, Blue);

if(Ticket < 0)

{

Print("Error in OrderSend : ", GetLastError());

}

else

{

ToTrade = false;

}

}

if(CurrOpen > PrevClose)

{

Ticket = OrderSend(Symbol(), OP_SELL, LotSize(MM_Risk, StopLoss), Bid, 3, Bid + StopLoss, Bid - TakeProfit, "Gap_Trader.S", 201187, 0, Red);

if(Ticket < 0)

{

Print("Error in OrderSend : ", GetLastError());

}

else

{

ToTrade = false;

}

}

}

//----

return(0);

}

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

//+ Check Open Trades |

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

int COT(int BS, int MN)

{

int Buys = 0, Sells = 0;

for(int cnt_COT = 0; cnt_COT < OrdersTotal(); cnt_COT++)

{

OrderSelect(cnt_COT, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol() == Symbol() && OrderMagicNumber() == MN && OrderType() == OP_BUY) Buys++;

if(OrderSymbol() == Symbol() && OrderMagicNumber() == MN && OrderType() == OP_SELL) Sells++;

}

if(BS == 1) return(Buys);

if(BS == 2) return(Sells);

}

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

//| LotSize |

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

double LotSize(double Risk, double SL)

{

double MaxLot = MarketInfo(Symbol(), MODE_MAXLOT);

double MinLot = MarketInfo(Symbol(), MODE_MINLOT);

double StopLoss = SL / Point / 10;

double Size = Risk / 100 * AccountBalance() / 10 / StopLoss;

if(Size < MinLot) Size = MinLot;

if(Size > MaxLot) Size = MaxLot;

return(NormalizeDouble(Size, 2));

}

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

//| New Bar |

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

bool NewBar()

{

static datetime PrevBar;

if(PrevBar < Time[0])

{

PrevBar = Time[0];

return(true);

}

else

{

return(false);

}

}

 

A simple requst

I am using the code below to detect a newbar, but I want to start the code on the next bar after the EA opens, any help here please

bool New_Bar()

{

bool New_Bar = false;

if (New_Time!= Time[0])

{

New_Time = Time[0];

New_Bar = true;

}

return(New_Bar);

}

 

I am tryng this code snippit

if (Bars > BarsCount)

{

temp = temp + candle0;

BarsCount = Bars;

}

so I want temp to increment with candle0 on the next bar after the EA opens not immediately the EA opens

 
increase:
I am tryng this code snippit

if (Bars > BarsCount)

{

temp = temp + candle0;

BarsCount = Bars;

}

so I want temp to increment with candle0 on the next bar after the EA opens not immediately the EA opens

It's very simple. Declare BarsCount in global section with some value - it can be 0, then in init() function put this line of code: BarsCount = Bars;

So at the beginning ea will remember number of bars available on chart, and it will start working only if new one will come.

 

Help with OrderSend( ) Function

New to mql4, I wrote the following code for practice, but can't figure out why I get an Error 130

I think it has to do with unNormalized numbers. I'm using IBFX broker and they use the 5th decimal on the price.

double Stoploss = 50;

double Takeprofit = 50;

int start()

{

int ticket = OrderSend(Symbol(),OP_BUY,1.0,Ask,3,Ask-Stoploss*Point,

Ask+Takeprofit*Point,"Order #1",1234,0,Red);

if(ticket == -1) Print("error-",GetLastError());

return(0);

}

I also tried setting SL and TP to 500 because of the 5th decimal but got the same error. Any suggestions? Thanks

 

Error 130 means the Stoploss is invalid, so the value you calculate seems to be out of range.

Check if the value does make sense.

Regards, Klaus

 

Help using OrderProfit() on Multiple Orders in same currency pair

Hi, I wrote an EA which can trade 1 to 3 Trades at a time on the same currency pair.

The code snippet below seems to calculate the Open Order profits okay but on close they are out by a few percent.

If I run one trade it is accurate.

Can anyone look at the code below and offer any advice?

for ( tradeCount=0; tradeCount < OrdersTotal(); tradeCount++ )

{

if(!OrderSelect(tradeCount,SELECT_BY_POS))continue ;

if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&O rderMagicNumber()==MagicNumber01)TradeProfit01 = OrderProfit() + OrderCommission() + OrderSwap();

if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&O rderMagicNumber()==MagicNumber02)TradeProfit02 = OrderProfit() + OrderCommission() + OrderSwap();

if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&O rderMagicNumber()==MagicNumber03)TradeProfit03 = OrderProfit() + OrderCommission() + OrderSwap();

if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber01)TradeProfit01 = OrderProfit() + OrderCommission() + OrderSwap();

if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber02)TradeProfit02 = OrderProfit() + OrderCommission() + OrderSwap();

if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&& OrderMagicNumber()==MagicNumber03)TradeProfit03 = OrderProfit() + OrderCommission() + OrderSwap();

}

I would like to be able to read the Profit displayed in the MT4 Account History and display it in an Alert().

Many thanks.

 

Required Help for MQL4 Programing (Indicator)

Hi,

I am New to MQL4. I don't have much experience in MQL4 and also normal programing.

I required help from seniors.

1st I have done average of 14 days high by mentioning

double high1,high2,high3,...........high13,high14;

high1=high[1];

high2=high[2];

high3=high[3];

...........

............

..................

high14=high[14];

double totalhigh,highavg;

totalhigh=high1+high2+high3+..................high13+high14;

highavg=totalhigh/14;

==================================================

from here onwards, I want help from you to proceed further.

I want to draw line on Indicator filed " highavg " value. simply i want to create an indicator with 14 days average of HIGH. The 14 days average I have assigned to "highavg" variable. So plz help me to code to draw line on indicator filed with the value of " highavg "