Unexplained incorrect data and non-advancing numbers

 

I've racked my brain around this stuff. Looked at the book, watched videos and dove into forums with no luck.

the code is in mql4.

I'm trying to use iCustom with quite a few hiccups.

I'm still new to coding in mql4 but i can't seem to find answers to my issues anywhere.

The red boxes have mismatching numbers. They are cole but still mis matching.

The blue arrows are an ATR multiplier set (in the EA) for the previous candle. The indicator is set for current candle. The code in the EA will not advance to the next candle. It just sticks on the initialized candle.

I use iCustom for most of my call functions. None of the indicators are mine, they are all free and I do not plan to sell my EA.

It's still in it's beginning stages and there are other indicators I would like to add for testing. I just need to get past these hiccups first.

Attached are pictures of my issues and the indies I'm using thus far. My code for my EA is also below.


//+------------------------------------------------------------------+
//|                                                      NNFTest.mq4 |
//|                                                               Me |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Me"
#property link      ""
#property version   "1.00"
#property strict


//+------------------------------------------------------------------+
//| Get Indicator Values                                             |
//+------------------------------------------------------------------+

double Lots=iCustom(NULL,0,"lot size calc",2,0,0);
double HMA_Slow=iCustom(NULL,0,"HMA",14,0,0);
double HMA_Fast=iCustom(NULL,0,"HMA",10,0,0);
double ATR_SL=iCustom(NULL,0,"ATR in SL-TP",14,1.5,1,0,0,1,0,1);
double ATR_TP=iCustom(NULL,0,"ATR in SL-TP",14,1.5,1,0,0,1,1,1);
double DRSI_UPZ=iCustom(NULL,0,"Dynamic Zone RSI",16,24,1,0);
double DRSI_DNZ=iCustom(NULL,0,"Dynamic Zone RSI",16,24,2,0);
double DRSI_BUF=iCustom(NULL,0,"Dynamic Zone RSI",16,24,0,0);
double magix=420;

//+------------------------------------------------------------------+
//| Other Identifiers                                                |
//+------------------------------------------------------------------+

//double TP=(Ask+ATR_TP*Point);
//double SL=(Ask-ATR_TP*Point);
//+------------------------------------------------------------------+
//| Custom Functions                                                 |
//+------------------------------------------------------------------+
//bool buy_sig()
//  {
//   if(HMA_Fast<HMA_Slow)return(true);
//   return(false);
//  }

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
//if(buy_sig()==true)
//  {
//   OrderSend(NULL,OP_BUY,Lots,Ask,3,SL,TP,"Buy",magix,0,clrLime);
//  }


   Print(ATR_SL);
   Print(ATR_TP);
   Print(DRSI_UPZ);
   Print(DRSI_DNZ);
   Print(DRSI_BUF);
   Print(HMA_Fast);
   Print(HMA_Slow);



  }
//+------------------------------------------------------------------+
//| ChartEvent function                                              |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
//---

  }
//+------------------------------------------------------------------+



Any help would be appreciated.

Most of the videos are either non-native english speaking, which makes their accent hard to understand.

The others are convoluted, run off on tangents a lot, and constantly repeat themselves un-necessarily. (Jim Dandy)

Any materials I've read are cryptic and I have a hard time understanding them.

Forum posts have little to nothing to do with my issues.

I do hope the get mql5 up to and more intuitive than mql4's standards.

Thanx in advance.

Files:
 
Underworldbros:

I've racked my brain around this stuff. Looked at the book, watched videos and dove into forums with no luck.

the code is in mql4.

I'm trying to use iCustom with quite a few hiccups.

Global variables and function calls are done once - when the EA is loaded. That's why all your iCustoms are not updated.

You should put your iCustom statements within your OnTick() so that it's called again, to retrieve updated values every Tick.

 
Is there a way to store them in some type of memory so I'm not calling several indicators at once, which would eat up CPU?
 
Underworldbros:
Is there a way to store them in some type of memory so I'm not calling several indicators at once, which would eat up CPU?

I haven't seen any such way... moreover, such calls are necessary.

However, if your strategy only requires you to look at the indicator results of fully formed bars, you can make iCustom calls with 1 and 2 as the last parameter, and only make the calls when a bar has been formed (i.e. skip all ticks within the latest bar). This will lighten up the CPU drastically.

 
Seng Joo Thio:

I haven't seen any such way... moreover, such calls are necessary.

However, if your strategy only requires you to look at the indicator results of fully formed bars, you can make iCustom calls with 1 and 2 as the last parameter, and only make the calls when a bar has been formed (i.e. skip all ticks within the latest bar). This will lighten up the CPU drastically.

Finally found the damn reply button...

That's another issue I'm running into, in several cases. Is there a way to take a non array to a buffer for a later call of the EA, ie. the lot calculator doesn't have an array but it being part of lots in an OP_ORDER, which then takes from ATR TP/SL. 

 

Underworldbrosthe code is in mql4.

I do hope the get mql5 up to and more intuitive than mql4's standards.

Is there a way to take a non array to a buffer for a later call of the EA,

  1. Why did you post your MT4 question in the Root / MT5 EA section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero.
    4. In MTx you should only initialize them with constants. There is no default. The MT4/MT5 actually compiles with non-constants. The order that they are initialized is unspecified. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
    5. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 and MetaTrader 4 - MQL4 programming forum

  3. Only indicators can have buffers.

  4. Why would you want an indicator to compute lots? Only the EA (or script,) can trade. Have it compute your risk. Never risk more than a small percentage of your account, certainly less than 2% per trade.
    1. In code (MT4): Risk depends on your initial stop loss, lot size, and the value of the pair.
      1. You place the stop where it needs to be - where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
      2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP­-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
      3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                  MODE_TICKVALUE is not reliable on non-fx instruments with many brokers.
      4. You must normalize lots properly and check against min and max.
      5. You must also check Free​Margin to avoid stop out
      Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5=0.1 Lots maximum.

 
Underworldbros:

Finally found the damn reply button...

That's another issue I'm running into, in several cases. Is there a way to take a non array to a buffer for a later call of the EA, ie. the lot calculator doesn't have an array but it being part of lots in an OP_ORDER, which then takes from ATR TP/SL. 

Looking at the codes for the lot calculator, it'll serve its purpose well by becoming just a function within your expert. As for the other indicators, I just moved their iCustom statements into OnTick(). If you want to reduce number of calls, try limiting the call to just the start of bars, but that also means you should only call iCustom with bar index 1, not 0.

Attached is a version that runs, without the discrepancies you mentioned in your first post. 

Files:
Reason: