- OrdersTotal() for specific currency pair
- {MQL4) How to know OrdersTotal() is decreased.
- OrdersTotal for every Symbol
You can do it like this
int totalForCurrentSymbol = 0; for (int i = OrdersTotal()-1; i>=0; i--) { if (!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; if (OrderSymbol() != "EURGBP") continue; totalForCurrentSymbol++; }And after that the totalForSymbol will contain the exact number of orders currently opened for EURGBP. Then it is just a matter of comparison
You can do it like this
And after that the totalForSymbol will contain the exact number of orders currently opened for EURGBP. Then it is just a matter of comparisonNow, the EA is opening SEVERAL orders, inifinite orders...
You can do it like this
And after that the totalForSymbol will contain the exact number of orders currently opened for EURGBP. Then it is just a matter of comparisonDidn't work! :/
Now, the EA is opening SEVERAL orders, inifinite orders...
If you check the following :
if (totalForCurrentSymbol==0) ... // then you can open new order for EURGBP in that example
it will work and there is no way that then you can open infinite orders for EURGBP. The code I posted counts open orders for the symbol you need. I am assuming that you can use the values stored in the totalForCurrent symbol in the rest of your code (of which I have no idea what are you doing with it)
If you check the following :
it will work and there is no way that then you can open infinite orders for EURGBP. The code I posted counts open orders for the symbol you need. I am assuming that you can use the values stored in the totalForCurrent symbol in the rest of your code (of which I have no idea what are you doing with it)
Thank you very much again! :)
But, I'm not getting the wished result...
But, the EA continues opening orders and more orders and more orders...
Look the code after your tip:
//+------------------------------------------------------------------+ //| Ben_trail_stop.mq4 | //| Copyright © 2010, www.compu-forex.com | //| www.compu-forex.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, www.compu-forex.com" #property link "www.compu-forex.com" //---- input parameters extern int MagicNumber = 0; extern double Trail_From = 10.0; extern double Trail_Max = 50.0; extern double Trail_Percent = 50; extern int Equity_Cut =10; int totalForCurrentSymbol = 0; int D_Factor = 1; double Pip; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { for (int i = OrdersTotal()-1; i>=0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderSymbol() != "BTCEUR") continue; totalForCurrentSymbol++; } Pip = Point; if (Digits == 3||Digits == 5){Pip *= 10; D_Factor = 10;} Trail_From *= Pip; Trail_Max *= Pip; //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { if(totalForCurrentSymbol==0){ OrderSend(Symbol(), 0, 0.01, Ask, 3, 0 ,0 ,0, 123, 0,CLR_NONE); OrderSend(Symbol(), 1, 0.01, Bid, 3, 0 ,0 ,0, 123, 0,CLR_NONE); } bool mod; double My_Profit, My_Trail; //---- for (int i = 0; i < OrdersTotal(); i++){ if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderMagicNumber() == MagicNumber || MagicNumber == 0){ RefreshRates(); switch(OrderType()){ case OP_BUY : My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice(); My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max); if(My_Profit > Trail_From){ if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red); if(OrderStopLoss() < OrderOpenPrice() + My_Trail){ mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red); if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError()); } } break; case OP_SELL : My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK); My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max); if(My_Profit > Trail_From){ if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red); if(OrderStopLoss() > OrderOpenPrice() - My_Trail){ mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red); if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError()); } } break; } } } else Print("Error selecting order"); } //---- return(0); } //+------------------------------------------------------------------+
Thank you very much again! :)
But, I'm not getting the wished result...
But, the EA continues opening orders and more orders and more orders...
Look the code after your tip:
Move that code from init() to start() (and execute it on each and every tick before checking the
if(totalForCurrentSymbol==0){ OrderSend(Symbol(), 0, 0.01, Ask, 3, 0 ,0 ,0, 123, 0,CLR_NONE); OrderSend(Symbol(), 1, 0.01, Bid, 3, 0 ,0 ,0, 123, 0,CLR_NONE); }
And move the
int totalForCurrentSymbol = 0;
declaration to start() too
Move that code from init() to start() (and execute it on each and every tick before checking the
And move the
declaration to start() too
Try like this
//+------------------------------------------------------------------+ //| Ben_trail_stop.mq4 | //| Copyright © 2010, www.compu-forex.com | //| www.compu-forex.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, www.compu-forex.com" #property link "www.compu-forex.com" //---- input parameters extern int MagicNumber = 0; extern double Trail_From = 10.0; extern double Trail_Max = 50.0; extern double Trail_Percent = 50; extern int Equity_Cut =10; int D_Factor = 1; double Pip; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { Pip = Point; if (Digits == 3||Digits == 5){Pip *= 10; D_Factor = 10;} Trail_From *= Pip; Trail_Max *= Pip; //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { bool mod; double My_Profit, My_Trail; int totalForCurrentSymbol = 0; for (int i = OrdersTotal()-1; i>=0; i--) { if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue; if(OrderSymbol() != "BTCEUR") continue; totalForCurrentSymbol++; } if(totalForCurrentSymbol==0){ OrderSend(Symbol(), 0, 0.01, Ask, 3, 0 ,0 ,0, 123, 0,CLR_NONE); OrderSend(Symbol(), 1, 0.01, Bid, 3, 0 ,0 ,0, 123, 0,CLR_NONE); } //---- for (int i = 0; i < OrdersTotal(); i++){ if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)){ if(OrderMagicNumber() == MagicNumber || MagicNumber == 0){ RefreshRates(); switch(OrderType()){ case OP_BUY : My_Profit = MarketInfo(OrderSymbol(), MODE_BID) - OrderOpenPrice(); My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max); if(My_Profit > Trail_From){ if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red); if(OrderStopLoss() < OrderOpenPrice() + My_Trail){ mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()+My_Trail,Digits),OrderTakeProfit(),0, Red); if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError()); } } break; case OP_SELL : My_Profit = OrderOpenPrice() - MarketInfo(OrderSymbol(), MODE_ASK); My_Trail = MathMin(My_Profit * Trail_Percent/100,Trail_Max); if(My_Profit > Trail_From){ if(OrderStopLoss() == 0)mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red); if(OrderStopLoss() > OrderOpenPrice() - My_Trail){ mod = OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(OrderOpenPrice()-My_Trail,Digits),OrderTakeProfit(),0,Red); if(!mod && GetLastError() > 1)Print("Error entering Trailing Stop - Error " + GetLastError()); } } break; } } } else Print("Error selecting order"); } //---- return(0); } //+------------------------------------------------------------------+
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use