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?
#21
        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("Ema 8  =", e8)
        print("Ema 13 =", e13)
        print("Ema 21 =", e21)
        print("Ema 55 =", e55)

        print("rating =", total)
Of course, I'm waiting for help, sorry for the delay. I had to go out for an unexpected reason, the final code is by, if I don't use "any" it gives the error I mentioned, when I use it, it doesn't return the correct result no matter what I do.
Reply
#22
Do you know what any does?
import numpy as np
a = np.array([1, 1, 1])
b = np.array([1000, 1000, 0])

print(any(a > b))
Output:
True
Because one element in a is greater than the corresponding element in b, any(a > b) is True. Is that the comparison you want to make?

I suggested you compare
if e8[-1] > e13[-1]:
because you are printing out e8[-1] and e13[-1] and using that in your argument that the if statement is producing the wrong result. For some reason you instead compared:
if e8 > e13:
Which is an error because Python does not know how you want to compare the two arrays.

I don't think comparing the very last element in the array is a good way to compare two arrays, but at least the results would align with what you think they should be. Maybe you should compare the mean?
import numpy as np
a = np.array([1, 1, 1])
b = np.array([1000, 1000, 0])

print("any a > b", any(a > b))
print("last a > b", a[-1] > b[-1])
print("average a > b", a.mean() > b.mean())
Output:
any a > b True last a > b True average a > b False
Or maybe there are other characteristics that are more important.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Building command in a looping construct DennisT 3 1,913 Sep-08-2020, 06:32 PM
Last Post: DennisT
  shortening an elif construct Skaperen 10 5,475 Jul-24-2018, 07:06 AM
Last Post: Skaperen
  Best construct? Array, class, other? PappaBear 1 2,989 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