Jan-26-2020, 01:05 PM
Based on responses I got from previously posting a question about global variables, the following code should work. But it's not. Clearly, my understanding of how to use global variables in Python is still lacking. Also, I know I'll get admonished for using globals in the first place, but in this instance, I don't know of a better way to do this.
I've re-read previous replies, searched the forum and the Internet, but I still can't find an answer to this specific question. I need to track the values of certain variables not only across functions but across functions residing in modules in separate .py files within my project.
The trouble is the variable "option_ceiling" is not changing the global version of the same variable name coming out of function "take_option_loss()".
Any assistance offered is greatly appreciated.
My error message is:
My main module is:
I've re-read previous replies, searched the forum and the Internet, but I still can't find an answer to this specific question. I need to track the values of certain variables not only across functions but across functions residing in modules in separate .py files within my project.
The trouble is the variable "option_ceiling" is not changing the global version of the same variable name coming out of function "take_option_loss()".
Any assistance offered is greatly appreciated.
My error message is:
Quote:Global variable 'option_ceiling' is undefined at the module level
My main module is:
import sys import openpyxl as xl from setup import process_first_row from equity_processing import check_for_exit_trade, take_equity_loss, take_equity_profit, initialize_equity_variables from option_processing import take_option_profits, check_for_option_trade, take_option_loss option_ceiling option_floor, prev_option_strike for row in range(2, sheet.max_row): if sym_open_cell.value is None: # <-- You reached EOF so get out wb.save('ko_history3.xlsx') sys.exit() elif prev_exit_price == 0: if sym_low_cell.value < option_floor: do_function_1(arg, arg) do_function_2() elif sym_high_cell.value > option_ceiling: do_function_1() prev_option_strike, option_strike_price, option_floor, option_ceiling = take_option_loss(prev_option_strike, option_strike_price, option_strike_price_cell, option_floor, option_profit_target, option_ceiling) else: do_function_3() elif prev_exit_price != 0: do_function_4() else: error_module() print(f"Option Ceiling: ", option_ceiling)The following function is stored in a .py file called "option_processing.py" within my project:
def take_option_loss(prev_option_strike, option_strike_price, option_strike_price_cell, option_floor, option_profit_target, option_ceiling): global option_ceiling print("Entered: take_option_loss") option_ceiling += option_profit_target option_strike_price += option_profit_target option_strike_price_cell.value = option_strike_price prev_option_strike = option_strike_price option_floor += option_profit_target print("Exited: take_option_loss") return prev_option_strike, option_ceiling, option_strike_price, option_floorThe return value output from the above function is not updating the global variables in my Main Program. I'm sure this is a simple problem for a Python pro, but for a newbie like me, it's monumental. Any ideas?