MQL4 Learning - page 121

 
mladen:
isNewBar() should be removed outside the init()
int init()

{

ObjectsDeleteAll(); // clear the chart

Comment(""); // clear the chart

}

bool isNewHour()

{

datetime BarTime;

//----

bool res=false;

if (BarTime!=iTime(NULL,PERIOD_H1,0)) {

BarTime=iTime(NULL,PERIOD_H1,0);

res=true;

}

return(res);

}

But if you are looking for a new bar than you should call the isNewBar() from start() to find if a new bar is formed since this way it is doing nothing (it is not referenced or called from anywhere)

With this code i get an alert every tick after i have attached the EA to the chart, so it doesn't work as it's described from christinaLi

Let me try to see if it works if i only have the alert kode in the start.

 

...

It will not work

Add word "static" before the datetime BarTime; (so it should be : static datetime BarTime; ) and it should work OK

Georgebaker:
With this code i get an alert every tick after i have attached the EA to the chart, so it doesn't work as it's described from christinaLi Let me try to see if it works if i only have the alert kode in the start.
 
mladen:
It will not work Add word "static" before the datetime BarTime; (so it should be : static datetime BarTime; ) and it should work OK

I have simplyfied the code now so it should gives me an alert every minute. Now it has stopped give me an alert for every tick, so that part works perfectly, but... when i attach it to a chart no matter the time i also get an alert. How can i avoid that?

#include

#include

int init()

{

ObjectsDeleteAll(); // clear the chart

Comment(""); // clear the chart

}

bool isNewHour()

{

static datetime BarTime;

//----

bool res=false;

if (BarTime!=iTime(NULL,PERIOD_M1,0)) {

BarTime=iTime(NULL,PERIOD_M1,0);

res=true;

}

return(res);

}

// Expert start

int start()

{

if (isNewHour())

Alert("The time is now: ", Minute());

}

 

Asc trend ea modification

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

//| AsctrendBuySellExpert_v1.mq4 code by Newdigital |

//| Forex TSD - forex forum |

//| using Gordago software Forex Software - Gordago |

//| - Asctrend code for this EA was taken from |

//| AscTrend_NonLag EA coded by of Igorad. |

//| - "Non-Trading Hours" on the screen fixing code by Locutus |

//| from Updated DayTradingMM EA |

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

#property copyright "newdigital"

#property link "https://www.forex-tsd.com"

extern int MAGIC = 100111;

extern string PARAMETERS_EXPERT = "PARAMETERS EXPERT";

extern color clOpenBuy = Blue;

extern color clCloseBuy = Aqua;

extern color clOpenSell = Red;

extern color clCloseSell = Violet;

extern color clModiBuy = Blue;

extern color clModiSell = Red;

extern string Name_Expert = "AsctrendBuySellExpert";

extern bool UseSound = False;

extern string NameFileSound = "alert.wav";

extern string PARAMETERS_FILTER = "PARAMETERS FILTER";

extern bool UseHourTrade = False;

extern int FromHourTrade = 6;

extern int ToHourTrade = 18;

extern string PARAMETERS_TRADE = "PARAMETERS TRADE";

extern double Lots = 0.10;

extern int Slippage = 4;

extern double lStopLoss = 1000;

extern double sStopLoss = 1000;

extern double lTakeProfit = 1000;

extern double sTakeProfit = 1000;

extern double lTrailingStop = 1000;

extern double sTrailingStop = 1000;

extern string PARAMETERS_INDICATOR_ONE = "ASCTrend";

extern bool UseASCtrend = True;

extern int RISK = 12;

int asctrend,asctrend1;

int ASCtrend,ASCtrend1;

void deinit() {

Comment("");

}

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

//| |

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

int start(){

if (UseHourTrade){

if((Hour()>=FromHourTrade)&&(Hour()<=ToHourTrade))

Comment("Trading Hours");

else

{

Comment("Non-trading Hours");

return(0);

}

}

if(Bars<100){

Print("bars less than 100");

return(0);

}

if(lStopLoss<10){

Print("StopLoss less than 10");

return(0);

}

if(lTakeProfit<10){

Print("TakeProfit less than 10");

return(0);

}

if(sStopLoss<10){

Print("StopLoss less than 10");

return(0);

}

if(sTakeProfit<10){

Print("TakeProfit less than 10");

return(0);

}

if (UseASCtrend)

{

ASCtrend = ASCTrend(RISK);

bool ASCtrendBuy = ASCtrend>0 && ASCtrend1<0;

bool ASCtrendSell = ASCtrend0;

}

else {ASCtrendBuy = true; ASCtrendSell = true;}

if(AccountFreeMargin()<(1000*Lots)){

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (!ExistPositions()){

if ((ASCtrendBuy)){

OpenBuy();

return(0);

}

if ((ASCtrendSell)){

OpenSell();

return(0);

}

}

if (ExistPositions()){

if(OrderType()==OP_BUY){

if ((ASCtrendSell)){

CloseBuy();

return(0);

}

}

if(OrderType()==OP_SELL){

if ((ASCtrendBuy)){

CloseSell();

return(0);

}

}

}

TrailingPositionsBuy(lTrailingStop);

TrailingPositionsSell(sTrailingStop);

ASCtrend1=ASCtrend;

return (0);

}

bool ExistPositions() {

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {

return(True);

}

}

}

return(false);

}

void TrailingPositionsBuy(int trailingStop) {

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {

if (OrderType()==OP_BUY) {

if (Bid-OrderOpenPrice()>trailingStop*Point) {

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

ModifyStopLoss(Bid-trailingStop*Point);

}

}

}

}

}

}

void TrailingPositionsSell(int trailingStop) {

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

if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {

if (OrderSymbol()==Symbol() && OrderMagicNumber()==MAGIC) {

if (OrderType()==OP_SELL) {

if (OrderOpenPrice()-Ask>trailingStop*Point) {

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

ModifyStopLoss(Ask+trailingStop*Point);

}

}

}

}

}

}

void ModifyStopLoss(double ldStopLoss) {

bool fm;

fm = OrderModify(OrderTicket(),OrderOpenPrice(),ldStopLoss,OrderTakeProfit(),0,CLR_NONE);

if (fm && UseSound) PlaySound(NameFileSound);

}

void CloseBuy() {

bool fc;

fc=OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, clCloseBuy);

if (fc && UseSound) PlaySound(NameFileSound);

}

void CloseSell() {

bool fc;

fc=OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, clCloseSell);

if (fc && UseSound) PlaySound(NameFileSound);

}

void OpenBuy() {

double ldLot, ldStop, ldTake;

string lsComm;

ldLot = GetSizeLot();

ldStop = GetStopLossBuy();

ldTake = GetTakeProfitBuy();

lsComm = GetCommentForOrder();

OrderSend(Symbol(),OP_BUY,ldLot,Ask,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenBuy);

if (UseSound) PlaySound(NameFileSound);

}

void OpenSell() {

double ldLot, ldStop, ldTake;

string lsComm;

ldLot = GetSizeLot();

ldStop = GetStopLossSell();

ldTake = GetTakeProfitSell();

lsComm = GetCommentForOrder();

OrderSend(Symbol(),OP_SELL,ldLot,Bid,Slippage,ldStop,ldTake,lsComm,MAGIC,0,clOpenSell);

if (UseSound) PlaySound(NameFileSound);

}

string GetCommentForOrder() { return(Name_Expert); }

double GetSizeLot() { return(Lots); }

double GetStopLossBuy() { return (Bid-lStopLoss*Point);}

double GetStopLossSell() { return(Ask+sStopLoss*Point); }

double GetTakeProfitBuy() { return(Ask+lTakeProfit*Point); }

double GetTakeProfitSell() { return(Bid-sTakeProfit*Point); }

int ASCTrend( int risk )

{

double smin, smax, bsmin, bsmax;

int len = 3 + 2*risk;

smin=Low[Lowest(NULL,0,MODE_LOW,len,1)];

smax=High;

bsmax = smax-(smax - smin)*(33.0-risk)/100.0;

bsmin = smin+(smax - smin)*(33.0-risk)/100.0;

asctrend = asctrend1;

if(Close[1]>bsmax) asctrend= 1;

if(Close[1]<bsmin) asctrend=-1;

asctrend1 = asctrend;

return(asctrend);

}

In the above EA please modify that Max. Bar Height = 6 pip for place the order.

Advance thanks

mduguna

 
Georgebaker:
I have simplyfied the code now so it should gives me an alert every minute. Now it has stopped give me an alert for every tick, so that part works perfectly, but... when i attach it to a chart no matter the time i also get an alert. How can i avoid that?

#include

#include

int init()

{

ObjectsDeleteAll(); // clear the chart

Comment(""); // clear the chart

}

bool isNewHour()

{

static datetime BarTime;

//----

bool res=false;

if (BarTime!=iTime(NULL,PERIOD_M1,0)) {

BarTime=iTime(NULL,PERIOD_M1,0);

res=true;

}

return(res);

}

// Expert start

int start()

{

if (isNewHour())

Alert("The time is now: ", Minute());

}

[/PHP]

This is how the code should look like to work right.

[PHP]

bool isNewHour()

{

static datetime BarTime;

if (BarTime==0) {BarTime=iTime(NULL,PERIOD_M1,0);}

//----

bool res=false;

if (BarTime!=iTime(NULL,PERIOD_M1,0)) {

BarTime=iTime(NULL,PERIOD_M1,0);

res=true;

}

return(res);

}

 

Yes, this should work now, except I think your alert should alert Hour() instead of Minute(), because your purpose is to alert every hour.

 
christinaLi:
Yes, this should work now, except I think your alert should alert Hour() instead of Minute(), because your purpose is to alert every hour.

Yes you're right. It's only becuase i don't want to wait for an hour to see if it works . That i will change later.

Thanks you for your help.

 

Showing gaps in an indicator

Hi,

I tried the following code to show my gaps:

for(counter = GapsDistance; counter >1; counter--)

{

if((iClose(NULL,PERIOD_D1,counter)-iOpen(NULL,PERIOD_D1,counter-1)) > GapsMin*pips2dbl && (iClose(NULL,PERIOD_D1,counter)-iOpen(NULL,PERIOD_D1,counter-1))<GapsMax*pips2dbl )

{

double low_value = iLow(NULL,PERIOD_D1,iLowest(NULL,PERIOD_D1,MODE_LOW,counter,0));

if(low_value > iClose(NULL,PERIOD_D1,counter))

{

ObjectCreate("Gaps_"+counter, OBJ_RECTANGLE, 0, iTime(NULL,PERIOD_D1,counter), iOpen(NULL,PERIOD_D1,counter-1), Time[0], iClose(NULL,PERIOD_D1,counter) );

ObjectSet("Gaps_"+counter, OBJPROP_COLOR, Gray);

ObjectSet("Gaps_"+counter, OBJPROP_RAY, true );

ObjectSet("Gaps_"+counter, OBJPROP_RAY, False);

}

}

if((iOpen(NULL,PERIOD_D1,counter-1)-iClose(NULL,PERIOD_D1,counter)) > GapsMin*pips2dbl && (iClose(NULL,PERIOD_D1,counter)-iOpen(NULL,PERIOD_D1,counter-1))<GapsMax*pips2dbl)

{

double high_value = iHigh(NULL,PERIOD_D1,iHighest(NULL,PERIOD_D1,MODE_HIGH,counter,0));

if(high_value < iClose(NULL,PERIOD_D1,counter))

{

ObjectCreate("Gaps_"+counter, OBJ_RECTANGLE, 0, iTime(NULL,PERIOD_D1,counter), iOpen(NULL,PERIOD_D1,counter-1), Time[counter]+line_adjustment+text_shift, iClose(NULL,PERIOD_D1,counter) );

ObjectSet("Gaps_"+counter, OBJPROP_COLOR, Gray);

ObjectSet("Gaps_"+counter, OBJPROP_RAY, true );

ObjectSet("Gaps_"+counter, OBJPROP_RAY, False);

}

}

}

My code doesn't work, but I can't understand how.

Does anybody see an error??

 

Calculating an indicator

Hi,

I tried calculate my MAs and my there indicators only with the prices from 9:00-17:30.

Does anybody has an tip or an code example?

 

Ema time limited ...

The easiest to do it would be EMA

Here is how it looks with the conditions you are trying to apply :

The others would need some custom "price extraction" function

sunshineh:
Hi,

I tried calculate my MAs and my there indicators only with the prices from 9:00-17:30.

Does anybody has an tip or an code example?
Reason: