I'm writing a script buy_strategy1 containing global variables and the script is called as function by the main program. The global variabls in the script buy_strategy1 are variables initialized in the main program. When the program is running, it is supposed to update the global variables count_1min_goal and hold in the main program if speific criterions are satisfied. The main program is as follows: #%% main program # parameters------------------------------------------------------------------- n = 5 m1 = 5 m2 = 10 m3 = 20 n1 = 8 count = 0 count_1min_goal = 0 # initialized and waiting for updating hold = False # initialized and waiting for updating # start strategy--------------------------------------------------------------- for idx1 in range(0,len(df)+1,n): if count==70:break count += 1 df_temp = df.iloc[n-1:idx1:n].copy() for idx2 in range(idx1,idx1+3): if hold is False: # no position try: df_temp = get_current_candle(df, df_temp, idx2) buy_strategy1(df_1min, df_temp, idx2) except Exception: pass The script is: import pandas_ta as ta def buy_strategy1(df_1min, df_n_min, idx, m1=5, m2=10, m3=20, n1=8, n_calc=100): global count_1min_goal, hold ma1 = ta.sma(df_n_min['Close'],length=m1) ma2 = ta.sma(df_n_min['Close'],length=m2) ma3 = ta.sma(df_n_min['Close'],length=m3) if df_n_min.iloc[-1]['Close']<min(ma1.iloc[-1], ma2.iloc[-1], ma3.iloc[-1]): corr_1min = df_1min.iloc[idx-n_calc:idx+1] # part of 1min dataframe ma_1min = ta.sma(corr_1min['Close'],length=n1) # calculate ma for 1 min if corr_1min['Close'].iloc[-1] > ma_1min.iloc[-1]: count_1min_goal += 1 else: count_1min_goal = 0 if count_1min_goal == 2: # 2 consecutive 1 min candle above ma hold = True # open a position print('count_1min_goal=' + str(count_1min_goal)) print('hold=' + str(hold)) It can be seen in the script buy_strategy1 that count_1min_goal and hold should be updated when the criterions are satisfied. When the program is running, it printed the following information as expected: count_1min_goal=2 hold=True That means that count_1min_goal and hold are updated successfully in the script. But in the main program it still gives count_1min_goal=0 hold=False I want to know why it failed to update the variables in the main program and how to fix it? Continue reading...