编码帮助......我如何让指标过滤而不是警报? - 页 8

 

你是个好人!

所以....,让我们看看我的理解是否正确......

如果我想根据移动平均线的反向交叉来平仓,我所需要的代码是这样的:....

if(currentlong>minorts) {CloseOrder(OP_SELL); // Close allsell orders}

其中currentlong是20ema,minorts是150ema,所以开仓是或现在是空头,已经运行了一段时间,现在20ema在150ema上方移动,这是空头交易的关闭信号。

和...

if (currentlong<minorts) {CloseOrder(OP_BUY); // Close all buy orders}。

其中currentlong是20ema,minorts是150ema,所以未平仓的头寸本来是或现在是多头,现在20ema移动到150ema以下,这是多头交易的关闭信号。

所以我可以把这两行放在一起

if (currentlong<minorts) {CloseOrder(OP_BUY); // Close all buy orders}.

如果(currentlong>minorts) {CloseOrder(OP_SELL); // 关闭所有卖出订单}。

就在进场代码之后,在所有其他的平仓和追踪止损之前,像这样吗? 它会工作吗?

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());

}

else Print("Error opening SELL order : ",GetLastError());

return(0);

}

}

//+---------end of order entry-------------------------+

//+------close on moving average cross-----------------+

if(currentlong<minorts) {CloseOrder(OP_BUY);} // Close all buy orders}

if(currentlong>minorts) {CloseOrder(OP_SELL);} // Close all sell orders}

//+--------end of close on moving average cross--------+

//+-------------------------Trailing Stop Code------------------------------------+

for(cnt=0;cnt<total;cnt++) {

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {

if(OrderType()==OP_BUY){

编译器这样说。

')' - 参数计数错误 C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 43)

')' - 错误的参数计数 C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (86, 44)

它不喜欢这些行。我觉得我的沟通不是很好。

 

这很有意思,突然间我在元编辑器上的搜索功能起作用了?

bool OrderCloseBy( int ticket, int opposite, color Color=CLR_NONE)

通过另一个相反的已开订单关闭一个已开订单。如果该函数成功,返回值为true。如果函数失败,返回值为false。要获得详细的错误信息,请调用GetLastError()

参数。

ticket - 订单票的唯一编号。

opposite - 相反的订单票的唯一编号。

颜色 - 图表上收盘箭头的颜色。如果参数缺失或有CLR_NONE值,收盘箭头将不会在图表上绘制。

示例。

如果(iRSI(NULL,0,14,PRICE_CLOSE,0)>75)

{

OrderCloseBy(order_id,opposite_id);

return(0);

}

所以我的设想是这样的......

if(currentlong<minorts)

{

OrderCloseBy(order_id,opposite_id);

return(0);

}[/PHP]

thing is this doesn't distinguish what kind of position I'm into first, long or short. So if I put the opposite side of this with it...like this...

[PHP] if(currentlong>minorts)

{

OrderCloseBy(order_id,opposite_id);

return(0);

}

我觉得自己就像一个还不会说话的孩子一样沮丧,因为我心里有完整的想法,却无法表达出来。

 
elihayun:
我忘了这个
void CloseOrder(int ticket,double numLots,double close_price)

{

int CloseCnt, err;

// try to close 3 Times

CloseCnt = 0;

color clr = Violet;

if (OrderType() == OP_SELL)

clr = Orange;

while (CloseCnt < 3)

{

if (OrderClose(ticket,numLots,close_price,Slippage,clr))

{

CloseCnt = 3;

}

else

{

err=GetLastError();

Print(CloseCnt," Error closing order : (", err , ") " + ErrorDescription(err));

if (err > 0) CloseCnt++;

}

}

}

[/PHP]

and dont forget to add this line after #property link

[PHP]#property link "http://www.elihayun.com"

#include

我必须下载其他东西才能调用吗?我想了解它是如何做的,它是如何区分多头和空头的。我想学习。

 
Aaragorn:
你是个好人!

所以....,让我们看看我的理解是否正确......

所以我可以只放这两行

if (currentlong<minorts) {CloseOrder(OP_BUY); // 关闭所有买入订单}.

如果(currentlong>minorts) {CloseOrder(OP_SELL); //关闭所有卖单}。

//+------,在移动平均线交叉时平仓-----------------+。

如果(currentlong<minorts) {CloseOrder(OP_BUY); }//关闭所有的买入订单}

如果(currentlong>minorts) {CloseOrder(OP_SELL);}//关闭所有的卖单。//关闭所有卖出订单}

如果(OrderType()==OP_BUY){[/PHP]

编译器这样说。

')' - 错误的参数计数 C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 43)

')' - 错误的参数计数 C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (86, 44)

它不喜欢这些线条。我不觉得我的沟通方式很好。

你必须在最后调用CloseOrders,而不是CloseOrder(只关闭一个订单)。

 
Aaragorn:
我还需要下载其他的东西才能调用它吗?我想了解它是如何做到的,它是如何区分多头和空头的。我想学习。

它是MQL4的一部分,包含ErrorDescription 函数

 
elihayun:
它是MQL4的一部分,包含ErrorDescription 函数

好的,我仍然需要一些代码,使我能够根据移动平均线 的交叉回来进行关闭。

 

这段代码应该可以让你开始工作......

当然,你将不得不修改它以适应你自己的需要。然而,这应该为你提供一个起点。这个程序使用SMA1线作为追踪止损。因此,你可以利用这个想法,看看你能从中得到什么。

//these two lines within start()

SMA1 = iMA(NULL,TimePeriod,SlowPeriod,0,SlowMode,SlowPrice,1);

TrailingAlls(TrailStart, SMA1);

// trailing routine using the value of SMA1

void TrailingAlls(int start, double currvalue)

{

int profit;

double stoptrade;

double stopcal;

// if(stop==0) return;

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

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

{

if(OrderType()==OP_BUY)

{

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Bid-(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);

}//Long

if(OrderType()==OP_SELL)

{

profit=NormalizeDouble((OrderOpenPrice()-Ask)/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Ask+(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);

}//Shrt

}

}//for

}

 

如果我能够首先弄清楚这个问题,跟踪止损收盘可能是有用的....。

elihayun给我的这个片段,我想如果有人能想出如何让它对ma backcross做出反应,并且方向正确的话,就可以发挥作用。我对op_buy和op_sell哪个是哪个感到困惑,哪个用于关闭多头头寸,哪个用于关闭空头头寸。

//+------close on moving average cross-----------------+

void CloseOrders(int op)

{

int tik[30], t = 0;

for(int i =0;i<OrdersTotal();i++){

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){

if(OrderSymbol()==Symbol() && MagicNum==OrderMagicNumber() && OrderType() == op){

tik[t] = OrderTicket(); t++;

}

}

}

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}

}

}

//+--------end of close on moving average cross--------+[/PHP]

I think I see that at this point in the code it has selected the orders by ticket and it's figuring out to set the close price based on the bid or the ask. So I'm not sure which is which for long or short positions. If the ticket is a long position that means it was opened at the ask price right? so it should close on the bid price? so this snippet is saying use the bid price to close so I assume that's for a long position. THEN it asks the

if (op == OP_SELL) prc = Ask;

so I assume this is the first place in this code where we now know if we are looking at a long or a short position ticket.

Then it moves immediately to close. But if I can put my closing criteria in here BEFORE it does that???

here it is as I received it...

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc);

}[/PHP]

so what I'm thinking is that this is the place in the code where I should insert the moving average criteria to trigger the close for long or short positions. Something like this...

[PHP]for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

if (op == OP_SELL) prc = Ask;

if (prc == Bid && currentlong minorts);

CloseOrder(tik, OrderLots(), prc);

}

Will this do it? I think it might if I understand correctly..

Please tell me coders if this is correct??

I think this connects the direction of the moving average cross to the type of position that the ticket is identifed as being.

if the bid is to close long positions so we know the ticket is for a long position then it can close long positions if the currentlongema < minortsEMA because it knows that the ticket is for a long position and the 20ema has moved below the 150ema.

If the ask is for closing short positions and the ticket is identified as a short position because it wants to close at the ask price and the currentlongEMA has moved above the minortsEMA because it knows the ticket is for a short position and the 20ema has moved above the 150ema.

if that is correct will adding this line to the code,

if (prc == Bid && currentlong minorts)

stop it from closing UNLESS each ticket fits this criteria?

...that for the long position the 20ema150ema?

if that is ok then the logic for deciding to close long or short based on the emacrossback is ok and it's worth fixing these errors

I get these errors from the compiler...

[PHP]Compiling 'whatever.mq4'...

'(' - function definition unexpected C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (85, 17)

'MagicNum' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (90, 40)

'op' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (90, 87)

'tik' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (91, 13)

't' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (91, 17)

't' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (91, 37)

't' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (96, 18)

'tik' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (98, 22)

'op' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (100, 14)

'tik' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (102, 21)

'cnt' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 5)

'cnt' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 5)

'cnt' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 11)

'cnt' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 11)

'total' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 15)

'total' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 15)

'cnt' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 21)

'cnt' - variable not defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 21)

'{' - expression on global scope not allowed C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (108, 28)

'i' - variable is already defined C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (135, 11)

'}' - unbalanced parentheses C:\Program Files\Interbank FX Trader 4-live mini\experts\whatever.mq4 (165, 1)

16 error(s), 5 warning(s)

 

给所有成员的一个建议,不要使用 "计数 "的程序来关闭交易。例如,不要使用类似这样的东西。

for (i = 0; i<t; i++)

{

if(OrderSelect(tik,SELECT_BY_TICKET)){

double prc = Bid;

如果(op == OP_SELL) prc = Ask;

CloseOrder(tik, OrderLots(), prc)。

}

如果你使用多个订单,它将不会关闭最后一个订单。使用一个 "倒数 "程序。以下是我第一次偶然发现这个恼人的 "bug "时与Metaquotes开发人员的讨论。

http://www.metaquotes.net/forum/2018/

 
Maji:
这段代码应该可以让你开始工作...

当然,你将不得不修改它以适应你自己的需要。但是,这应该为你提供一个起点。这个程序使用SMA1线作为追踪止损。因此,请采纳这个想法,看看你能从中得到什么。

//these two lines within start()

SMA1 = iMA(NULL,TimePeriod,SlowPeriod,0,SlowMode,SlowPrice,1);

TrailingAlls(TrailStart, SMA1);

// trailing routine using the value of SMA1

void TrailingAlls(int start, double currvalue)

{

int profit;

double stoptrade;

double stopcal;

// if(stop==0) return;

int trade;

for(trade=OrdersTotal()-1;trade>=0;trade--)

{

if(!OrderSelect(trade,SELECT_BY_POS,MODE_TRADES))

continue;

if(OrderSymbol()!=Symbol()||OrderMagicNumber()!=MagicNumber)

continue;

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

{

if(OrderType()==OP_BUY)

{

profit=NormalizeDouble((Bid-OrderOpenPrice())/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Bid-(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal>stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Blue);

}//Long

if(OrderType()==OP_SELL)

{

profit=NormalizeDouble((OrderOpenPrice()-Ask)/Point,0);

if(profit<start)

continue;

stoptrade=OrderStopLoss();

// stopcal=Ask+(stop*Point);

stopcal=NormalizeDouble(currvalue, Digits);

if(stoptrade==0||(stoptrade!=0&&stopcal<stoptrade))

OrderModify(OrderTicket(),OrderOpenPrice(),stopcal,OrderTakeProfit(),0,Red);

}//Shrt

}

}//for

}

非常感谢你。我期待着对其进行剖析,看看我能做出什么来......

我想先让emacrossback工作的原因是,它基本上将是我的止损和我的默认退出策略。

一旦它开始工作,我将添加像这样的东西来增加盈利能力。但是,由于我不可能在不搞砸的情况下设置止损,而且我不愿意允许巨大的止损参数,所以我想先让移动平均线交叉收盘工作起来。如果你有机会验证一下我到目前为止在交叉收盘方面所做的工作,我将不胜感激。