How to code? - page 293

 
lincoln2012:
I'm trying to learn MQ4. My first simple program is supposed to pop a alert when 2 MAs cross. I can't figure out why

this code is not working.

Any help will be really appreciated! Thanks in advance.

extern int Period_MA1 = 5;

extern int Period_MA2 = 1;

double MA1;

double MA2;

int init()

{

return(0);

}

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

int deinit()

{

return(0);

}

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

int start()

{

//--------------------------------------------------------------------

MA1=iMA(NULL,0,Period_MA1,0,MODE_SMA,PRICE_CLOSE,0);

MA2=iMA(NULL,0,Period_MA2,0,MODE_SMA,PRICE_CLOSE,0);

//--------------------------------------------------------------------

if (MA2 == MA1 )

Alert("Price is the same(",Period_MA2,").");// Alert

return;

}

Hello Lincoln2012,

Might check out the code of this Ma Cross

Files:
macross-1.mq4  7 kb
 
lincoln2012:
I'm trying to learn MQ4. My first simple program is supposed to pop a alert when 2 MAs cross. I can't figure out why

this code is not working.

}

The key to code a cross of any 2 indicator lines is this concept:

1. If current bar line1>line2, and previous bar line1<line2, then there is a Up cross.

2. If current bar line1line2, then there is a Down cross.

 
mj10:
hi

well i was doing little programing in the Mt4 its fun but i m not pro just simple editing of old indicator. Ok i want to make a simple indicator but don't know how to do it.

its simple indicator it simply check for the value if the value of the all the varible are same it draw that point where the value of the all the variable match.

my simple solution

let suppose we got variables

double movingavg1;

double movingavg12;

double movingavg13;

double movingavg14;

double movingavg15;

double movingavg16;

double movingavg17;

double movingavg18;

double movingavg19;

double movingavg10;

double movingavgall;

we declare the variable i am just giving an example i am using iStochastic(1), but we can use for any indicator.

movingavg12=iStochastic(1)

movingavg13=iStochastic(2)

.

.

.

so on

now

if (movingavg12=movingavg13=movingavg14,....) (put value in in variable "movingavgall") //put the value which is equal for vria

draw movingavgall on bars

Application

let suppose i want to check where the moving average 3 and moving average 5 have the same value

even this indicator can be used with RSI where the RSI 1 and RSI 2 value = 80

and many more

even if we add MTF that will be added bonus

looks simple in writing but i was not able to find the solution hope someone can help

hope you people understand what i am saying

thanks

Once you have values to compare you would use code similar to the following.

Checking for values being equal uses ==. A single = is used to assign a value to a variable.

Checking more than 1 set must use && to signify AND

movingavgall = 0;

if ((a == b) && (b == c) && (c == d)) movingavgall = a;

You can then check if movingavgall has a value with

if (movingavgall != 0)

{

// do something here

}

Do a search for metatrader programming using Google.

You should find several good tutorials for programming in MT4.

A good book on C programming will also help.

Robert Hill aka MrPip

 

simplify this code

hi everyone!

i'm trying to semplify this code using stringconcatenate command from this:

ObjectSetText("uitog1v", StringConcatenate("/\\ ", uitog1v, "%"),12, hr1v, DodgerBlue );

to this:

ObjectSetText("uitog1v", StringConcatenate("/\\ ", uitog1v, "%","/\\ ", uitog2v, "%","/\\ ", uitog3v, "%"),12, StringConcatenate(hr1v,hr2v,hr3v), DodgerBlue );

Hope someone could help me.

Best regards

doc

trend_alexcud_v_2men.mq4

Files:
 

...

Doc

If you need all those elements, there is no simpler way of writing it

dr.house7:
hi everyone!

i'm trying to semplify this code using stringconcatenate command from this:

ObjectSetText("uitog1v", StringConcatenate("/\\ ", uitog1v, "%"),12, hr1v, DodgerBlue );

to this:

ObjectSetText("uitog1v", StringConcatenate("/\\ ", uitog1v, "%","/\\ ", uitog2v, "%","/\\ ", uitog3v, "%"),12, StringConcatenate(hr1v,hr2v,hr3v), DodgerBlue );

Hope someone could help me.

Best regards

doc

trend_alexcud_v_2men.mq4
 

How to: Alert every 60 sec

How can I make some code that e.g. gives me an alert every 60 seconds?

I have created this code below, but it gives me an alert every minute from when I attach it to the chart and I want it to follow the broker time so it alert me for e.g. every 60 seconds.

#include

#include

// exported variables

// local variables

double PipValue=1; // this variable is here to support 5-digit brokers

bool Terminated = false;

string LF = "\n"; // use this in custom or utility blocks where you need line feeds

int NDigits = 4; // used mostly for NormalizeDouble in Flex type blocks

int ObjCount = 0; // count of all objects created on the chart, allows creation of objects with unique names

int ExpectedTime2 = 0;

int init()

{

NDigits = Digits;

ObjectsDeleteAll(); // clear the chart

Comment(""); // clear the chart

}

// Expert start

int start()

{

if (Bars < 10)

{

Comment("Not enough bars");

return (0);

}

if (Terminated == true)

{

Comment("EA Terminated.");

return (0);

}

OnEveryTick1();

}

void OnEveryTick1()

{

if (true == false && true) PipValue = 10;

if (true && (NDigits == 3 || NDigits == 5)) PipValue = 10;

OncePerMinutes2();

}

void OncePerMinutes2()

{

int datetime800 = TimeLocal();

if (ExpectedTime2 == 0 || datetime800 > ExpectedTime2 + 60)

{

ExpectedTime2 = datetime800 + 60 * 1; // reset paused time

}

if (datetime800 >= ExpectedTime2 && datetime800 < ExpectedTime2 + 60)

{

ExpectedTime2 = datetime800 + 60 * 1;

Alert3();

}

}

void Alert3()

{

Alert("One minute gone");

}

int deinit()

{

if (true) ObjectsDeleteAll();

}

 
mladen:
Doc If you need all those elements, there is no simpler way of writing it

Uhm, bad luck this time, thanks anyway.

best regards

doc

 

There is a problem with the second StringConcatenate call.

You are adding together 3 different font names and there will be no valid resulting name.

Robert

dr.house7:
hi everyone!

i'm trying to semplify this code using stringconcatenate command from this:

ObjectSetText("uitog1v", StringConcatenate("/\\ ", uitog1v, "%"),12, hr1v, DodgerBlue );

to this:

ObjectSetText("uitog1v", StringConcatenate("/\\ ", uitog1v, "%","/\\ ", uitog2v, "%","/\\ ", uitog3v, "%"),12, StringConcatenate(hr1v,hr2v,hr3v), DodgerBlue );

Hope someone could help me.

Best regards

doc

trend_alexcud_v_2men.mq4
 

Need help with EA ... trades not being closed in tester

I am working on an EA, but no matter what I do, I can't make it close trades. It opens them just fine, but when I want them closed, the EA will not close them.

I'll post part of my EA code so maybe one of you experienced coders can figure out why I'm having this problem. The vars 'buyTrade' and 'sellTrade' are global to whole EA.

NOTE: I only want ONE trade open at any given time, either long or short. The trades are "always in" so when a long signal comes, the existing short should be closed and vice versa.

It is NOT doing this. What it is doing is opening trades and then opens more thrades ... and opens more ... and keeps them open until they all eventually hit their stops.

Thanks in advance

//---- Is there a new signal?

// Buy signal ---------------------------------------

//if(buyTrade==false || sellTrade==false) {

if (STC_VIDYAprev = 10.00) {

if (sellTrade==true) {

for (int i = OrdersTotal() - 1; i >= 0; i--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderType() == OP_BUY )

{ int OldTicket=OrderTicket(); double lots=OrderLots();}

RefreshRates(); //OrderSelect() needed ?

double symAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits );

bool closeSellOrder = OrderClose( OldTicket, lots, symAsk, 0, CLR_NONE );

}

sellTrade = false;

}

if(buyTrade==false) {

EnterLong(Sym, Lots, "");

buyTrade = true;

}

}

//else

//return(0);

// Sell signal ---------------------------------------

if (STC_VIDYAprev > 90.00 && STC_VIDYAcur <= 90.00 ) {

if (buyTrade==true) {

for (int j = OrdersTotal() - 1; j >= 0; j--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderType() == OP_SELL )

{ int oldTicket=OrderTicket(); double lotts=OrderLots();}

RefreshRates(); //OrderSelect() needed ?

double symBid = NormalizeDouble( MarketInfo( Sym, MODE_BID ), SymDigits );

bool closeBuyOrder = OrderClose( oldTicket, lotts, symBid, 0, CLR_NONE );

}

buyTrade = false;

}

if(sellTrade==false) {

EnterShrt( Sym, Lots, "");

sellTrade = true;

}

}

//else

//return(0);
 

...

Since that is a partial code : try replacing symBid with symAsk in closing sell and symAsk with symBid when closing buy order (invert the closing prices, you can not close a sell at bid and you can not close a buy at ask price)

outofdebt:
I am working on an EA, but no matter what I do, I can't make it close trades. It opens them just fine, but when I want them closed, the EA will not close them.

I'll post part of my EA code so maybe one of you experienced coders can figure out why I'm having this problem. The vars 'buyTrade' and 'sellTrade' are global to whole EA.

NOTE: I only want ONE trade open at any given time, either long or short. The trades are "always in" so when a long signal comes, the existing short should be closed and vice versa.

It is NOT doing this. What it is doing is opening trades and then opens more thrades ... and opens more ... and keeps them open until they all eventually hit their stops.

Thanks in advance

//---- Is there a new signal?

// Buy signal ---------------------------------------

//if(buyTrade==false || sellTrade==false) {

if (STC_VIDYAprev = 10.00) {

if (sellTrade==true) {

for (int i = OrdersTotal() - 1; i >= 0; i--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderType() == OP_BUY )

{ int OldTicket=OrderTicket(); double lots=OrderLots();}

RefreshRates(); //OrderSelect() needed ?

double symAsk = NormalizeDouble( MarketInfo( Sym, MODE_ASK ), SymDigits );

bool closeSellOrder = OrderClose( OldTicket, lots, symAsk, 0, CLR_NONE );

}

sellTrade = false;

}

if(buyTrade==false) {

EnterLong(Sym, Lots, "");

buyTrade = true;

}

}

//else

//return(0);

// Sell signal ---------------------------------------

if (STC_VIDYAprev > 90.00 && STC_VIDYAcur <= 90.00 ) {

if (buyTrade==true) {

for (int j = OrdersTotal() - 1; j >= 0; j--) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderType() == OP_SELL )

{ int oldTicket=OrderTicket(); double lotts=OrderLots();}

RefreshRates(); //OrderSelect() needed ?

double symBid = NormalizeDouble( MarketInfo( Sym, MODE_BID ), SymDigits );

bool closeBuyOrder = OrderClose( oldTicket, lotts, symBid, 0, CLR_NONE );

}

buyTrade = false;

}

if(sellTrade==false) {

EnterShrt( Sym, Lots, "");

sellTrade = true;

}

}

//else

//return(0);
Reason: