简体   繁体   English

Java趋势和模式查找器算法

[英]Java trend and pattern finder algorithm

I am writing a java program to attempt to identify trends in the stock market and I want to identify the following pattern: 我正在编写一个Java程序来尝试确定股市趋势,并且想确定以下模式:

Where EMA value drops below SMA by over approx. EMA值低于SMA约超过。 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. 0.7%(第1天),则可以在接下来的2-3天中寻找两者之间的0.2%-0.5%波动( SMAEMA之间的关系向上或向下的变化)。 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. 我有一个用于EMASMA的浮点值数组,然后有一个单独的浮点数组,称为percentDif ,其中包含EMA和SMA之间的百分比差。 For example these are the the first two sets of values for the EMA , SMA and percentDif arrays 例如,这些是EMASMApercentDif数组的前两组值

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 我在percentDif数组中输入了以下值: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. 这些值适合该模式的描述,因此应触发打印语句,但实际上并非如此。

By far the most inefficient thing in this code is writing to the console. 到目前为止,此代码中最无效的事情是写入控制台。 Given you most likely have only daily data over say 10 years ie 2500 samples, this should take about 1 milli-second to scan this way. 假设您很可能只有10年的每日数据(即2500个样本),以这种方式扫描大约需要1毫秒。

I suspect your loop is ending too early try 我怀疑您的循环结束为时过早尝试

for(int i = 0; i < percentDif.length - 1; i++)

and you min/max is the wrong way around for 而你的最小/最大是错误的方法

float minFluxNeg = -0.5f;
float maxFluxNeg = -0.2f;

You have the comparison operators the wrong way round for the negative flux values. 对于负通量值,比较运算符的处理方式错误。 The if statement should be: if语句应为:

if( ((percentDif[i+1] > minFluxPos) && (percentDif[i+1] < maxFluxPos))
                || ((percentDif[i+1] < minFluxNeg) && (percentDif[i+1] > maxFluxNeg)) )

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). 在第一次迭代中,外部条件成立(因为5> -0.7),但内部条件没有成立(因为-0.8既不在0.2到0.5之间,也不在-0.2到-0.5之间)。 On the second and final iteration the outer condition fails as -0.8 < -0.7. 在第二次也是最后一次迭代中,外部条件由于-0.8 <-0.7而失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM