How to code? - page 106

 

That works for price. He's wanting to find the highest and lowest of an idicator.

Lux

 

Hello,

You can try this :

....

int highest=0, lowest=0, bar=WindowBarsPerChart();

for(int shift=0;shift<bar;shift++)

{

double indie=iCustom(.........,shift);

if(highest<indie) highest=indie;

if(lowest==0) lowest=indie;

if(lowest>indie) lowest=indie;

}

.....

note: this code calculating the current open candle too, if you want to calculate the closed candle only, use shift=1.

Hope this helps,

Ardie

 
:: iBarShift will find for you the bar that start on that day/hour... or also the end bar for that day:hour... (depends what timeframe or chart you want to start to find your high/low).

int iBarShift( string symbol, int timeframe, datetime time, bool exact=false)

next...

:: use those bar positions to find the results of iHighest and iLowest

int iHighest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

int iLowest( string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

:: results & done , don't use any loops in this!

 

Programmaticaly refresh the repaint indicator

Hello,

I'm looking to find a way to refresh a repaint indicator for every x minutes.

The only way to refresh it currently, is to click in the indicator on the chart and then click "ok". Can we automate it with MQL4 code?

I found something on codersguru's site, Programmatically Refresh your charts | www.metatrader.info, but it seems not working for me. Or is there anybody has try it and get different result (working)?

Thank you

 

Sorry for my Englsih.

I want count number times condition is true only once per bar. Computer add up many times per bar. What wrong I do?

 
IngvarDagmar:
Sorry for my Englsih. I want count number times condition is true only once per bar. Computer add up many times per bar. What wrong I do?

Use a function like this...

bool NewBar() {

static datetime LastTime = 0;

if (Time[0] != LastTime) {

LastTime = Time[0];

return (true);

} else

return (false);

}

[/php]

Then put an if statement round your main code, like...

[php]

if(NewBar() == true){

// do the main processing here

}

Hope that helps.

Lux

 

That was nice of you, Lux.

I found this:

Only process each bar once - MQL4 forum

Automated 2008.01.15 18:54 You could execute your code at the very first tick of a new bar ( i.e. right after the previous bar has closed ).

Here's a function that will return TRUE if a new bar has just formed:

// This function returns TRUE at the very first tick a bar i.e. after the previous bar has just closed

bool NewBar()

{

if(PreviousBarTime<Time[0])

{

PreviousBarTime = Time[0];

return(true);

}

return(false); // in case if - else statement is not executed

} you need to declare datetime PreviousBarTime at the beginning of your EA...

then in your code you could just use

if ( NewBar() )

{

...... code you need to be executed after a bar has closed here ....

} thank you

automatedfx@gmail.com

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

I noticed you used STATIC... I looked it up... what's the advantage of using STATIC vs a global variable?

 

multiple entry ea

please help. i would like to find or need help creating an ea with the following input parameters. four separate trade entries each having no. of lots, stop loss, trailing stop, break even, and profit target. trades will open upon clicking on expert advisor button.

thank you

 

Need help on trailing stop option

I found this EA at MQL4 forum, quite an interesting EA.

could somebody help to add a trailing stop option which can set trailing stop only activate after the hit profit value that I set?

themastermind2.mq4

Files:
 

Hi all..

i have a problem with the EA that i wrote.. actually, the EA based on MACD indicator.. when the MACD become like 'n' shape, open post Sell, and when MACD become like 'u' shape, the EA will open Buy..

the problem is, the EA didn't open any post.. after i doing some backtest also, there no open post by this EA.. can someone please help me find what's wrong with the code??

here is the code..

extern double TakeProfit = 20;

extern double Lots = 0.1;

extern double StopLoss = 20;

extern double MagicNumber = 17384;

extern int FastEMA=12;

extern int SlowEMA=26;

extern int SignalSMA=9;

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

//| expert initialization function |

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

double MacdBuffer1[];

double MacdBuffer2[];

double MacdBuffer3[];

double MacdBuffer4[];

double MacdBuffer5[];

double MacdBuffer6[];

double MacdBuffer7[];

double MacdBuffer8[];

int init()

{

//----

//SetIndexBuffer(0, lag1_buffer);

//SetIndexBuffer(1, lag2_buffer);

//----

return(0);

}

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

//| expert deinitialization function |

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

int deinit()

{

//----

//----

return(0);

}

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

//| expert start function |

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

int start()

{

int limit;

int counted_bars=IndicatorCounted();

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

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

//---- macd counted in the 1-st buffer

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

MacdBuffer1=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);

MacdBuffer2=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i-1)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i-1);

MacdBuffer3=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i+1)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i+1);

MacdBuffer4=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i-2)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i-2);

MacdBuffer5=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i+2)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i+2);

MacdBuffer6=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i-3)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i-3);

MacdBuffer7=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i+3)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i+3);

/*Alert( "MacdBuffer7 =",MacdBuffer7);

Alert( "MacdBuffer5 =",MacdBuffer5);

Alert( "MacdBuffer3 =",MacdBuffer3);

Alert( "MacdBuffer1 =",MacdBuffer1);

Alert( "MacdBuffer2 =",MacdBuffer2);

Alert( "MacdBuffer4 =",MacdBuffer4);

Alert( "MacdBuffer6 =",MacdBuffer6);*/

//----

int ticket_buy, ticket_sell, total;

total=OrdersTotal();

//MACD become 'u' shape

if (MacdBuffer7>MacdBuffer5&&MacdBuffer5>MacdBuffer3&&MacdBuffer3>MacdBuffer1

&&MacdBuffer1<MacdBuffer2&&MacdBuffer2<MacdBuffer4&&MacdBuffer4<MacdBuffer6)

{

if (total < 1) {

ticket_buy=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"scalp 1 min - buy",MagicNumber,0,Green);

if(ticket_buy>0)

{

if(OrderSelect(ticket_buy,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

} else {

}

}

//MACD become 'n' shape

if(MacdBuffer7<MacdBuffer5&&MacdBuffer5<MacdBuffer3&&MacdBuffer3<MacdBuffer1

&&MacdBuffer1>MacdBuffer2&&MacdBuffer2>MacdBuffer4&&MacdBuffer4>MacdBuffer6)

{

if (total < 1) {

ticket_sell=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"scalp 1 min - sell",MagicNumber,0,Red);

if(ticket_sell>0)

{

if(OrderSelect(ticket_sell,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

} else {

}

}

//----

return(0);

}

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

hope someone can help me solve the problem.. i'm not a good in programming codes.. thanks..

Reason: