Questions from Beginners MQL5 MT5 MetaTrader 5 - page 827

 

help tweak the indicator - can't get a handle on the last bar!

The indicator considers a series of continuous wins for binary options, taking into account that the bet will be made every 2 bars (ie, after one - win/loss, wait for the current bar), the external variable is a price analysis of the previous bar in an upward or downward price

I have a feeling it works, but online the last bar is not considered correctly, and as a consequence if I place the indicator on M1 the data from the history will be correct, but everything online is not calculated correctly, the code:

//+------------------------------------------------------------------+
//|                                                         test.mq5 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "IgorM"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- plot Label1
#property indicator_label1  "Label1"
#property indicator_type1   DRAW_HISTOGRAM
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  4
#property indicator_level1  2
#property indicator_level2  4
#property indicator_level3  6
//--- input parameters
input bool     on_a_higher=true;
//--- indicator buffers
double         Label1Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int count;
bool win;
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
   count = 0;
   win = false;
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
      int start;
      if(prev_calculated==rates_total) return(rates_total);
      if(prev_calculated==0) start=1; else start=prev_calculated-1;
      for(int i=start;i<rates_total;i++){
            if (win){  // признак выигрыша на предыдущей ставке
                  win = false;
                  Label1Buffer[i]=count;
            }
            else{
                  if(on_a_higher){    // on_a_higher = true - расчитываем на рост цены относительно предидущего бара
                     if (price[i]>price[i-1]){
                           count++;
                           win = true;
                     } else{
                                 count = 0;
                                 win = false;
                     }
                  Label1Buffer[i]=count;
                  }else{              // on_a_higher = false - расчитываем на падение цены относительно предидущего бара
                        if (price[i]<price[i-1]){
                           count++;
                           win = true;
                     } else{
                                 count = 0;
                                 win = false;
                     }
                  Label1Buffer[i]=count;
                  }
            }
      }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

thanks in advance!

Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • www.mql5.com
Задавайте вопросы по техническому анализу, обсуждайте торговые системы и улучшайте свои навыки программирования торговых стратегий на языке MQL5. Общайтесь и обменивайтесь опытом на форуме с трейдерами всего мира и помогайте ответами новичкам — наше сообщество развивается вместе с вами. Ошибки, баги, вопросы Ошибка после команды CopyClose...
 
Hello! Please help with the close signals module. What am I doing wrong?
Files:
 
yur4ik:
Hello! Please help me with the signals module to close positions. What am I doing wrong?

Your file is the SIGNAL module. In the Expert Advisor system, it is integrated in the same way as the USER indicator's SIGNALS module. In other words, the signals generated by your module have a WEIGHT, and it may happen that this weight is not enough to trigger the signal.


You need to think about the best way to do the closing in your case. Perhaps you should not do it in the signal module

 
Vladimir Karputov:

Your file is the SIGNAL module. In the Expert Advisor system, it is integrated in the same way as the USER indicator's SIGNALS module. In other words, the signals generated by your module have a WEIGHT, and it may happen that this weight is not enough to trigger the signal.


You need to think about the best way to do the closing in your case. You may not want to do it in a signal module.

I would like it to be done as a signals module that could be collected by an Expert Advisor generator. At the moment when the module is to be triggered, LongCondition() and ShortCondition() in the second EA module return zero values - i.e. weight=0, I understand it correctly? Accordingly, the weight of this module must outweigh the signal, but it does not work. In CheckCloseLong() and CheckCloseShort(), I have inserted the Print("-------------"); and Print(time_GMT_DST()); which, during normal module operation, should make appropriate entries in the log upon receipt of each tick. The entries are not made; CheckCloseLong() and CheckCloseShort() are not called. What's the catch?

I'm clumsy, of course, but I'm just learning.
 
yur4ik:

I would like it to be in the form of a signals module, so that it would be possible to assemble through the EA generator. At the moment when the module is to be triggered, LongCondition() and ShortCondition() in the second module of the EA return zero values - i.e. weight=0, I understand it correctly? Accordingly, the weight of this module must outweigh the signal, but it does not work. In CheckCloseLong() and CheckCloseShort(), I have inserted the Print("-------------"); and Print(time_GMT_DST()); which, during normal module operation, should make appropriate entries in the log upon receipt of each tick. The entries are not made; CheckCloseLong() and CheckCloseShort() are not called. What's the catch?

I'm certainly a clumsy explanation, but I'm just learning

Use methods

   //--- methods of checking if the market models are formed
   virtual int       LongCondition(void);
   virtual int       ShortCondition(void);

Instead of their CheckCloseLong and CheckCloseShort.

 
fxsaber:

Thank you.

 

Hello, can you tell me how to make a counter for losing trades?
The Expert Advisor analyses the last trade on the history - if it is unprofitable, then it adds +1 to the variable, which will be the number of consecutive losing trades.

As soon as a profitable trade comes in - the variable needs to be reset to zero.

 
Nikita Chernyshov:

Hello, could you please advise how to make a counter for losing trades?
The Expert Advisor analyzes the last trade on the history - if it is losing, then it adds +1 to the variable, which will be the number of consecutive losing trades.

As soon as a profitable trade comes, the variable must be zeroed.

We can refuse from working with trading history and work only with OnTradeTransaction() - there we will catch a deal of type "OUT" - i.e. it will be the position closing. Once this trade is found - we determine whether it was profitable or unprofitable. Respectively, the counter"losses" should be reset or increased.

//+------------------------------------------------------------------+
//| TradeTransaction function                                        |
//+------------------------------------------------------------------+
void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &result)
  {
//--- get transaction type as enumeration value 
   ENUM_TRADE_TRANSACTION_TYPE type=trans.type;
//--- if transaction is result of addition of the transaction in history
   if(type==TRADE_TRANSACTION_DEAL_ADD)
     {
      long     deal_ticket       =0;
      long     deal_order        =0;
      long     deal_time         =0;
      long     deal_time_msc     =0;
      long     deal_type         =-1;
      long     deal_entry        =-1;
      long     deal_magic        =0;
      long     deal_reason       =-1;
      long     deal_position_id  =0;
      double   deal_volume       =0.0;
      double   deal_price        =0.0;
      double   deal_commission   =0.0;
      double   deal_swap         =0.0;
      double   deal_profit       =0.0;
      string   deal_symbol       ="";
      string   deal_comment      ="";
      string   deal_external_id  ="";
      if(HistoryDealSelect(trans.deal))
        {
         deal_ticket       =HistoryDealGetInteger(trans.deal,DEAL_TICKET);
         deal_order        =HistoryDealGetInteger(trans.deal,DEAL_ORDER);
         deal_time         =HistoryDealGetInteger(trans.deal,DEAL_TIME);
         deal_time_msc     =HistoryDealGetInteger(trans.deal,DEAL_TIME_MSC);
         deal_type         =HistoryDealGetInteger(trans.deal,DEAL_TYPE);
         deal_entry        =HistoryDealGetInteger(trans.deal,DEAL_ENTRY);
         deal_magic        =HistoryDealGetInteger(trans.deal,DEAL_MAGIC);
         deal_reason       =HistoryDealGetInteger(trans.deal,DEAL_REASON);
         deal_position_id  =HistoryDealGetInteger(trans.deal,DEAL_POSITION_ID);

         deal_volume       =HistoryDealGetDouble(trans.deal,DEAL_VOLUME);
         deal_price        =HistoryDealGetDouble(trans.deal,DEAL_PRICE);
         deal_commission   =HistoryDealGetDouble(trans.deal,DEAL_COMMISSION);
         deal_swap         =HistoryDealGetDouble(trans.deal,DEAL_SWAP);
         deal_profit       =HistoryDealGetDouble(trans.deal,DEAL_PROFIT);

         deal_symbol       =HistoryDealGetString(trans.deal,DEAL_SYMBOL);
         deal_comment      =HistoryDealGetString(trans.deal,DEAL_COMMENT);
         deal_external_id  =HistoryDealGetString(trans.deal,DEAL_EXTERNAL_ID);
        }
      else
         return;
      if(deal_symbol==Symbol() && deal_magic==m_magic)
         if(deal_entry==DEAL_ENTRY_OUT)
           {
            if(deal_commission+deal_swap+deal_profit>0)
              {
               losses=0;
              }
            else
              {
               losses++;
              }
           }
     }
  }
And the check in the trading history should be done in OnInit() - once at start of the Expert Advisor.
 

Hi all,

Can you tell me how I can programmatically clear the EA log before displaying information in it :?

Only through Win API?

Thank you!

 
Vitaliy Sendyaev:

Hi all,

Could you tell me how to programmatically clear the EA log before displaying information in it :?

Only through Win API?

Thank you!

Log only via Win API.