Posts: 11
Threads: 1
Joined: Mar 2022
Mar-19-2022, 10:10 AM
(This post was last modified: Mar-19-2022, 10:10 AM by egemynet.
Edit Reason: Added code tags
)
Why is the result wrong in the if structure below, it gives 1 every time, but it should be 2 in my checks (python 3.1)
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
sum = 0
if any(e8 > e13):
sum = 1
elif any(e8 < e13):
sum = 0
elif any(e13 > e21):
sum = 1
elif any(e13 < e21):
sum = 0
elif any(e21 > e55):
sum = 1
else:
sum = 0
print(e8, e13, e21, e55)
print(sum)
Posts: 11,872
Threads: 474
Joined: Sep 2016
Mar-19-2022, 01:54 PM
(This post was last modified: Mar-19-2022, 02:13 PM by Larz60+.)
once the if is satisfied, the remaining chain of elif's will not get executed. That's the way that it is intended.
If you want each statement to be executes, use a list of if's
the else is not necessary as you initialize sum to 0 before starting
Now, since sum is replaced each time, sum will be overwritten by the last successful if statement.
example:
def method1(e8, e13, e21, e55):
sum = 0
print(f"\nmethod1")
if (e8 > e13):
print(f"e8 > e13, so I'm done")
sum = 1
elif (e8 < e13):
print(f"e8 < e13, so I'm done")
sum = 0
elif (e13 > e21):
print(f"e813 > e21, so I'm done")
sum = 1
elif (e13 < e21):
print(f"e13 < e21, so I'm done")
sum = 0
elif (e21 > e55):
print(f"e21 > e55, so I'm done")
sum = 1
print(f"sum: {sum}")
def method2(e8, e13, e21, e55):
sum = 0
print(f"\nmethod2")
if (e8, e13, e21, e55):
print(f"e8 > e13")
sum = 1
if (e8 < e13):
print(f"e8 < e13")
sum = 0
if (e13 > e21):
print(f"e813 > e21")
sum = 1
if (e13 < e21):
print(f"e13 < e21")
sum = 0
if (e21 > e55):
print(f"e21 > e55")
sum = 1
print(f"sum: {sum}")
def main():
method1(e8 = 1, e13 = 10, e21=6, e55 = 4)
method2(e8 = 1, e13 = 10, e21=6, e55 = 4)
if __name__ == '__main__':
main() Output: method1
e8 < e13, so I'm done
sum: 0
method2
e8 > e13
e8 < e13
e813 > e21
e21 > e55
sum: 1
which is what should be expected.
Posts: 11
Threads: 1
Joined: Mar 2022
Unfortunately it didn't work sir, I reconfigured my codes as follows
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
sum = 0
if any(e8 > e13):
sum = 1
if any(e8 < e13):
sum = 0
if any(e13 > e21):
sum = 1
if any(e13 < e21):
sum = 0
if any(e21 > e55):
sum = 1
if any(e21 < e55):
sum = 0
print("Ema 8 = " + str(e8[len(df.index) - 2]))
print("Ema 13 = " + str(e13[len(df.index) - 2]))
print("Ema 21 = " + str(e21[len(df.index) - 2]))
print("Ema 55 = " + str(e55[len(df.index) - 2]))
print("rating = " + str(sum)) I used the following prites to check the results, the returning values are as follows;
Ema 8 = 3.341592238802603
Ema 13 = 3.346348533185547
Ema 21 = 3.34587458698077
Ema 55 = 3.311466932868334
rating = 0
accordingly the rating should have been 2 but the value is returning 0.
Posts: 2,166
Threads: 35
Joined: Sep 2016
sum is never assigned as 2
sum is only assigned as 0 or 1
no actually summing is done
Posts: 11
Threads: 1
Joined: Mar 2022
Mar-19-2022, 06:14 PM
(This post was last modified: Mar-19-2022, 06:14 PM by egemynet.)
Thank you for your warning, I re-edited the code as follows
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
total1 = 0
if any(e8 > e13):
total1 = 1
if any(e8 < e13):
total1 = 0
if any(e13 > e21):
total1 = 1
if any(e13 < e21):
total1 = 0
if any(e21 > e55):
total1 = 1
if any(e21 < e55):
total1 = 0
print("Ema 8 = " + str(e8[len(df.index) - 2]))
print("Ema 13 = " + str(e13[len(df.index) - 2]))
print("Ema 21 = " + str(e21[len(df.index) - 2]))
print("Ema 55 = " + str(e55[len(df.index) - 2]))
print("rating = " + str(total1)) However, the result has not changed. The rating should still be 0, but it is still wrong, the results are below. Or maybe I'm doing something wrong because of my ignorance, sorry. Could you please edit this for me?
Ema 8 = 3.338349519068691
Ema 13 = 3.3435844570161835
Ema 21 = 3.3441587154370636
Ema 55 = 3.312021685894074
rating = 0
Posts: 2,166
Threads: 35
Joined: Sep 2016
If you want to actually add the total you need to add the additional amount to the previous total
>>> total = 1
>>> total = total + 1
>>> total
2
Posts: 11
Threads: 1
Joined: Mar 2022
Mar-19-2022, 06:42 PM
(This post was last modified: Mar-19-2022, 06:43 PM by egemynet.)
Sorry, there was no change, it is still collecting incorrectly.
Ema 8 = 402.3259792233209
Ema 13 = 402.67762274519856
Ema 21 = 402.66692333411396
Ema 55 = 401.0368378680514
rating = 0
Posts: 6,202
Threads: 16
Joined: Feb 2020
Mar-20-2022, 04:26 AM
(This post was last modified: Mar-20-2022, 04:26 AM by deanhystad.)
Your if statements sets total = 0 or 1, never 2. Total makes it sound like there should be adding, but there is no adding going on. Have you changed your code? If so, please post new code.
My guess is you want something like this:
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 > e13):
total += 1
if any(e13 > e21):
total += 1
if any(e21 > e55):
total += 1
index = len(df.index) -2
print("Ema 8 =", e8[index])
print("Ema 13 =", e13[index])
print("Ema 21 =", e21[index])
print("Ema 55 =", e55[index])
print("rating =", total)
Posts: 11
Threads: 1
Joined: Mar 2022
Mar-20-2022, 07:02 AM
(This post was last modified: Mar-20-2022, 07:02 AM by egemynet.)
This is the final form of the code, but there is no change in the situation. What I want to do is if the result from the if command is True, if it is 1, if it is False, add -1 to the total and create a rating, but unfortunately I could not succeed.
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
total1 = 0
index = len(df.index) - 1
if (float(e8[index])) - float(e13[index] == True):
total1 += 1
if (float(e8[index]) - float(e13[index]) == False):
total1 -= 1
if (float(e13[index]) - float(e21[index]) == True):
total1 += 1
if (float(e13[index]) - float(e21[index]) == False):
total1 -= 1
if (float(e21[index]) - float(e55[index]) == True):
total1 += 1
if (float(e21[index]) - float(e55[index]) == False):
total1 -= 1
print("Ema 8 = " + str(e8[len(df.index) - 1]))
print("Ema 13 = " + str(e13[len(df.index) - 1]))
print("Ema 21 = " + str(e21[len(df.index) - 1]))
print("Ema 55 = " + str(e55[len(df.index) - 1]))
print("rating = " + str(total1)) Ema 8 = 396.7588162749833 this has to be -1
Ema 13 = 396.95757807407034 this has to be -1
Ema 21 = 397.48536512624753 this has to be -1
Ema 55 = 397.86351648654266
rating = 1 this has to be -3
Posts: 1,822
Threads: 2
Joined: Apr 2017
Why are you expecting the difference between two floats to be True or False ?
|