Python Forum
Global Variable Not Updating
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Global Variable Not Updating
#1
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:
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_floor
The 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?
Reply


Messages In This Thread
Global Variable Not Updating - by joew - Jan-26-2020, 01:05 PM
RE: Global Variable Not Updating - by jefsummers - Jan-26-2020, 02:01 PM
RE: Global Variable Not Updating - by joew - Jan-26-2020, 04:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  It's saying my global variable is a local variable Radical 5 1,219 Oct-02-2023, 12:57 AM
Last Post: deanhystad
  Variable scope - "global x" didn't work... ptrivino 5 3,087 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  Spyder Quirk? global variable does not increment when function called in console rrace001 1 2,255 Sep-18-2020, 02:50 PM
Last Post: deanhystad
  NameError for global variable leviporton 3 2,577 Jul-10-2020, 06:51 PM
Last Post: bowlofred
  Variable Not Updating NectDz 1 2,839 Jun-07-2020, 11:21 PM
Last Post: bowlofred
  Function Recognises Variable Without Arguments Or Global Variable Calling. OJGeorge4 1 2,278 Apr-06-2020, 09:14 AM
Last Post: bowlofred
  Variable not updating Rayaan 3 6,080 Mar-29-2020, 04:42 PM
Last Post: SheeppOSU
  Help with Global/Coerced Variable (Understanding Scope) Rev2k 6 3,552 Jan-09-2020, 03:43 AM
Last Post: Rev2k
  Declaring a Global Variable in a Function Bob1948 4 3,077 Sep-14-2019, 11:16 PM
Last Post: ichabod801
  Global variable does not seem to be global. Columbo 6 3,753 Jul-15-2019, 11:00 PM
Last Post: Columbo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020