My indicator only has one value in the buffer , which is giving me a straight line

My indicator only has one value in the buffer , which is giving me a straight line

Manage alerts

Loading saved threads...

Max · External communityPost link
External question — Stack Overflow Stack Exchange Author: Max Original post: https://stackoverflow.com/questions/67140609 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to convert an indicator I found on tradingview (which uses pinescript) to MQL4: ============================================== BullTrend = (close - lowest(low, 50)) / atr(5) BearTrend = (highest(high, 50) - close) / atr(5) BearTrend2= -1*BearTrend Trend = BullTrend - BearTrend ============================================== The code is supposed to perform the calculation shown in the my mql4 version on every bar. However, it shows the same value as far back as I try to go. How do I make sure that the calculation is performed on each bar or if there is a more efficient way to code this I would appreciate that highly. :) //+------------------------------------------------------------------+ //| BBPT.mq4 | //| Maxwell | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Maxwell" #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 3 //--- plot Trend #property indicator_label1 "Trend" #property indicator_type1 DRAW_LINE #property indicator_color1 clrWhite #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot BullTrend #property indicator_label2 "BullTrend" #property indicator_type2 DRAW_LINE #property indicator_color2 clrMediumSeaGreen #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot BearTrend2 #property indicator_label3 "BearTrend2" #property indicator_type3 DRAW_LINE #property indicator_color3 clrRed #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- indicator buffers double TrendBuffer[]; double BullTrendBuffer[]; double BearTrend2Buffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrWhite); SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrGreen); SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrRed); SetIndexLabel(0,"Trend"); SetIndexLabel(1,"BullTrend"); SetIndexLabel(2,"BearTrend2"); SetIndexBuffer(0,TrendBuffer); SetIndexBuffer(1,BullTrendBuffer); SetIndexBuffer(2,BearTrend2Buffer); //--- 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[]) { //--- int uncalculatedBar = rates_total - prev_calculated; for(int i=0; i < uncalculatedBar; i++) { double lowestPrice = Low[iLowest(Symbol(),0,MODE_LOW,50,0)]; double highestPrice = High[iHighest(Symbol(),0,MODE_HIGH,50,0)]; double lowestCloseCalc = (iClose(NULL,0,0) - lowestPrice); double highestCloseCalc = (highestPrice - iClose(NULL,0,0)); double atr = (iATR(NULL,0,5,0)); double BullTrend = lowestCloseCalc / (atr); double BearTrend = highestCloseCalc / (atr); double BearTrend2 = (-1 * BearTrend); double Trend = (BullTrend - BearTrend); TrendBuffer[i] = Trend; BullTrendBuffer[i] = BullTrend; BearTrend2Buffer[i] = BearTrend2; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+
Quote
Report

Post Reply

Quoted from Forex.com.bd-Editorial External question — Stack Overflow Stack Exchange Author: Max Source score (net votes, not local likes): 2 Original post: https://stackoverflow.com/questions/67140609 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to convert an indicator I found on tradingview (which uses pinescript) to MQL4: ============================================== BullTrend = (close - lowest(low, 50)) / atr(5) BearTrend = (highest(high, 50) - close) / atr(5) BearTrend2= -1*BearTrend Trend = BullTrend - BearTrend ============================================== The code is supposed to perform the calculation shown in the my mql4 version on every bar. However, it shows the same value as far back as I try to go. How do I make sure that the calculation is performed on each bar or if there is a more efficient way to code this I would appreciate that highly. :) //+------------------------------------------------------------------+ //| BBPT.mq4 | //| Maxwell | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Maxwell" #property link "https://www.mql5.com" #property version "1.00" #property strict #property indicator_separate_window #property indicator_buffers 3 #property indicator_plots 3 //--- plot Trend #property indicator_label1 "Trend" #property indicator_type1 DRAW_LINE #property indicator_color1 clrWhite #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot BullTrend #property indicator_label2 "BullTrend" #property indicator_type2 DRAW_LINE #property indicator_color2 clrMediumSeaGreen #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot BearTrend2 #property indicator_label3 "BearTrend2" #property indicator_type3 DRAW_LINE #property indicator_color3 clrRed #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- indicator buffers double TrendBuffer[]; double BullTrendBuffer[]; double BearTrend2Buffer[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2,clrWhite); SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2,clrGreen); SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2,clrRed); SetIndexLabel(0,"Trend"); SetIndexLabel(1,"BullTrend"); SetIndexLabel(2,"BearTrend2"); SetIndexBuffer(0,TrendBuffer); SetIndexBuffer(1,BullTrendBuffer); SetIndexBuffer(2,BearTrend2Buffer); //--- 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[]) { //--- int uncalculatedBar = rates_total - prev_calculated; for(int i=0; i < uncalculatedBar; i++) { double lowestPrice = Low[iLowest(Symbol(),0,MODE_LOW,50,0)]; double highestPrice = High[iHighest(Symbol(),0,MODE_HIGH,50,0)]; double lowestCloseCalc = (iClose(NULL,0,0) - lowestPrice); double highestCloseCalc = (highestPrice - iClose(NULL,0,0)); double atr = (iATR(NULL,0,5,0)); double BullTrend = lowestCloseCalc / (atr); double BearTrend = highestCloseCalc / (atr); double BearTrend2 = (-1 * BearTrend); double Trend = (BullTrend - BearTrend); TrendBuffer[i] = Trend; BullTrendBuffer[i] = BullTrend; BearTrend2Buffer[i] = BearTrend2; } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+

Cancel quote

Checking account access…
My indicator only has one value in the buffer , which is giving me a straight line | Forex.com.bd