How to code? - page 271

 

Need Help with iCustom function for this EA can Call an custom indicator.

Hi, Everyone,

I would like to add this (Entry.mq4) indi to this (swb grid 4.1.0.3_EA) EA with

iCustom function so I and we could use this indicator in the strategy of the EA.

the EA already uses 3 MT4 indicators Bollinger Bands , Stochastic and RSI and have

switches , so you can select either or all of them to be used , by selecting TRUE or FALSE

and I would like to add this 4th indi(Entry.mq4) to the EA.

I would appreciated very much if any one could help me.

thank you.

the EA and indi attached bellow.

Files:
 

Noob MQL4 help needed to understand this array

Hi, coders

Would anyone care to educate me on arrays slightly

Or perhaps I'm having more trouble understanding this loop as it pertains to this array.

int i=Bars;

#property copyright "Unfinished POS by Agent86"

double v1[];

double v2[];

double val1;

double val2;

int start()

{

while(i>=0)

{

val1=iFractals(NULL, 0, MODE_UPPER,i);

if (val1 > 0)

{

v1=High;

}

else

{

v1=v1;

}

val2=iFractals(NULL, 0, MODE_LOWER,i);

if (val2 > 0)

{

v2=Low;

}

else v2=v2;

i--;

}

return(0);

}

It's a basic fractal, and yet I can't seem to refer to any elements in the array.

I want to be able to increment i++ somehow and Print (v1); or print the previous value prior to the 0 or EMPTY_VALUE.

If I'm even going in the right direction with this.

I'm having trouble.

Print (v1): always == 0 and prints 0

I sort of understand because it's actually looping down to -1 which == 0

How and where might I put the Print statements so I can see whats going on ?

I've tried multitudes or methods and re-initialized i=0 to attempt and alternate loop so that I might refer to v1 elements but all attempts have failed.

Basically I want to view the previous fractals or mark them as A high, B low etc. for possible future use in an ABCD scheme type of EA

For now, mainly I want to Print v1 so that I can see what it's doing.

Perhaps I might design something to select a Previous Fractal such as A_high and/or B_low or some such scheme.

Am I on the wrong track with this ?

Please advise

Thanks

 

I would go with A FOR loop instead. While loops can lead to lockups if you forget to include the i--. Since you know the exact number of bars, it's easier to use, plus the i will never be < 0. It helps avoid confusion.

For (int i=Bars-1;i>0;i--) {

// code

}

Try setting all of the array values to 0, then go back through and assign indicator values. Also, try a print() directly after assigning your indicator value. See if it's actually returning anything at all.

Your best bet, in my guess, is to actually assign a size to the array. Say, 1000 positions. You're unlikely to need that many, plus it will reduce the calculations you're doing. All you need to do then is loop through the last 999 bars to 0 and assign values. I seem to recall MT4 has problems with array initilizations and whatnot.

 

Array Questions

Trader5050-Deployed:
I would go with A FOR loop instead. While loops can lead to lockups if you forget to include the i--. Since you know the exact number of bars, it's easier to use, plus the i will never be < 0. It helps avoid confusion.

For (int i=Bars-1;i>0;i--) {

// code

}

Try setting all of the array values to 0, then go back through and assign indicator values. Also, try a print() directly after assigning your indicator value. See if it's actually returning anything at all.

Your best bet, in my guess, is to actually assign a size to the array. Say, 1000 positions. You're unlikely to need that many, plus it will reduce the calculations you're doing. All you need to do then is loop through the last 999 bars to 0 and assign values. I seem to recall MT4 has problems with array initilizations and whatnot.

Ok, thanks I'll try it.

Although the while statements are working well, it's not helping me to get the array elements I want to print / view etc.

I'll work on what you said and do some testing and post back.

Thanks

 
Trader5050-Deployed:
I would go with A FOR loop instead. While loops can lead to lockups if you forget to include the i--. Since you know the exact number of bars, it's easier to use, plus the i will never be < 0. It helps avoid confusion.

For (int i=Bars-1;i>0;i--) {

// code

}

Try setting all of the array values to 0, then go back through and assign indicator values. Also, try a print() directly after assigning your indicator value. See if it's actually returning anything at all.

Your best bet, in my guess, is to actually assign a size to the array. Say, 1000 positions. You're unlikely to need that many, plus it will reduce the calculations you're doing. All you need to do then is loop through the last 999 bars to 0 and assign values. I seem to recall MT4 has problems with array initilizations and whatnot.

Still just printing the array with all 0's entries

Strange

 

Argent86,

There are a couple of errors in your code :

- arrays you are using are not arrays yet (if you do not specify array size, as you did in the code, then it is a dynamic array and the array size must be managed at run time) so you are having arrays of size 0 (that is the first reason for always having 0 - it is metatrader error handling routine preventing fatal error and returning 0 as a result for such cases - in C/C++ it would cause a crash)

- if you do not specify that the array is "series like" (in "series like" 0th element is the last one while in normal arrays 0th element is the first one) so expression i+1 is the next element of an array in that case not the previous. You can use arrays in the "series like" manner, but then you have to do a couple more things in order to make it work run-time, and even then when bar numbers change you are going to get crawling code, so it is better to use arrays in their native - "C like" mode

- also make sure that you start at least from the 3rd bar on a new tick (that is where a new fractal will appear, not on a current bar)


Here is what you are trying to do without those issues
#property indicator_chart_window

double v1[];

double v2[];

int start()

{

int i,a,limit,counted_bars=IndicatorCounted();

if(counted_bars<0) return(-1);

if(counted_bars>0) counted_bars--;

limit = MathMin(Bars-counted_bars,Bars-1);

if (ArraySize(v1)!=Bars) ArrayResize(v1,Bars);

if (ArraySize(v2)!=Bars) ArrayResize(v2,Bars);

//

//

//

//

//

for (i=limit, a=Bars-i-1; i>=0; i--,a++)

{

v1[a]=v1[a-1]; if (iFractals(NULL,0,MODE_UPPER,i) > 0) v1[a]=High;

v2[a]=v2[a-1]; if (iFractals(NULL,0,MODE_LOWER,i) > 0) v2[a]=Low;

}

Comment("current upper frctal : "+DoubleToStr(v1,Digits)+"\n"+

"current lower frctal : "+DoubleToStr(v2,Digits));

return(0);

}
 

Access from MQ4 a dll function with: variable-argument lists

hi,

Anyone knows if this is possible: to import an dll function which uses as argument a: variable-argument lists

e.g. vprintf - C++ Reference

int vprintf ( const char * format, va_list arg );

if so how would one do it?

Thanks

MJ

 

How to call function with no return value

Hello Guys and Girls.

At some point in my programm I'd like to call for a function.

However the function has no return value (void). So how do I call the function,

so it executes the code inside the function exactly on the place where I call for it,

as if it was written on that place itself?

I made some example code below.. where I check some parameters, and if Ok, I want to execute the function...

Hope someone can help me with this, tx in advance, Jonkie76

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

//| expert start function |

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

int start()

{

//----

if (Price >= PRICE_CLOSE)

call function ; // At this point I'd like to call the function.How do I do that?

else

Alert("Do nothing"); //

//----

return(0);

}

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

//===================================================================+

//FUNCTIONS

//===================================================================+

//----------------------- CLOSE ORDER FUNCTION ----------------------+

void subCloseOrder()

{

int

i,

total = 0,

ticket = 0,

err = 0,

c = 0;

total = OrdersTotal();

for(i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()==Symbol() &&

OrderMagicNumber()==Magic)

{

switch(OrderType())

{

case OP_BUY :

for(c=0;c<NumberOfTries;c++)

{

ticket=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);

err=GetLastError();

if(err==0)

{

if(ticket>0) break;

}

else

{

if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

if(ticket>0) break;

}

}

}

break;

}

}

}

}

 

It's very simple. You just need to call it as it would return something, just you don't need to save any result because there is no result at all with void type. (returns nothing) Like this:

int start()

{

//----

if (Price >= PRICE_CLOSE)

subCloseOrder(); // At this point I'd like to call the function.How do I do that?

else

Alert("Do nothing"); //

//----

return(0);

}

 

Use "void" instead. Like this :

void someFunction(someParameters)

{

...

//

//

// no need to place return at the end, but if you want then use "just" return, like this

//

//

return;

}

Void means that it is not going to return value from a function and that way it effectively becomes a procedure, not a function

Jonkie76:
Hello Guys and Girls.

At some point in my programm I'd like to call for a function.

However the function has no return value (void). So how do I call the function,

so it executes the code inside the function exactly on the place where I call for it,

as if it was written on that place itself?

I made some example code below.. where I check some parameters, and if Ok, I want to execute the function...

Hope someone can help me with this, tx in advance, Jonkie76

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

//| expert start function |

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

int start()

{

//----

if (Price >= PRICE_CLOSE)

call function ; // At this point I'd like to call the function.How do I do that?

else

Alert("Do nothing"); //

//----

return(0);

}

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

//===================================================================+

//FUNCTIONS

//===================================================================+

//----------------------- CLOSE ORDER FUNCTION ----------------------+

void subCloseOrder()

{

int

i,

total = 0,

ticket = 0,

err = 0,

c = 0;

total = OrdersTotal();

for(i=total-1;i>=0;i--)

{

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if(OrderSymbol()==Symbol() &&

OrderMagicNumber()==Magic)

{

switch(OrderType())

{

case OP_BUY :

for(c=0;c<NumberOfTries;c++)

{

ticket=OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Violet);

err=GetLastError();

if(err==0)

{

if(ticket>0) break;

}

else

{

if(err==0 || err==4 || err==136 || err==137 || err==138 || err==146) //Busy errors

{

Sleep(5000);

continue;

}

else //normal error

{

if(ticket>0) break;

}

}

}

break;

}

}

}

}