编码帮助 - 页 156 1...149150151152153154155156157158159160161162163...786 新评论 Mladen Rakic 2013.10.01 18:28 #1551 Marco320: 亲爱的MLaden。接下来我可以在我的真实账户和Markets.com的模拟账户(差价合约)上获得图表。在Liquid Markets的模拟账户上,是否有问题?因为Liquid提供了大量的工具选择(尤其是MT4的期货),我想在该平台上把它显示出来。你能不能看到这个问题的出现,因为我找不到。 谢谢你,马可 这可能是由于该指标是一个反编译的指标。从Build 500开始,metarader就阻止了此类指标的编译。 Marco Hinderink 2013.10.01 19:14 #1552 mladen: 这可能是由于该指标是一个反编译的指标。从500版本开始,metarader就阻止了此类指标的编译。 你好,MLaden。 谢谢,但奇怪的是,我使用的所有经纪公司的MT4平台都是500版本的。 问候马可 Chrisstoff 2013.10.02 09:52 #1553 我有一个EA,我想在其中使用部分获利 功能。我对这个功能进行了编码,有一次工作得很好,其他时候就不行了。我不知道问题出在哪里... 亲爱的Mladen或MrTools,你能不能看一下代码,给我一个建议,让我找到这个错误并解决部分获利功能? //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int signal; CheckOrders(); int numTrades = GetNumTickets(); signal = CheckSignal(); if (signal == OP_BUY) { OpenBuy(); lasttime = TimeCurrent(); } if (signal == OP_SELL) { OpenSell(); lasttime = TimeCurrent(); } return; } // +----------------------------------------------------------------------------+ // | Order checking function // +----------------------------------------------------------------------------+ void CheckOrders() { int typ,i,cnt,ticket; int Mv1 = 0; int SL1 = 0; int Mv2 = 0; int SL2 = 0; int Mv3 = 0; int id; double PipDist, CLots, buy_nextTP, sell_nextTP; cnt = OrdersTotal(); for (i=cnt-1; i>=0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { typ = OrderType(); // if (typ == OP_BUY) { if (TrailingStopLoss) { if (AdjTrailOnAllOrders(2, buy_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false) Alert("Scalp_RSI: Modifying BUY order failed!"); } PipDist = (NormalizeDouble(Bid,Digits) - NormalizeDouble(OrderOpenPrice(),Digits)); PipDist = MathFloor(PipDist/myPoint); } if (typ == OP_SELL) { if(TrailingStopLoss) { if (AdjTrailOnAllOrders(2, sell_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false) Alert("Scalp_RSI: Modifying SELL order failed!"); } PipDist = (NormalizeDouble(OrderOpenPrice(),Digits) - NormalizeDouble(Ask,Digits)); PipDist = MathFloor(PipDist/myPoint); } if (TakePartialProfit == true && PartCloseLots > 0) { if (UseMoneyMgmt != 1) Lotsi = Lots; CLots = NormalizeDouble(Lotsi * PartCloseLots/100,Decimals); // Computing lots to close if (CLots < MarketInfo(Symbol(), MODE_MINLOT)) CLots = MarketInfo(Symbol(), MODE_MINLOT); PartialTP(PipDist,OrderTicket(),CLots); } } } } } //+------------------------------------------------------------------+ //| PartialTP - i.e. TakePartialProfit function //+------------------------------------------------------------------+ void PartialTP(int pipsval, int ticket, double CLots) // { int gle; int loopcount = 0; string bER; string myInfo = "PartialTP"; if(OrderSelect(ticket, SELECT_BY_TICKET)==true) { if (OrderType() == OP_BUY) { if ((pipsval >= buy_PartialTP) && (pipsval < buy_TakeProfit+1)) // { while(true) { OrderClose(ticket,CLots,Bid,3,YellowGreen); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0 || gle==1) { logwrite(TradeComment,"CLOSE BUY "+myInfo+ " " + CLots + " at Bid= " + Bid); buy_PartialTP = buy_TakeProfit+1; // break; } else { logwrite(TradeComment,"---ERROR--- CLOSE BUY "+myInfo+bER+ " " + CLots + " at Bid= " + Bid); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"---ERROR--- Giving up on closing BUY order"); return(gle); } } } } if (OrderType() == OP_SELL) // { if ((pipsval >= sell_PartialTP) && (pipsval < sell_TakeProfit)) // { while(true) { OrderClose(ticket,CLots,Ask,3,YellowGreen); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0 || gle==1) { logwrite(TradeComment,"CLOSE SELL "+myInfo+ " " + CLots + " at Ask= " + Ask); sell_PartialTP = sell_TakeProfit; // break; } else { logwrite(TradeComment,"---ERROR--- CLOSE SELL "+myInfo+bER+ " " + CLots + " at Ask= " + Ask); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"---ERROR--- Giving up on closing SELL order"); return(gle); } } } } } else { gle=GetLastError(); bER=" Error="+gle+" "+ErrorDescription(gle); if (gle != 0 || gle!=1) { logwrite(TradeComment,"---ERROR--- in selecting order in PartialTP function "+bER); } } } //+------------------------------------------------------------------+ //| Get number of open trades //+------------------------------------------------------------------+ int GetNumTickets() { int i; int typ; numTickets = 0; for (i = OrdersTotal()-1; i >= 0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { typ = OrderType(); if (typ == OP_BUY || typ == OP_SELL) { numTickets ++; } } } } return (numTickets); } //+------------------------------------------------------------------+ //| Get signal //+------------------------------------------------------------------+ int CheckSignal() { return (SignalScalp()); } //+------------------------------------------------------------------+ //| Get scalp signal //+------------------------------------------------------------------+ int SignalScalp() { int signal=6; // if (enable_buy){ // Buy-Condition { signal = OP_BUY; } } // // if (enable_sell){ // Sell-Condition { signal = OP_SELL; } } // // // return(signal); } //----- // +----------------------------------------------------------------------------+ // | Buy Order // +----------------------------------------------------------------------------+ void OpenBuy() { int gle = 0; int ticket = 0; double SL = 0; double TP = 0; double LotsB = Lots; //GetLots(); int loopcount = 0; while(true) { ticket=OrderSend(Symbol(),OP_BUY,LotsB,Ask,Slippage,0,0,TradeComment,Magic,White); } } // +----------------------------------------------------------------------------+ // | Sell Order // +----------------------------------------------------------------------------+ void OpenSell() { int gle = 0; int ticket = 0; double SL = 0; double TP = 0; double LotsS = Lots; int loopcount = 0; while(true) { ticket=OrderSend(Symbol(),OP_SELL,LotsS,Bid,Slippage,0,0,TradeComment,Magic,Red); } } bool AdjTrailOnAllOrders( int TrailType, int TrailPips, int Magic, int Direction, int FirstMove, int FirstStopLoss, int SecondMove, int SecondStopLoss, int ThirdMove) { double retValue; return(retValue); } //+------------------------------------------------------------------+ //| SetComment function //+------------------------------------------------------------------+ void SetComment(string s) { TradeComment = s; } // +----------------------------------------------------------------------------+ // | Pip setting // +----------------------------------------------------------------------------+ double SetPoint() { double mPoint; return(mPoint); } // +----------------------------------------------------------------------------+ // | Log write function // +----------------------------------------------------------------------------+ void logwrite (string filename, string mydata) { int myhandle; } Coding help EA不会在多对上运行,需要帮助 EA: Lucky Mladen Rakic 2013.10.02 10:44 #1554 chrisstoff: 我有一个EA,我想在其中使用部分获利功能。我对这个功能进行了编码,有的时候效果很好,有的时候就不行了。我没能找出问题所在......亲爱的Mladen或MrTools,你能不能看一下代码,给我一个建议,我怎么能找到这个错误,解决部分取舍功能? //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { int signal; CheckOrders(); int numTrades = GetNumTickets(); signal = CheckSignal(); if (signal == OP_BUY) { OpenBuy(); lasttime = TimeCurrent(); } if (signal == OP_SELL) { OpenSell(); lasttime = TimeCurrent(); } return; } // +----------------------------------------------------------------------------+ // | Order checking function // +----------------------------------------------------------------------------+ void CheckOrders() { int typ,i,cnt,ticket; int Mv1 = 0; int SL1 = 0; int Mv2 = 0; int SL2 = 0; int Mv3 = 0; int id; double PipDist, CLots, buy_nextTP, sell_nextTP; cnt = OrdersTotal(); for (i=cnt-1; i>=0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { typ = OrderType(); // if (typ == OP_BUY) { if (TrailingStopLoss) { if (AdjTrailOnAllOrders(2, buy_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false) Alert("Scalp_RSI: Modifying BUY order failed!"); } PipDist = (NormalizeDouble(Bid,Digits) - NormalizeDouble(OrderOpenPrice(),Digits)); PipDist = MathFloor(PipDist/myPoint); } if (typ == OP_SELL) { if(TrailingStopLoss) { if (AdjTrailOnAllOrders(2, sell_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false) Alert("Scalp_RSI: Modifying SELL order failed!"); } PipDist = (NormalizeDouble(OrderOpenPrice(),Digits) - NormalizeDouble(Ask,Digits)); PipDist = MathFloor(PipDist/myPoint); } if (TakePartialProfit == true && PartCloseLots > 0) { if (UseMoneyMgmt != 1) Lotsi = Lots; CLots = NormalizeDouble(Lotsi * PartCloseLots/100,Decimals); // Computing lots to close if (CLots < MarketInfo(Symbol(), MODE_MINLOT)) CLots = MarketInfo(Symbol(), MODE_MINLOT); PartialTP(PipDist,OrderTicket(),CLots); } } } } } //+------------------------------------------------------------------+ //| PartialTP - i.e. TakePartialProfit function //+------------------------------------------------------------------+ void PartialTP(int pipsval, int ticket, double CLots) // { int gle; int loopcount = 0; string bER; string myInfo = "PartialTP"; if(OrderSelect(ticket, SELECT_BY_TICKET)==true) { if (OrderType() == OP_BUY) { if ((pipsval >= buy_PartialTP) && (pipsval < buy_TakeProfit+1)) // { while(true) { OrderClose(ticket,CLots,Bid,3,YellowGreen); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0 || gle==1) { logwrite(TradeComment,"CLOSE BUY "+myInfo+ " " + CLots + " at Bid= " + Bid); buy_PartialTP = buy_TakeProfit+1; // break; } else { logwrite(TradeComment,"---ERROR--- CLOSE BUY "+myInfo+bER+ " " + CLots + " at Bid= " + Bid); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"---ERROR--- Giving up on closing BUY order"); return(gle); } } } } if (OrderType() == OP_SELL) // { if ((pipsval >= sell_PartialTP) && (pipsval < sell_TakeProfit)) // { while(true) { OrderClose(ticket,CLots,Ask,3,YellowGreen); gle=GetLastError(); bER=" error="+gle+" "+ErrorDescription(gle); if(gle==0 || gle==1) { logwrite(TradeComment,"CLOSE SELL "+myInfo+ " " + CLots + " at Ask= " + Ask); sell_PartialTP = sell_TakeProfit; // break; } else { logwrite(TradeComment,"---ERROR--- CLOSE SELL "+myInfo+bER+ " " + CLots + " at Ask= " + Ask); RefreshRates(); Sleep(500); } loopcount++; if(loopcount>maxloop) { logwrite(TradeComment,"---ERROR--- Giving up on closing SELL order"); return(gle); } } } } } else { gle=GetLastError(); bER=" Error="+gle+" "+ErrorDescription(gle); if (gle != 0 || gle!=1) { logwrite(TradeComment,"---ERROR--- in selecting order in PartialTP function "+bER); } } } //+------------------------------------------------------------------+ //| Get number of open trades //+------------------------------------------------------------------+ int GetNumTickets() { int i; int typ; numTickets = 0; for (i = OrdersTotal()-1; i >= 0; i--) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) { if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic) { typ = OrderType(); if (typ == OP_BUY || typ == OP_SELL) { numTickets ++; } } } } return (numTickets); } //+------------------------------------------------------------------+ //| Get signal //+------------------------------------------------------------------+ int CheckSignal() { return (SignalScalp()); } //+------------------------------------------------------------------+ //| Get scalp signal //+------------------------------------------------------------------+ int SignalScalp() { int signal=6; // if (enable_buy){ // Buy-Condition { signal = OP_BUY; } } // // if (enable_sell){ // Sell-Condition { signal = OP_SELL; } } // // // return(signal); } //----- // +----------------------------------------------------------------------------+ // | Buy Order // +----------------------------------------------------------------------------+ void OpenBuy() { int gle = 0; int ticket = 0; double SL = 0; double TP = 0; double LotsB = Lots; //GetLots(); int loopcount = 0; while(true) { ticket=OrderSend(Symbol(),OP_BUY,LotsB,Ask,Slippage,0,0,TradeComment,Magic,White); } } // +----------------------------------------------------------------------------+ // | Sell Order // +----------------------------------------------------------------------------+ void OpenSell() { int gle = 0; int ticket = 0; double SL = 0; double TP = 0; double LotsS = Lots; int loopcount = 0; while(true) { ticket=OrderSend(Symbol(),OP_SELL,LotsS,Bid,Slippage,0,0,TradeComment,Magic,Red); } } bool AdjTrailOnAllOrders( int TrailType, int TrailPips, int Magic, int Direction, int FirstMove, int FirstStopLoss, int SecondMove, int SecondStopLoss, int ThirdMove) { double retValue; return(retValue); } void SetComment(string s) { TradeComment = s; } double SetPoint() { double mPoint; return(mPoint); } void logwrite (string filename, string mydata) { int myhandle; } 克里斯托夫 我看不出你对 "小数 "变量使用的是什么值,但这可能是你问题的原因。订单的结果部分也必须符合手数步长值(意思是:如果手数步长是0.1,你不能尝试关闭0.11手)。检查在不成功的部分平仓后的最后一个错误 是什么(你可以通过简单地在像这样的行后添加一个注释来做到这一点。 bER=" error="+gle+" "+ErrorDescription(gle)。 if (gle>1) Comment(bER); 因为代码中的logwrite函数没有操作性。 Chrisstoff 2013.10.02 11:14 #1555 Mladen, 谢谢你的答复。 嗯,我缩短了代码,因为一个帖子不可能超过10,000个字符。所以,在原来的代码中,小数变量是在init()中,如下图。 LotStep = MarketInfo(Symbol(),MODE_LOTSTEP); if(LotStep == 0.01) {Decimals = 2;} if(LotStep == 0.1) {Decimals = 1;} if(LotStep == 1) {Decimals = 0;}[/PHP] Yes, logwrite function is not functionable in the code posted, also because of the shortening. Normally it looks like: [PHP] void logwrite (string filename, string mydata) { int myhandle; string wcalend=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS); Print(mydata+" "+wcalend); if(IsTesting()) return(0); myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";"); if(myhandle>0) { FileSeek(myhandle,0,SEEK_END); FileWrite(myhandle, mydata+" "+wcalend); FileClose(myhandle); } } 所以,我认为问题的出现不是因为小数变量。 Coding help Problem with AccountMargin() 在MT5的EA中能否把 Print("xxxxx ) 和 Mladen Rakic 2013.10.02 11:22 #1556 chrisstoff: Mladen,谢谢你的答复。 嗯,我缩短了代码,因为一个帖子不可能超过10,000个字符。所以,在原来的代码中,小数变量是在init()中,如下所示。 LotStep = MarketInfo(Symbol(),MODE_LOTSTEP); if(LotStep == 0.01) {Decimals = 2;} if(LotStep == 0.1) {Decimals = 1;} if(LotStep == 1) {Decimals = 0;}[/PHP] Yes, logwrite function is not functionable in the code posted, also because of the shortening. Normally it looks like: [PHP] void logwrite (string filename, string mydata) { int myhandle; string wcalend=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS); Print(mydata+" "+wcalend); if(IsTesting()) return(0); myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";"); if(myhandle>0) { FileSeek(myhandle,0,SEEK_END); FileWrite(myhandle, mydata+" "+wcalend); FileClose(myhandle); } } 所以,我认为问题不是因为小数变量而出现的。 克里斯托夫 这样一来,小数变量就应该没问题了 无论如何,请检查在订单(部分)关闭不成功的情况下,你得到的错误代码 是什么? Hock 2013.10.02 11:35 #1557 mladen: 我看到你没有使用神奇数字,所以我的假设是,神奇数字或符号是什么并不重要。在这种情况下,首先要检查是否已经有一个开放的订单--像这样。if (OrdersTotal()<1) { ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0); stop=(Ask-stopsize*Point); prof=(Ask+profsize*Point); OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue); } 如果你决定使用神奇数字和符号检查,那么最好做一个函数,计算指定符号和/或神奇数字的已开订单。 谢谢mladen。 如果(OrdersTotal()<1 这解决了我的问题。 总共可以订购一个,但我希望每一对货币都能打开。 对不起,我误解了。 但我的意思是在每个货币对中执行一个订单并停止。 例如。 现在我在欧元兑美元开立订单,只执行一个订单。 那么其他货币也可以在同一时间打开,执行一个订单。 很抱歉,我的英语不好。 ----------------------------------------------------------------- 谢谢。 Mladen Rakic 2013.10.02 11:48 #1558 hock87: 谢谢mladen。如果(OrdersTotal()<1 它解决了我的问题。 它总共可以订购一个,但我希望每一对货币都能打开。 对不起,我误解了。 但我的意思是在每个货币对中执行一个订单并停止。 例如。 现在我在欧元兑美元开立订单,只执行一个订单。 那么其他货币也可以在同一时间打开,执行一个订单。 很抱歉,我的英语不好。 ----------------------------------------------------------------- 谢谢。 hock87 添加这个: int TotalOrders = 0; for (int i=0; i <= OrdersTotal(); i++) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if (OrderSymbol() == Symbol()) TotalOrders++; } 在if (OrdersTotal()<1) 一行之前,用if (TotalOrders<1) 替换这一行。这样一来,每个符号只允许有一个开仓订单 Hock 2013.10.02 13:51 #1559 非常感谢你,mladen。 我现在试试。 Mladen Rakic 2013.10.02 13:53 #1560 hock87: 谢谢mladen。如果(OrdersTotal()<1 它解决了我的问题。 它总共可以订购一个,但我希望每一对货币都能打开。 对不起,我误解了。 但我的意思是在每个货币对中执行一个订单并停止。 例如。 现在我在欧元兑美元开立订单,只执行一个订单。 那么其他货币也可以在同一时间打开,执行一个订单。 很抱歉,我的英语不好。 ----------------------------------------------------------------- 谢谢。 hock87 请阅读您帖子上方的帖子,了解该问题的解决方案 该问题的完整代码如下。 int TotalOrders = 0; for (int i=0; i <= OrdersTotal(); i++) { if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) if (OrderSymbol() == Symbol()) TotalOrders++; } if (TotalOrders<1) { ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0); stop=(Ask-stopsize*Point); prof=(Ask+profsize*Point); OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue); } Coding help Basic questions ... [存档]任何菜鸟问题,为了不使论坛变得杂乱无章。专业人士,不要路过。没有你就无处可去 - 3. 1...149150151152153154155156157158159160161162163...786 新评论 您错过了交易机会: 免费交易应用程序 8,000+信号可供复制 探索金融市场的经济新闻 注册 登录 拉丁字符(不带空格) 密码将被发送至该邮箱 发生错误 使用 Google 登录 您同意网站政策和使用条款 如果您没有帐号,请注册 可以使用cookies登录MQL5.com网站。 请在您的浏览器中启用必要的设置,否则您将无法登录。 忘记您的登录名/密码? 使用 Google 登录
亲爱的MLaden。
接下来我可以在我的真实账户和Markets.com的模拟账户(差价合约)上获得图表。在Liquid Markets的模拟账户上,是否有问题?因为Liquid提供了大量的工具选择(尤其是MT4的期货),我想在该平台上把它显示出来。你能不能看到这个问题的出现,因为我找不到。
谢谢你,马可这可能是由于该指标是一个反编译的指标。从Build 500开始,metarader就阻止了此类指标的编译。
这可能是由于该指标是一个反编译的指标。从500版本开始,metarader就阻止了此类指标的编译。
你好,MLaden。
谢谢,但奇怪的是,我使用的所有经纪公司的MT4平台都是500版本的。
问候马可
我有一个EA,我想在其中使用部分获利 功能。我对这个功能进行了编码,有一次工作得很好,其他时候就不行了。我不知道问题出在哪里...
亲爱的Mladen或MrTools,你能不能看一下代码,给我一个建议,让我找到这个错误并解决部分获利功能?
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int signal;
CheckOrders();
int numTrades = GetNumTickets();
signal = CheckSignal();
if (signal == OP_BUY)
{
OpenBuy();
lasttime = TimeCurrent();
}
if (signal == OP_SELL)
{
OpenSell();
lasttime = TimeCurrent();
}
return;
}
// +----------------------------------------------------------------------------+
// | Order checking function
// +----------------------------------------------------------------------------+
void CheckOrders()
{
int typ,i,cnt,ticket;
int Mv1 = 0;
int SL1 = 0;
int Mv2 = 0;
int SL2 = 0;
int Mv3 = 0;
int id;
double PipDist, CLots, buy_nextTP, sell_nextTP;
cnt = OrdersTotal();
for (i=cnt-1; i>=0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
typ = OrderType();
//
if (typ == OP_BUY)
{
if (TrailingStopLoss)
{
if (AdjTrailOnAllOrders(2, buy_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)
Alert("Scalp_RSI: Modifying BUY order failed!");
}
PipDist = (NormalizeDouble(Bid,Digits) - NormalizeDouble(OrderOpenPrice(),Digits));
PipDist = MathFloor(PipDist/myPoint);
}
if (typ == OP_SELL)
{
if(TrailingStopLoss)
{
if (AdjTrailOnAllOrders(2, sell_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)
Alert("Scalp_RSI: Modifying SELL order failed!");
}
PipDist = (NormalizeDouble(OrderOpenPrice(),Digits) - NormalizeDouble(Ask,Digits));
PipDist = MathFloor(PipDist/myPoint);
}
if (TakePartialProfit == true && PartCloseLots > 0)
{
if (UseMoneyMgmt != 1) Lotsi = Lots;
CLots = NormalizeDouble(Lotsi * PartCloseLots/100,Decimals); // Computing lots to close
if (CLots < MarketInfo(Symbol(), MODE_MINLOT))
CLots = MarketInfo(Symbol(), MODE_MINLOT);
PartialTP(PipDist,OrderTicket(),CLots);
}
}
}
}
}
//+------------------------------------------------------------------+
//| PartialTP - i.e. TakePartialProfit function
//+------------------------------------------------------------------+
void PartialTP(int pipsval, int ticket, double CLots) //
{
int gle;
int loopcount = 0;
string bER;
string myInfo = "PartialTP";
if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
{
if (OrderType() == OP_BUY)
{
if ((pipsval >= buy_PartialTP) && (pipsval < buy_TakeProfit+1)) //
{
while(true)
{
OrderClose(ticket,CLots,Bid,3,YellowGreen);
gle=GetLastError();
bER=" error="+gle+" "+ErrorDescription(gle);
if(gle==0 || gle==1)
{
logwrite(TradeComment,"CLOSE BUY "+myInfo+ " " + CLots + " at Bid= " + Bid);
buy_PartialTP = buy_TakeProfit+1; //
break;
}
else
{
logwrite(TradeComment,"---ERROR--- CLOSE BUY "+myInfo+bER+ " " + CLots + " at Bid= " + Bid);
RefreshRates();
Sleep(500);
}
loopcount++;
if(loopcount>maxloop)
{
logwrite(TradeComment,"---ERROR--- Giving up on closing BUY order");
return(gle);
}
}
}
}
if (OrderType() == OP_SELL) //
{
if ((pipsval >= sell_PartialTP) && (pipsval < sell_TakeProfit)) //
{
while(true)
{
OrderClose(ticket,CLots,Ask,3,YellowGreen);
gle=GetLastError();
bER=" error="+gle+" "+ErrorDescription(gle);
if(gle==0 || gle==1)
{
logwrite(TradeComment,"CLOSE SELL "+myInfo+ " " + CLots + " at Ask= " + Ask);
sell_PartialTP = sell_TakeProfit; //
break;
}
else
{
logwrite(TradeComment,"---ERROR--- CLOSE SELL "+myInfo+bER+ " " + CLots + " at Ask= " + Ask);
RefreshRates();
Sleep(500);
}
loopcount++;
if(loopcount>maxloop)
{
logwrite(TradeComment,"---ERROR--- Giving up on closing SELL order");
return(gle);
}
}
}
}
} else {
gle=GetLastError();
bER=" Error="+gle+" "+ErrorDescription(gle);
if (gle != 0 || gle!=1)
{
logwrite(TradeComment,"---ERROR--- in selecting order in PartialTP function "+bER);
}
}
}
//+------------------------------------------------------------------+
//| Get number of open trades
//+------------------------------------------------------------------+
int GetNumTickets()
{
int i;
int typ;
numTickets = 0;
for (i = OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
typ = OrderType();
if (typ == OP_BUY || typ == OP_SELL)
{
numTickets ++;
}
}
}
}
return (numTickets);
}
//+------------------------------------------------------------------+
//| Get signal
//+------------------------------------------------------------------+
int CheckSignal()
{
return (SignalScalp());
}
//+------------------------------------------------------------------+
//| Get scalp signal
//+------------------------------------------------------------------+
int SignalScalp()
{
int signal=6; //
if (enable_buy){
// Buy-Condition
{
signal = OP_BUY;
}
}
//
//
if (enable_sell){
// Sell-Condition
{
signal = OP_SELL;
}
}
//
//
//
return(signal);
}
//-----
// +----------------------------------------------------------------------------+
// | Buy Order
// +----------------------------------------------------------------------------+
void OpenBuy()
{
int gle = 0;
int ticket = 0;
double SL = 0;
double TP = 0;
double LotsB = Lots; //GetLots();
int loopcount = 0;
while(true)
{
ticket=OrderSend(Symbol(),OP_BUY,LotsB,Ask,Slippage,0,0,TradeComment,Magic,White);
}
}
// +----------------------------------------------------------------------------+
// | Sell Order
// +----------------------------------------------------------------------------+
void OpenSell()
{
int gle = 0;
int ticket = 0;
double SL = 0;
double TP = 0;
double LotsS = Lots;
int loopcount = 0;
while(true)
{
ticket=OrderSend(Symbol(),OP_SELL,LotsS,Bid,Slippage,0,0,TradeComment,Magic,Red);
}
}
bool AdjTrailOnAllOrders(
int TrailType,
int TrailPips,
int Magic,
int Direction,
int FirstMove,
int FirstStopLoss,
int SecondMove,
int SecondStopLoss,
int ThirdMove)
{
double retValue;
return(retValue);
}
//+------------------------------------------------------------------+
//| SetComment function
//+------------------------------------------------------------------+
void SetComment(string s) { TradeComment = s; }
// +----------------------------------------------------------------------------+
// | Pip setting
// +----------------------------------------------------------------------------+
double SetPoint()
{
double mPoint;
return(mPoint);
}
// +----------------------------------------------------------------------------+
// | Log write function
// +----------------------------------------------------------------------------+
void logwrite (string filename, string mydata)
{
int myhandle;
}
我有一个EA,我想在其中使用部分获利功能。我对这个功能进行了编码,有的时候效果很好,有的时候就不行了。我没能找出问题所在......
亲爱的Mladen或MrTools,你能不能看一下代码,给我一个建议,我怎么能找到这个错误,解决部分取舍功能?
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int signal;
CheckOrders();
int numTrades = GetNumTickets();
signal = CheckSignal();
if (signal == OP_BUY)
{
OpenBuy();
lasttime = TimeCurrent();
}
if (signal == OP_SELL)
{
OpenSell();
lasttime = TimeCurrent();
}
return;
}
// +----------------------------------------------------------------------------+
// | Order checking function
// +----------------------------------------------------------------------------+
void CheckOrders()
{
int typ,i,cnt,ticket;
int Mv1 = 0;
int SL1 = 0;
int Mv2 = 0;
int SL2 = 0;
int Mv3 = 0;
int id;
double PipDist, CLots, buy_nextTP, sell_nextTP;
cnt = OrdersTotal();
for (i=cnt-1; i>=0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
typ = OrderType();
//
if (typ == OP_BUY)
{
if (TrailingStopLoss)
{
if (AdjTrailOnAllOrders(2, buy_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)
Alert("Scalp_RSI: Modifying BUY order failed!");
}
PipDist = (NormalizeDouble(Bid,Digits) - NormalizeDouble(OrderOpenPrice(),Digits));
PipDist = MathFloor(PipDist/myPoint);
}
if (typ == OP_SELL)
{
if(TrailingStopLoss)
{
if (AdjTrailOnAllOrders(2, sell_Trailpips, Magic, typ, Mv1, SL1, Mv2, SL2, Mv3) == false)
Alert("Scalp_RSI: Modifying SELL order failed!");
}
PipDist = (NormalizeDouble(OrderOpenPrice(),Digits) - NormalizeDouble(Ask,Digits));
PipDist = MathFloor(PipDist/myPoint);
}
if (TakePartialProfit == true && PartCloseLots > 0)
{
if (UseMoneyMgmt != 1) Lotsi = Lots;
CLots = NormalizeDouble(Lotsi * PartCloseLots/100,Decimals); // Computing lots to close
if (CLots < MarketInfo(Symbol(), MODE_MINLOT))
CLots = MarketInfo(Symbol(), MODE_MINLOT);
PartialTP(PipDist,OrderTicket(),CLots);
}
}
}
}
}
//+------------------------------------------------------------------+
//| PartialTP - i.e. TakePartialProfit function
//+------------------------------------------------------------------+
void PartialTP(int pipsval, int ticket, double CLots) //
{
int gle;
int loopcount = 0;
string bER;
string myInfo = "PartialTP";
if(OrderSelect(ticket, SELECT_BY_TICKET)==true)
{
if (OrderType() == OP_BUY)
{
if ((pipsval >= buy_PartialTP) && (pipsval < buy_TakeProfit+1)) //
{
while(true)
{
OrderClose(ticket,CLots,Bid,3,YellowGreen);
gle=GetLastError();
bER=" error="+gle+" "+ErrorDescription(gle);
if(gle==0 || gle==1)
{
logwrite(TradeComment,"CLOSE BUY "+myInfo+ " " + CLots + " at Bid= " + Bid);
buy_PartialTP = buy_TakeProfit+1; //
break;
}
else
{
logwrite(TradeComment,"---ERROR--- CLOSE BUY "+myInfo+bER+ " " + CLots + " at Bid= " + Bid);
RefreshRates();
Sleep(500);
}
loopcount++;
if(loopcount>maxloop)
{
logwrite(TradeComment,"---ERROR--- Giving up on closing BUY order");
return(gle);
}
}
}
}
if (OrderType() == OP_SELL) //
{
if ((pipsval >= sell_PartialTP) && (pipsval < sell_TakeProfit)) //
{
while(true)
{
OrderClose(ticket,CLots,Ask,3,YellowGreen);
gle=GetLastError();
bER=" error="+gle+" "+ErrorDescription(gle);
if(gle==0 || gle==1)
{
logwrite(TradeComment,"CLOSE SELL "+myInfo+ " " + CLots + " at Ask= " + Ask);
sell_PartialTP = sell_TakeProfit; //
break;
}
else
{
logwrite(TradeComment,"---ERROR--- CLOSE SELL "+myInfo+bER+ " " + CLots + " at Ask= " + Ask);
RefreshRates();
Sleep(500);
}
loopcount++;
if(loopcount>maxloop)
{
logwrite(TradeComment,"---ERROR--- Giving up on closing SELL order");
return(gle);
}
}
}
}
} else {
gle=GetLastError();
bER=" Error="+gle+" "+ErrorDescription(gle);
if (gle != 0 || gle!=1)
{
logwrite(TradeComment,"---ERROR--- in selecting order in PartialTP function "+bER);
}
}
}
//+------------------------------------------------------------------+
//| Get number of open trades
//+------------------------------------------------------------------+
int GetNumTickets()
{
int i;
int typ;
numTickets = 0;
for (i = OrdersTotal()-1; i >= 0; i--)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
typ = OrderType();
if (typ == OP_BUY || typ == OP_SELL)
{
numTickets ++;
}
}
}
}
return (numTickets);
}
//+------------------------------------------------------------------+
//| Get signal
//+------------------------------------------------------------------+
int CheckSignal()
{
return (SignalScalp());
}
//+------------------------------------------------------------------+
//| Get scalp signal
//+------------------------------------------------------------------+
int SignalScalp()
{
int signal=6; //
if (enable_buy){
// Buy-Condition
{
signal = OP_BUY;
}
}
//
//
if (enable_sell){
// Sell-Condition
{
signal = OP_SELL;
}
}
//
//
//
return(signal);
}
//-----
// +----------------------------------------------------------------------------+
// | Buy Order
// +----------------------------------------------------------------------------+
void OpenBuy()
{
int gle = 0;
int ticket = 0;
double SL = 0;
double TP = 0;
double LotsB = Lots; //GetLots();
int loopcount = 0;
while(true)
{
ticket=OrderSend(Symbol(),OP_BUY,LotsB,Ask,Slippage,0,0,TradeComment,Magic,White);
}
}
// +----------------------------------------------------------------------------+
// | Sell Order
// +----------------------------------------------------------------------------+
void OpenSell()
{
int gle = 0;
int ticket = 0;
double SL = 0;
double TP = 0;
double LotsS = Lots;
int loopcount = 0;
while(true)
{
ticket=OrderSend(Symbol(),OP_SELL,LotsS,Bid,Slippage,0,0,TradeComment,Magic,Red);
}
}
bool AdjTrailOnAllOrders(
int TrailType,
int TrailPips,
int Magic,
int Direction,
int FirstMove,
int FirstStopLoss,
int SecondMove,
int SecondStopLoss,
int ThirdMove)
{
double retValue;
return(retValue);
}
void SetComment(string s) { TradeComment = s; }
double SetPoint()
{
double mPoint;
return(mPoint);
}
void logwrite (string filename, string mydata)
{
int myhandle;
}
克里斯托夫
我看不出你对 "小数 "变量使用的是什么值,但这可能是你问题的原因。订单的结果部分也必须符合手数步长值(意思是:如果手数步长是0.1,你不能尝试关闭0.11手)。检查在不成功的部分平仓后的最后一个错误 是什么(你可以通过简单地在像这样的行后添加一个注释来做到这一点。
bER=" error="+gle+" "+ErrorDescription(gle)。
if (gle>1) Comment(bER);
因为代码中的logwrite函数没有操作性。
Mladen,
谢谢你的答复。
嗯,我缩短了代码,因为一个帖子不可能超过10,000个字符。所以,在原来的代码中,小数变量是在init()中,如下图。
LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
if(LotStep == 0.01) {Decimals = 2;}
if(LotStep == 0.1) {Decimals = 1;}
if(LotStep == 1) {Decimals = 0;}[/PHP]
Yes, logwrite function is not functionable in the code posted, also because of the shortening. Normally it looks like:
[PHP]
void logwrite (string filename, string mydata)
{
int myhandle;
string wcalend=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);
Print(mydata+" "+wcalend);
if(IsTesting()) return(0);
myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";");
if(myhandle>0)
{
FileSeek(myhandle,0,SEEK_END);
FileWrite(myhandle, mydata+" "+wcalend);
FileClose(myhandle);
}
}所以,我认为问题的出现不是因为小数变量。
Mladen,
谢谢你的答复。
嗯,我缩短了代码,因为一个帖子不可能超过10,000个字符。所以,在原来的代码中,小数变量是在init()中,如下所示。
LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
if(LotStep == 0.01) {Decimals = 2;}
if(LotStep == 0.1) {Decimals = 1;}
if(LotStep == 1) {Decimals = 0;}[/PHP]
Yes, logwrite function is not functionable in the code posted, also because of the shortening. Normally it looks like:
[PHP]
void logwrite (string filename, string mydata)
{
int myhandle;
string wcalend=TimeToStr(CurTime(),TIME_DATE|TIME_SECONDS);
Print(mydata+" "+wcalend);
if(IsTesting()) return(0);
myhandle=FileOpen(Symbol()+"_"+filename, FILE_CSV|FILE_WRITE|FILE_READ, ";");
if(myhandle>0)
{
FileSeek(myhandle,0,SEEK_END);
FileWrite(myhandle, mydata+" "+wcalend);
FileClose(myhandle);
}
}克里斯托夫
这样一来,小数变量就应该没问题了
无论如何,请检查在订单(部分)关闭不成功的情况下,你得到的错误代码 是什么?
我看到你没有使用神奇数字,所以我的假设是,神奇数字或符号是什么并不重要。在这种情况下,首先要检查是否已经有一个开放的订单--像这样。
{
ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0);
stop=(Ask-stopsize*Point);
prof=(Ask+profsize*Point);
OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);
}
谢谢mladen。
如果(OrdersTotal()<1
这解决了我的问题。
总共可以订购一个,但我希望每一对货币都能打开。
对不起,我误解了。
但我的意思是在每个货币对中执行一个订单并停止。 例如。
现在我在欧元兑美元开立订单,只执行一个订单。 那么其他货币也可以在同一时间打开,执行一个订单。 很抱歉,我的英语不好。
-----------------------------------------------------------------
谢谢。
谢谢mladen。
如果(OrdersTotal()<1
它解决了我的问题。
它总共可以订购一个,但我希望每一对货币都能打开。
对不起,我误解了。
但我的意思是在每个货币对中执行一个订单并停止。 例如。
现在我在欧元兑美元开立订单,只执行一个订单。 那么其他货币也可以在同一时间打开,执行一个订单。 很抱歉,我的英语不好。
-----------------------------------------------------------------
谢谢。
hock87
添加这个:
for (int i=0; i <= OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderSymbol() == Symbol())
TotalOrders++;
}
在if (OrdersTotal()<1) 一行之前,用if (TotalOrders<1) 替换这一行。这样一来,每个符号只允许有一个开仓订单
非常感谢你,mladen。
我现在试试。
谢谢mladen。
如果(OrdersTotal()<1
它解决了我的问题。
它总共可以订购一个,但我希望每一对货币都能打开。
对不起,我误解了。
但我的意思是在每个货币对中执行一个订单并停止。 例如。
现在我在欧元兑美元开立订单,只执行一个订单。 那么其他货币也可以在同一时间打开,执行一个订单。 很抱歉,我的英语不好。
-----------------------------------------------------------------
谢谢。
hock87
请阅读您帖子上方的帖子,了解该问题的解决方案
该问题的完整代码如下。
int TotalOrders = 0;
for (int i=0; i <= OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
if (OrderSymbol() == Symbol())
TotalOrders++;
}
if (TotalOrders<1)
{
ticket=OrderSend(Symbol(), OP_BUY, lots, Ask, 3, 0, 0);
stop=(Ask-stopsize*Point);
prof=(Ask+profsize*Point);
OrderModify( ticket, OrderOpenPrice(), stop, prof, 0, Blue);
}