Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 78

 
Please respond in order of priority. I have an indicator displayed by a bar graph. When joining or after reconnecting it shows an op-penny value. I have set the condition on the first start to reset the 0th cell of the array. And I set it in inite - it did not help. I reset it at start - ok. (I put condition at start: if(GetLastError()==6)zas[0]=0.0; - does not help.
 
dikson1976-1:
Please respond in order of priority. I have an indicator displayed by a bar graph. When joining or after reconnecting it shows an op-penny value. I have set the condition on the first start to reset the 0th cell of the array. And I set it in inite - it did not help. I reset it at start - ok. (I put condition at start: if(GetLastError()==6)zas[0]=0.0; - does not help.

I cannot do without code.
 

Dozol:



Try going through the orders in reverse order.

 for(i=OrdersTotal()-1;i>=0;i--)
      {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
         {
     // и тд
         }
       }
 
Handled it myself. Take it easy. Thank you all.
 
Dozol:

Friends, help me out. I need to close all orders on a selected currency pair. Here's what I wrote (part of EA code):

int Zakrit_vse() ...

Use SRC to paste code on forum

I will paste it here, maybe you will find something interesting for yourselves:

int Zakrit_vse()
{  
if (OrdersTotal()>0)
{  for (int i=OrdersTotal()-1; i>=0; i--)
   {  if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
      {  if (OrderSymbol()!=Symbol()) continue;
      
         //--- Close pending orders
         if (OrderType()==OP_BUYLIMIT  ||
             OrderType()==OP_BUYSTOP   ||
             OrderType()==OP_SELLLIMIT ||
             OrderType()==OP_SELLSTOP)         
         {  
            OrderDelete(OrderTicket(),CLR_NONE)
            Prov_oshibok();
            return(0);
         }
         
         //--- Close opened position BUY
         if (OrderType()==OP_BUY)
         {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),Bid,100,CLR_NONE);
            Prov_oshibok();
            return(0);
         }
         //--- Close opened position SELL
         if (OrderType()==OP_SELL)
         {
            RefreshRates();
            OrderClose(OrderTicket(),OrderLots(),Ask,100,CLR_NONE);
            Prov_oshibok();
            return(0);
         }
}  }  }
}
 
Dozol:

Friends, help me out. I need to close all orders on a selected currency pair. Here is what I have written (part of the EA code):


Why reinvent the wheel when everything has already been invented before you. For example. Don't forget this.
 

Help! I have code for an external channel indicator (when the upper limit of the channel is reached, the trade opens to sell - closes when the lower limit is reached, to buy in reverse order). For unknown reasons, it does not open trades, I cannot find the error. Works perfectly with another channel indicator. I would like to specify right away that all of the buffers are set.

The code is attached.

extern double Lots = 0.1;
extern string Comment = "TMA Canal2";
extern int TakeProfit = 20;
extern int StopLoss = 20;
extern int Slippage = 2; // slippage
extern int Magic = 124;

extern string Indi = "Indicator data";
extern string TimeFrame = "current time frame;
extern int TMAPeriod = 56;
extern int Price = PRICE_CLOSE;
extern double ATRMultiplier = 2.0;
extern intTRPeriod = 100;
extern double TrendThreshold = 0.5;
extern bool ShowCenterLine = false;

double PriceHigh, PriceLow, SL, TP;
int ticket;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
if (Digits ==3 || Digits ==3) // for a five-digit broker
{
TakeProfit *= 10;
StopLoss *= 10;
Slippage *= 10;
}
return(0);
}

//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//----
for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderSymbol()==Symbol() && OrderMagicNumber()==124)
{
if(OrderType()==OP_BUY) buys++;
if(OrderType()==OP_SELL) sells++;
}
}
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}

//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
double PriceHigh, PriceLow;
int ticket;
//---- go trading only for first tics of new bar
if(Volume[0]>1) return;

PriceHigh = iCustom (Symbol(), 0, "TMALine", TimeFrame, TMAPeriod, Price, ATRMultiplier, ATRPeriod, TrendThreshold, ShowCenterLine, 1, 0);
PriceLow = iCustom (Symbol(), 0, "TMALine", TimeFrame, TMAPeriod, Price, ATRMultiplier, ATRPeriod, TrendThreshold, ShowCenterLine, 2, 0);


if(Open[1]>PriceHigh || Close[1]>PriceHigh || High[1]>PriceHigh && Ask<=PriceHigh)

{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0, "TMA Canal2",124,0,Red);
return;
}
//---- buy conditions
if(Open[1]<PriceLow || Close[1]<PriceLow || Low[1]<PriceLow && Bid>=PriceLow)

{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0, "TMA Canal2",124,0,Blue);
return;
}
//----
}
//+------------------------------------------------------------------+
//| Check for close order conditions |
//+------------------------------------------------------------------+
void CheckForClose()
{
double PriceHigh, PriceLow;
//---- go trading only for first tics of new bar
if(Volume[0]>1) return;

PriceHigh = iCustom (Symbol(), 0, "TMALine", TimeFrame, TMAPeriod, Price, ATRMultiplier, ATRPeriod, TrendThreshold, ShowCenterLine, 1, 0);
PriceLow = iCustom (Symbol(), 0, "TMALine", TimeFrame, TMAPeriod, Price, ATRMultiplier, ATRPeriod, TrendThreshold, ShowCenterLine, 2, 0);

for(int i=0;i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderMagicNumber()!=124 || OrderSymbol()!=Symbol()) continue;
//---- check order type
if(OrderType()==OP_BUY)
{
if(Open[1]>PriceHigh || Close[1]>PriceHigh || High[1]>PriceHigh) OrderClose(OrderTicket(),OrderLots(),Bid,3,White);
break;
}
if(OrderType()==OP_SELL)
{
if(Open[1]<PriceLow || Close[1]<PriceLow || Low[1]<PriceLow) OrderClose(OrderTicket(),OrderLots(),Ask,3,White);
break;
}
}
//----
}

//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(Bars<100 || IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
else CheckForClose();
//----
}
//+------------------------------------------------------------------+

 
ZahvatkiN:

I am new to mql4, i know how to work with SL and TP, but i have a problem when i need to close orders by counter signal of the indicator. I don't really understand how OrderSelect() function works. I know that in order to search for an order we use a cycle, it is in the code, but in the process of trading it turns out that the order has just opened and immediately closed, because the conditions for opening a buy coincides with the conditions for closing a sell and how to explain it that this should not do I do not understand.

The SRC button is used to paste the code!

As you have written it, also make additions using the Documentation!

If you have copied, please contact the author for revisions or to Work!

 


I'll add one more time. How many times have I told you, use the button on the top line to insert the code

And it is desirable to stick to code writing styles.... Desirable, but not essential. But most people are more likely to reply with easy-to-understand code.

 

I am new to mql4, i know how to work with SL and TP, but i have a problem when i need to close orders by counter signal of the indicator. I don't really understand how OrderSelect() function works. I know that the loop is used to search for orders, it is in the code, but in the process of trading it turns out that the order has just opened and immediately closed, because the conditions for opening a buy coincide with the conditions for closing the sale and how to explain it that this should not do I do not understand, please explain.

borilunad thanks for the tip, redone it, then you can also delete that message with the quote so as not to clutter it up.

if(buy1sOpen())
         {
         OrderSend(Symbol(), OP_BUY, lots, Ask, Slippage, 0, 0, "", magic, 0, Blue);
         }
      if(sell1sOpen()) 
         {
         OrderSend(Symbol(), OP_SELL, lots, Bid, Slippage, 0, 0, "", magic, 0, Red);
         }
         
      for(int i=0; i<OrdersTotal(); i++)
         {
         OrderSelect(i,SELECT_BY_POS);
         if(buy1sClose()) 
           {
           OrderClose(i, lots, Ask, Slippage, Blue);
           }
         if(sell1sClose()) 
           {
           OrderClose(i, lots, Bid, Slippage, Red);
           }
         }
         
      }
   }
bool buy1sOpen()
   {
   Sm1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,1);
   Ss1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,1);
   Sm2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,2);
   Ss2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,2);
   if(Sm1>Ss1 && Sm2<Ss2 && Sm1<30)
      return(true);
      return(false);
   }
bool sell1sOpen()
   {
   Sm1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,1);
   Ss1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,1);
   Sm2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,2);
   Ss2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,2);
   if(Sm1<Ss1 && Sm2>Ss2 && Sm1>70)
      return(true);
      return(false);
   }
bool buy1sClose()
   {
   Sm1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,1);
   Ss1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,1);
   Sm2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,2);
   Ss2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,2);
   if(Sm1<Ss1 && Sm2>Ss2 && Sm1>70)
      return(true);
      return(false);
   }
bool sell1sClose()
   {
   Sm1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,1);
   Ss1=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,1);
   Sm2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_MAIN,2);
   Ss2=iStochastic(NULL,0,13,5,5,MODE_SMA,0,MODE_SIGNAL,2);
   if(Sm1>Ss1 && Sm2<Ss2 && Sm1<30)
      return(true);
      return(false);
   }
Reason: