How to code? - page 269

 

Dema Variable incorrectly set

I am not able to set the DEMA Variables' section correctly, its syntaxes. The DEMA1, DEMA2,DEMA3 codes are correct.

Please help in correcting the DEMA Variable section.

if (TradeOrAlert) {

if (DEMA1)>(DEMA2) {CLOSESHORT(Symbol()) ;}

if (DEMA1)<(DEMA2) {CLOSELONG(Symbol()) ;}

if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3)) {BUY(Symbol(), B_Symbol_LS_0, B_Symbol_TP_0, B_Symbol_SL_0, B_Symbol_TS_0, "if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3))";}

if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3)) {SELL(Symbol(), S_Symbol_LS_0, S_Symbol_TP_0, S_Symbol_SL_0, S_Symbol_TS_0, "if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3))";}

CheckTrailingStop(Symbol(), 0, MagicNumberLong, B_Symbol_TS_0);

CheckTrailingStop(Symbol(), 1, MagicNumberShort, S_Symbol_TS_0);

} else {

if (DEMA1)>(DEMA2) {MyAlert("if (DEMA1)>(DEMA2)", "CLOSESHORT(Symbol(),0.1,100,15,17)", Symbol());}

if (DEMA1)<(DEMA2) {MyAlert("if (DEMA1)<(DEMA2)", "CLOSELONG(Symbol(),0.1,100,15,17)", Symbol());}

if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3)) {MyAlert("if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3))", "BUY(Symbol(),0.1,100,15,17)", Symbol());}

if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3)) {MyAlert("if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3))", "SELL(Symbol(),0.1,100,15,17)", Symbol());}

}

g_bars_276 = Bars;

return (0);

}

//----DEMA VARIABLES----Not properly set, help needed-----

int init() {

double DEMA1 = (iMA(NULL,NULL,4,0,MODE_EMA,PRICE_CLOSE,0))*2 - (iMA(NULL,NULL,4,0,MODE_EMA,iMA(NULL,NULL,4,0,MODE_EMA,PRICE_CLOSE,0),0));

return(DEMA1);

}

int start() {

double DEMA2 = (iMA(NULL,NULL,6,0,MODE_EMA,PRICE_CLOSE,0))*2 - (iMA(NULL,NULL,6,0,MODE_EMA,iMA(NULL,NULL,6,0,MODE_EMA,PRICE_CLOSE,0),0));

return(DEMA2);

}

int start() {

double DEMA3 = (iMA(NULL,NULL,12,0,MODE_EMA,PRICE_CLOSE,0))*2 - (iMA(NULL,NULL,12,0,MODE_EMA,iMA(NULL,NULL,12,0,MODE_EMA,PRICE_CLOSE,0),0));

return(DEMA3);

}

//

 

Dema Variable Set Incorrectly

I am facing the problem of setting the syntaxes correctly in the "DEMA VARIABLES" section of the below code.

Also do tell what to include after "#include " section with respect to Dema Variables.

I get the errors

'DEMA1' - variable not defined.

'DEMA2' - variable not defined.

'DEMA3' - variable not defined.

Please help.

if (TradeOrAlert) {

if (DEMA1)>(DEMA2) {CLOSESHORT(Symbol()) ;}

if (DEMA1)<(DEMA2) {CLOSELONG(Symbol()) ;}

if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3)) {BUY(Symbol(), B_Symbol_LS_0, B_Symbol_TP_0, B_Symbol_SL_0, B_Symbol_TS_0, "if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3))";}

if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3)) {SELL(Symbol(), S_Symbol_LS_0, S_Symbol_TP_0, S_Symbol_SL_0, S_Symbol_TS_0, "if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3))";}

CheckTrailingStop(Symbol(), 0, MagicNumberLong, B_Symbol_TS_0);

CheckTrailingStop(Symbol(), 1, MagicNumberShort, S_Symbol_TS_0);

} else {

if (DEMA1)>(DEMA2) {MyAlert("if (DEMA1)>(DEMA2)", "CLOSESHORT(Symbol(),0.1,100,15,17)", Symbol());}

if (DEMA1)<(DEMA2) {MyAlert("if (DEMA1)<(DEMA2)", "CLOSELONG(Symbol(),0.1,100,15,17)", Symbol());}

if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3)) {MyAlert("if ((DEMA1)>(DEMA2)) && ((DEMA2)>(DEMA3))", "BUY(Symbol(),0.1,100,15,17)", Symbol());}

if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3)) {MyAlert("if ((DEMA1)<(DEMA2)) && ((DEMA2)<(DEMA3))", "SELL(Symbol(),0.1,100,15,17)", Symbol());}

}

g_bars_276 = Bars;

return (0);

}

//----DEMA VARIABLES----Not properly set, help needed-----

int start() {

double DEMA1 = (iMA(NULL,NULL,4,0,MODE_EMA,PRICE_CLOSE,0))*2 - (iMA(NULL,NULL,4,0,MODE_EMA,iMA(NULL,NULL,4,0,MODE_EMA,PRICE_CLOSE,0),0));

return(DEMA1);

}

int start() {

double DEMA2 = (iMA(NULL,NULL,6,0,MODE_EMA,PRICE_CLOSE,0))*2 - (iMA(NULL,NULL,6,0,MODE_EMA,iMA(NULL,NULL,6,0,MODE_EMA,PRICE_CLOSE,0),0));

return(DEMA2);

}

int start() {

double DEMA3 = (iMA(NULL,NULL,12,0,MODE_EMA,PRICE_CLOSE,0))*2 - (iMA(NULL,NULL,12,0,MODE_EMA,iMA(NULL,NULL,12,0,MODE_EMA,PRICE_CLOSE,0),0));

return(DEMA3);

}

//

 

MA+Stoch Alert...please

can you help me build indicator : MA+stoch with alert

stoch(5,3,3);MA(8);MA(15);MA(30)....

alert when MA(8)>MA(15)>MA(30) and %K cross above %D but less than level 50

alert when MA(8)<MA(15)<MA(30) and %D cross above %K but more than level 50

thanks you very much

my english is Foreign Language

sympathetically!

 
lequoctrieu:
can you help me build indicator : MA+stoch with alert

stoch(5,3,3);MA(8);MA(15);MA(30)....

alert when MA(8)>MA(15)>MA(30) and %K cross above %D but less than level 50

alert when MA(8)<MA(15)<MA(30) and %D cross above %K but more than level 50

thanks you very much

my english is Foreign Language

sympathetically!

Hi,

code snippet from one of my indis, you have to adapt it to your needs:

extern bool AlertON=false;

extern bool EmailON=true;

extern bool SoundON=false;

extern bool VoiceON=false;

extern bool SMSON=false;

extern string ExtSoundFileName = "";//external soundfiles required, than delete

FastMA = fastMAnow;

SlowMA = slowMAnow;

if ((fastMAnow >= slowMAnow) && (fastMAprevious < slowMAprevious))

{

if (i == 1 && flagval1==0)

{

flagval1=1;

flagval2=0;

if (AlertON==true) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());

if (EmailON==true) SendMail("2754 BUY-ALP EUR","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SMSON==true) SendMail("2754 BUY-ALP EUR","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SoundON==true) PlaySound(ExtSoundFileName);//peep etc

if (VoiceON==true) PlaySound(ExtSoundFileName);// own speech wav

}

CrossUp = Low - Range*0.5;

}

else if ((fastMAnow slowMAprevious))

{

if (i == 1 && flagval2==0)

{

flagval2=1;

flagval1=0;

if (AlertON==true) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());

if (EmailON==true) SendMail("2754 SELL-ALP EUR","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SMSON==true) SendMail("2754 SELL-ALP EUR","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SoundON==true) PlaySound(ExtSoundFileName);//peeps etc

if (VoiceON==true) PlaySound(ExtSoundFileName);// own speech wav

}

CrossDown = High + Range*0.5;

}

}

return(0);

}

 

MA+Stoch Alert...please

ixbone:
Hi,

code snippet from one of my indis, you have to adapt it to your needs:

extern bool AlertON=false;

extern bool EmailON=true;

extern bool SoundON=false;

extern bool VoiceON=false;

extern bool SMSON=false;

extern string ExtSoundFileName = "";//external soundfiles required, than delete

FastMA = fastMAnow;

SlowMA = slowMAnow;

if ((fastMAnow >= slowMAnow) && (fastMAprevious < slowMAprevious))

{

if (i == 1 && flagval1==0)

{

flagval1=1;

flagval2=0;

if (AlertON==true) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());

if (EmailON==true) SendMail("2754 BUY-ALP EUR","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SMSON==true) SendMail("2754 BUY-ALP EUR","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SoundON==true) PlaySound(ExtSoundFileName);//peep etc

if (VoiceON==true) PlaySound(ExtSoundFileName);// own speech wav

}

CrossUp = Low - Range*0.5;

}

else if ((fastMAnow slowMAprevious))

{

if (i == 1 && flagval2==0)

{

flagval2=1;

flagval1=0;

if (AlertON==true) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period());

if (EmailON==true) SendMail("2754 SELL-ALP EUR","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SMSON==true) SendMail("2754 SELL-ALP EUR","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period());

if (SoundON==true) PlaySound(ExtSoundFileName);//peeps etc

if (VoiceON==true) PlaySound(ExtSoundFileName);// own speech wav

}

CrossDown = High + Range*0.5;

}

}

return(0);

}

Thank a lot...ixbone

I write code very bad...So,I can't understand your code....you can help me to build indicator(.mq4) in order to use

if so, I thank you very much

wish you a good working day!

 

How do i check if current bar high is 2 pips greater that previous bars high?

Sorry wrong place for this question.

Please delete this admin.

Thanks!

 

Partially Closing an Order

Hi,

I'm trying to code an EA that will take profits at two levels. To do so I want to split the original order so that "x" lots are taken off at the first profit target and the remaining lots taken off at the second profit target. To do this I'm using two OrderClose() functions.

The first OrderClose() function works perfectly but it also appears to close the existing order and generate a new ticket for the remaining lots.

Could someone give me some guidance on how to handle this "new" ticket/order so that it can be processed by the second CloseOrder() function?

Cheers

Jim

 
jimdee:
Hi,

I'm trying to code an EA that will take profits at two levels. To do so I want to split the original order so that "x" lots are taken off at the first profit target and the remaining lots taken off at the second profit target. To do this I'm using two OrderClose() functions.

The first OrderClose() function works perfectly but it also appears to close the existing order and generate a new ticket for the remaining lots.

Could someone give me some guidance on how to handle this "new" ticket/order so that it can be processed by the second CloseOrder() function?

Cheers

Jim

Just an idea. You can try to use two trades instead of one if partial closure for profit is harder to achieve. And managing those trades by their open time.

 
jimdee:
Hi,

I'm trying to code an EA that will take profits at two levels. To do so I want to split the original order so that "x" lots are taken off at the first profit target and the remaining lots taken off at the second profit target. To do this I'm using two OrderClose() functions.

The first OrderClose() function works perfectly but it also appears to close the existing order and generate a new ticket for the remaining lots.

Could someone give me some guidance on how to handle this "new" ticket/order so that it can be processed by the second CloseOrder() function?

Cheers

Jim

People have different references, I like using partial close because there's less trades for broker to handle.

Every time when partial close take place, broker will act as the remaining order is a new order so assign a new ticket. But it doesn't really matter, as long as your next step the method for identify trades is not based by order ticket, it can be based on order lots, or you can assign a variable to indicate EA when stage it's in.

Partial close is a bit tricky to handle completely correctly if the following situation exists:

1. Your orders can be different lot size because of MM.

2. You have multiple orders existing at the same time of different lot size.

If your EA has the above situations and if you are not experienced in coding, I think you can use the multiple order approach.

 

Quick fix for simple problem. Thank *you : )

I would like to return the following variables :

1. The first bar open price of the last recorded Monday (Hour Bar).

2. The last bar close price of the last recorded Wednesday (Hour Bar).

Anybody know how to do this ? Ive tried getting the variables as they pass with :

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

if (currentday == 1 && currenttime == 0)

{

MondayOpenPrice = iOpen(Symbol(),PERIOD_H1,0);

}

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

The problem with the above is I cant always keep my metatrader running the whole week to keep these variables.

So I will need the EA to look back in history for the first recorded open price on a monday etc...

Thank you so much for your time.

Reason: