Python Forum
Why is the if construct not calculating correctly?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is the if construct not calculating correctly?
#11
[Image: UYtzroz]
https://imgur.com/UYtzroz
if help also explanation like this

If you have a suggestion, I would appreciate it if you could edit the code, I tried a lot, but I could not get a successful result.
Reply
#12
The difference between two numbers should be a number, not a boolean. Also, remember that floating point arithmetic isn't exact, so you likely want to check the difference is within some bounds.
Reply
#13
Unfortunately, I couldn't do it, please give it to my naivety, could you please edit my codes as you said?
Reply
#14
This makes no sense:
 if (float(e8[index])) - float(e13[index] == True):
According to your e8 and e13 values from your previous post this is:
if 396.7588162749833 - 396.95757807407034 == True:
First off, you never compare anything to True or False. True or False are results of a comparison. a > 2 will be True or False. It will be True if a=3 and False if a=1. It does not make any sense to ask:
if (a > 2) == True:
This will be True if a > 2, otherwise False. The addition of "== True" has no effect.

Second, the result of a subtraction will never equal True or False, so "(a - b) == True" will always be False. (a - b) results in a number. True and False are not numbers. At least you should not treat them as such. Testing if a number is equal to True or False is nonsensical.

Finally, your parenthesis are in the wrong places and there is no need for float(). If you want to subtract and compare the result, there is no need for parenthesis at all. This works fine:
if e8[index] - e13[index] > 0:
If you are uneasy about operator precedence you can wrap the subtraction in parenthesis.
if (e8[index] - e13[index]) > 0:
Your use doesn't make any sense:
if (e8[index]) - e13[index] > 0:
Wrapping e8[index] in parenthesis doesn't affect anything. It doesn't hurt anything, except understanding. It made me look at the code twice, wondering what the parenthesis were for before realizing they were doing nothing.

I think this may be what you want.
bars = exchange.fetch_ohlcv(symbol, timeframe=zamanAraligi, since=None, limit=500)
df = pd.DataFrame(bars, columns=["timestamp", "open", "high", "low", "close", "volume"])
 
#ema control
e8 = ta.ma("ema", df.close, length=8)
e13 = ta.ma('ema', df.close, length=13)
e21 = ta.ma('ema', df.close, length=21)
e55 = ta.ma('ema', df.close, length=55)
 
#ema rating
total = 0
if any(e8[-1] > e13[-1]):
    total += 1
else:
    total -= 1

if any(e13[-1] > e21[-1]):
    total += 1
else:
    total -= 1

if any(e21[-1] > e55[-1]):
    total += 1
else:
    total -= 1

print("Ema 8  =", e8[-1])
print("Ema 13 =", e13[-1])
print("Ema 21 =", e21[-1])
print("Ema 55 =", e55[-1])
 
print("rating =", total)
I am stunned that you can used Pandas technical analysis package and not know how how if statements or comparisons work. It makes me wonder if comparing e8[-1] against e13[-1] makes any sense at all.
Reply
#15
        bars = exchange.fetch_ohlcv(symbol, timeframe=zamanAraligi, since=None, limit=500)
        df = pd.DataFrame(bars, columns=["timestamp", "open", "high", "low", "close", "volume"])

        #ema control
        e8 = ta.ma("ema", df.close, length=8)
        e13 = ta.ma('ema', df.close, length=13)
        e21 = ta.ma('ema', df.close, length=21)
        e55 = ta.ma('ema', df.close, length=55)
        
        total = 0
        if any(e8 > e13):
            total += 1
        else:
            total -= 1

        if any(e13 > e21):
            total += 1
        else:
            total -= 1

        if any(e21 > e55):
            total += 1
        else:
            total -= 1

        print("rating =", total)
no matter what I do it always returns the result as 3 but this result is not correct, it always returns 3 where the result should be 2 or -1 where it should be.
Reply
#16
I am sorry. There should be no "any". I saw that you changed your logic to only compare the last value in the array. Not sure how the "any" snuck in there. Cut and paste error I supose.
if e8[-1] > e13[-1]:
Reply
#17
no it gives an error if it doesn't
Reply
#18
I do not understand your last response. What gives an error? What is the error?
Reply
#19
Error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Yoriz write Mar-20-2022, 05:20 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#20
Where is the code? Where is the complete error trace? Do you want help or not?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Is it possible to multi line a Basic Function Construct line statement? If so how? BrandonKastning 7 469 May-23-2024, 03:02 PM
Last Post: deanhystad
  Building command in a looping construct DennisT 3 2,027 Sep-08-2020, 06:32 PM
Last Post: DennisT
  shortening an elif construct Skaperen 10 5,686 Jul-24-2018, 07:06 AM
Last Post: Skaperen
  Best construct? Array, class, other? PappaBear 1 3,054 May-10-2017, 06:02 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020