如何编码? - 页 114

 
matrixebiz:
我如何在EA中设置交易时间?

我正在尝试这样做。

int TradeHour;

if(Hour()17) TradeHour = false;

但似乎没有遵守规则,它只是在任何时候进行交易,而且我在买入/卖出语句中确实有&&TradeHour。

谢谢

你有没有试着把TradeHour定义为布尔值而不是整数?

bool TradeHour;

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

另外你可能需要额外的括号。

 
matrixebiz:
我如何在EA中设置交易时间?

我正在尝试这样做。

int TradeHour;

if(Hour()17) TradeHour = false;

但似乎没有遵守规则,它只是在任何时候进行交易,而且我在买入/卖出语句中确实有&&TradeHour。

谢谢

Hour()怎么可能小于9又大于17呢?你是说Or (||)吗?

拉克斯

 
luxinterior:
Hour()怎么可能小于9又大于17呢?你是说Or (||)吗? Lux

说得好,Luxinterior! 这是一个绝对的问题。 我忽略了这一点。

 

对不起,我的意思是||

而我是想这样做的。

//+---------Trade小时变量------------------

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

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

因此,如果StartHour=5,EndHour=17,它应该只在这些时间内交易,对吗?

但它还是随时都在交易,这是不是使用策略测试器 的问题?

 

为什么不直接使用帮助文件中的例子?

bool is_siesta=false;

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

is_siesta=true;

卢克斯

 

从c/c++ DLL导出的函数 中返回字符串

大家好。

我已经开发了一组函数来管理EA的配置设置。

这些函数是由一个C++ DLL导出的,每个导出的函数都有__stdcall调用信念,要求我的MQL4。

当一个函数需要返回一个字符串给EA时,我的问题就出现了。

当然,这个函数不能。

- 返回一个指向本地变量的指针(变量会超出范围)

- 返回一个指向dll全局变量的指针(并发访问的问题)。

- 返回一个指向堆分配的字符串的指针(需要释放内存的函数来从EA中调用:我不喜欢这种方法)

所以我决定从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) );

并且不添加结尾的空字符。

但是,当从MQL调用时,根本就没有字符串返回。

有人能解答吗?

 
luxinterior:
为什么不直接使用帮助文件中的例子?

bool is_siesta=false;

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

is_siesta=true;

拉克斯

是的,我试着这样做了。

bool TradeHour=false。

如果(Hour()>=12 || Hour()<17) TradeHour=true

但OR行不会起作用,因为如果Hour刚好是22,那么它就满足了第一部分 "if(Hour()>=12",并且仍然可以随时交易,而且我确实在买入/卖出语句中加入了&&TradeHour。我所举的第二个例子应该是这样的,我不明白

 
luxinterior:
为什么不直接使用帮助文件中的例子呢?

bool is_siesta=false;

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

is_siesta=true;

[/code]

Lux

我认为,如果你的目标是在12:00和17:00之间进行交易,你只需做以下工作。

[代码]

bool TradingEnabled=false; // 全局变量

...

...

TradingEnabled=false; // 重置每一次Tic运行。

如果(Hour()>=12&& Hour()<17)

TradingEnabled=true。

...

...

如果( TradingEnabled )

{

// 这里的交易逻辑

}

...

...

 
gorgoroth:
我认为如果你的目标是在12:00和17:00之间交易,你只需做以下工作。

bool TradingEnabled=false; // Global variable

...

...

TradingEnabled=false; // Reset every tic run

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

TradingEnabled=true;

...

...

if( TradingEnabled )

{

// Trading logic here

}

...

...

我这样做不是在做同样的事情吗?

如果(Hour() 17) TradeHour = false。

意思是,如果小时在12和17之间,TradeHour=true ,对吗?

(在我的买入/卖出语句中添加&&TradeHour)

 
luxinterior:
Hour()怎么可能小于9又大于17呢?你是说Or (||)吗? Lux

哦,我的意思是&&,因为你说的对,小时不可能小于9,也不可能大于17,这使得声明为假,但如果小时是10,那么它不小于9,也不大于17,所以声明为真。

也许这样做会让MT4感到困惑,但这种方式是可行的。

int TradeHour;

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

谢谢