How does this work......

 

On the internet, I got the sample code where I can enter and close orders everytime there is a crossover of Moving Averagea.

When I tested the sample code, it worked. When I analyzed the sample code, I didn't get how it worked.

I do not understand how this sample code could close orders at crossover.

I mean, don't you need this kind of code when you close at crossover too? ↓↓↓

if( prev_ma10 < prev_ma5 && ma10>ma5)

How is closing orders at the next crossover even possible with this sample code... 

I have attached the sample code bellow. If anybody could help, THANKS!!!


//+------------------------------------------------------------------+
//|                                                     ma cross.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
datetime dt;
 
input int magic = 123;
 
bool check;
 
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   dt = Time[0];
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(isnewbar()){
      //you can replace the period here
      double prev_ma10 = iMA(_Symbol, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, 2);
      double prev_ma5 = iMA(_Symbol, PERIOD_CURRENT, 5, 0, MODE_SMA, PRICE_CLOSE, 2);
      double ma10 = iMA(_Symbol, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
      double ma5 = iMA(_Symbol, PERIOD_CURRENT, 5, 0, MODE_SMA, PRICE_CLOSE, 1);
     
      if(prev_ma10>prev_ma5 && ma10<ma5){
         if(OrdersTotal()!=0) closeexisting(); //
         //send the order
         check = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 10, 0,0,NULL, magic);
      }
      else if( prev_ma10 < prev_ma5 && ma10>ma5){
         if(OrdersTotal()!=0) closeexisting();
         
         check = OrderSend(_Symbol, OP_SELL, 0.01, Bid, 10, 0, 0, NULL, magic);
      }
     
   }
  }
 
 
//close all existing position
void closeexisting(){
   for(int i = OrdersTotal()-1; i>=0; i--){ 
      check = OrderSelect(i, SELECT_BY_POS);
      if(check && OrderMagicNumber() == magic){
         check = OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);
      }
   }
}
 
 
//Check if new bar
bool isnewbar(){
   if(Time[0] != dt){
      dt = Time[0];
      return true;
   }
   return false;
}
//+------------------------------------------------------------------+
 
sen124be:

On the internet, I got the sample code where I can enter and close orders everytime there is a crossover of Moving Averagea.

When I tested the sample code, it worked. When I analyzed the sample code, I didn't get how it worked.

I do not understand how this sample code could close orders at crossover.

I mean, don't you need this kind of code when you close at crossover too? ↓↓↓

How is closing orders at the next crossover even possible with this sample code... 

I have attached the sample code bellow. If anybody could help, THANKS!!!


if previous ma10 of last 2 bar is greater than previous ma5 of last 2 bar ( ma10 coming from upside going downside)

and previous ma10 of last 1 bar is smaller than previous ma5 of last 1 bar (confirmation of crossover, ma5 is going up)

close any existing and open long position based on ma5 period

the opposite logic goes for short position. hope this helps.

basically the code does not care regardless any position type as long as there's a crossover, the code execute trend riding position although there's no validation whether it's really a new trend

 

The signal to open a trade in one direction is also the signal to close a trade in the opposite direction.

if(prev_ma10>prev_ma5 && ma10<ma5){
         if(OrdersTotal()!=0) closeexisting(); //
         //send the order
         check = OrderSend(_Symbol, OP_BUY, 0.01, Ask, 10, 0,0,NULL, magic);

It is very poorly coded though, so I would look for a better example in the codebase if I were you.

 
roshjardine:

if previous ma10 of last 2 bar is greater than previous ma5 of last 2 bar ( ma10 coming from upside going downside)

and previous ma10 of last 1 bar is smaller than previous ma5 of last 1 bar (confirmation of crossover, ma5 is going up)

close any existing and open long position based on ma5 period

the opposite logic goes for short position. hope this helps.

basically the code does not care regardless any position type as long as there's a crossover, the code execute trend riding position although there's no validation whether it's really a new trend

now, it totally makes sense! thank you so much!

just one more thing that I'm stuck with.

Why did he put MODE_BID+OrderType()

 as a closing price instead of just MODE_BID or MODE_ASK? what is the definition behind it..

         check = OrderClose(OrderTicket(), OrderLots(), MarketInfo(_Symbol, MODE_BID+OrderType()), 10);
 
Keith Watford:

The signal to open a trade in one direction is also the signal to close a trade in the opposite direction.

It is very poorly coded though, so I would look for a better example in the codebase if I were you.

thank you!!

 
sen124be:

Why did he put MODE_BID+OrderType()

 as a closing price instead of just MODE_BID or MODE_ASK? what is the definition behind it..

As I said, poorly coded.

 
sen124be: Why did he put MODE_BID+OrderType() as a closing price instead of just MODE_BID or MODE_ASK? what is the definition behind it..
Most codes are like this
if(OrderType() == OP_Buy)
   … OrderClose(… Bid, …);
else
   … OrderClose(… Ask, …);
This code tried to be direction independent by mapping OP_BUY…OP_SELL to MODE_BID…MODE_ASK and then getting the closing price.
… OrderClose(… MarketInfo(_Symbol, MODE_BID+OrderType() ), …
You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price.
OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10);
 
You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type for close price. But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.
OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 10);

I already said you this is not exact, if you don't believe me did you check ? You obviously didn't.

Forum on trading, automated trading systems and testing trading strategies

automatic order closing

Alain Verleyen, 2019.08.09 15:32

So it seems I have to repeat again as you don't listen. (and by the way you explicitly wrote "before the next OrderSelect" and it's plain wrong concerning OrderClosePrice()).

RefreshRates() has nothing to do with OrderClosePrice(). RefreshRates() will NOT update OrderClosePrice(). OCP is NOT a Predefined Variables or series arrays.

OrderSelect() will refresh OrderClosePrice() and nothing else.

Please check it before arguing more.

Fix your answer and stop providing this wrong advice please.

Trailingstop EA won't work
Trailingstop EA won't work
  • 2016.04.16
  • www.mql5.com
Hi Everyone. I cannot get my EA to work properly. I am running it on 10 different pairs/charts simultaneously. There seem to be two problems: 1...
 
Alain Verleyen: I already said you this is not exact, if you don't believe me did you check ? You obviously didn't.

Fix your answer and stop providing this wrong advice please.

Checked and verified.

 
William Roeder:

Checked and verified.

Thank you.
 
Alain Verleyen:
Thank you.

I've lost track of who's agreeing with who, so...

  • EA in an infinite loop within OnTick()
  • Open buy trade for the same symbol as the chart (so, close price = bid)
  • Displays:
    • A: close price
    • B: bid, via predefined variable  
    • C: bid, via MarketInfo

void OnTick()
{
   OrderSelect(0, SELECT_BY_POS);
   
   while (!IsStopped()) {
      // See below... RefreshRates();
      Comment("A: ", DoubleToString(OrderClosePrice(), 5), "  B: " , DoubleToString(Bid, 5), "  C: " , DoubleToString(MarketInfo(OrderSymbol(), MODE_BID), 5));
      ChartRedraw(); // To make sure that a lack of updates isn't a purely visual artifact
   }
}

With the code as above, with no use of RefreshRates(), then C updates but A and B do not.

If RefreshRates() is added, then B also updates but A does not.

To make A update, the OrderSelect() needs to be moved inside the loop.

Removing the RefreshRates() doesn't then prevent A from updating. So, updating the close price does require a new OrderSelect() and does not require a RefreshRates().

Or, putting that another way:

  • MarketInfo() always updates
  • Predefined variables such as Bid require RefreshRates() in order to update
  • Order values such as close price require a new OrderSelect() in order to update
... All as Alain said.
Reason: