Candle Indication - page 2

 
I have been experimenting with an indicator today, the problem is OnCalculate is only running once every new candle, which is useless. The MQL4 reference says the 'symbol' (within OnCalculate) will update on tick, if it changes but bizarrely current price (Bid price) doesnt seem to be a symbol! I have a bought indicator that updates arrows on the chart with live price changes, so I know its possible!?
 
Bostock58:
I have been experimenting with an indicator today, the problem is OnCalculate is only running once every new candle, which is useless. The MQL4 reference says the 'symbol' (within OnCalculate) will update on tick, if it changes but bizarrely current price (Bid price) doesnt seem to be a symbol! I have a bought indicator that updates arrows on the chart with live price changes, so I know its possible!?

OnCalculate() runs every tick always.

Maybe you have the code in OnCalculate() that only executes once per bar.

 

Its good to hear that, here is the file I have been practicing with. Everything I try only produces an output at the beginning of a new candle :-/

Close[0] gives the close price of the last candle, while the current builds.

Files:
script.jpg  201 kb
 
Bostock58:

Its good to hear that, here is the file I have been practicing with. Everything I try only produces an output at the beginning of a new candle :-/

Close[0] gives the close price of the last candle, while the current builds.

If you want help with code, then paste your code here.

 

The purpose of the indicator was simply to test the logic and develop the scirpt. The Comments were an attempt to figure out what it was doing. I thought I had found the problem yesterday when it suddenly started working i.e every tick, even the arrows moved on the screen with price change. I changed some of the i values to adjust which candle close was used, it stopped working and Commented i value 300 hundred points off the graph! I was about to give up with it, then while watching the chart it sprang into life, started updating the output on every tick and showing accurate values, never touched a thing!

The Index,Timeframe & OHLC is shown in the top left corner of my charts by default and updates on tick without problem. I have been running it on the UK100 & NASDAQ. I dropped it on the chart today and it doesnt work i.e only updates on a new candle, prone to showing bizarre i values.


//+------------------------------------------------------------------+
//|                                                     NEW2Candle.mq4 |
//|              Copyright 2018, MetaQuotes Software Corp. |
//|                                            https://www.mql5.com |
//+------------------------------------------------------------------+

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 clrBlack
#property indicator_width1 2
#property indicator_color2 clrBlack
#property indicator_width2 2

double         BullEntry[];
double         BearEntry[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                     |
//+------------------------------------------------------------------+

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BullEntry);//associates array with buffer
   SetIndexStyle(0,DRAW_ARROW,EMPTY);
   SetIndexArrow(0,162);//drawing wingding 224           
   SetIndexLabel(0,"Buy Signal");

   SetIndexBuffer(1,BearEntry);//associates array with buffer
   SetIndexStyle(1,DRAW_ARROW,EMPTY);
   SetIndexArrow(1,171);//drawing wingding 171     
   SetIndexLabel(1,"Sell Signal");
//----
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                           |
//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]) 
//+------------------------------------------------------------------+
//| Return true if a new bar appears for the symbol/period pair      |
//+------------------------------------------------------------------+

 {
  {
//---
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   int limit=Bars-counted_bars;

//First Run Through Rule.
   if(counted_bars==0){limit-=1;}
  
   for(int i=1;i<limit;i++)
    {
double current_bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);
//int time1=TimeMinute(TimeCurrent()); 
//double Min=(0);
//   Min=time1%5;
//   if(Min<1)

 
    //Bull Entry
    if(current_bid-Close[i]>=4)
      {Comment("Direct Entry Bull ",Close[i]," ",current_bid);}
     
    if(Close[i+1]-Close[i]>=2)
      {BearEntry[i]=Close[i+1];}
          
     //Bear Entry
    if(Close[i]-current_bid>=4)
      {Comment("Direct Entry Bear ",Close[i]," ",current_bid );}
     
    if(Close[i]-Close[i+1]>=2) 
      {BearEntry[i]=Close[i+1];}    
    }   
  }
  return(rates_total); 
}


I may well have to scrap it and start again.
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. How to convert the price difference into...
 

You really should not be using IndicatorCounted() anymore.

You should try to code using prev_calculated as it gives the true value or 0 if bars have been missed 

 
Can anyone offer a simple explanation of Mql4 programing 'hierarchy' or Logic paths through a script...?
 
Bostock58: Can anyone offer a simple explanation of Mql4 programing 'hierarchy' or Logic paths through a script...?

First off, don't call it "script" because it has a specific meaning in MQL. Instead you could call it program or just code.

There are mainly 3 types of programs in MQL - Indicators, Expert Advisors (EAs) and Scripts. There is also a 4th type which can be considered which is Libraries.

For the 3 main types, they are all driven by event handling and not exactly by a sequential procedural path. The Scripts do however follow more of a sequential processing path and less of an event driven one.

The main entry path or event handler for Scripts is the OnStart() handler.

For Indicators and EA there are several Event Handlers that are common, such as the OnInit(), OnDeinit(), OnTimer() and OnChartEvent().

The main functionality of an Indicator is handled by the OnCalculate() event handler, while on the EA, the main functionality is handled by the OnTick() event handler.

EAs however have other event types that it can handle, so read the online documentation for more information.

Libraries however, don't handle any events.

Event Handling Functions - Functions - Language Basics - MQL4 Reference
Event Handling Functions - Functions - Language Basics - MQL4 Reference
  • docs.mql4.com
The MQL4 language provides processing of some predefined events. Functions for handling these events must be defined in a MQL4 program; function name, return type, composition of parameters (if there are any) and their types must strictly conform to the description of the event handler function. The event handler of the client terminal...
 
Hi, many thanks for that, I have looked through the links but it was the next layer down I was looking for info on i.e code within OnCalculate?
 
Bostock58: Hi, many thanks for that, I have looked through the links but it was the next layer down I was looking for info on i.e code within OnCalculate?

There are many, many examples of Indicators in the CodeBase. Study several examples of Indicators that use the more modern code structure OnCalculate() and with functionality similar to what you are trying to achieve.

While you go through the example code, just make sure to also reference the documentation.

Reason: