Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1943

 

Why does MT5 code work but MT4 does not? build 1Z5Z

Saves the pattern but does not apply the pattern

//+------------------------------------------------------------------+
//|                                              File Read Write.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string SYMBOL;
string ccname;
void OnStart()
  {

   SYMBOL = _Symbol;
   ccname = SYMBOL;
   SaveTemplate();
 
 DownloadTemplate();
     
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void SaveTemplate(void)
  {
   if(ChartSaveTemplate(0, ccname))
      Print("Template successfully saved!  " + SYMBOL);
   else
      Print("Template save Failed!  " + SYMBOL);
   return;
  }
//+------------------------------------------------------------------+
void DownloadTemplate(void)
  {

   if(ChartApplyTemplate(0,ccname))
      return;
   else
      Print("Template download Failed!  " + SYMBOL);
  }

 
Can you please tell me how to make the program work only when a new candle has started and, after running the code, hold still and wait for the next candle to start?
 
Ivan Butko a new candle has started and after running the code to lurk and wait for the start of the next candle?

it's called opening prices.
it's elementary.

 
Pavel Malyshko #:

it's called opening prices.
is elementary.

I don't figure it out. A tick comes in, the check goes through. The next tick comes in and it checks again. The same timer does not fit in the opening.

The only thing I can think of is to memorize the time of the opening, add the time of the candle to it and check the current time against the new value

 
Ivan Butko #:

I don't get it. A tick comes in, it's checked. The next tick comes, it's checked again. The same timer doesn't fit in the opening.

I can only remember the opening time, add the candle time to it and check the current time against the new value

Forum on trading, automated trading systems and strategy testing.

Any questions from newbies in MQL4 and MQL5, any help and discussion of algorithms and codes

Valeriy Yastremskiy, 2022.03.30 09:37

 
static datetime OldTime=0;
if(iTime(NULL,0,0)!=OldTime)
      {
      Alert("ДИВЕРГЕНЦИЯ НА "+Symbol()); OldTime=iTime(NULL,0,0);
      }

If you do not know the algorithm, you will get an alert when the price reaches the end of the day. When a new bar appears,iTime(NULL,0,0) will be changed and the equality will not be once, then the equality and the if will be false.

You can also do it this way.

static bool FlagNewBar=false;
   if(BarTime!=Time[0])
     {
      BarTime=Time[0];
      FlagNewBar=true;
     }

Or we could get a new bar on any timeframe.

bool FlagNewBarF(int prd, datetime &ArgBarTime,bool &FlagNbar)
  {
   FlagNbar=false;
   if(ArgBarTime!=iTime(NULL,prd,0))
     {
      ArgBarTime=iTime(NULL,prd,0);
      FlagNbar=true;
     }

   return(FlagNbar);
  }

The call would be as follows

 if(FlagNewBarF(1, BarTime1,FlagNewBar1))
     {
      Alert("FlagNbar1 ",FlagNewBar1,"BarTime1 ",TimeToStr(BarTime1,TIME_DATE|TIME_SECONDS));
      
     }

   if(FlagNewBarF(5, BarTime5,FlagNewBar5))
     {
      Alert("FlagNbar5 ",FlagNewBar5,"BarTime5 ",TimeToStr(BarTime5,TIME_DATE|TIME_SECONDS));
     
     }

 
Valeriy Yastremskiy #:
You didn't write whether this is on the same instrument or on different positions?

Let it be all within the same currency pair, EurUsd for example. Both are open in Buy.

 
makssub #:

Let it be all within the same currency pair, EurUsd for example. Both are open in Buy.

Generally these are different tasks, on different pairs and on the same one. On one currency pair with the same lot 2 orders/positions have different options: non-crossing and crossing. Above the one below that is either loss or profit depending on the distance to the orders. For one direction between, one in profit the other in loss, above below both in profit or in loss.

It should be set according to your preferences. If the volume is the same, you should close the same volume. If it is not the same, you should calculate the amount of money in profit, the amount of loss in the money, calculate the ratio of profit to loss, multiply this ratio by the number of lots of a losing position and close this part.

 
Ivan Butko a new candle has started and after running it through the code, hold still and wait for the next candle to start?

function

  bool NewBar()
   {
      static datetime LastTime = 0;
      if(iTime(Symbol(), PERIOD_CURRENT, 0) != LastTime)
      {
         LastTime = iTime(Symbol(), PERIOD_CURRENT, 0);
         return (true);
      }
      else
         return (false);
   }
 
Valeriy Yastremskiy #:

These are different tasks, on different pairs and on the same pair. On one, with the same lot 2 orders/positions have options divergent not crossed, and crossed. price between non-crossed ones is loss for both, crossed ones have profit only between orders. Above the one below that is either loss or profit depending on the distance to the orders. For one direction between, one in profit the other in loss, above below both in profit or in loss.

It should be set according to your preferences. If the volume is the same, you should close the same volume. If it is not the same, you should calculate the amount of money in profit, the amount of loss in the money, calculate the ratio of profit to loss, multiply this ratio by the number of lots of a losing position and close this part.

That's right) That's exactly what I want. To calculate and close the losing part.
Any examples?
I tried while, got confused and failed.

 
Valeriy Yastremskiy #:

sashasonik #:

function

Thank you very much!
Reason: