I am writing a java program to attempt to identify trends in the stock market and I want to identify the following pattern:
Where EMA value drops below SMA by over approx. 0.7% (Day 1), you can look for 0.2%-0.5% fluctuations (changes in the relationship between SMA & EMA up or down) between the two for the next 2-3 days. This indicates sell signal.
I have an array of float values for the EMA and SMA and then I have a separate float array called percentDif that contains the percentage difference between the EMA and SMA. For example these are the the first two sets of values for the EMA, SMA and percentDif arrays
EMA 314.395 SMA 314.9859884 percentDif -0.001876237
EMA 313.9476 SMA 314.4888908 percentDif -0.001721176
I have wrote this method to identify the pattern that I want but it is not working correctly, is there a better way that I can do this?
public void trend(){
/*
* Trend indicator pattern
*/
float valueDrop = -0.7f;
float minFluxPos = 0.2f;
float maxFluxPos = 0.5f;
float minFluxNeg = -0.2f;
float maxFluxNeg = -0.5f;
for(int i = 0; i< percentDif.length-2; i++)
{
//If the difference between EMA and SMA is greater or equal to -0.7 percent
if(percentDif[i] >= valueDrop){
//If the next day the percentage difference fluctuates between 0.2 or 0.5 either way
if( ((percentDif[i+1] > minFluxPos) && (percentDif[i+1] < maxFluxPos))
|| ((percentDif[i+1] > minFluxNeg) && (percentDif[i+1] < maxFluxNeg)) )
{
//Indicates price drop and therefore sell signal
System.out.println("Trend Indicated - SELL");
}
}
}
}
I have entered the following values in the percentDif array :
5
-0.8
-0.3
These values fit the description of the pattern and should therefore trigger the print statement but it is not doing so.
You have the comparison operators the wrong way round for the negative flux values. The if statement should be:
Also, given the data and the boundaries you have provided, the conditions will never hold true. In the first iteration, the outer condition holds (as 5 > -0.7) but the inner one doesn’t (as -0.8 is neither between 0.2 and 0.5 nor between -0.2 and -0.5). On the second and final iteration the outer condition fails as -0.8 < -0.7.