How to code? - page 151

 
luxinterior:
Use the OrderReliable include file. Lux

seems to be a bit missing on this for the OrderSendReliable function do you have a later version?

 

How to use Time in calculations

I am new to MQL4 and was just trying to see if I could make a simple EA that would use some basic functions. I know C++ well but must be awful at MQL4 because I something is worng with this code and I think it has to do with the time functions or possibably the buy/sell functions, heres the code: ...and by the way this wasnt designed to make money just test the functions Thanks!

int start()

{

//----

double currenttimeN, currentvalue;

double timeM = TimeMinute (TimeCurrent());

double timeH = TimeHour(TimeCurrent());

currenttimeN = (timeH / 24) + (timeM / 1440);//gives the time of day as a percent or 1 (1 = one complete day) so always less than 1

currentvalue = Ask;

if (currentvalue > currenttimeN){

OrderSend(Symbol(),OP_SELL,1,Ask,3,Ask-25*Point,Ask+25*Point,"My order #2",16384,0,Green);

}

/* BUY COMMAND */

else if (currentvalue < currenttimeN){

OrderSend(Symbol(),OP_BUY,1,Ask,3,Ask-25*Point,Ask+25*Point,"My order #2",16384,0,Green);

}

//----

return(0);

}

 

...................

 

Wrong string:

currenttimeN = (timeH / 24) + (timeM / 1440);//gives the time of day as a percent or 1 (1 = one complete day) so always less than 1[/CODE]

could be:

[CODE]currenttimeN = (timeH *60 + timeM) / 1440;

How can you compare part of day and price?

 

buy and sell at the same time

can someone help to program so tat i can buy and sell at the same time...some scripts or etc...

i appreciate it...

example:

if (Stoch1>=Stoch2)

{

ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask-Stoploss*Point, Ask+TakeProfit*Point, "-", 0, 0, Green);

ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Ask-Stoploss*Point, Bid-TakeProfit*Point,"-", 0, 0, Red);

}

the problem is it wil only buy..but i ned it to execute both order...thanks..

 
kk81:
can someone help to program so tat i can buy and sell at the same time...some scripts or etc...

i appreciate it...

example:

if (Stoch1>=Stoch2)

{

ticket=OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Ask-Stoploss*Point, Ask+TakeProfit*Point, "-", 0, 0, Green);

ticket=OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, Ask-Stoploss*Point, Bid-TakeProfit*Point, "-", 0, 0, Red);

}

the problem is it wil only buy..but i ned it to execute both order...thanks..

https://www.mql5.com/en/forum/177583/page37

 

how to get OrderClose return price ?

I want to know, at what price the order is closed

Files:
closeorder.jpg  62 kb
 

Forex freedom Bar

hi

thanks very much all friends , specific Roger09

I want expert (automatic trade) ForexFreedomBars .

when square 5m , 15m ,30m ,1H is Red , send order SELL by 10-20 TP

when square 5m , 15m ,30m ,1H is Blue , send order BUY by 10-20 TP

(TP, SL , lot, maxtrade) ability have to change

please help me .

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

//| #The Forex Freedom Bars

//|

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

#property copyright "Programmed by Eli hayun"

#property indicator_separate_window

#property indicator_minimum -0.5

//#property indicator_maximum 5

#property indicator_buffers 8

#property indicator_color1 Red

#property indicator_color2 DodgerBlue

#property indicator_color3 Red

#property indicator_color4 DodgerBlue

#property indicator_color5 Red

#property indicator_color6 DodgerBlue

#property indicator_color7 Red

#property indicator_color8 DodgerBlue

//---- buffers

double buf4_up[];

double buf4_down[];

double buf3_up[];

double buf3_down[];

double buf2_up[];

double buf2_down[];

double buf1_up[];

double buf1_down[];

extern double Gap = 1; // Gap between the lines of bars

extern int Period_1 = PERIOD_M5;//M15

extern int Period_2 = PERIOD_M15;//M30

extern int Period_3 = PERIOD_M30;//H1

extern int Period_4 = PERIOD_H1;//H4

extern int cci_1 = 2;//50

extern int cci_2 = 2;//14

extern int cci_3 = 2;

extern int cci_4 = 2;

extern int StopLoss=100;

extern int TakeProfit=100;

extern double Lots=0.1;

extern bool AutoDisplay = false;

string shortname = "";

bool firstTime = true;

int UniqueNum = 228;

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

//| Custom indicator initialization function |

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

int init()

{

SetAutoDisplay();

shortname = "# Forex Freedom("+Period_1+","+Period_2+","+Period_3+","+Period_4+")";

firstTime = true;

IndicatorShortName(shortname);

//---- indicators

SetIndexStyle(0,DRAW_ARROW);

SetIndexArrow(0,110);

SetIndexBuffer(0,buf4_up);

SetIndexEmptyValue(0,0.0);

SetIndexStyle(1,DRAW_ARROW);

SetIndexArrow(1,110);

SetIndexBuffer(1,buf4_down);

SetIndexEmptyValue(1,0.0);

SetIndexStyle(2,DRAW_ARROW);

SetIndexArrow(2,110);

SetIndexBuffer(2,buf3_up);

SetIndexEmptyValue(2,0.0);

SetIndexStyle(3,DRAW_ARROW);

SetIndexArrow(3,110);

SetIndexBuffer(3,buf3_down);

SetIndexEmptyValue(3,0.0);

SetIndexStyle(4,DRAW_ARROW);

SetIndexArrow(4,110);

SetIndexBuffer(4,buf2_up);

SetIndexEmptyValue(4,0.0);

SetIndexStyle(5,DRAW_ARROW);

SetIndexArrow(5,110);

SetIndexBuffer(5,buf2_down);

SetIndexEmptyValue(5,0.0);

SetIndexStyle(6,DRAW_ARROW);

SetIndexArrow(6,110);

SetIndexBuffer(6,buf1_up);

SetIndexEmptyValue(6,0.0);

SetIndexStyle(7,DRAW_ARROW);

SetIndexArrow(7,110);

SetIndexBuffer(7,buf1_down);

SetIndexEmptyValue(7,0.0);

//----

return(0);

}

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

//| Custom indicator deinitialization function |

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

int deinit()

{

//----

SetAutoDisplay();

shortname = "# Forex Freedom("+Period_1+","+Period_2+","+Period_3+","+Period_4+")";

firstTime = true;

//----

return(0);

}

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

//| Custom indicator iteration function |

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

int start()

{

int counted_bars=IndicatorCounted();

int i=0, y5m=0, y1h=0, y30m=0, y15m=0, yy;

int limit=Bars-counted_bars;

datetime TimeArray_1H[], TimeArray_30M[], TimeArray_15M[], TimeArray_5M[];

//----

if (firstTime || NewBar())

{

firstTime = false;

int win = UniqueNum; // WindowFind(shortname);

double dif = Time[0] - Time[1];

for (int ii=ObjectsTotal()-1; ii>-1; ii--)

{

if (StringFind(ObjectName(ii),"FF_"+win+"_") >= 0)

ObjectDelete(ObjectName(ii));

else

ii=-1;

}

double shift = 0.2;

for (ii=0; ii<4; ii++)

{

string txt = "??";

double gp;

switch (ii)

{

case 0: txt = tf2txt(Period_1); gp = 1 + shift; break;

case 1: txt = tf2txt(Period_2); gp = 1 + Gap + shift; break;

case 2: txt = tf2txt(Period_3); gp = 1 + Gap*2 + shift; break;

case 3: txt = tf2txt(Period_4); gp = 1 + Gap*3 + shift; break;

}

string name = "FF_"+win+"_"+ii+"_"+txt;

ObjectCreate(name, OBJ_TEXT, WindowFind(shortname), iTime(NULL,0,0)+dif*3, gp);

ObjectSetText(name, txt,8,"Arial", Silver);

}

}

ArrayCopySeries(TimeArray_1H,MODE_TIME,Symbol(),Period_4);

ArrayCopySeries(TimeArray_30M,MODE_TIME,Symbol(),Period_3);

ArrayCopySeries(TimeArray_15M,MODE_TIME,Symbol(),Period_2);

ArrayCopySeries(TimeArray_5M,MODE_TIME,Symbol(),Period_1);

for(i=0, y5m=0, y1h=0, y30m=0, y15m=0;i<limit;i++)

{

if (Time<TimeArray_5M[y5m]) y5m++;

if (Time<TimeArray_1H[y1h]) y1h++;

if (Time<TimeArray_30M[y30m]) y30m++;

if (Time<TimeArray_15M[y15m]) y15m++;

int cci_n = cci_1;

for (int tf = 0; tf < 4; tf++)

{

int prd;

switch (tf)

{

case 0: prd = Period_1; cci_n = cci_1; yy = y5m; break;

case 1: prd = Period_2; cci_n = cci_2; yy = y15m; break;

case 2: prd = Period_3; cci_n = cci_3; yy = y30m; break;

case 3: prd = Period_4; cci_n = cci_4; yy = y1h; break;

}

double cci = iCCI(NULL, prd, cci_n,PRICE_TYPICAL, yy);

double dUp = EMPTY_VALUE; // iCustom(NULL, prd, "SuperTrend", false, 1, yy);

double dDn = EMPTY_VALUE; //iCustom(NULL, prd, "SuperTrend", false, 0, yy);

if (cci < 0) dUp = 1; else dDn = 1;

switch (tf)

{

case 0: if (dUp == EMPTY_VALUE) buf1_down = 1; else buf1_up = 1; break;

case 1: if (dUp == EMPTY_VALUE) buf2_down = 1 + Gap * 1; else buf2_up = 1 + Gap * 1; break;

case 2: if (dUp == EMPTY_VALUE) buf3_down = 1 + Gap * 2; else buf3_up = 1 + Gap * 2; break;

case 3: if (dUp == EMPTY_VALUE) buf4_down = 1 + Gap * 3; else buf4_up = 1 + Gap * 3; break;

}

if (NewBar())

{

string sDir = "";

if (buf1_up[0] + buf2_up[0] + buf3_up[0] + buf4_up[0] == 4)

sDir = "Up";

if (buf1_down[0] + buf2_down[0] + buf3_down[0] + buf4_down[0] == 4)

sDir = "Down";

if (sDir != "")

{

PlaySound("alert1.wav");

Print("Forex freeway - Direction ",sDir);

}

}

}

}

if (buf1_up[0] + buf2_up[0] + buf3_up[0] + buf4_up[0] == 4)

OrderSend(Symbol(),OP_BUY,Lots,Ask,3,StopLoss ,TakeProfit); else

return(0);

}

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

string tf2txt(int tf)

{

if (tf == PERIOD_M1) return("M1");

if (tf == PERIOD_M5) return("M5");

if (tf == PERIOD_M15) return("M15");

if (tf == PERIOD_M30) return("M30");

if (tf == PERIOD_H1) return("H1");

if (tf == PERIOD_H4) return("H4");

if (tf == PERIOD_D1) return("D1");

if (tf == PERIOD_W1) return("W1");

if (tf == PERIOD_MN1) return("MN1");

return("??");

}

void SetValues(int p1, int p2, int p3, int p4)

{

Period_1 = p1; Period_2 = p2; Period_3 = p3; Period_4 = p4;

}

void SetAutoDisplay()

{

if (AutoDisplay)

{

switch (Period())

{

case PERIOD_M1 : SetValues(PERIOD_M1, PERIOD_M5, PERIOD_M15,PERIOD_M30); break;

case PERIOD_M5 : SetValues(PERIOD_M5, PERIOD_M15,PERIOD_M30,PERIOD_H1); break;

case PERIOD_M15 : SetValues(PERIOD_M5, PERIOD_M15,PERIOD_M30,PERIOD_H1); break;

case PERIOD_M30 : SetValues(PERIOD_M5, PERIOD_M15,PERIOD_M30, PERIOD_H1); break;

case PERIOD_H1 : SetValues(PERIOD_M15, PERIOD_M30,PERIOD_H1, PERIOD_H4); break;

case PERIOD_H4 : SetValues(PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1); break;

case PERIOD_D1 : SetValues(PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1); break;

case PERIOD_W1 : SetValues(PERIOD_H4, PERIOD_D1, PERIOD_W1,PERIOD_MN1); break;

case PERIOD_MN1 : SetValues(PERIOD_H4, PERIOD_D1, PERIOD_W1,PERIOD_MN1); break;

}

}

}

bool NewBar()

{

static datetime dt = 0;

if (Time[0] != dt)

{

dt = Time[0];

return(true);

}

return(false);

}

 

Problem migrating Indicator into EA as a filter function

When attempting to use part of an indicator as a function in my EA, I now realize that there are three types of functions. The first type is a Void which performs a process and returns 0. The second type performs calculations and returns one result. The third type expects to be given a set of specific values to process and then returns one result.

What can I do if my calculations produce three results which need to by used at least two different ways? Those calculations may need to be located in the Start() function for the following reason: Information flows from the External variables (which may be manually adjusted) and the Global variables (which remain static unless recompiled) into the various functions of the EA. The Start() function is the brain of the EA and reaches out to the other functions for their information. It may pass a set of variables to a function as arguments of that function if that function has declared those variables within it's () brackets. It then receives a result back and may pass more variables to another function to finish it's process.

Once I bagan to pass variables as arguments to other functions, I was hoping to enjoy a level of success with my EA coding. Unfortunately, I created a zero divide error, possibly because I tried to pass a value along as an argument from one function to another so that the value did not arrive were needed. Since it was actually missing, I got the zero divide error. My next revision removed intermediate steps and delivered values directly to the functions where they were used.

(I don't thing I do myself any favors by recording this point in my first two months experience learning MQL4 coding. However, anyone who may be googling "MQL4 zero divide error," might be glad to know how I managed to resolve it.)

My next EA revision cured my zero divide error, but the journal comment of my stratagy tester now indicated a stack overrun. For those googling "MQL4 stack overrun," I'll post the cause I found. It was failure to convert lots to their decimal equivelant by use of the "Points" constant when changing a price for opening or modifying an order.

The last tip I'd like to post here (after my initial two months coding experience and more than 12 EA rewrites) is a way I was able to solve a hard to find "unequal left parantheses" bracket. The MetaEditor does not highlight bracket text in color. If you have an AutoDesk product with Visual LISP Editor, you can open your MQ4 file in that editor and see almost all the bracket pairs in color. This makes it much easier to proof read for the missing bracket.

Thanks to everyone who shares their answers on this forum,

Happy Coding!

 

Any help?

Also wish to know if Metatrader orders can be run from the command line. Or in general if it is possible to interface external applications with Metatrader using some sort of API.

Thanks

Jeff

Reason: