1

I am writing a program that identifies patterns in stock market data and I am trying to identify the following short term pattern:

if the the low value is less than the open value by at least 3 and the close value is within 2 of the open value.

I am reading in the values from a CSV file in the following format but without the headers:

Open    High    Low     Close
353.4   359.2   347.7   349
351.4   354.08  349.1   353.1
350.1   354     349.3   350.2
352.4   353.28  348.7   349.8
345.7   352.3   345.7   351.5

The values are stored in float arraylists called closePrice, openPrice, lowPrice. I am calculating the This is the code I have wrote to try and identify the pattern within the data.

    for(int i = 0; i < closePrice.size(); i ++)
    {
        //Difference between opening price and the price low
        float priceDrop = Math.abs(openPrice.get(i) - lowPrice.get(i));
        //Difference between opening price and close price (regardless of positive or negative)
        float closingDiff = Math.abs(openPrice.get(i) - closePrice.get(i));

        float dropTolerance = 3.0f;
        float closingTolerance = 2.0f;

        if( (priceDrop > dropTolerance) || (closingDiff < closingTolerance) )
        {
            System.out.println("price drop = " + priceDrop + " closing diff = " + closingDiff);
            System.out.println("Hangman pattern" + "\n");
        }
    }

So what it should do is test if the price drops more than 3 and then the closing price is within 2 of the opening price however when I run the program it seems to let everything bypass the if statement. My output is:

price drop = 5.6999817 closing diff = 4.399994
Hangman pattern
price drop = 2.2999878 closing diff = 1.7000122
Hangman pattern
price drop = 0.8000183 closing diff = 0.1000061
Hangman pattern

Is it because I am comparing floats? Any help would be appreciated.

4

1 回答 1

4

看起来您混淆了 AND 运算符和 OR 运算符。

您声明您只想在满足两个条件时输出,但您的代码说如果满足任一条件,您将输出。

于 2013-02-06T16:23:53.670 回答