Help needed - chart shows different trades than code - page 2

 
Vladimir Karputov #:

It's hard for me to follow your thoughts - I don't understand at all what you are doing that you don't like? What is not working for you?

Remember - this is a technical forum: Your task is to describe the task in detail so that everyone can reproduce it.

Therefore, I insist that the example consists of a single file with very short logic.
Understand: no one wants to poke around in a thousand lines of code.
I understand, apologies. I was trying to say that the indicators on the chart do not match my logic.

 There were a number of things that went wrong, so I went back to basics but there is something I still cannot get right. I fixed a number of things myself but I'll just paste the very short code below which I'm struggling with.

Describing an example what I want to do but what does not appear to be happening:

  • Indicators X,Y,Z all looking for different things.
  • I want to wait for indicator X to be true before I check for Indicator Y and Z to be true. X, Y and Z may not happen at the same time
  • If X, Y and Z are not true within/equal to 5 bars, reset everything. If X, Y and Z are true within/equal to 5 bars, this generates a buy.
  • Start looking for X to be true again and try the procedure above again.
OnTick{

        // code only starts on a new bar. all variables below are global
   if(barcount <=5 && signalX == true) 
   {
      barcount++ ;
   }
   else {
      barcount=0 ; buycount=0;

      signalX=false; signalY=false; signalZ=false;
   }

   if(buycount>3) buycount = 0; signalX=false; signalY=false; signalZ=false; signalXvalue=false; signalYvalue=false; signalZvalue=false;
   if(signalXvalue) == true && signalX == false) signalX = true; buycount++;
   if(signalX ==  true)
   {
      if(signalYvalue == true && signalY == false) signalY = true; buycount++;
      if(signalZvalue == true && signalZ == false) signalZ = true; buycount++;

      if(buycount<=3)
      {
         if(signalX== true && signalY == true && signalZ == true)
                {
                        m_buy = true;buycount=0;signalX=false; signalY=false; signalZ=false; signalXvalue=false; signalYvalue=false; signalZvalue=false;
                }
      }
      
   }

}

 

Start with the basics: first learn how to receive indicator signals - an example is here.

Code 'iCustom AMA smoothed RSI (fl) value on chart.mq5'

//+------------------------------------------------------------------+
//|                 iCustom AMA smoothed RSI (fl) value on chart.mq5 |
//|                              Copyright © 2022, Vladimir Karputov |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2022, Vladimir Karputov"
#property version   "1.000"
#property tester_indicator "AMA smoothed RSI (fl)"
//--- input parameters
input int                inpRsiPeriod  = 14;          // RSI period
input int                inpPeriod     = 14;          // AMA Period
input int                inpFastPeriod =  2;          // AMA Fast end period
input int                inpSlowPeriod = 30;          // AMA Slow end period
input ENUM_APPLIED_PRICE inpPrice      = PRICE_CLOSE; // Price
enum enColorChangeOn
  {
   cchange_onSlope,  // Change color on slope change
   cchange_onLevels, // Change color on outer levels cross
   cchange_onZero    // Change color on middle level cross
  };
input int                inpFlPeriod    = 25;               // Floating levels period
input double             inpFlLevelUp   = 90;               // Floating levels up %
input double             inpFlLevelDn   = 10;               // Floating levels down %
input enColorChangeOn    inpColorChange = cchange_onLevels; // Color changing mode
//---
int      handle_iCustom;                     // variable for storing the handle of the iCustom indicator
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create handle of the indicator iMACD ([data folder]\MQL5\Indicators\AMA smoothed RSI (fl).mq5)
   handle_iCustom=iCustom(Symbol(),Period(),"AMA smoothed RSI (fl)",
                          inpRsiPeriod,
                          inpPeriod,
                          inpFastPeriod,
                          inpSlowPeriod,
                          inpPrice,
                          inpFlPeriod,
                          inpFlLevelUp,
                          inpFlLevelDn,
                          inpColorChange);
//--- if the handle is not created
   if(handle_iCustom==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the 'AMA smoothed RSI (fl)' indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   if(handle_iCustom!=INVALID_HANDLE)
      IndicatorRelease(handle_iCustom);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   double levu[],levm[],levd[],val[];
   ArraySetAsSeries(levu,true);
   ArraySetAsSeries(levm,true);
   ArraySetAsSeries(levd,true);
   ArraySetAsSeries(val,true);
   int start_pos=0,count=3;
   if(!iGetArray(handle_iCustom,0,start_pos,count,levu) || !iGetArray(handle_iCustom,1,start_pos,count,levm) ||
      !iGetArray(handle_iCustom,2,start_pos,count,levd) || !iGetArray(handle_iCustom,3,start_pos,count,val))
     {
      return;
     }
//---
   string text=" Upper level | Middle level | Lower level | AMA smoothed RSI"+"\n";
   for(int i=count-1; i>=0; i--)
     {
      text=text+"#"+IntegerToString(i)+
           " | "+DoubleToString(levu[i],Digits()+1)+
           " | "+DoubleToString(levm[i],Digits()+1)+
           " | "+DoubleToString(levd[i],Digits()+1)+
           " | "+DoubleToString(val[i],Digits()+1)+"\n";
     }
   Comment(text);
  }
//+------------------------------------------------------------------+
//| Get value of buffers                                             |
//+------------------------------------------------------------------+
bool iGetArray(const int handle,const int buffer,const int start_pos,
               const int count,double &arr_buffer[])
  {
   bool result=true;
   if(!ArrayIsDynamic(arr_buffer))
     {
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, this a no dynamic array!",__FILE__,__FUNCTION__);
      return(false);
     }
   ArrayFree(arr_buffer);
//--- reset error code
   ResetLastError();
//--- fill a part of the iBands array with values from the indicator buffer
   int copied=CopyBuffer(handle,buffer,start_pos,count,arr_buffer);
   if(copied!=count)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("ERROR! EA: %s, FUNCTION: %s, amount to copy: %d, copied: %d, error code %d",
                  __FILE__,__FUNCTION__,count,copied,GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
   return(result);
  }
//+------------------------------------------------------------------+
Reason: