Download MetaTrader 5

My EA program loops prints same values! Why?

To add comments, please log in or register
flwilliams87
222
flwilliams87  

I have an EA program that buys/sales based on the value of defined and calculated indicators.  But when I write those values to a flat file that are they never change and the counter never increments.  I think Im too close to the problem to diagnose it and would appreciate another set of eyes to point out what I am obviously missing.  The output to the flat file is below and I've also attached the code.  Thanks


[1] MACD   Main Value at time of buy =-6e-005
[1] MACD   Signal Value to Time of buy =-6e-005
[1] EMA5   Value at time of buy =1.20255
[1] EMA10  Value at time of buy =1.20253
[1] SMA50  Value at time of buy =1.20272
[1] SMA100 Value at time buy =1.2026
[1] RSI    Value at time of buy =50.82119
--
[1] MACD   Main Value at time of buy =-6e-005
[1] MACD   Signal Value to Time of buy =-6e-005
[1] EMA5   Value at time of buy =1.20255
[1] EMA10  Value at time of buy =1.20253
[1] SMA50  Value at time of buy =1.20272
[1] SMA100 Value at time buy =1.2026
[1] RSI    Value at time of buy =50.82119
--
[1] MACD   Main Value at time of buy =-6e-005
[1] MACD   Signal Value to Time of buy =-6e-005
[1] EMA5   Value at time of buy =1.20255
[1] EMA10  Value at time of buy =1.20253
[1] SMA50  Value at time of buy =1.20272
[1] SMA100 Value at time buy =1.2026
[1] RSI    Value at time of buy =50.82119
--

//+------------------------------------------------------------------+
//|                                                 ForExpertsV1.mq5 |
//|                                                  Copyright 2015, |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015,  ForExperts"
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <TradeAlgorithms.mqh>
//+-------------------------------------------------+
//| Indicator Parameters: SlopeDirectionLine        |
//+-------------------------------------------------+
input string             Indicators_Settings  = "*** RSI Settings ***"; // 
input int                RSIPeriod            = 14;            // RSI Period
input ENUM_APPLIED_PRICE InpAppliedPrice      = PRICE_CLOSE;   // Applied price 
input string             Indicators_Settings2 = "*** MA Settings ***";  // 
input int                MAPeriod1            = 5;             // MA 1 Period
input int                MAShift1             = 0;             // MA 1 Shift
input ENUM_MA_METHOD     MA_Mode1             = MODE_EMA;      // MA 1 Mode
input ENUM_APPLIED_PRICE InpAppliedPrice1     = PRICE_CLOSE;   // MA 1 appl.price
input int                MAPeriod2            = 10;            // MA 2 Period
input int                MAShift2             = 0;             // MA 2 Shift
input ENUM_MA_METHOD     MA_Mode2             = MODE_EMA;      // MA 2 Mode
input ENUM_APPLIED_PRICE InpAppliedPrice2     = PRICE_CLOSE;   // MA 2 appl.price
input int                MAPeriod3            = 50;            // MA 3 Period
input int                MAShift3             = 0;             // MA 3 Shift
input ENUM_MA_METHOD     MA_Mode3             = MODE_SMA;      // MA 3 Mode
input ENUM_APPLIED_PRICE InpAppliedPrice3     = PRICE_CLOSE;   // MA 3 appl.price
input int                MAPeriod4            = 100;           // MA 4 Period
input int                MAShift4             = 0;             // MA 4 Shift
input ENUM_MA_METHOD     MA_Mode4             = MODE_SMA;      // MA 4 Mode
input ENUM_APPLIED_PRICE InpAppliedPrice4     = PRICE_CLOSE;   // MA 4 appl.price
MarginMode MMMode = LOT; 
//+-------------------------------------------------+
//+-------------------------------------------------+
int TimeShiftSec, RSIHandle,MACDHandle,EMAHandle,SMAHandle,InpInd_Handle1,InpInd_Handle2,
InpInd_Handle3,InpInd_Handle4, min_rates_total;
int EMA5_Handle,EMA10_Handle,SMA50_Handle,SMA100_Handle;
//+-------------------------------------------------+
//| Trade Parameters                                |
//+-------------------------------------------------+
input bool PrintToJournalValues=true; //Print last Indicator Values to journal
input double Volume=0.1; //Volume
input double SL = 90; //SL pips
input double TP = 20; //TP pips
input int Slippage=9; //Max allowed slippage pips
input int FastEmaPeriod = 20; //MACD fast EMA period
input int SlowEmaPeriod = 40; //MACD slow EMA period
input int SignalPeriod=25; //MACD signal period
input int Fast=13;
input ENUM_APPLIED_PRICE RSIAppliedPrice=PRICE_CLOSE; //MACD applied price
input bool CloseOpposite=true; //Close previous order on opposite signal
input ENUM_MA_METHOD MA_smooth=MODE_SMA; // Get SMA data for Array
input ENUM_MA_METHOD smootMode=MODE_EMA; // Get EMA data for Array
input int RSIperiod=14; // Period for calculating the RSI. At least that's what the book says
input datetime InpDateStart=D'2015.06.18 00:00';
input datetime InpDateFinish=D'2016.06.18 00:00';


// Indicator Arrays
double MACDMainArray[];
double MACDSignalArray[];
double SMA_ARRAY[];
double SMA50_ARRAY[];
double SMA100_ARRAY[];
double EMA5_ARRAY[];
double EMA10_ARRAY[];
double RSI_ARRAY[];
double MSL,MTP;
int Slip;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
  
   RSIHandle=iRSI(Symbol(),0,RSIPeriod,InpAppliedPrice);
   if(RSIHandle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator RSI");
      return(INIT_FAILED);
     }  
//---      
   EMA5_Handle=iMA(Symbol(),0,MAPeriod1,MAShift1,MA_Mode1,InpAppliedPrice1);
   if(EMA5_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     }
//---      
   EMA10_Handle=iMA(Symbol(),0,MAPeriod2,MAShift2,MA_Mode2,InpAppliedPrice2);
   if(EMA10_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     } 
//---      
   SMA50_Handle=iMA(Symbol(),0,MAPeriod3,MAShift3,MA_Mode3,InpAppliedPrice3);
   if(SMA50_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     } 
//---      
   SMA100_Handle=iMA(Symbol(),0,MAPeriod4,MAShift4,MA_Mode4,InpAppliedPrice4);
   if(SMA100_Handle==INVALID_HANDLE)
     {
      Print(" Was not possible receive handle of the indicator MA");
      return(INIT_FAILED);
     }                       
//--- 
   TimeShiftSec=PeriodSeconds(0);
   TimeShiftSec=PeriodSeconds(0);
//--- 
   int LengthR=int(MathMax(MathSqrt(RSIPeriod),1));
   int LengthR2=int(MathMax(MathSqrt(RSIPeriod),1));

  // min_rates_total+=int(2+SignalBar);

IndicatorSetString(INDICATOR_SHORTNAME,"iRSI");


// Init Indicator Handles
   MACDHandle=iMACD(NULL,PERIOD_CURRENT,FastEmaPeriod,SlowEmaPeriod,SignalPeriod,RSIAppliedPrice);
   
// Set for 5 digits brokerage
   if(_Digits%2==1)
     {
      MSL=10.0*SL;
      MTP=10.0*TP;
      Slip=10*Slippage;
     }
   else
     {
      MSL=SL;
      MTP=TP;
      Slip=Slippage;
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

// Information for writing to a flat file
string file_name = "ForExpertsReport.txt";
int file_handle = -1;
file_handle=FileOpen(file_name,FILE_READ|FILE_WRITE|FILE_TXT);
if(file_handle==INVALID_HANDLE)
   Print("Error opening rep-file: "+file_name);
//--
FileSeek(file_handle,0,SEEK_END);

   

   CopyBuffer(RSIHandle,0,0,9,RSI_ARRAY);
   ArraySetAsSeries(MACDMainArray,true);

   CopyBuffer(MACDHandle,MAIN_LINE,0,9,MACDMainArray);
   ArraySetAsSeries(MACDMainArray,true);

   CopyBuffer(MACDHandle,SIGNAL_LINE,0,9,MACDSignalArray);
   ArraySetAsSeries(MACDSignalArray,true);
   
   CopyBuffer(EMA5_Handle,0,0,17,EMA5_ARRAY);
   ArraySetAsSeries(EMA5_ARRAY,true);
   
   CopyBuffer(EMA10_Handle,0,0,17,EMA10_ARRAY);
   ArraySetAsSeries(EMA10_ARRAY,true);
   
   CopyBuffer(SMA50_Handle,MODE_SMA,0,17,SMA50_ARRAY);
   ArraySetAsSeries(SMA50_ARRAY,true);
   
   CopyBuffer(SMA100_Handle,MODE_SMA,0,17,SMA100_ARRAY);
   ArraySetAsSeries(SMA100_ARRAY,true);
 

//-- Code to compare every element of an array to a static value

   for(int x=0; x<8 && PrintToJournalValues==true; x++) 
     {
      Print("MACD Main Array [ ",x," ] = ",NormalizeDouble(MACDMainArray[x],_Digits));
      Print("MACD Signal Array [ ",x," ] = ",NormalizeDouble(MACDSignalArray[x],_Digits));
      Print("EMA 5 Array [ ",x," ] = ",NormalizeDouble(EMA5_ARRAY[x],_Digits));
      Print("EMA 10 Array [ ",x," ] = ",NormalizeDouble(EMA10_ARRAY[x],_Digits));
      Print("SMA 50 Array [ ",x," ] = ",NormalizeDouble(SMA50_ARRAY[x],_Digits));
      Print("SMA 100 Array [ ",x," ] = ",NormalizeDouble(SMA100_ARRAY[x],_Digits));
      Print("RSI Array[ ",x," ] = ",NormalizeDouble(RSI_ARRAY[x],_Digits));
     }

//--- Calculate getting entry signals to buy and sale

   int EntrySignal;
   int SMASignal;
   int EMASignal;
   int RSISignal;
   int MACDSignal;
   double SMA50Value;
   double SMA100Value;
   double MACDSignalValue;
   double MACDMainValue;
   double EMA5Value;
   double EMA10Value;
   double RSIValue;
   int y;
   int x;
   int b;
   int s;
   b=0;  // Buy Counter
   s=0;  // Sale Counter
   EntrySignal=-1;     // Entry Set not to buy or sale
   SMASignal=-1;       // SMA  Signal to buy or sale 
   EMASignal=-1;       // EMA  Signal to buy or sale 
   RSISignal=-1;       // RSI  Signal to buy or sale
   MACDSignal=-1;      // MACD Signal to buy or sale 
   SMA50Value=0;
   SMA100Value=0;
   MACDSignalValue=0;
   MACDMainValue=0;
   EMA5Value=0;
   EMA10Value=0;
   RSIValue=0;
   
   
  
  for(x=0; x<9; x++)
  {
      if(SMA50_ARRAY[x] > SMA100_ARRAY[x])
      {
         SMASignal=0;  // SMA Buy Signal
         SMA50Value=SMA50_ARRAY[x];
         SMA100Value=SMA100_ARRAY[x];
         if(EMA5_ARRAY[x] < EMA10_ARRAY[x] )
         {
            for(y=0; y<9; y++)
            { 
               if(EMA5_ARRAY[y] > EMA10_ARRAY[y])
                  {
                  EMASignal=0;  // EMA Buy SIgnal
                  EMA5Value=EMA5_ARRAY[y];
                  EMA10Value=EMA10_ARRAY[y];
                  }
            }
            if(MACDMainArray[x] < MACDSignalArray[x] && MACDMainArray[x] < 0 && MACDSignalArray[x] < 0)
            {
               for(y=0; y<9; y++)
               { 
               if(MACDMainArray[y] > MACDSignalArray[y])
                  {
                  MACDSignal=0;  // MACD Buy Signal
                  MACDMainValue=MACDMainArray[y];
                  MACDSignalValue=MACDSignalArray[y];
                  }
               }
            }
          if(RSI_ARRAY[x] > 40)
            {
            RSISignal=0;  // RSI Buy Signal
            RSIValue=RSI_ARRAY[x];
            }
          }
    }
    else
    {  
      if(SMA50_ARRAY[x] < SMA100_ARRAY[x])
      {
         SMASignal=1;  // SMA Sell Signal
         SMA50Value=SMA50_ARRAY[x];
         SMA100Value=SMA100_ARRAY[x];
         if(EMA5_ARRAY[x] > EMA10_ARRAY[x] )
         {
            for(y=0; y<9; y++)
            { 
               if(EMA5_ARRAY[y] < EMA10_ARRAY[y])
               {
                  EMASignal=1;  // EMA Sell SIgnal
                  EMA5Value=EMA5_ARRAY[y];
                  EMA10Value=EMA10_ARRAY[y];
               }
            }
             if(MACDMainArray[x] > MACDSignalArray[x] && MACDMainArray[x] > 0 && MACDSignalArray[x] > 0)
            {
               for(y=0; y<9; y++)
               { 
                  if(MACDMainArray[y] < MACDSignalArray[y])
                  {
                     MACDSignal=1;  // MACD Sell Signal
                     MACDMainValue=MACDMainArray[y];
                     MACDSignalValue=MACDSignalArray[y];
                  }
               }
            }
            if(RSI_ARRAY[x] < 60)
            {
               RSISignal=1;  // RSI Sell Signal
               RSIValue=RSI_ARRAY[x];
            }
          }
      }
   }
   
    
 }  
 
//--- Check for posibility to enter the market

   bool PosSelect;
   PosSelect=PositionSelect(_Symbol);

   if(SMASignal==0 && EMASignal==0 && MACDSignal==0 && RSISignal==0)
   {  
      b++;
      EntrySignal=0;       // All signals indicate buy
      FileWrite(file_handle,"[",b,"] MACD   Main Value at time of buy =",NormalizeDouble(MACDMainValue,_Digits));
      FileWrite(file_handle,"[",b,"] MACD   Signal Value to Time of buy =",NormalizeDouble(MACDSignalValue,_Digits));
      FileWrite(file_handle,"[",b,"] EMA5   Value at time of buy =",NormalizeDouble(EMA5Value,_Digits));
      FileWrite(file_handle,"[",b,"] EMA10  Value at time of buy =",NormalizeDouble(EMA10Value,_Digits));
      FileWrite(file_handle,"[",b,"] SMA50  Value at time of buy =",NormalizeDouble(SMA50Value,_Digits));
      FileWrite(file_handle,"[",b,"] SMA100 Value at time buy =",NormalizeDouble(SMA100Value,_Digits));
      FileWrite(file_handle,"[",b,"] RSI    Value at time of buy =",NormalizeDouble(RSIValue,_Digits));
      FileWrite(file_handle,"--"); 
      // Have to reset all signal after a buy or sale
      SMASignal=-1;       // SMA  Signal to buy or sale 
      EMASignal=-1;       // EMA  Signal to buy or sale 
      RSISignal=-1;       // RSI  Signal to buy or sale
      MACDSignal=-1;      // MACD Signal to buy or sale 
   }
   if(SMASignal==1 && EMASignal==1 && MACDSignal==1 && RSISignal==1)
   {  
      s++;
      EntrySignal=1;  // All signals indicate sell
      FileWrite(file_handle,"[",s,"] MACD   Main Value at time of buy = ",NormalizeDouble(MACDMainValue,_Digits));
      FileWrite(file_handle,"[",s,"] MACD   Signal Value to Time of buy =",NormalizeDouble(MACDSignalValue,_Digits));
      FileWrite(file_handle,"[",s,"] EMA5   Value at time of buy =",NormalizeDouble(EMA5Value,_Digits));
      FileWrite(file_handle,"[",s,"] EMA10  Value at time of buy =",NormalizeDouble(EMA10Value,_Digits));
      FileWrite(file_handle,"[",s,"] SMA50  Value at time of buy =",NormalizeDouble(SMA50Value,_Digits));
      FileWrite(file_handle,"[",s,"] SMA100 Value at time buy =",NormalizeDouble(SMA100Value,_Digits));
      FileWrite(file_handle,"[",s,"] RSI    Value at time of buy =",NormalizeDouble(RSIValue,_Digits));
      FileWrite(file_handle,"--");
      // Have to reset all signal after a buy or sale
      SMASignal=-1;       // SMA  Signal to buy or sale 
      EMASignal=-1;       // EMA  Signal to buy or sale 
      RSISignal=-1;       // RSI  Signal to buy or sale
      MACDSignal=-1;      // MACD Signal to buy or sale
   }
FileFlush(file_handle);
FileClose(file_handle);

   if(EntrySignal==0 && PosSelect==false)
     {
      if(SendOrder(0)==false)
        {
         Print("Order send buy failed with error: ",GetLastError());
         ResetLastError();
        }
     }

   if(EntrySignal==1 && PosSelect==false)
     {
      if(SendOrder(1)==false)
        {
         Print("Order send sell failed with error: ",GetLastError());
         ResetLastError();
        }
     }

   PosSelect=PositionSelect(_Symbol);

   if(PosSelect==true)
     {
      SettingTPSL();
     }
  }
//+------------------------------------------------------------------+
//| Get Ask price                                                    |
//+------------------------------------------------------------------+
double Ask()
  {
   MqlTick MQLTick;
   ZeroMemory(MQLTick);
   SymbolInfoTick(_Symbol,MQLTick);
   return(MQLTick.ask);
  }
//+------------------------------------------------------------------+
//| Get Bid price                                                    |
//+------------------------------------------------------------------+
double Bid()
  {
   MqlTick MQLTick;
   ZeroMemory(MQLTick);
   SymbolInfoTick(_Symbol,MQLTick);
   return(MQLTick.bid);
  }
//+------------------------------------------------------------------+
//| Send Order                                                       |
//+------------------------------------------------------------------+
bool SendOrder(int Tnd)
  {
   MqlTradeRequest MQLTradeRequest;
   MqlTradeResult MQLTradeResult;
   ZeroMemory(MQLTradeRequest);
   ZeroMemory(MQLTradeResult);
   MQLTradeRequest.action=TRADE_ACTION_DEAL;
   MQLTradeRequest.symbol=_Symbol;
   MQLTradeRequest.volume=Volume;
   if(Tnd==0)
     {
      MQLTradeRequest.price=Ask();
      MQLTradeRequest.type=ORDER_TYPE_BUY;
     }
   else
     {
      MQLTradeRequest.price=Bid();
      MQLTradeRequest.type=ORDER_TYPE_SELL;
     }
   MQLTradeRequest.deviation=4;
   MQLTradeRequest.type_filling=ORDER_FILLING_FOK;
   bool OrdSend;
   OrdSend=OrderSend(MQLTradeRequest,MQLTradeResult);
   return(OrdSend);
  }
//+------------------------------------------------------------------+
//| Setting TP SL                                                    |
//+------------------------------------------------------------------+
bool SettingTPSL()
  {
   bool PosSel;
   PosSel=PositionSelect(_Symbol);
   if(PosSel==true)
     {
      MqlTradeRequest MQLTradeRequest;
      MqlTradeResult MQLTradeResult;
      ZeroMemory(MQLTradeRequest);
      ZeroMemory(MQLTradeResult);
      MQLTradeRequest.action=TRADE_ACTION_SLTP;
      MQLTradeRequest.symbol=_Symbol;
      long OrdTyp;
      OrdTyp=PositionGetInteger(POSITION_TYPE);
      double OrdSL;
      OrdSL=PositionGetDouble(POSITION_SL);
      if(OrdTyp==POSITION_TYPE_BUY && OrdSL==0.0)
        {
         MQLTradeRequest.sl=Ask()-MSL*_Point;
         MQLTradeRequest.tp=Ask()+MTP*_Point;
         bool OrdSend;
         OrdSend=OrderSend(MQLTradeRequest,MQLTradeResult);
         return(OrdSend);
        }
      if(OrdTyp==POSITION_TYPE_SELL && OrdSL==0.0)
        {
         MQLTradeRequest.sl=Bid()+MSL*_Point;
         MQLTradeRequest.tp=Bid()-MTP*_Point;
         bool OrdSend;
         OrdSend=OrderSend(MQLTradeRequest,MQLTradeResult);
         return(OrdSend);
        }
     }
   return(false);
  }
//+------------------------------------------------------------------+
Alain Verleyen
Moderator
32825
Alain Verleyen  
flwilliams87:

I have an EA program that buys/sales based on the value of defined and calculated indicators.  But when I write those values to a flat file that are they never change and the counter never increments.  I think Im too close to the problem to diagnose it and would appreciate another set of eyes to point out what I am obviously missing.  The output to the flat file is below and I've also attached the code.  Thanks


The question is why are you expecting the values will change ? Only the values of the current candle are changing. You are logging the signal (buy/sell) values, this candle can be from 0 to 8, the candle 8 being tested in last. Your value will change only in very specific cases.
JD4
1100
JD4  

Was looking through your code, and noticed a couple of ideas that might help you out making your code a little easier to deal with in the future.  First, it is standard practice with some languages to use all caps in the names of constants.  Like here, the names that are all initials are fine, but SMA50_Array would work, unless you are only planning to write to the array once.  I did not look through the rest of your code to see if that is the case, as I am still learning to code in MQL.

// Indicator Arrays
double MACDMainArray[];
double MACDSignalArray[];
double SMA_ARRAY[];
double SMA50_ARRAY[];
double SMA100_ARRAY[];
double EMA5_ARRAY[];
double EMA10_ARRAY[];
double RSI_ARRAY[];
double MSL,MTP;

Also, another idea is when you are creating your variables and constants, if you are doing it like the code section below...

//--- Calculate getting entry signals to buy and sale

   int EntrySignal;
   int SMASignal;
   int EMASignal;
   int RSISignal;
   int MACDSignal;
   double SMA50Value;
   double SMA100Value;
   double MACDSignalValue;
   double MACDMainValue;
   double EMA5Value;
   double EMA10Value;
   double RSIValue;
   int y;
   int x;
   int b;
   int s;
   b=0;  // Buy Counter
   s=0;  // Sale Counter
   EntrySignal=-1;     // Entry Set not to buy or sale
   SMASignal=-1;       // SMA  Signal to buy or sale 
   EMASignal=-1;       // EMA  Signal to buy or sale 
   RSISignal=-1;       // RSI  Signal to buy or sale
   MACDSignal=-1;      // MACD Signal to buy or sale 
   SMA50Value=0;
   SMA100Value=0;
   MACDSignalValue=0;
   MACDMainValue=0;
   EMA5Value=0;
   EMA10Value=0;
   RSIValue=0;

You could combine the two sections and create and initialize them at the same time, like below.  If you had to define the values later on in the program instead, obviously you would give them the value then.  Another thing I did is what some programmers do with the alignment to make it a little easier to read the names and values.  Since the compiler basically ignores anything that isn't program code, including spaces, this works as well as the other way.

//--- Calculate getting entry signals to buy and sale

   int    EntrySignal     = -1;   // Entry Set not to buy or sale
   int    SMASignal       = -1;   // SMA Signal to buy or sale
   int    EMASignal       = -1;   // EMA Signal to buy or sale
   int    RSISignal       = -1;   // RSI Signal to buy or sale
   int    MACDSignal      = -1;   // MACD Signal to buy or sale
   double SMA50Value      = 0;
   double SMA100Value     = 0;
   double MACDSignalValue = 0;
   double MACDMainValue   = 0;
   double EMA5Value       = 0;
   double EMA10Value      = 0;
   double RSIValue        = 0;
   int    y;
   int    x;
   int    b               = 0;    // Buy Counter
   int    s               = 0;    // Sale Counter

If you do this or not is totally a personal preference, but it might be something to help you in the future when looking for issues in your code.  I combined the colors to show the combined version.

flwilliams87
222
flwilliams87  
Alain Verleyen:
The question is why are you expecting the value will change ? Only the values of the current candle are changing. You are logging the signal (buy/sell) values, this candle can be from 0 to 8, the candle 8 being tested in last. Your value will change only in very specific cases.

Then my program is structurally flawed because it should A) Read indicator values into an array, B.1) check indicator arrays for certain conditions. B.2) if condition are met buy/sale and write indicator values at time of buy/sale to a flat file.


Obviously this is not happening probably because I need to move the buy/sale option within the 1st for loop.  The write function is probably just writing the last value of the loop.

flwilliams87
222
flwilliams87  
JD4:

Was looking through your code, and noticed a couple of ideas that might help you out making your code a little easier to deal with in the future.  First, it is standard practice with some languages to use all caps in the names of constants.  Like here, the names that are all initials are fine, but SMA50_Array would work, unless you are only planning to write to the array once.  I did not look through the rest of your code to see if that is the case, as I am still learning to code in MQL.

Also, another idea is when you are creating your variables and constants, if you are doing it like the code section below...

You could combine the two sections and create and initialize them at the same time, like below.  If you had to define the values later on in the program instead, obviously you would give them the value then.  Another thing I did is what some programmers do with the alignment to make it a little easier to read the names and values.  Since the compiler basically ignores anything that isn't program code, including spaces, this works as well as the other way.

If you do this or not is totally a personal preference, but it might be something to help you in the future when looking for issues in your code.  I combined the colors to show the combined version.

Thanks for the pointers. I will definitely make those changes so I am more consistent with coding standards.
flwilliams87
222
flwilliams87  
Alain Verleyen:
The question is why are you expecting the values will change ? Only the values of the current candle are changing. You are logging the signal (buy/sell) values, this candle can be from 0 to 8, the candle 8 being tested in last. Your value will change only in very specific cases.

Rats! Still not working right!  I moved the buy/sale code into the 1st for loop thinking that was it but my flat file ForExpertsReports still keeps printing the exact same values! Any thoughts?


[1] MACD   Main Value at time of buy = 0.00015
[1] MACD   Signal Value to Time of buy =0.00043
[1] EMA5   Value at time of buy =1.07453
[1] EMA10  Value at time of buy =1.07474
[1] SMA50  Value at time of buy =1.07457
[1] SMA100 Value at time buy =1.07518
[1] RSI    Value at time of buy =49.59316
--
[1] MACD   Main Value at time of buy = 0.00015
[1] MACD   Signal Value to Time of buy =0.00043
[1] EMA5   Value at time of buy =1.07453
[1] EMA10  Value at time of buy =1.07474
[1] SMA50  Value at time of buy =1.07457
[1] SMA100 Value at time buy =1.07518
[1] RSI    Value at time of buy =49.59316
--
[1] MACD   Main Value at time of buy = 0.00015
[1] MACD   Signal Value to Time of buy =0.00043
[1] EMA5   Value at time of buy =1.07453
[1] EMA10  Value at time of buy =1.07474
[1] SMA50  Value at time of buy =1.07457
[1] SMA100 Value at time buy =1.07518
[1] RSI    Value at time of buy =49.59316
JD4
1100
JD4  
flwilliams87:
Thanks for the pointers. I will definitely make those changes so I am more consistent with coding standards.

It is not so much a coding standard as one possible person's style.  Some people code it just like you did all the time, and some code it how I showed you.  If you were to call it a "standard," then both styles qualify for that name.   My intent was to show you a way to make your code shorter and more readable for you.


As far as your loop problem, the simplest explanation is that within the loop, you are comparing two things that are basically the same, probably the same original source of the data (like actually from the same candle like was said before).  Where and how to fix it isn't so clear, and I do not know MQL code well enough yet to be able to help other than with generalities.  One method that has been used by me and many others is the liberal use of print or log  statements.  Obviously specific to the area they are placed, but output the names of the variables and the values you are expecting to see at that point.  Also, not sure how exactly in MQL, but if you can use a generic sort of print that says what variable or function the data is obtained from.  That might make your problem leap out at you.

Alain Verleyen
Moderator
32825
Alain Verleyen  
flwilliams87:

Rats! Still not working right!  I moved the buy/sale code into the 1st for loop thinking that was it but my flat file ForExpertsReports still keeps printing the exact same values! Any thoughts?


It's difficult to help, only you know what you are trying to achieve. Obviously it's not what you coded.
flwilliams87
222
flwilliams87  
Alain Verleyen:
It's difficult to help, only you know what you are trying to achieve. Obviously it's not what you coded.

What I'm attempting to do and I admit failing miserably is capture the moment when SMA.50 crosses above SMA.100 write the values for both to a flat file and set the flag for the SMA condition to sale.  The trick is the arrays should hold values for 8 candle sticks.  I wonder if I should be doing a shift as opposed to a summation to increment through the values of the arrays?

JD4
1100
JD4  

One possible thing that might be happening if you are using an array to store values from your candles.  I tried following your posted code, but did not see something like what I am talking about below.  Unless you set the array up right to read the new values and let the old ones fall off, that could also be causing issues.


For example, let's say you have the 8 bars (indexed at 0-7) worth stored in your array.  The value from a new bar comes in, and where do you put it?  If you just put it in the last array position (index 7), then you have the 7 older candles, and the new information for the latest candle in the last position.  Unless I am guessing wrong, this is not how you want it to work.  You need a section of code that will cycle through, and effectively drop the data located at index 0.  How I would do this is copy the data from index 1 to index 0, overwriting it, which you don't need any more anyway.  Copy the data from index 2 to index 1, and so on up the line, then at the end of this process, index 6 and 7 both have the same data, until you write the new data into index 7.  Then when the next candle information you need comes in, do this whole process all over again.

flwilliams87
222
flwilliams87  
JD4:

One possible thing that might be happening if you are using an array to store values from your candles.  I tried following your posted code, but did not see something like what I am talking about below.  Unless you set the array up right to read the new values and let the old ones fall off, that could also be causing issues.


For example, let's say you have the 8 bars (indexed at 0-7) worth stored in your array.  The value from a new bar comes in, and where do you put it?  If you just put it in the last array position (index 7), then you have the 7 older candles, and the new information for the latest candle in the last position.  Unless I am guessing wrong, this is not how you want it to work.  You need a section of code that will cycle through, and effectively drop the data located at index 0.  How I would do this is copy the data from index 1 to index 0, overwriting it, which you don't need any more anyway.  Copy the data from index 2 to index 1, and so on up the line, then at the end of this process, index 6 and 7 both have the same data, until you write the new data into index 7.  Then when the next candle information you need comes in, do this whole process all over again.

That makes sense but let me ask you this... I'm populating my arrays in the OnInit() function but then I'm reading from and testing those arrays in the OnTick() function, so during the the OnInit() doesn't the array just get populated once? Im thinking it must because I only size it for 9 elements and anything more than 9 would error out. So when we get to the OnTick() function the array only contains eight elements correct? Or am I not understanding the flow of my program?

12
To add comments, please log in or register