ANy ideas?
Consider this: rounding to the nearest 0.10 and also rounding to the near 0.25 is problematic --- particularly since the latter case (ie 0.25) is not really "rounding"; therefore, I suggest you implement a "for" loop and compare the Bid to each element in the Stop_Array, and create a variable that records the lowest differential in that comparison process.
Also, wouldn't it be wiser to move the Stop_Array declaration outside the start() function instead of redefining it with each new incoming tick?
Consider this: rounding to the nearest 0.10 and also rounding to the near 0.25 is problematic --- particularly since the latter case (ie 0.25) is not really "rounding"; therefore, I suggest you implement a "for" loop and compare the Bid to each element in the Stop_Array, and create a variable that records the lowest differential in that comparison process.
Also, wouldn't it be wiser to move the Stop_Array declaration outside the start() function instead of redefining it with each new incoming tick?
Something like this?
Not sure about the 10*MathRound in the JPY pairs part...
//+------------------------------------------------------------------+ //| StopHunting101_PriceIndicator.mq4 | //| Copyright © 2009, MetaQuotes Software Corp. | //| http://www.metaquotes.net | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, MetaQuotes Software Corp." #property link "http://www.metaquotes.net" #property indicator_chart_window bool SoundAlert = true; datetime LastAlertTime; //set the psych levels for stop hunting double stops_array_5dec[23] = {0.1, 0.2, 0.25, 0.3, 0.4, 0.5, 0.6, 0.7, 0.75, 0.8, 0.9, 1.0, 1.1, 1.2, 1.25, 1.3, 1.4, 1.5, 1.6, 1.7, 1.75, 1.8, 1.9, 2.0}; double stops_array_JPYdec[22] = {200, 190, 180, 175, 170, 160, 150, 140, 130, 125, 120, 110, 100, 90, 80, 75, 70, 60, 50, 40, 30, 25, 20}; //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { if (Hour()>23 || Hour()<8) {SoundAlert=false;} else {SoundAlert=true;} //50 SMA calculation double MA50 = iMA(NULL,PERIOD_H1,50,0,MODE_SMA,PRICE_MEDIAN,0); //Work out nearest price level //Standard pairs if (Point==0.00001) { int count=ArraySize(stops_array_5dec); for(int i=0; i<count; i++) { if (MathAbs(Bid-stops_array_5dec[i])<0.0250) { double currPANearest = stops_array_5dec[i];//NormalizeDouble(stops_array_5dec[i],2); } } double distanceAway = MathAbs(currPANearest-Bid); Comment("currPANearest="+DoubleToStr(currPANearest,2)+ "\nDistance away="+DoubleToStr(distanceAway*10000,0) ); if (SoundAlert && LastAlertTime < Time[0] && (distanceAway*10000)<50) { if ((Bid<currPANearest&&Bid>MA50) || (Bid>currPANearest&&Bid<MA50)) //ie in up trend or downtrend to the line { Alert("Nearing psych level on "+Symbol()); LastAlertTime = Time[0]; } } } //JPY pairs if (Point==0.001) { count=ArraySize(stops_array_JPYdec); for(i=0; i<count; i++) { if (MathAbs(Bid-stops_array_JPYdec[i])<2.50) { currPANearest = 10*MathRound(stops_array_JPYdec[i]/10); } } distanceAway = MathAbs(currPANearest-Bid); Comment("currPANearest="+DoubleToStr(currPANearest,0)+ "\nDistance away="+DoubleToStr(distanceAway*100,0) ); if (SoundAlert && LastAlertTime < Time[0] && (distanceAway*100)<50) { Alert("Nearing psych level on "+Symbol()); LastAlertTime = Time[0]; } } }
SanMiguel,
I would do the "for" loop something like this:
int count=ArraySize(stops_array_5dec);
for(int i=0; i<count; i++)
{
double currDifferential=MathAbs(Bid-stops_array_5dec[i])
if(currDifferential < double smallestDifferential)
{
smallestDifferential=currDifferential;
double desiredLevel=stops_array_5dec[i];
}
}
Since your array includes 0.10 and 0.25 intervals, I would search for the value in the array that is closest to the Bid by calculating the size of the differential between each array element and the Bid. Thus, the value derived from each calculation must be compared to the smallest differential calculated thus far. If the current calculation is smaller than the smallest differential calculated thus far, then the smallest differential needs to be updated ---- that is what is done inside the conditional-if. Additionally, the stops_array value that yields the smallest differential also has to be recorded --- that is the variable entitled "desiredLevel" and it too is updated inside the conditional-if.
You now know the value that you are seeking and can use it in your EA as desired.
Note: With respect to the JPY pairs you do not have to use the "for" loop routine because you were not trying to round to a number ending with "5"; "Rounding" to "5" is not rounding --- that is why you had to do something different with 5-decimal pairs. The single formula that I gave you in a previous post should do the trick that you want for the JPY pairs.
Cheers
SanMiguel,
I would do the "for" loop something like this:
int count=ArraySize(stops_array_5dec);
for(int i=0; i<count; i++)
{
double currDifferential=MathAbs(Bid-stops_array_5dec[i])
if(currDifferential < double smallestDifferential)
{
smallestDifferential=currDifferential;
double desiredLevel=stops_array_5dec[i];
}
}
Since your array includes 0.10 and 0.25 intervals, I would search for the value in the array that is closest to the Bid by calculating the size of the differential between each array element and the Bid. Thus, the value derived from each calculation must be compared to the smallest differential calculated thus far. If the current calculation is smaller than the smallest differential calculated thus far, then the smallest differential needs to be updated ---- that is what is done inside the conditional-if. Additionally, the stops_array value that yields the smallest differential also has to be recorded --- that is the variable entitled "desiredLevel" and it too is updated inside the conditional-if.
You now know the value that you are seeking and can use it in your EA as desired.
Note: With respect to the JPY pairs you do not have to use the "for" loop routine because you were not trying to round to a number ending with "5"; "Rounding" to "5" is not rounding --- that is why you had to do something different with 5-decimal pairs. The single formula that I gave you in a previous post should do the trick that you want for the JPY pairs.
Cheers
Thanks - I'll try that.
Actually the JPY pairs need 75, 125, 175, etc. in their code so it's slightly similar.
Thanks - I'll try that.
Actually the JPY pairs need 75, 125, 175, etc. in their code so it's slightly similar.
In your original post the intervals were in units of 10 only ---- your request has morphed. Oh well. Looks like you will have to use the "for" loop procedure and check the for least differential with JPY pairs as well.
Happy Trading.
Example:
To find prices with last two digits 00 25 50 75
if( MathMod(Bid/Point, 25) == 0) {
...do something...
}
San Miguel,
I intended to post a follow-up to this topic late last week, but was unable to log-in so now I have a new username.
The MathMod() function is a good function, but I do not think it's a good application in your case because you are also mixing in 10 intervals with the 25 intervals
Thus if you had a 168 value it would round to 175 when in fact you want 170. Therefore, I think the for loop routine I suggested in earlier posts is the easiest way to resolve your need.
Cheers
San Miguel,
I intended to post a follow-up to this topic late last week, but was unable to log-in so now I have a new username.
The MathMod() function is a good function, but I do not think it's a good application in your case because you are also mixing in 10 intervals with the 25 intervals
Thus if you had a 168 value it would round to 175 when in fact you want 170. Therefore, I think the for loop routine I suggested in earlier posts is the easiest way to resolve your need.
Cheers
Hi guys,
it's much more simple than that.......
just do something like this:
(mathround (numertoberounded*4))/4
it should work.... let me know it!!!
ciao puma
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I need to get the following code to also look at 0.25, 0.5, and 0.75 (ie 2 decimal places) in the range but not sure how to check that as well as checking the 1 st decimal place only.
For example, it should alert me at all main 1.x numbers (1.1, 1.2, 1.3, etc.) but also 1.25, 1.5, 1.75, 0.75, 0.5, 0.25