I'm new to Python as of 1 week ago and I'm trying to write a simple stock market backtester. In my program, I want to change a global variable called 'prev_entry_price' within a function I've defined and saved in a .py file within my project. But the variable doesn't change outside the function, and I don't understand why.
# -----------------
# Here is my code:
# -----------------
# --------------------------
# My input looks like this:
# --------------------------
# --------------------------
# My output looks like this:
# --------------------------
The third column on the third row should contain '46.939999' but instead, it always comes back as '0'. When I use the debugger, it shows the modified value within the function, as expected. It even shows the modified value in both my calling argument, and called parameter. But still, I get '0' when I print. I've read and tried everything I could find on the Internet about how to use global variables to do this, and I can't get it to work. One other thing, the parameter on my function is grayed out, and when I hover my cursor over it, I see "Parameter 'prev_entry_price' value is not used." Does this matter?
Any help I receive would be greatly appreciated.
# -----------------
# Here is my code:
# -----------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
prev_entry_price = 0 prev_exit_price = 0 for row in range ( 2 , 5 ): # for row in range(2, sheet.max_row + 1): sym_date_cell = sheet.cell(row, 1 ) sym_open_cell = sheet.cell(row, 2 ) sym_high_cell = sheet.cell(row, 3 ) sym_low_cell = sheet.cell(row, 4 ) sym_close_cell = sheet.cell(row, 5 ) sym_volume_cell = sheet.cell(row, 7 ) entry_price_cell = sheet.cell(row, 12 ) exit_price_cell = sheet.cell(row, 13 ) setup_eoddata(row, sym_open_cell, entry_price_cell, prev_entry_price, exit_price_cell, prev_exit_price) print ( f "Row " , row, prev_entry_price, entry_price_cell.value, exit_price_cell.value) def setup_eoddata(row, sym_open_cell, entry_price_cell, exit_price_cell, prev_exit_price): if row = = 2 : global prev_entry_price entry_price = sym_open_cell.value entry_price_cell.value = entry_price prev_entry_price = entry_price exit_price = 0 exit_price_cell.value = exit_price prev_exit_price = exit_price return prev_entry_price, prev_exit_price |
# My input looks like this:
# --------------------------
1 2 3 4 |
1 / 2 / 2019 46.94 47.22 46.56 46.93 45.83 11 , 603 , 700 1 / 3 / 2019 46.82 47.37 46.53 46.64 45.55 14 , 714 , 400 1 / 4 / 2019 46.75 47.57 46.64 47.57 46.45 13 , 013 , 700 1 / 7 / 2019 47.57 47.75 46.90 46.95 45.85 13 , 135 , 500 |
# My output looks like this:
# --------------------------
Output:Row 2 0 46.939999 0
Row 3 0 None None
Row 4 0 None None
Process finished with exit code 0The third column on the third row should contain '46.939999' but instead, it always comes back as '0'. When I use the debugger, it shows the modified value within the function, as expected. It even shows the modified value in both my calling argument, and called parameter. But still, I get '0' when I print. I've read and tried everything I could find on the Internet about how to use global variables to do this, and I can't get it to work. One other thing, the parameter on my function is grayed out, and when I hover my cursor over it, I see "Parameter 'prev_entry_price' value is not used." Does this matter?
Any help I receive would be greatly appreciated.