How to code? - page 18

 

Michel,

Thank you very much for taking the time to look at the code and reply. Sometimes all it takes is another pair of eyes! You were right that the error was division by 0 with a missing bar. All taken care of now.

BW

 

Code explaination - Ind-Fractals-1

can anyone explain how the fractals are determined in this (https://c.mql5.com/forextsd/forum/165/ind-fractals-1.mq4) indicator. i've noticed that like normal fractals they there is a delay between the turning point and the actual fractal being drawn, however i'd like to know how long that delay is. i think a 15min fractal is drawn 40-50 1min bars after the point is established. i've also noticed that the color of fractals change (though their position does not) as some 15min fractals become 1hr fractals.

 

how to code the following?

Hi all,

I'm a newcomer to metatrader 4. i know not much about coding in mt4.

i want metatrader to give out alert when

1. 30 period simple moving average rises above 60 period simple moving average.

2. 30 period simple moving average falls below 60 period simple moving average.

Can anyone help me post the code here??

thanks a lot

 
adamk203:
Hi all,

I'm a newcomer to metatrader 4. i know not much about coding in mt4.

i want metatrader to give out alert when

1. 30 period simple moving average rises above 60 period simple moving average.

2. 30 period simple moving average falls below 60 period simple moving average.

Can anyone help me post the code here??

thanks a lot

Why not use this? put in your MA and choose 0 for simple, gives you lines and arrows

Files:
 

thx a lot~~

 
increase:
Why not use this? put in your MA and choose 0 for simple, gives you lines and arrows

Hi, I would like to make the signal come up when the 2 clean candle has been finished after the cross over.thx in advance

Files:
gbp_4h.gif  19 kb
 

pls help correct my code

First of all, I believe this code is very handy, many ppl will love it. So please help me improve it.

In fact , this code already works great. But I'm just tired of the simple Alert popup, with NO Symbol shown. So I changed/added some lines to try , which is the %%%%%%%%% part.

I don't know why, print, comment... whatever, nothing happens.

And, weirdly, you can see in the "----alert----" part, I changed UPBREAK alert into something else, but it still shows "UP BREAK". ---- I'm sure everytime I rewrite the code, I compile it, delete it from chart and load it again.

Any idea?

#property indicator_chart_window

#import "usr32.dll" // %%%%%%%%%%

int MessageBoxA(int hWnd,string lpText,string lpCaption,int uType); //%%%%%%%%%%

#import //%%%%%%%%%%

int PrevAlertTime = 0;

int init()

{

return(0);

}

int deinit()

{

return(0);

}

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

int start()

{

int i=ObjectsTotal();

int redtotal=0,j,typ1;

double price1,price2,up,down;

color col;

string name1,name2,nameup,namedown,warn1,sym=Symbol();

warn1=sym + "Break Up"; //%%%%%%%%%%

Print(sym); //%%%%%%%%%%%

//----make sure exactly 2 red lines on chart---------------------

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

if(ObjectGet(ObjectName(j),OBJPROP_COLOR)==255 && ObjectType(ObjectName(j))==OBJ_TREND)

{ redtotal++;

if (redtotal==1) name1=ObjectName(j);

if (redtotal==2) name2=ObjectName(j);

}

if (redtotal==2)

{

if(ObjectFind("remind")==0) ObjectDelete("remind");

}

else

{

ObjectCreate("remind",OBJ_LABEL,0,0,0);

ObjectSet("remind", OBJPROP_XDISTANCE,0);

ObjectSet("remind", OBJPROP_YDISTANCE,20);

ObjectSetText("remind", "Keep exactly 2 red lines on chart" ,16,"Times New Roman",Blue);

return(0); // question here: what should I use, 0 or -1 ?

}

//--------decide which line is up / down ---------------------

price1=NormalizeDouble(ObjectGetValueByShift(name1,1),

MarketInfo(Symbol(),MODE_DIGITS));

price2=NormalizeDouble(ObjectGetValueByShift(name2,1),

MarketInfo(Symbol(),MODE_DIGITS));

if (price2>=price1)

{

nameup=name2; namedown=name1;

up=price2; down=price1;

}

else

{ nameup=name1; namedown=name2;

up=price1; down=price2;

}

//-------- alert ----------------------------------------------

if (TimeCurrent() - PrevAlertTime > Period()*60/3)

{

if (Close[1]up)

{ MessageBoxA(NULL,"tupole","11111",0); // %%%%%%%%%%

Print(Symbol()); // %%%%%%%%%%

PrevAlertTime = TimeCurrent();

}

if (Close[1]>=down && Close[0]<down)

{ Alert("DOWN BREAK",Symbol());

PrevAlertTime = TimeCurrent();

}

}

return(0);

}

 

I found what's wrong:

Usually, I create or edit a code by: "Metaeditor---file---new/open..."

But I just find out: after attach to chart, and then some changes in this code, finally more changes do not affect the chart anymore (surely I compiled !)----- but "Metaeditor--navigator--files---my code..." do ! That's weird, they should be the same code!

 

Help with trail code

void TrailIt( int byPips ) // based on trailing stop code from MT site... thanks MT!

{

if (byPips >=5)

{

for (int i = 0; i < OrdersTotal(); i++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == uniqueGridMagic) || (OrderComment() == GridName)) ) // only look if mygrid and symbol...

{

if (OrderType() == OP_BUY) {

//if (Bid > (OrderValue(cnt,VAL_OPENPRICE) + TrailingStop * Point)) {

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

// break;

//}

if (Bid - OrderOpenPrice() > byPips * MarketInfo(OrderSymbol(), MODE_POINT)) {

if (OrderStopLoss() < Bid - byPips * MarketInfo(OrderSymbol(), MODE_POINT)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Bid - byPips * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);

}

}

} else if (OrderType() == OP_SELL) {

if (OrderOpenPrice() - Ask > byPips * MarketInfo(OrderSymbol(), MODE_POINT)) {

if ((OrderStopLoss() > Ask + byPips * MarketInfo(OrderSymbol(), MODE_POINT)) ||

(OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(),

Ask + byPips * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);

}

}

}

}

}

}

} // proc TrailIt()

That is the code. As seen in the attachment on one order, it will modify the stop but will also modify it in the wrong direction and I cant fix it. I would really appreciate the help in finding the solution!!

Files:
ordermodify.txt  40 kb
 

Try this. It looks like you're possibly trying to modify orders with different symbols. If so, the Bid and Ask will be pulling market data only from the symbol of the Chart window that the EA is attached. Using MarketData(symbol, MODE_BID) is the way to get the appropriate data.

void TrailIt( int byPips ) // based on trailing stop code from MT site... thanks MT!

{

if (byPips >=5)

{

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

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

double bid=MarketInfo(OrderSymbol(),MODE_BID);

double ask=MarketInfo(OrderSymbol(),MODE_ASK);

if ( OrderSymbol()==Symbol() && ( (OrderMagicNumber() == uniqueGridMagic) || (OrderComment() == GridName)) ) // only look if mygrid and symbol...

{

if (OrderType() == OP_BUY)

{

if (bid - OrderOpenPrice() >= byPips * MarketInfo(OrderSymbol(), MODE_POINT))

{

if (OrderStopLoss() < bid - byPips * MarketInfo(OrderSymbol(), MODE_POINT)|| (OrderStopLoss() == 0))

{

OrderModify(OrderTicket(), OrderOpenPrice(), bid - byPips * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);

}

}

}

else if (OrderType() == OP_SELL)

{

if (OrderOpenPrice() - ask >= byPips * MarketInfo(OrderSymbol(), MODE_POINT))

{

if ((OrderStopLoss() > ask + byPips * MarketInfo(OrderSymbol(), MODE_POINT)) || (OrderStopLoss() == 0))

{

OrderModify(OrderTicket(), OrderOpenPrice(),ask + byPips * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);

}

}

}

}

}

}

return;

}