[ARCHIVE] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 3. - page 497

 
sss2019:

Tell me how to write an EA on this condition:

There are three currency pairs on which the EA will open deals and the EA is attached to only one chart and on the other two it works by itself, in general, multicurrency.

The second condition is when the price reaches a certain level on any of the currency pairs, a deal opens. Only one deal can be opened at a time.

As soon as the deal is closed, the Expert Advisor can reopen the deal at any pair if the price approaches.

I have set a static variable so that as long as the variable's value is false, the deals are allowed to be opened and as soon as the order is opened on any of the pairs, the variable takes the value true. As soon as the order is closed and the for loop cannot find any trade, the variable again takes the value false.

And if for example only one trade should be opened for each of the pairs, it means that each pair has its own variable.

Is there a more rational solution to this problem?


There is a parameter such as "Magik" in the order - just count orders with a certain magik and allow or forbid trades.
 

How do I close orders correctly and how do I collect information about them?

I am sorry for stupid questions beforehand) I have written the following algorithm: I want to open a buy position when the opening is lower than the fractal up and the closing is higher than the fractal up, and a sell position when the opening is higher than the fractal down and the closing is lower; I also want to close all positions if they fall short by 85 points or if the order is opened within 38 candles (const. S).I have a feeling nothing gets closed at all, just closed orders and in general I have a feeling I'm dealing with orders the wrong way, I operate incorrectly and then I close them the wrong way)) Please, tell me how to close them correctly, so they won't slip closed and where are there errors in this code. Or can be an example of how nyu walk, in which all these operations are done adequately. Thanks in advance for your help!)


extern int S=38; //number of candlesticks, after which the position will be closed
extern int SL=85; //the stop, at which the positions will be closed in any case
extern int HIGH=0; //on which timeframe extremums will be searched (5 or 1 or 0(D1))
extern int l=1; //number of lots to trade
int massorder[30][30]; //there is data about OPEN lots
int numb=1; //the number of el-ta in the array, which is ootc. open lot
int i=0;
int ticket=0;
int x=0;
double down,up=0;
double highH1,lowH1=0;

int start()

{
if(Hour()>x)
{
searchH1();
up=highH1;
down=lowH1;
i++;
}
//----
if(Open[1]<up && Close[1]>up)//if the candle is lower at the opening and lower at the closing than the fractal level up, then
{
ticket=OrderSend(Symbol(),OP_BUY,l,Bid,5,Bid-SL*Point,NULL);
i=0;
savedata();
}
if(Open[1]>down && Close[1]<down)//if the candle is higher on the open and lower on the close than the down fractal level, then
{
ticket=OrderSend(Symbol(),OP_SELL,l,Ask,5,Ask+SL*Point,NULL);
i=0;
savedata();
}
if(i==S)
{
closeAllticket();
closeAllPos();
}
i++;
//----
x=Hour();
if(Hour()==23)
x=-1;
return(0);
}
//+------------------------------------------------------------------+
void closeAllticket()
{
for(int j=0;j<=numb;j++)
{
OrderClose(massorder[j][0],l,Ask,5);
OrderClose(massorder[j][0],l,Bid,5);
}
numb=0;
}
void closeAllPos()
{

for(int j=0;j<=OrdersTotal();j++)
{
OrderSelect(j,SELECT_BY_POS);
OrderClose(OrderTicket(),l,Ask,5);
OrderClose(OrderTicket(),l,Bid,5);
}
}
void searchH1()//function to find the fractal
{
if(iHigh(Symbol(),PERIOD_H1,3)>iHigh(Symbol(),PERIOD_H1,2) && iHigh(Symbol(),PERIOD_H1,3)>iHigh(Symbol(),PERIOD_H1,1) &&& iHigh(Symbol(),PERIOD_H1,3)>iHigh(Symbol(),PERIOD_H1,4) && iHigh(Symbol(),PERIOD_H1,3)>iHigh(Symbol(),PERIOD_H1,5))
highH1=iHigh(Symbol(),PERIOD_H1,3)

if(iHigh(Symbol(),PERIOD_H1,3)<iHigh(Symbol(),PERIOD_H1,2) && iHigh(Symbol(),PERIOD_H1,3)<iHigh(Symbol(),PERIOD_H1,1) && iHigh(Symbol(),PERIOD_H1,3)<iHigh(Symbol(),PERIOD_H1,4) && iHigh(Symbol(),PERIOD_H1,3)<iHigh(Symbol(),PERIOD_H1,5))
lowH1=iHigh(Symbol(),PERIOD_H1,3);
return (0);

}

void savedata() //program to save data to the array
{

OrderSelect(ticket,SELECT_BY_TICKET);
massorder[numb][0]=OrderTicket();
massorder[numb][1]=OrderOpenPrice();
massorder[numb][2]=OrderStopLoss();
massorder[numb][3]=OrderTakeProfit();
if(OrderType()==OP_BUY)
massorder[numb][4]=0;
if(OrderType()==OP_SELL)
massorder[numb][4]=1;
numb++;

}
 
Qwertee:

How do I close orders correctly and how do I collect information about them?

I am sorry for stupid questions beforehand) I have written the following algorithm: I want to open a buy position when the opening is lower than the fractal up and the closing is higher than the fractal up, and a sell position when the opening is higher than the fractal down and the closing is lower; I also want to close all positions if they fall short by 85 points or if the order is opened within 38 candles (const. S).I have a feeling nothing gets closed at all, just closed orders and in general I have a feeling I'm dealing with orders the wrong way, I operate incorrectly and then I close them the wrong way)) Please, tell me how to close them correctly, so they won't slip closed and where are there errors in this code. Or can be an example of how nyu walk, in which all these operations are done adequately. Thanks in advance for your help!)


In global variables, where you have specified stops in the beginning

extern int MagicNumber = 11113;
int cntBuy = 0, cntSell = 0, totalBuy, totalSell;

before you open orders:

           totalBuy = CountTradesBuy();
           totalSell = CountTradesSell();

in the signal to open

if((Open[1]<up) && (Close[1]>up) && (totalBuy < 1) && (totalSell < 1))

if((Open[1]>down) && (Close[1]<down) && (totalBuy < 1) && (totalSell < 1))

at the end of the EA

int CountTradesBuy() {
   int countBuy = 0;
   for (int tradeBuy = OrdersTotal() - 1; tradeBuy >= 0; tradeBuy--) {
      OrderSelect(tradeBuy, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_BUY) countBuy++;
   }
   return (countBuy);
}

int CountTradesSell() {
   int countSell = 0;
   for (int tradeSell = OrdersTotal() - 1; tradeSell >= 0; tradeSell--) {
      OrderSelect(tradeSell, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != MagicNumber) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         if (OrderType() == OP_SELL) countSell++;
   }
   return (countSell);
}

i guess i didn't forget anything

 
belck:


in the global variables, where you have specified the stops at the beginning

before the orders opened:

in the signal to open

at the end of the EA

like nothing was forgotten



I put everything you wrote but still nothing has changed for some reason. the result is the same as it was and still remains, orders remain open for a large amount of time, and i need them to close after S bars, what is it?????????????????

Files:
ikdgna.mq4  7 kb
 
Qwertee:


I put everything you wrote but still nothing has changed. the result is as it was and as it is now, orders are still open a lot of time and i need them to close after S bars, what's the deal?????????????????

The call to close orders should be placed before the opening of orders. I have these on closure:

before the most global

#include <stdlib.mqh>


This is how to call before opening orders:

if ()
   {
    CloseAllBuy();
    }
    
       
   if () 
   {
   CloseAllSell();
   }

или

if ()
   {
    CloseAllBuy();
    CloseAllSell();
     }

and this at the very end of the EA

void CloseAllBuy()
{
        int i;
        int Orders = OrdersTotal();
        bool result;
        datetime begin;
        
        if(Orders > 0)
        {
                for(i = Orders-1; i >= 0; i--)
                {
                        if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
                        {
                                if(OrderSymbol() == Symbol() && OrderType() == OP_BUY && OrderMagicNumber() == MagicNumber)
                                {
                                        RefreshRates();
                                        begin = TimeCurrent();
                    result = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), slip, Yellow); // закрываем все встречные ордера
                                        while(!result && TimeCurrent() - begin <= 60 && !IsTesting())
                                        {
                                                Sleep(100);
                                                RefreshRates();
                    result = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Bid, Digits), slip, Yellow);//закрываем все встречные ордера
                                        }
                                        if(!result)
                                        {
                                                int error = GetLastError();
                                                Print("Ошибка закрытия ордера BUY #" + OrderTicket() + " " + ErrorDescription(error));
                                        }
                                }
                        }
                        else
                        {
                                Print("Не удалось выбрать открытый ордер:" + ErrorDescription(error));
                        }
                }
        }
}

void CloseAllSell()
{
        int i;
        int orders = OrdersTotal();
        bool result;
        datetime begin;
        
        if(orders > 0)
        {
                for(i = orders-1; i >= 0; i--)
                {
                        if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) )
                        {
                                if(OrderSymbol() == Symbol() && OrderType() == OP_SELL && OrderMagicNumber() == MagicNumber)
                                {
                                        RefreshRates();
                                        begin = TimeCurrent();
                    result = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), slip, Yellow); // закрываем все встречные ордера
                                        while(!result && TimeCurrent() - begin <= 120 && !IsTesting())
                                        {
                                                Sleep(100);
                                                RefreshRates();
                   result = OrderClose(OrderTicket(), OrderLots(), NormalizeDouble(Ask, Digits), slip, Yellow);//закрываем все встречные ордера
                                        }
                                        if(!result)
                                        {
                                                int error = GetLastError();
                                                Print("Ошибка закрытия ордера SELL #" + OrderTicket() + " " + ErrorDescription(error));
                                        }
                                }
                        }
                        else
                        {
                                Print("Не удалось выбрать открытый ордер:" + ErrorDescription(error));
                        }
                }
        }
}

 

Question to experts, are there any criteria for EAs, such as the minimum number of lots for a certain time period of the test? Percentage of loss, etc.?

I just have 2 EAs, and they make 100 trades a year, and another makes 30 trades altogether... (timeframe 15, 30 respectively). The profitability is good, the first one is in a demo on real quotes, it's now being tested ... but can someone tell me if there is a better one ... But maybe someone will tell me if there are better tests than in MT4?

 
Can you tell me how to execute a condition? We have a condition to open a buy order at the current price as soon as the price reaches 1.2550. So, when the price is higher or equal to 1.2550, the order is opened and when the order is opened, the static variable blocks the opening of new orders, until the order reaches 0. So, as soon as the order is closed and there are no more orders with this magic, the ban on opening is removed and a new order is opened, but the price has moved away from this level long ago. We need the Expert Advisor to open market orders when the price reaches this level, not the pending ones.
 

Hi all connoisseurs))))

There is an indicator that draws a trend line on the last two fractals, but it draws it after an "unconfirmed fractal", i.e. the second bar has not yet formed after the fractal but the line has already been drawn. How to make it draw after the second bar but not after the first one? The indicator is in the tab.

Files:
 
rigc:

Hi all connoisseurs))))

There is an indicator that draws a trend line on the last two fractals, but it draws it after an "unconfirmed fractal", i.e. the second bar has not yet formed after the fractal but the line has already been drawn. How to make it draw after the second bar but not after the first one? The indicator is in the tab.


Try
Files:
 
Figar0:

Try
not drawing at all)))
Reason: