How to code? - page 114

 
matrixebiz:
How do I setup Time Trading hours in an EA?

I was trying this;

int TradeHour;

if(Hour()17) TradeHour = false;

but doesn't seem to be obeying the rule, it just trades whenever and I do have && TradeHour in my buy/sell statements.

Thanks

Have you tried defining TradeHour as a boolean instead of an integer?

bool TradeHour;

if ((Hour()17)){ TradeHour = false;}

Plus you may need the additional brackets.

 
matrixebiz:
How do I setup Time Trading hours in an EA?

I was trying this;

int TradeHour;

if(Hour()17) TradeHour = false;

but doesn't seem to be obeying the rule, it just trades whenever and I do have && TradeHour in my buy/sell statements.

Thanks

How can Hour() be less than 9 AND greater than 17?? Did you mean Or (||)?

Lux

 
luxinterior:
How can Hour() be less than 9 AND greater than 17?? Did you mean Or (||)? Lux

Good point luxinterior! That's a definite problem. I missed that.

 

Sorry, I meant ||

and I was trying this to;

//+---------Trade hour variables------------------

// if (Hour() < StartHour) TradeHourS = false;

// if (Hour() > EndHour) TradeHourE = false;

So if StartHour=5 and EndHour=17 it should only trade within those hours correct?

But it trade anytime still ?? is it a problem using the Strategy Tester?

 

Why not just use the example straight from the help file?

bool is_siesta=false;

if(Hour()>=12 || Hour()<17)

is_siesta=true;

Lux

 

Returning string from a c/c++ DLL exported functions

Hi everyone,

I've developed a set of functions to manage configuration settings from an EA.

Those functions are exported by a c++ DLL and each of the exported function has the __stdcall calling convetion requested my MQL4.

My problem arises when a function need to return a string to the EA.

Naturally the function cannot:

- return a pointer to a local variabile (variable goes out of scope)

- return a pointer to a dll global variable (problems with concurrent access)

- return a pointer to a heap allocated string (need functions to free memory to be called from the EA: I don't not like this approach)

So i resolved to pass a string and string size from the EA. Es:

string buffer;

GetString( buffer, 30 );

[/CODE]

and from the c++ dll, something like this

void __stdcall GetString( LPTSTR buffer, int BufSize )

{

// Read a string from a some source

....

// -1 to take into account the terminating null character

StringCchCopy( buffer, BufSize-1, ReadStringFromASource );

}

[/CODE]

Here starts the weird behaviour of MQL managing strings returned from a DLL.

using the following code:

string buffer;

GetString( buffer, 30 );

the first time buffer contains the right string. A first question arises: buffer is not initialized but after calling GetString it contains the string returned. I have to suppose that MQL allocates space for a string variable when it's declared.

Next time GetString() is called the string returned seems to be truncated to the length-1 of the previous string length and not resetted as expected because of the 'string buffer;' statement.

Tried even:

[CODE]

string buffer = " "; // 'allocate' 30 blank characters

GetString( buffer, StringLen(buffer) );

but after the first time, when the execution returns to this code, the assignment of buffer does not work any more and buffer still contains the previous read string, and it seems it can only contains the number of characters of his content.

At first I have thought that the null character is not handled very well by MQL and modified the c++ code like this ...

[CODE]

CopyMemory( buffer, ReadStringFromASource, min(BufferSize,ReadStringFromASourceLength) );

and not adding the terminating null character.

But when called from MQL, no string at all is returning.

Has someone an answer ?

 
luxinterior:
Why not just use the example straight from the help file?

bool is_siesta=false;

if(Hour()>=12 || Hour()<17)

is_siesta=true;

Lux

yeah, I tried that to;

bool TradeHour=false;

if(Hour()>=12 || Hour()<17) TradeHour=true

but the OR line won't work because if Hour happens to be 22 then it satisfies the first part "if(Hour()>=12" and Still trades whenever it wants and I did add && TradeHour to by Buy/Sell statements. The second example I gave should to the trick, I don't get it

 
luxinterior:
Why not just use the example straight from the help file?

bool is_siesta=false;

if(Hour()>=12 || Hour()<17)

is_siesta=true;

[/code]

Lux

I think if your goal is to trade between 12:00 and 17:00 you simply have to do the following:

[code]

bool TradingEnabled=false; // Global variable

...

...

TradingEnabled=false; // Reset every tic run

if(Hour()>=12 && Hour()<17)

TradingEnabled=true;

...

...

if( TradingEnabled )

{

// Trading logic here

}

...

...

 
gorgoroth:
I think if your goal is to trade between 12:00 and 17:00 you simply have to do the following:

bool TradingEnabled=false; // Global variable

...

...

TradingEnabled=false; // Reset every tic run

if(Hour()>=12 && Hour()<17)

TradingEnabled=true;

...

...

if( TradingEnabled )

{

// Trading logic here

}

...

...

Am I not doing the same thing by doing this?;

if (Hour() 17) TradeHour = false;

Meaning, if hour is between 12 and 17 TradeHour=true , correct?

(Adding && TradeHour in my Buy/Sell statement)

 
luxinterior:
How can Hour() be less than 9 AND greater than 17?? Did you mean Or (||)? Lux

Oh, and I did mean && because your right the hour can't be less than 9 AND greater than 17 which makes the statement false but if Hour is 10 then it is not less than 9 AND not greater than 17 so statement turns true.

Maybe doing it that way confuses MT4 buy anyway this way works;

int TradeHour;

TradeHour = ((Hour()>=StartHour) && (Hour()<EndHour));

Thanks

Reason: