MQL4 Learning - page 111

 
sjalappat:
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 "

First, instead of writing your own indicator for this, you can use the technical indicator supplied by MQL iMA(), but if you want to learn more about programming take a look at Creation of Custom Indicators - Simple Programs in MQL4 - MQL4 Tutorial - you can also find more articles on maling indicators there.

Klaus

------------------------------

Software and Consulting for MetaTrader4 - MQLware - www.mqlware.com

 

I want this EA with no TP and no SL. Who can help me please?

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!

Just change this :

Ask - StopLoss,

Ask + TakeProfit,

Bid + StopLoss,

Bid - TakeProfit,

to 0

like this:

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

 

@ Kalenzo thanks thats great.

Do you know why the EA does not open the order with TP and SL?

Is it a problem from my broker?

 
phil26:
@ Kalenzo thanks thats great.

Do you know why the EA does not open the order with TP and SL?

Is it a problem from my broker?

Hey!

Yes this may be broker problem. Some brokers requires from you to send order without limit levels (tp = 0, sl = 0) and then after the order is succesfully filled, modify it and set TP and SL levels.

Regards

Kale

 

Thank you. You mean like this? It dont works, I dont know how.

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

{

OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);

OrderModify(ticket,OrderOpenPrice(),TradeStopLoss,TradeTakeProfit,0,Blue);

}

 
phil26:
Thank you. You mean like this? It dont works, I dont know how.

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

{

OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES);

OrderModify(ticket,OrderOpenPrice(),TradeStopLoss,TradeTakeProfit,0,Blue);

}

It does not work because in OrderSend function you put SL and TP values instead of 0

 

I dont know it dont work. I'm still amateur.

If you can show me I will be happy.

 

Here is a little code snippet that you may use to open order with ECN broker:

void openOrderECN(double LTS,int type, double sl,double tp, string description = "" )

{

int ticket = 0;

if( type == OP_SELL )

{

while(true)

{

RefreshRates();

ticket = OrderSend(Symbol(),OP_SELL,LTS,MarketInfo(Symbol(),MODE_BID),0,0,0, description ,0,0,Pink);

if(ticket<=0)

{

Print("SELL ORDER ERROR:"+GetLastError());

}

else

{

if(sl>0 || tp>0)

{

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green);

}

OrderPrint();

break;

}

}

}

else if( type == OP_BUY )

{

while(true)

{

RefreshRates();

ticket = OrderSend(Symbol(),OP_BUY,LTS,MarketInfo(Symbol(),MODE_ASK),0,0,0, description ,0,0,Lime);

if(ticket<=0)

{

Print("BUY ORDER ERROR:"+GetLastError());

}

else

{

if(sl>0 || tp>0)

{

OrderSelect(ticket,SELECT_BY_TICKET);

OrderModify(OrderTicket(),OrderOpenPrice(),sl,tp,0,Green);

}

OrderPrint();

break;

}

}

}

}

 

Where is the worm

Hi,

Here is the beginning of my EA :

extern double Lots=0.01;

extern int TP= 500;

extern int SL= 500;

extern int TP_SL_Increment=100;

extern int Difference=60;

extern double Loss_Lots_Multiply_Factor=2.1;

{

if (l==0){

mtr=1;

GlobalVariableSet("mt_Vager1"+Symbo l()+Period(),mt r);

TP_SL_Inc=TP_SL_Increment;

GlobalVariableSet("l_Vager1"+Symbol ()+Period(),TP_ SL_Increment);

if (OrderSend(Symbol(),OP_BUY,Lots,Nor malizeDouble(As k,Digits),Slippage,NormalizeDouble( Ask-SL*Point,Digits),NormalizeDouble(As k+TP*Point,Digi ts),com,Magic_Number,0,Blue)==-1){

i=GetLastError();

if (i!=1 && i!=0) Alert("Buy Error :"+error(i)+" at "+Symbol()+" "+Period());

}

}

else if (l>0){

if (OrderSend(Symbol(),OP_BUY,l*Loss_L ots_Multiply_Fa ctor,NormalizeDouble(Ask-SL*Point,Digits),Slippage,Normalize Double(Ask-TP_SL_Inc*Point,Digits),NormalizeDo uble(Ask+TP*Poi nt+TP_SL_Inc*Point,Digits),com,Magi c_Number,0,Ligh tBlue)==-1){

i=GetLastError();

if (i!=1 && i!=0) Alert("Buy 1 Error :"+error(i)+" at "+Symbol()+" "+Period());

}else {

TP_SL_Inc=TP_SL_Inc+TP_SL_Increment ;

mtr=mtr+1;

GlobalVariableSet("mt_Vager1"+Symbo l()+Period(),mt r);

GlobalVariableSet("l_Vager1"+Symbol ()+Period(),TP_ SL_Inc);

}

}

}

and I have always the message : "Buy (or Sell) Error : Invalid Stops".

Where is the error ?

Thanks in advance.

Jo

Reason: