[ARCHIVE!] Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Can't go anywhere without you - 4. - page 515

 
ser7051200:
Please advise how to buy a trading robot ?
How to buy a trading robot on MQL5 Market?
 

Hello,

Please have this question about the EA:

I have an order opening by swinging, for example, if MA_1 > MA_2 then we buy. But when I exit the trade this rule is triggered again.

How can I tell the EA to open a position only if the price has moved no more than 4 bars away from MA_2?


Thank you!

If something is wrong or wrong place, do not kick me. This is my first post on this huge forum.

 
Jony5Proz:

Hello,

Please have this question about the EA:

I have an order opening by swinging, for example, if MA_1 > MA_2 then we buy. But when I exit the trade this rule is triggered again.

How can I tell the EA to open a position only if the price has moved no more than 4 bars away from MA_2?


Thank you!

If something is wrong or wrong place, do not kick me. This is my first post on this huge forum.

https://book.mql4.com/ru/samples/expert
 

Thanks, I'm just at that level of knowledge. There's the same problem with the script in the example. It's inconvenient when you turn on the script and it goes in at the end of the trend.

How do I tell the script to trigger only if there are less than 4 bars after the crossing of two bars?

 
https://www.mql5.com/ru/code/10773 last time I bathe
 
Jony5Proz:

Thanks, I'm just at that level of knowledge. There's the same problem with the script in the example. It's inconvenient when you turn on the script and it goes in at the end of the trend.

How do I tell the script to trigger only if there are less than 4 bars after the crossing of two bars?

The script does not understand words. It's in your hands - run the script no later than 4 bars after 2 MAs have been crossed.
 

People, I've already asked here how to make an order open only when a new candle appears. I am using a D1 timeframe.

I have decided to place pending orders at certain levels (buystop_lvl and sellstop_lvl), based on analysis of previous candlesticks.

I wanted to place them at the beginning of the day and delete them when a new candlestick comes. It doesn't matter if the pending orders were activated or not, if they were activated, then market orders should also be closed. At most 2 orders, buystop and sellstop.

Here is what I wrote:

1. First I check whether the market orders or pending orders are active, and if they are, I delete/close them.

int total=OrdersTotal();
   if (total!=0 && NewBar()==true){
                 for (int i=0; i<total; i++)
                    {
                     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
                     if(OrderType()==OP_BUY)  OrderClose(OrderTicket(),OrderLots(),Bid,10);
                     if(OrderType()==OP_SELL) OrderClose(OrderTicket(),OrderLots(),Ask,10);
                     if(OrderType()==OP_BUYSTOP)  OrderDelete(OrderTicket());
                     if(OrderType()==OP_SELLSTOP) OrderDelete(OrderTicket());
                    }
                }

2. Then I place pending orders by calculated levels:

if(NewBar() == True && OrdersTotal()==0)
    {
     OrderSend(Symbol(),OP_BUYSTOP ,Lot,buystop_lvl ,10,buystop_SL ,buystop_TP,"Expert buystop\sellstop");
     Sleep(10000);
     OrderSend(Symbol(),OP_SELLSTOP,Lot,sellstop_lvl,10,sellstop_SL,sellstop_TP,"Expert buystop\sellstop");
     
    }

3. If I have a Sell or Sell Sell order, I will open a new bar on it:

//Функция нового бара
bool NewBar()
{
  static datetime lastbar = 0;
  datetime curbar = iTime(Symbol(),1440,0);
  if(lastbar != curbar)
  {
    lastbar = curbar;
    return(true);
  }
  else return(false);
}

As a result, when a new candle opens, the orders are placed and immediately deleted. The orders are placed and immediately deleted.

When I commented out the code from point (1) I couldn't get it to open orders in the tester, but the orders didn't open, i.e. the orders didn't become market ones.

Can you tell me what might be the problem?

 

This NewBar() function cannot be used twice in a row

Something like this is better:

bool newBar = NewBar();

// and further in the text, replacing NewBar() with newBar

 
Shit, I still don't understand how to do it. I need the code to delete and close orders only on the next candlestick. But there will also be others to open on it. This means the program will again get into the deletion. I need some flag, but my brain is already sweating to understand how to do it :)
 
Noterday:
And in general, you throw out NewBar, look at the time of the last order opening and compare it with Time[0]. If it's higher, it's your case, you can delete everything.