C.G. C.G. - 1 month ago 6
Python Question

I don't know why my code outputs nothing

This is my code:

import pandas as pd


def readFile(dailyFile, hourlyFile):
daily = pd.read_csv(dailyFile, parse_dates=[0], header=0, dayfirst=True)
hourly = pd.read_csv(hourlyFile, parse_dates=[0], header=0, dayfirst=True)
d_lines = sum(1 for line in open(dailyFile))
h_lines = sum(1 for line in open(hourlyFile))
return daily, hourly, d_lines, h_lines


def enter(daily, hourly, d_lines, h_lines):
for i in range(d_lines - 2):
for j in range(h_lines - 1):
if daily['Gmt time'][i + 1].day == hourly['Gmt time'][j].day and daily['Gmt time'][i + 1].month == \
hourly['Gmt time'][j].month and daily['Gmt time'][i + 1].year == hourly['Gmt time'][j].year:
if daily['RSI'][i] > 50:
if daily['Close'][i] > 0.75 * (daily['High'][i] - daily['Low'][i]) + daily['Low'][i]:
if hourly['Close'][j] > daily['High'][i]:
buyingRate = hourly['Close'][j]
sellingRate = None
holding = True
timeline = j
return buyingRate, sellingRate, holding, timeline


def hold(buyingRate, sellingRate, holding, timeline, d_lines, h_lines, daily, hourly):
if buyingRate != None:
for i in range(d_lines - 2):
for j in range(timeline, h_lines - 1):
if daily['Gmt time'][i + 1].day == hourly['Gmt time'][j].day and daily['Gmt time'][i + 1].month == \
hourly['Gmt time'][j].month and daily['Gmt time'][i + 1].year == hourly['Gmt time'][j].year:
if hourly['Close'][j] < daily['High'][i]:
holding = False
profit = (hourly['Close'][j] - buyingRate) / buyingRate
return profit, holding


def main():
holding = False
daily, hourly, d_lines, h_lines = readFile('data/AUDUSD_D_Demo.csv', 'data/AUDUSD_h_Demo.csv')
buyingRate, sellingRate, holding, timeline = enter(daily, hourly, d_lines, h_lines)
while holding == False:
enter(daily, hourly, d_lines, h_lines)
if holding == True:
profit, holding = hold(buyingRate, sellingRate, holding, timeline, d_lines, h_lines, daily, hourly)
print(profit)

main()


These are some of the csv data:

dailyFile:

Gmt time Open High Low Close Volume RSI
15.06.2017 00:00:00.000 0.75892 0.76313 0.7568 0.75858 107799.5406 0
16.06.2017 00:00:00.000 0.75857 0.76294 0.75759 0.76202 94367.4299 0
18.06.2017 00:00:00.000 0.76202 0.76236 0.76152 0.76188 5926.0998 0
19.06.2017 00:00:00.000 0.76189 0.76289 0.75848 0.75902 87514.849 0


hourlyFile:

Gmt time Open High Low Close Volume
15.06.2017 00:00:00.000 0.75892 0.75933 0.75859 0.75883 4777.4702
15.06.2017 01:00:00.000 0.75885 0.76313 0.75833 0.76207 7452.5601
15.06.2017 02:00:00.000 0.76207 0.76214 0.76106 0.76143 4798.4102
15.06.2017 03:00:00.000 0.76147 0.76166 0.76015 0.76154 4961.4502
15.06.2017 04:00:00.000 0.76154 0.76162 0.76104 0.76121 2977.6399


This is part of my unfinishe forex strategy but I was assuming the codes above are supposed to output something but it came out nothing, and no bugs either. Could anyone help me identify where I did wrong?

DYZ DYZ
Answer Source

At least one problem is in the following code fragment:

while holding == False:
    enter(daily, hourly, d_lines, h_lines)
    if holding == True:
        ....
        print(profit)

If holding == False, then the next iteration of the loop is executed, but the if statement (which has the print) is ignored, because holding cannot be True and False at the same time and you do not change it anywhere between the while and if lines. But if holding == False is false, then the loop stops and nothing is printed, anyway.