How to code? - page 179

 
Roger09:
I tryed several times on Alpari account, no mistakes. Just once, when I put script in experts directory and launch as EA.

It's because I asked you about directories.

Sorry.

i think i figured out the problem. I am on window vista and i usually do "run as administrator". i tried it without doing that and seemed to work fine now

 

Passing an Array from MT4 .mq4 to C++ DLL

Any Help Please.

I am having Problem passing a single Array that contains 8 indicator values to a C++ DLL . It is very simple but i seem to get -1 when i call the

Forexindex() function which actually calls the imported function gepModel ( d [] ) from the DLL . ForexIndex Function should return 0 or 1 from the DLL

I suspect that i am doing something wrong in the .mq4 file in the Forexindex() . Any help will be highly appreciated. Below is the simple code . The C++ DLL file is fine it compiles OK without errors so the issue is with the mq4 file .

================================MQ4 file====================

#import "JadeClass.dll"

int gepModel( double& d[] );

#import

int start()

{

........

........

........

ForexIndex();

.......

......

......

.....

}

int ForexIndex()

{

int myindex;

double ind[] , MA_Close, MA_High, MA_Low, MA_Open, Macd, Rsi, Stoch, Vol;

int counted_bars=IndicatorCounted();

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

if (counted_bars>0) counted_bars--;

int pos=Bars-counted_bars;

MA_Close = iMA(NULL,PERIOD_M5,8,0,0,0,0) ;

MA_High = iMA(NULL,PERIOD_M5,8,0,0,0,0) ;

MA_Low = iMA(NULL,PERIOD_M5,8,0,0,0,0) ;

MA_Open = iMA(NULL,PERIOD_M5,8,0,0,0,0) ;

Macd = iMACD(NULL,PERIOD_M5,12,26,9,0,0,0);

Rsi = iRSI(NULL,PERIOD_M5,10,0,0);

Stoch = iStochastic(NULL,PERIOD_M5,14,3,3,0,0,0,0);

Vol = iVolume(NULL,PERIOD_M5,0);

while(pos>=0)

{

ind[0] = MA_Close;

ind[1] = MA_High;

ind[2] = MA_Low;

ind[3] = MA_Open;

ind[4] = Macd;

ind[5] = Rsi;

ind[6] = Stoch;

ind[7] = Vol;

myindex = gepModel(ind);

pos--;

}

Print( " Myindex = " , myindex);

return (myindex);

}

=========================DLL Source code ===========================

#include "stdafx.h"

#include "JadeClass.h"

#include

#ifdef _DEBUG

#define new DEBUG_NEW

#endif

#define MT4_EXPFUNC __declspec(dllexport)

// The one and only CJadeClassApp object

CJadeClassApp theApp;

MT4_EXPFUNC int __stdcall gepModel(double d[]);

MT4_EXPFUNC int __stdcall gepModel(double d[])

{

.....

.....

.....

.....

}

============================ The Def file for DLL Source code ===================

; JadeClass.def : Declares the module parameters for the DLL.

LIBRARY "JadeClass"

EXPORTS

; Explicit exports can go here

gepModel

========================================

 

Need help adding code for only one trade per signal

I'm just learning mq4 code and have spent many hours trying to figure how to code only one trade per signal. I've tried a few ways but cant get it to make only one trade. Example would be if the TP is hit it would place another same side trade because of the signal still being good. I would like it to wait untill the reverse signal and then put in that trade. I've attached the EA here without any of my changes. Thanks in advance for help. Marty

Files:
 
martypatty:
I'm just learning mq4 code and have spent many hours trying to figure how to code only one trade per signal. I've tried a few ways but cant get it to make only one trade. Example would be if the TP is hit it would place another same side trade because of the signal still being good. I would like it to wait untill the reverse signal and then put in that trade. I've attached the EA here without any of my changes. Thanks in advance for help. Marty

I found out how to do this recently over at mql4.com after having problems with too many opened trades on one bar. The articles at mql4 helped me out quite a lot in the beginning, plus they have a code database that gives you good examples to work from. Still this website has an excellent history of posts and some really good coders, just use the search facility and click the button display as posts, saves you looking through pages and pages of a topic just for the one reply that you need. As for your problem just use this code:

int BarsCount=0;

if(Bars<BarsCount)

{

//Code to do once goes here

BarsCount=Bars;

}

[/CODE]

Personally I'm having problems with an array. I cant for the life of me figure out why the following code returns 0 all the time... Any help would be much appreciated. I'm trying to create an array of length STD_MA_Period+1 to later use with iMAOnArray but it doesnt work so its back to the drawing board I guess.

[CODE]for(i=0;i<=STD_MA_Period+1;i++)

{

STD_MA_Buffer=iStdDev(Symbol(),Period(),STD_Period,0,MODE_SMA,PRICE_CLOSE,i);

}

Print(STD_MA_Buffer[1]);

PS double STD_MA_Buffer[]; is defined in the global variables section.

 

fix my EA, please

i have some idea but i can write it to mql4 code, can you help me???

this my problem

this is my system definition :

A. if orders=0 >> OP buy && OP sell

B. make grid by + or - condition

ex. if OPbuy + grid = OPbuy

if OPbuy - grid = OPsell

it same for OPsell

C. if profit > 0 && profit > minimum profit ( profit locking )

stop any order ( OP buy || OP sell ) and moving trailing equity or profit point

D. close by profit it can by equity or by point

E. .mq4 format because i wanna know where's my fault

i have problem on point B. please help me

 
mazgen:
i have some idea but i can write it to mql4 code, can you help me???

this my problem

this is my system definition :

A. if orders=0 >> OP buy && OP sell

B. make grid by + or - condition

ex. if OPbuy + grid = OPbuy

if OPbuy - grid = OPsell

it same for OPsell

C. if profit > 0 && profit > minimum profit ( profit locking )

stop any order ( OP buy || OP sell ) and moving trailing equity or profit point

D. close by profit it can by equity or by point

E. .mq4 format because i wanna know where's my fault

i have problem on point B. please help me

What about send source code insted of ex4 ?

 
figurelli:
What about send source code insted of ex4 ?

just show me how to code it,

this my last detailStatement

fix this.rar

i hope it can figure my EA, the problem that loop on OP sell why ???

 

trend line question

New to coding and just doing things for practice, I wanted to stop a trend line at the current bar. What I came up with was to simply change the color of it at the current bar to the background color. In essence just blending the line past the bar into the background and making it disapear at that point.

So now I ask the experts what the correct way to do it was. I know there are many ways to accomplish the same thing, so I am just looking to see some other ideas on what I could have done, in an effort to learn more.

Thanks

 

Very Strange MQL Code Behaviour

Hello all,

Metatrader Version: 4.00 Build 224 (15 May 2009)

Metaeditor Version: 4.00 Build 224 (14 May 2009)

I have encountered a very strange bug in MQL that has me completely baffled, so much so that I am looking back at ALL my old code to make sure that it is written to avoid this problem.

I hope someone can shed some light on what exactly is going on here.

Here is the code, a simple indicator that essentially does nothing. However in the init() section I have placed a call to 2 functions when the chart period is H1.

The two functions are identical in all but one respect. One has a static int array defined and the other has a non-static int array defined. All the functions do is print out the size of its locally defined array, assign some values to the array elements and then print them out.

#property indicator_chart_window

void afunc() {

int i;

int array[4];

array[0] = 100;

array[1] = 101;

array[2] = 102;

array[3] = 103;

Print("afunc array[4] size="+ArraySize(array));

for (i = 0; i < 4; i++) {

Print("afunc array["+i+"]="+array);

}

}

void afunc_static() {

int i;

static int array[4];

array[0] = 100;

array[1] = 101;

array[2] = 102;

array[3] = 103;

Print("afunc_static array[4] size="+ArraySize(array));

for (i = 0; i < 4; i++) {

Print("afunc_static array["+i+"]="+array);

}

}

int init() {

if (Period() == PERIOD_H1) {

afunc();

afunc_static();

}

return(0);

}

int deinit() {

return(0);

}

int start() {

return(0);

}

If you look at the code there should never be any circumstance where the two functions behave differently from one another.

However, if I attach the indicator to an M5 chart and then switch to the H1 timeframe I get the following output.

2009.05.23 18:12:47 afunc EURUSD,H1: initialized

2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[3]=103

2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[2]=102

2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[1]=101

2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[0]=100

2009.05.23 18:12:47 afunc EURUSD,H1: afunc_static array[4] size=4

2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[3]=0

2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[2]=0

2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[1]=0

2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[0]=0

2009.05.23 18:12:47 afunc EURUSD,H1: afunc array[4] size=0

2009.05.23 18:12:47 afunc EURUSD,M5: uninit reason 3

2009.05.23 18:12:47 afunc EURUSD,M5: deinitialized

2009.05.23 18:12:45 afunc EURUSD,M5: initialized

2009.05.23 18:12:43 afunc EURUSD,M5: loaded successfully

Notice that even though I have defined 'int array[4]' in afunc() the size of that array is actually ZERO and I cannot assign values to the elements of the array. This function obviously does not work as expected.

Notice that I have defined 'static int array[4]' in afunc_static() and the size is correctly shown as 4 as are the values I assigned to the elements of the array. This function works fine.

Bottom line. If you define a non-static array in a function and if that function does not get called by your indicator then you could have a problem.

If you subsequently switch the chart timeframe and then call the function the size of any locally defined arrays in the function will be zero irresepective of what you have defined the size of the arrays to be.

The only way around this problem as far as I can see is to make sure that any locally defined arrays in your functions are defined as being static arrays. Nuts I know, but I cannot see any other way to avoid the problem.

I really would appreciate any feedback on this issue.

Regards,

Laurence.

Files:
afunc.mq4  1 kb