Hey guys, question about code :)

 

Hey guys just looking for some help with some code on an EA Im working on. Essentially im trying to build an Array to store 3 candles worth of data that i have calculated.

now i know how to set up an array etc, however due to the fact that i have no starting value to put into the array until it has been worked out it is coming back as array out of range?

The value is based on the close of the previous candle, not the current candle, i hope what i put below can help.


 if (CurrentCandleClose>X[2] && LastCandleClose>X[2])

      

         X[0]= MathMax(X[2],(CurrentCandleClose+calc1));

         

   if (CurrentCandleClose<X[2] && LastCandleClose<X[2])

      

         X[0]= MathMin(X[2],(CurrentCandleClose-calc1));

         

   if (CurrentCandleClose>X[1])

         X[0]= (CurrentCandleClose-nLoss);

         

   else X[0]= (CurrentCandleClose+nLoss);


So X should always have a value if none of the above statements are correct? 


any help would be appreciated

The checks a trading robot must pass before publication in the Market
The checks a trading robot must pass before publication in the Market
  • www.mql5.com
Before any product is published in the Market, it must undergo compulsory preliminary checks, as a small error in the expert or indicator logic can cause losses on the trading account. That is why we have developed a series of basic checks to ensure the required quality level of the Market products. If any errors are identified by the Market...
 
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. Why did you post your coding question in the MT5 Systems section instead of the MT5 General section, MT5 EA section, MT5 Indicators section, or MQL4 section?
              Moderators requested to manage this section. - Trading Systems - MQL5 programming forum 2020.05.25
    Next time post in the correct place. The moderators will likely move this thread there soon.

  3. TheNinjaTrader: however due to the fact that i have no starting value to put into the array until it has been worked out it is coming back as array out of range?

    1. Is that a question or a statement? Values in the array has nothing to do with "out of range."

    2. Your code only references three elements, so you can't have "out of range," if your array is at least three in size.

      Always post all relevant code.
           How To Ask Questions The Smart Way. 2004
                Be precise and informative about your problem

  4. TheNinjaTrader: So X should always have a value if none of the above statements are correct? 
    1. Is that a question or a statement? X will always have values. They won't be meaningful unless you assign to them.

    2. You only read from X[2] and only write to X[0]. Only X[0] changes, X[1] is random garbage, as is X[2].



 

1- First time posting all i can do i change and apologise!

2. See 1, again so sorry

3 Essentially what i am trying to create is a line LIKE an MA, as you can see the Value for X is a calculated one based on the values of X before, so i need somewhere to store the vale of X for the last 3 candles. Im trying to record the data like an MA would so i can use the last and candles before last. i hope this explains this better? Im trying to use the else 

as you can see im trying to use X as a line to use for crossovers which is why i want to be able to recall it for exmaple MA8Array[1]>XArray[1]  MA8Array[2]>XArray[2]

#include<Trade\Trade.mqh>


// Create an instance of CTrade
CTrade trade;

input int MA=30;
input int ATRLegnth=14;
input int ADXLegnth=14;
input int StopLossAmmount=150;
input int BreakEvenTop=100;
input int BreakEvenTakePrice=20;
input int TakeProfitAmmount=500;
input int Sensitivity=2;   /////////////////////// nLoss
input int SignalStrength=20; /////////////////////// ADX


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OnTick()
  {

   //  Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);      //////////////////////////////////////////  ASK
   //   Bid price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);      //////////////////////////////////////////  BID
   //Get Account Equity
   double Balance=AccountInfoDouble(ACCOUNT_MARGIN_FREE);                         ////////////////////////////////////////// FREE MARGIN
   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);                               ////////////////////////////////////////// ACC EQUITY
   // Set Position Size Dynamically
   double DynamicPositionSize=NormalizeDouble((Equity/15000),2);                  ////////////////////////////////////////// PSITION SIZE
   double DynamicPositionSize2=NormalizeDouble((Equity/25000),2);
   double DynamicPositionSize3=NormalizeDouble((Equity/5000),2);
   MqlRates PriceInfo[];
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   // Strings for Various Signals

   string signal="";
   string MovingAverageControl="";
   double MAArray[],ADXArray[],ATRArray[],X[];
   //////////////////////////////////////
   int PriceData = CopyRates(Symbol(),Period(),0,3,PriceInfo);
   int ATRDefinition= iATR(_Symbol,_Period,ATRLegnth);
   int ADXDefinition = iADX(_Symbol,_Period,ADXLegnth);                                    ////////////////////////////////////////// ADX Config
   int MADefinition = iMA(_Symbol,_Period,MA,0,MODE_EMA,PRICE_CLOSE);             ////////////////////////////////////////// MOVING
   //////////////////////////////////////
   ArraySetAsSeries(ADXArray,true);
   ArraySetAsSeries(ATRArray,true);
   ArraySetAsSeries(MAArray,true);
   ArraySetAsSeries(PriceInfo,true);
   //////////////////////////////////////
   CopyBuffer(ATRDefinition,0,0,3,ATRArray);
   CopyBuffer(ADXDefinition,0,0,3,ADXArray);             //////////////////////////////////////////
   CopyBuffer(MADefinition,0,0,3,MAArray);             //////////////////////////////////////////
   //////////////////////////////////////
   //////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   double ATRValue=NormalizeDouble(ATRArray[1],5);
   double ADXValue= NormalizeDouble(ADXArray[1],2);
   double MAValue=NormalizeDouble(MAArray[1],2);
   double CurrentCandleClose=PriceInfo[1].close;                                //////////////////////////////////////////
   double LastCandleClose=PriceInfo[2].close;
   double nLoss =ATRValue*Sensitivity;

   //////////////////////////////////////
   //////////////////////////////////////
   ////////////Calcs/////////////////////
   
   if (PriceInfo[1].close>X[2] && LastCandleClose>X[2])
      
         X[0]= MathMax(X[1],(CurrentCandleClose-nLoss));
         
   if (CurrentCandleClose<X[2] && LastCandleClose<X[2])
      
         X[0]= MathMin(X[1],(CurrentCandleClose+nLoss));
         
   if (CurrentCandleClose>X[1])
         X[0]= (CurrentCandleClose-nLoss);
         
   else X[0]= (CurrentCandleClose+nLoss);

//////////////////// build signals from here//////////////////////

In short terms i want to start X[0] as the default value at the else statement then for the calc to work itself out from there, so that X[0] would become x[1] when the next candle gets printed

 

Create the indicator handles in OnInit() not every tick.

I will move this topic to the EA section.

 
Keith Watford:

Create the indicator handles in OnInit() not every tick.

I will move this topic to the EA section.

Thanks you for moving the topic again apologies for that! Wont happen again.

How do yo mean? Do you mean define the indicators in the OnInit section?

 
Keith Watford:

Create the indicator handles in OnInit() not every tick.

I will move this topic to the EA section.

are you saying to create the arrays in the OnInit function instead of the OnTick so that the base value at the start of initialization is 0???


how would that affect the creation of an array to fill data with the calculation shown?


if so, thanks very much that should work will post update later

 
Keith Watford:

Create the indicator handles in OnInit() not every tick.

I will move this topic to the EA section.

Sorry but no luck, im wondering what you mean?


Im trying to use a default setting as you can see in the else to start it off and from there work out the current candle based on the last 2. I want to store it like you would an indicator using copybuffer to fill the array with the calculated data and to show the data like you would see in an indicator. If you were to show [0],[1],[2] they each have their own individual value based on the market moving. im sure this is simple and i know its convoluted  but I've got a complete mind block and its the only thing stopping me from moving foreward with this EA and its doing my head in lol. Basically i ONLY want to fill the [0] with the newly worked out calcs so that the next candle can be worked out off of old data, it may not even be an array that is needed.

any help would be great

 
TheNinjaTrader:

Im trying to use a default setting as you can see in the else to start it off and from there work out the current candle based on the last 2. I want to store it like you would an indicator using copybuffer to fill the array with the calculated data and to show the data like you would see in an indicator. If you were to show [0],[1],[2] they each have their own individual value based on the market moving. im sure this is simple and i know its convoluted  but I've got a complete mind block and its the only thing stopping me from moving foreward with this EA and its doing my head in lol. Basically i ONLY want to fill the [0] with the newly worked out calcs so that the next candle can be worked out off of old data, it may not even be an array that is needed.

any help would be great

Sorry, but I have no idea what you are trying to do.

 
Keith Watford:

Sorry, but I have no idea what you are trying to do.

im trying to make a line of data like a moving average so that i can use the past data. i want to be able to use it as a crossover so i will need 3 candles worth of data.

 
TheNinjaTrader: Basically i ONLY want to fill the [0] with the newly worked out calcs …

Then only element zero has data. I don't know what you are trying to do either. Perhaps on a new bar, shift the values [2]= [1]; [1]= [0]; [0]=…;

 

Ok so ive maned to get myself a signal that will only trigger when there is a new candle, how do i make the data only copy once per signal of New Candle?!?! This is driving me insane lol


linking code below


Like i said before its something really simple and ive just got a block, thanks for the tip with the new candles it really worked, if i can get my value for X[1] to stay static until the next candle im golden. 


Thanks Again


#include<Trade\Trade.mqh>


// Create an instance of CTrade
CTrade trade;

input int MA=30;
input int ATRLegnth=14;
input int ADXLegnth=14;
input int StopLossAmmount=150;
input int BreakEvenTop=100;
input int BreakEvenTakePrice=20;
input int TakeProfitAmmount=500;
input int Sensitivity=2;   /////////////////////// nLoss
input int SignalStrength=20; /////////////////////// ADX

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void OnTick()
  {

   //  Ask price
   double Ask=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_ASK),_Digits);      //////////////////////////////////////////  ASK
   //   Bid price
   double Bid=NormalizeDouble(SymbolInfoDouble(_Symbol,SYMBOL_BID),_Digits);      //////////////////////////////////////////  BID
   //Get Account Equity
   double Balance=AccountInfoDouble(ACCOUNT_MARGIN_FREE);                         ////////////////////////////////////////// FREE MARGIN
   double Equity=AccountInfoDouble(ACCOUNT_EQUITY);                               ////////////////////////////////////////// ACC EQUITY
   // Set Position Size Dynamically
   double DynamicPositionSize=NormalizeDouble((Equity/15000),2);                  ////////////////////////////////////////// PSITION SIZE
   double DynamicPositionSize2=NormalizeDouble((Equity/25000),2);
   double DynamicPositionSize3=NormalizeDouble((Equity/5000),2);
   MqlRates PriceInfo[];
   string signal="";
    //Calc Current Candle
      int CandleNumber = Bars(_Symbol,_Period);
      //Create String Variable
      string NewCandleAppeared="";
      //User Func
      NewCandleAppeared= CheckForNewCandle(CandleNumber);
      
   static double MAArray[],ADXArray[],ATRArray[],XArray[3];
   //////////////////////////////////////
   int PriceData = CopyRates(Symbol(),Period(),0,3,PriceInfo);
   int ATRDefinition= iATR(_Symbol,_Period,ATRLegnth);
   int ADXDefinition = iADX(_Symbol,_Period,ADXLegnth);                                    ////////////////////////////////////////// ADX Config
   int MADefinition = iMA(_Symbol,_Period,MA,0,MODE_EMA,PRICE_CLOSE);             ////////////////////////////////////////// MOVING 
   //////////////////////////////////////
   ArraySetAsSeries(ADXArray,true);
   ArraySetAsSeries(ATRArray,true);
   ArraySetAsSeries(MAArray,true);
   ArraySetAsSeries(PriceInfo,true);
   ArraySetAsSeries(XArray,true);
   //////////////////////////////////////

   CopyBuffer(ATRDefinition,0,0,3,ATRArray);
   CopyBuffer(ADXDefinition,0,0,3,ADXArray);             //////////////////////////////////////////
   CopyBuffer(MADefinition,0,0,3,MAArray);             //////////////////////////////////////////
   //////////////////////////////////////
   //////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
   double ATRValue=NormalizeDouble(ATRArray[1],2);
   double ADXValue= NormalizeDouble(ADXArray[1],2);
   double MAValue=NormalizeDouble(MAArray[1],2);
   double nLoss = NormalizeDouble(ATRValue*Sensitivity,2);


   
   
   if (NewCandleAppeared=="New Candle!")
      {
        Print (XArray[2]=XArray[1]);
        }
   //////////////////////////////////////
   //////////////////////////////////////
   ////////////Calcs/////////////////////
      if (PriceInfo[1].close>XArray[2] && PriceInfo[2].close>XArray[2])
      {
         XArray[1]= MathMax(XArray[2],(PriceInfo[1].close-nLoss));
         }
   if (PriceInfo[1].close<XArray[2] && PriceInfo[2].close<XArray[2])    // what if you done it as signals, make it a 4 way gate
      {
         XArray[1]= MathMin(XArray[2],(PriceInfo[1].close+nLoss));
        }
   if (PriceInfo[1].close>XArray[2])
       {  XArray[1]= (PriceInfo[1].close-nLoss);
        }
   else XArray[1]= (PriceInfo[1].close+nLoss);

   
   //////////////////////////////////////
   ////////////BUILD SIGNALS////////////
   /////////////////////////////////////
   
   
   Comment (

                  
                  "LCC:   ",PriceInfo[1].close,  "\n",
                  
                  "CCC:   ",PriceInfo[2].close,  "\n",
                  
                  "ADX:   ", ADXValue, "\n",
                  
                  "ATR:   ", (ATRValue), "\n",
                  
                  "MA:   ", MAValue, "\n",
                  
                  "nLoss:   ", nLoss, "\n",
                  
                  "X0:   ", XArray[0], "\n",
                  
                  "X1:   ", XArray[1], "\n",
                  
                  "X2:   ", XArray[2], "\n",
                  "Bars on Chart:  ",CandleNumber,"\n",
                  "New Candle Appeared:  ",NewCandleAppeared,"\n",
                  



                  "Current Signal:   ",signal);
}



string CheckForNewCandle(int CandleNumber)
   {  
      //static int variable
      static int LastCandleNumber;
      // string for new return value
      string IsNewCandle="No New Candle";
      // Check if theres a differance
      if (CandleNumber>LastCandleNumber)
      {
         //Pos Return Value
         IsNewCandle="New Candle!";
         //next time
         LastCandleNumber=CandleNumber;
      }
      
    return IsNewCandle;
    }
         
Reason: