MQL4 Learning - page 112

 
 

My broker told me, it is not allowed T/P and S/L with the ending like .02 only with ending .0 or .5

Because the price change is 0.5 points. If /L is .03 its not working.

Is that the 5 digit problem? How can I fix this problem on my EA?

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);

}

}

 

Hey!

No it is not 5 digit broker problem. It's the problem with step. You need to round lot size as your broker says (to 0.5, so allowed values would be 0.5, 1, 1.5 and so on)

 

Thank you.

How can I do that?

My broker makes me crazy.

 
phil26:
Thank you.

How can I do that?

My broker makes me crazy.

Here is example of lot calculation.

I assumed that if the calculated lot size is = 0.5 i will round it to 0.5 (so if it will be 0.65 or 0.98 i will change it to 0.5). The way you will round it is just your choice it may be 0.5 and 1 instead of 0 and 0.5 like in my example - it depends from your choice.

So here is the code and explenation:

double lotdouble = 1.65;// this is the calculated lot size - for example % of equity

int lotInt = lotdouble;//now we change lot to INT, if our calculated lot size is 1.65, after this conversion it will give us 1. If this would be 2.75 it would give us 2.

double dig = lotdouble-lotInt;//we need to substract the number that we will be rounding. In our example we have 1.65, but we are interested in rounding .65 so we substract full number which is 1 and we get 0.65

Now we are checking the condition, comparing value that we need to round to the step that we need to use.

if(dig<0.5) dig = lotInt;

else dig = lotInt+0.5;

Variable dig is our calculated lot size. In this example calculated result is 1.5 instead of 1.65

 

Thank you very much.

But I do not know where I must insert that?

I get the error "variable not defined"

 

Conditional line in graph

I hope someone can help me. What I try to achieve is a conditional line in a graph i.e. for the Stochastics indicator I only want the base line and the signal line to be drawn when they are rising or if the are above 30. How can I do this?

Do I need to add an if statement in this part of the code:

//---- last counted bar will be recounted

if(counted_bars>0) counted_bars--;

int limit=Bars-counted_bars;

//---- signal line is simple movimg average

for(i=0; i<limit; i++)

SignalBuffer=iMAOnArray(MainBuffer,Bars,DPeriod,0,MODE_SMA,i);

//----

 

Simple question about a piece of code from MA Cross EA

A couple quick questions:

1) The statement:

if( SymPoints == 0.001)[/CODE]checks to see if the double variable 'SymPoints' (the points size) is equal to 0.001.

If true, then these statements:

[CODE]{ SymPoints = 0.01; SymDigits = 3; }

are executed, which change the double variable 'SymPoints' to 0.01, and the int variable ' SymDigits' to 3. This I understand 100%

2) What I don't understand is: what exactly is the purpose of this? Is it because the rates are now quoted with 3 digits (JPY) to the right of the decimals, and 5 decimals (EUR, GBP, etc...) ?

3) If so, then does that mean these statements are telling the program to ignore the last digits to the right (the 3rd or 5th or sub-pips) and read only the 2nd and 4th? (that is, the 'traditional pip' from the older style rates that were quoted with 2 or 4 digits to the right of the decimal)

I'm a bit confused.

 

Need a hand on closing order

I have trouble in closing the order in my EA. for a long order, if the price close below the MovingPeriodFast (a Moving average), then close the order. I just could not figure out what's wrong. Please check it for me. Thanks you very much!!!!!!!!!!!!

for(cnt=0;cnt<total;cnt++) //total = OrdersTotal(); cnt to count how many order in hand

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol())

{

if(OrderType()==OP_BUY) // long position is opened

{

// should it be closed?

if(Open[1]>MovingPeriodFast && Close[1]<MovingPeriodFast) //for a buy order, close the order if the price crossed Moving Average Fast

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

return(0); // exit

}

// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>Point*TrailingStop)

{

if(OrderStopLoss()<Bid-Point*TrailingStop)

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else

{

// should it be closed?

if(Open[1]MovingPeriodFast)

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

return(0); // exit

}

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(Point*TrailingStop))

{

if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

return(0);

}

 

How can I call current spread in MQL4

Hello

I want to add current spread value to ATRstop value for stoploss but I dont know how to call spread in programing. Please somebody help me how to code.

million thanks

mithetnme

Reason: