mql4 iLowest() giving the wrong candle ID? - mql4

I am trying to get the lowest candle ID inside for loop
Use case:
Step 1: i am using for loop to check candle ID of the candle on which the indicator is visible most recently, and then break the loop
Step 2: i am using iLowest() to get the candle ID of the bar having the lowest value out of 5 ,
Step3 : using the candle id (from step 2) to get low value of that candle and then calculate the SL for that candle.
But i am getting wrong ID of candle in Step 2.
please have a look at the below screenshot, the comment of bar should be 74 but it shows 135
Here is my code :
for(int i=0;i<1000;i++)
{
double btt1= iCustom(NULL,0,"Arrows",0,i);
double btt2= iCustom(NULL,0,"Arrows",1,i);
if(btt1!=EMPTY_VALUE)
{
if(Digits==5|| Digits==3)
{
baar =iLowest(NULL,0,MODE_LOW,5+i,i);
lowestb = Low[baar];
stoploss = lowestb- 20*Point*10;
Comment("value of i : ",i," value of baar : " , baar );
ObjectCreate(chart_ID,name,OBJ_LABEL,sub_window,0,0);
ObjectSetInteger(chart_ID,name,OBJPROP_XDISTANCE,xx1x);
ObjectSetInteger(chart_ID,name,OBJPROP_YDISTANCE,yy1y);
ObjectSetInteger(chart_ID,name,OBJPROP_CORNER,corner);
ObjectSetString(chart_ID,name,OBJPROP_TEXT, stoploss );
ObjectSetString(chart_ID,name,OBJPROP_FONT,font);
ObjectSetInteger(chart_ID,name,OBJPROP_FONTSIZE,font_size);
ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clrRed);
ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
break;
}
}
}
What am i doing wrong ?

Change it to :
baar =iLowest(NULL,0,MODE_LOW,5,i);
Because the 4th parameter of the iLowest function counts the number of bars that you want to analyze starting from the counter i (the last parameter).

Related

How to make a function to count (add) return 1 value every hour? MQL4

I have a normal loop,where im trying to make dynamic the variable "counter"
int counter;
for ( double i=0; i<counter; i++)
{
What i want to do
};
and then when I run "what I want to do" the "counter = 0". I reset the counter and im looking to add a 1 value to the counter every hour.
I hope you can understand to help me.
Thanks!!

Why my OrderSend function returns a price higher than the candle high price?

I'm trying to write an Ea with 2 moving averages, and I want it to send an order when they cross.
Sell orders seem fine, but for Buy orders in 1-min chart, the Ask price which I use to send the order is not in the candle price range. Actually I did Print("Ask=",Ask) right before my OrderEntry function and right after OrderSend(). They are equal to some price way above the candle-[High] and previous candle-[close]
But when I try higher time frames, it get closer to the candle and in daily time frame, the Ask price for orders would be in the candle range (High-Low). Does anybody know why this is happening?
void CheckForMaTrade(){
double PreviousFast = iMA(NULL,0,FastMaPeriod,FastMaShift,FastMaMethod,FastMaAppliedTo,2);
double CurrentFast = iMA(NULL,0,FastMaPeriod,FastMaShift,FastMaMethod,FastMaAppliedTo,1);
double PreviousSlow = iMA(NULL,0,SlowMaPeriod,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,2);
double CurrentSlow = iMA(NULL,0,SlowMaPeriod,SlowMaShift,SlowMaMethod,SlowMaAppliedTo,1);
if(PreviousFast<PreviousSlow && CurrentFast>CurrentSlow)
OrderEntry(0);
if(PreviousFast>PreviousSlow && CurrentFast<CurrentSlow)
OrderEntry(1);
}
void OrderEntry(int direction){
if(direction == 0){
double bsl=0;
double btp=0;
if(StopLoss != 0)
bsl = Ask-(StopLoss*pips);
if(TakeProfit != 0)
btp = Ask+(TakeProfit*pips);
if(OpenOrdersThisPair(Symbol()) < 10 )
OrderSend(NULL,OP_BUY,LotSize,Ask,0,bsl,btp,NULL,MagicNumber,0,Gold);
}
enter image description here
In MT4, the candles are drawn by BID prices only. ASK prices are not taken into consideration. So it is possible that Ask > highest bid.

Can't calculate the right Volume RSI in MQL4 with a functioning Pine-Script Example

I want to "translate" a Pine-Script to MQL4 but I get the wrong output in MQL4 compared to the Pine-Script in Trading-view.
I wrote the Indicator in Pine-Script since it seems fairly easy to do so.
After I got the result that I was looking for I shortened the Pine-Script.
Here the working Pine-Script:
// Pinescript - whole Code to recreate the Indicator
study( "Volume RSI", shorttitle = "VoRSI" )
periode = input( 3, title = "Periode", minval = 1 )
VoRSI = rsi( volume, periode )
plot( VoRSI, color = #000000, linewidth = 2 )
Now I want to translate that code to MQL4 but I keep getting different outputs.
Here is the MQL4 code I wrote so far:
// MQL4 Code
input int InpRSIPeriod = 3; // RSI Period
double sumn = 0.0;
double sump = 0.0;
double VoRSI = 0.0;
int i = 0;
void OnTick() {
for ( i; i < InpRSIPeriod; i++ ) {
// Check if the Volume is buy or sell
double close = iClose( Symbol(), 0, i );
double old_close = iClose( Symbol(), 0, i + 1 );
if ( close - old_close < 0 )
{
// If the Volume is positive, add it up to the positive sum "sump"
sump = sump + iVolume( Symbol(), 0, i + 1 );
}
else
{
// If the Volume is negative, add it up to the negative sum "sumn"
sumn = sumn + iVolume( Symbol(), 0, i + 1 );
}
}
// Get the MA of the sump and sumn for the Input Period
double Volume_p = sump / InpRSIPeriod;
double Volume_n = sumn / InpRSIPeriod;
// Calculate the RSI for the Volume
VoRSI = 100 - 100 / ( 1 + Volume_p / Volume_n );
// Print Volume RSI for comparison with Tradingview
Print( VoRSI );
// Reset the Variables for the next "OnTick" Event
i = 0;
sumn = 0;
sump = 0;
}
I already checked if the Period, Symbol and timeframe are the same and also have a Screenshoot of the different outputs.
I already tried to follow the function-explanations in the pine-script for the rsi, max, rma and sma function but I cant get any results that seem to be halfway running.
I expect to translate the Pine-Script into MQL4.
I do not want to draw the whole Volume RSI as a Indicator in the Chart.
I just want to calculate the value of the Volume RSI of the last whole periode (when new candel opens) to check if it reaches higher than 80.
After that I want to check when it comes back below 80 again and use that as a threshold wether a trade should be opened or not.
I want a simple function that gets the Period as an input and takes the current pair and Timeframe to return the desired value between 0 and 100.
Up to now my translation persists to provide the wrong output value.
What am I missing in the Calculation? Can someone tell me what is the right way to calculate my Tradingview-Indicator with MQL4?
Q : Can someone tell me what is the right way to calculate my Tradingview-Indicator with MQL4?
Your main miss of the target is in putting the code into a wrong type of MQL4-code. MetaTrader Terminal can place an indicator via a Custom Indicator-type of MQL4-code.
There you have to declare so called IndicatorBuffer(s), that contain pre-computed values of the said indicator and these buffers are separately mapped onto indicator-lines ( depending on the type of the GUI-presentation style - lines, area-between-lines, etc ).
In case you insist on having a Custom-Indicator-less indicator, which is pretty legal and needed in some use-cases, than you need to implement you own "mechanisation" of drawing lines into a separate sub-window of the GUI in the Expert-Advisor-code, where you will manage all the settings and plotting "manually" as you wish, segment by segment ( we use this for many reasons during prototyping, so as to avoid all the Custom-Indicator dependencies and calling-interface gritty-nitties during the complex trading exosystem integration - so pretty well sure about doability and performance benefits & costs of going this way ).
The decision is yours, MQL4 can do it either way.
Q : What am I missing in the Calculation?
BONUS PART : A hidden gem for improving The Performance ...
In either way of going via Custom-Indicator-type-of-MQL4-code or an Expert-Advisor-type-of-MQL4-code a decision it is possible to avoid a per-QUOTE-arrival re-calculation of the whole "depth" of the RSI. There is a frozen-part and a one, hot-end of the indicator line and performance-wise it is more than wise to keep static records of "old" and frozen data and just update the "live"-hot-end of the indicator-line. That saves a lot of the response-latency your GUI consumes from any real-time response-loop...

How to hide value labels when zooming out in MPAndroidChart?

I'm using Swift version of MPAndroidChart and trying to figure out how to hide value labels when a few of them are located too close together and start to overlap on a line chart.
Hiding all the value labels when zoomed out to a certain point works too(still don't know how to do it), but hiding only the ones that are overlapping would be the best.
I know that I can manually call setDrawValues = false, but I want it to be called automatically on zoom.
enter image description here
In Android, I use the same as iOS
1: Find the difference between 1st and 2nd index y-axis label(YAxisRenderer)
for (int i = from; i < to; i++)
{
String text = mYAxis.getFormattedLabel(i);
double value1=Double.parseDouble(mYAxis.getFormattedLabel(1).replace(",",""));
double value2=Double.parseDouble(mYAxis.getFormattedLabel(2).replace(",",""));
result=(value2-value1)-40;
//POPreferences.setDifference(String.valueOf(result));
c.drawText(text, fixedPosition, positions[i * 2 + 1] + offset, mAxisLabelPaint);
}
2: Use result variable when text draws on bar or line(BarChartRenderer.java):
if (result >= vals[k / 2])
{
drawValue(c,dataSet.getValueFormatter(),vals[k / 2], entry, i, x, y,color);
}

Changing data in HighCharts series causes y-axis to blow up

I'm seeing some odd behavior in a Highcharts line chart. I have multiple series displayed, and need to let the user change what's called the "Map level" on the chart, which is a straight line across all time periods. Assuming that the correct series is
chart.series[i]
and that the new level that I want it set to is stored in var newMapLevel,
I'm changing that series' data like so:
data = chart.series[i].data;
for(j=0; j<data.length; j++){
data[j].y = newMapLevel;
}
chart.series[i].setData(data);
Calling this function has the desired effect UNLESS the new map level y_value is ONE greater than the highest y_value of all other series, in which case the y-axis scale blows up. In other words, if the y_axis scale is normally from 0 to 275,000, and the highest y_value of any of the other series is, say, 224,000, setting the new map level value to 224,001 causes the y_axis scale to become 0 to 27500M. Yes, that's 27.5 billion.
Might this be a bug in Highcharts? Or is there a better way to change the data in a series?
I've posted a fiddle: http://jsfiddle.net/earachefl/4FuNE/4/
I got my answer from the Highcharts forum:
http://highslide.com/forum/viewtopic.php?f=9&t=13594&p=59888#p59888
This doesn't work as smoothly as I'd like. When you go from 8 as your line to 2 as your line, the scale doesn't adjust back down until you enter another value. Perhaps it's a start in the right direction.
$(document).ready(function(){
$('#clickme').click(function(){
var newMapLevel = $('#newMAP').val();
if(newMapLevel){
for(i=0; i<chart.series.length; i++){
if(chart.series[i].name == 'Map Level'){
data = chart.series[i].data;
for(j=0; j<data.length; j++){
data[j].y = newMapLevel;
}
// get the extremes
var extremes = chart.yAxis[0].getExtremes();
//alert("dataMin: " + extremes.dataMin);
//alert("dataMax: " + extremes.dataMax);
// define a max YAxis value to use when setting the extremes
var myYMax = extremes.dataMax;
if (newMapLevel >= myYMax) {
myYMax = Number(newMapLevel) + 1; // number conversion required
}
if (myYMax > chart.yAxis[0].max) {
alert('cabbbie');
myYMax = chart.yAxis[0].max + 1;
}
//alert("myYMax: " + myYMax);
chart.yAxis[0].setExtremes(extremes.dataMin, myYMax)
// finally, set the line data
chart.series[i].setData(data);
}
}
}
}); });

Resources