问吧! - 页 106

 

图表的时间段

找出图表运行的时间段的代码是什么?这样我就可以为每个时间段改变变量设置。

if(?????) . . .

戴夫

 
Dave137:
找出图表运行的时间段的代码是什么?这样我就可以为每个时间段改变变量设置。

if(?????) . . .

戴夫
if(Period() == PERIOD_M15) ...

[/PHP]

or:

switch(Period())

{

case PERIOD_M1:

...

break;

case PERIOD_M5:

...

break;

...

}

[/PHP]

Sometime it maybe easier to work with indices:[PHP]

int tfIndex = ArrayBsearch({PERIOD_M1, PERIOD_M5, PERIOD_M15, ...}, Period());

Example : how to display the period by the string you want:[PHP]

int Periods[] = {PERIOD_M1, PERIOD_M5, PERIOD_M15, ...};

string sPeriods[] = {" M1", " M5", " M15", " M30", " Hourly", " 4 hours", " Daily", " Weekly"...};

int tfIndex = ArrayBsearch(Periods, Period());

comment(Symbol() + sPeriods[tfIndex]);

 

谢谢你,米歇尔。

我希望我正在创建的这个EA将是一个好的选择。 非常感谢您的帮助!

戴夫

 

你好!

有人能帮我添加这个功能 吗?让它在条形图完成时关闭交易,或者换句话说,让它在下一个条形图出现时关闭交易(不管交易是盈利还是亏损)。

外部int SystemMagicNumber=197;

External double TakeProfit = 100;

extern double StopLoss = 500;

外置双倍数Lots=0.1;

外置双倍追踪止损=0;

最大买入交易数=5。

最大卖出交易量=5。

开始()

{

if( HavePosThisBar(SystemMagicNumber)==false

&&iMA(NULL,0,5,0,MODE_SMA, PRICE_CLOSE,1)<iMA(NULL,0,15,0,MODE_EMA, PRICE_CLOSE,1)

&& iMA(NULL,0,5,0,MODE_SMA, PRICE_CLOSE,2)>iMA(NULL,0,15,0,MODE_EMA, PRICE_CLOSE,2)

&& SellPositionsCount()<MaxSellTrades

)

{

OrderSend(Symbol(),OP_SELL,Lots,Bid,0,Bid+StopLoss*Point,Bid-TakeProfit*Point,"",SystemMagicNumber,0,Red)。

返回(0)。

}

如果( HavePosThisBar(SystemMagicNumber)==false

&& iMA(NULL,0,5,0,MODE_SMA, PRICE_CLOSE,1)>iMA(NULL,0,15,0,MODE_EMA, PRICE_CLOSE,1)

&& iMA(NULL,0,5,0,MODE_SMA, PRICE_CLOSE,2)<iMA(NULL,0,15,0,MODE_EMA, PRICE_CLOSE,2)

&& BuyPositionsCount()<MaxBuyTrades

)

{

OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-StopLoss*Point,Ask+TakeProfit*Point,"",SystemMagicNumber,0,Blue)。

返回(0)。

}

for(int cnt=OrdersTotal()-1;cnt>=0;cnt--)

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)。

如果(OrderSymbol() == Symbol()

{

如果(OrderType()==OP_SELL)

{

如果(TrailingStop>0)

{

如果(OrderOpenPrice()-Ask>TrailingStop*Point)

{

如果 (OrderStopLoss() == 0 || OrderStopLoss()>(Ask+Point*TrailingStop))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Purple) 。

返回(0)。

}

}

}

}

如果(OrderType()==OP_BUY)

{

如果(TrailingStop>0)

{

如果(Bid-OrderOpenPrice()>TrailingStop*Point)

{

如果(OrderStopLoss()<(Bid-Point*TrailingStop))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop, OrderTakeProfit(),0,Yellow)。

返回(0)。

}

}

}

}

}

}

//----

return(0);

}

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

bool HavePosThisBar(int magic)

{

int cnt;

bool Result=false;

for(cnt=0; cnt<=OrdersHistoryTotal()-1; cnt++)

如果(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY))

如果((OrderSymbol() == Symbol()) &&(OrderMagicNumber() == magic) && Time[0]<=OrderOpenTime()

{

结果=true。

休息。

}

for(cnt=0; cnt<=OrdersTotal()-1; cnt++)

if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))

if((OrderSymbol() == Symbol()) &&(OrderMagicNumber() == magic) && Time[0]<=OrderOpenTime()

{

结果=true。

休息。

}

return(Result)。

}

int BuyPositionsCount()

{

int Result=0;

for(int cnt=0; cnt<=OrdersTotal()-1; cnt++)

if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))

if((OrderSymbol() == Symbol()) &&(OrderMagicNumber() == SystemMagicNumber) &&

(OrderType()==OP_BUY)

)

{

结果++。

}

return(Result);

}

int SellPositionsCount()

{

int Result=0;

for(int cnt=0; cnt<=OrdersTotal()-1; cnt++)

if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))

如果((OrderSymbol() == Symbol()) &&(OrderMagicNumber() == SystemMagicNumber) &&

(OrderType()==OP_SELL)

)

{

结果++。

}

return(Result);

}

 
bearfoot090:
你好!

有人能帮我添加这个功能吗?让它在条形图完成时关闭交易,或者换句话说,让它在下一个条形图出现时关闭交易(不管交易是盈利还是亏损)。

要检测一个新的条形图,有几个解决方案。

1)

if(Volume[0] == 1) CloseOrders(); [/PHP] Not very reliable, for example if the terminal has to be restarted you may loose the first tick of the bar.

2)

if(BarsCnt < Bars) {BarsCnt = Bars; CloseOrders();}[/PHP] Not very reliable, for example if new bars are added to the left of the chart.

3)

if(Time1 < Time[0]) {Time1 = Time[0]; CloseOrders();}

Better, but again, restarting the terminal may produce wrong behaviors.

So my opinion is that the best is to write the lifetime max of each order into the order itself, using the "Comment" field :[PHP]OrderSend(..., ""+(Time[0] + Period()*60), ..);
Then you can scan the orders and check :[PHP]if(TimeCurrent() > OrderComment()) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, CLR_NONE);

.这是一个很好的解决方案,因为如果你有几个订单要关闭,你有足够的时间去做。

 

谢谢Michel的回答。

但我应该在评论中写些什么?我喜欢什么?

 
bearfoot090:
谢谢Michel的回答,我会尝试的,但我应该在评论里写什么?

输入你想关闭订单的时间。评论字段必须是一个字符串,这就是为什么在值前面有""+的原因。

""+(Time[0] + Period()*60) // begin of the next bar on the current timeframe

""+(TimeCurrent() + 2*360 + 30*60) // 2 hours 30 minutes after the openning

""+(iTime(NULL,PERIOD_D1,0) + 23*360 + 45*60) // today at 23:45 (server time)

""+(iTime(NULL,PERIOD_W1,0) + (PERIOD_W1 - 10)*60) // next friday at 23:50
 

谢谢

Michel:
如果需要,请先检查你是否晚于上午8点。
if(Hour() < 8) return;[/PHP]

Then, find the max and min of the current day. (if its ok for you, its easier than from 8 am): [PHP]double Max = iHigh(Symbol(), PERIOD_D1, 0);

double Min = iLow(Symbol(), PERIOD_D1, 0);

int Range = (Max - Min) / Point;

if(Range > 90) return;

...

你好。

很抱歉花了这么长的时间来说 "谢谢你"。

我很感谢你的帮助。

再次感谢。

 
Michel:
要检测一个新柱子,有几种解决方案。

1)

if(Volume[0] == 1) CloseOrders(); [/PHP] Not very reliable, for example if the terminal has to be restarted you may loose the first tick of the bar.

2)

if(BarsCnt < Bars) {BarsCnt = Bars; CloseOrders();}[/PHP] Not very reliable, for example if new bars are added to the left of the chart.

3)

if(Time1 < Time[0]) {Time1 = Time[0]; CloseOrders();}[/PHP] Better, but again, restarting the terminal may produce wrong behaviors.

So my opinion is that the best is to write the lifetime max of each order into the order itself, using the "Comment" field :

OrderSend(..., ""+(Time[0] + Period()*60), ..);[/PHP] Then you can scan the orders and check :[PHP]if(TimeCurrent() > OrderComment()) OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, CLR_NONE);
. This is a good solution because if you have several orders to close, you have all the time needed to do it.

i got the error below.what does ot mean?

[PHP]'>' - different types in comparison F:\Program Files\MetaTrader - FXOpen\experts\EMA_10.mq4 (88, 22)

I make it like this

for the send order

[PHP]OrderSend(Symbol(),OP_BUY,Lots,Ask,0,Ask-25*Point,Ask+TakeProfit*Point, ""+(Time[0] + Period()*60),SystemMagicNumber,0,Blue);

而对于平仓指令

[PHP]如果(TimeCurrent() > OrderComment())

{

OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 0, CLR_NONE)。

}

我得到了上面显示的错误。

这样做对吗?

 

我的错误,对不起。

这应该可以了。

if(TimeCurrent() > StringToInteger(OrderComment())