当我无法将变量传递给函数时使用什么而不是全局变量
What to use instead of global when I can't pass the variable into the function
我正在使用 websockets Python 包并且正在执行的代码在 on_message
函数中,所以我无法将我的变量传递给函数,但我不想使用 global
.
我在定义函数之前设置了一些变量:
Symbol = "BTCUSDT"
interval = "1m"
EMA_period = 28
MACD_fast_period = 12
MACD_slow_period = 26
MACD_signal_period = 9
historic_highs = []
historic_lows = []
historic_closes = []
MACD_CrossBool = [None, None]
in_position = False
在我的 on_message
函数中,我需要在某些 if 语句中使用 in_position
:
def on_message(ws, message):
global in_position
# ------------------------- Unpack JSON message data ------------------------- #
json_message = json.loads(message)
candle = json_message['k'] # Accesses candle data
......[Other code here]......
if MACD_CrossOver == True and trend == 1:
if not in_position:
print("Buy order")
in_position = True
if MACD_CrossUnder == True and trend == -1:
if not in_position:
print("Sell order")
in_position = True
......[Other code here]......
if in_position:
if buy_order['status'] and close <= long_stop_price:
sell_order = margin_order(buy_order['symbol'], SIDE_SELL, buy_order['quantity'], close, ORDER_TYPE_LIMIT)
print("Long stop-loss triggered")
if sell_order['status'] and close >= short_stop_price:
buy_order = margin_order(sell_order['symbol'], SIDE_BUY, sell_order['quantity'], close, ORDER_TYPE_LIMIT)
print("Short stop-loss triggered")
那么有没有一种不同的方法可以让我在函数开始时不必使用 global in_position
来完成这项工作?另外,我无法在每条消息上设置变量,因为这无法满足我的需要。
您可以像 database
一样使用 dict
database = {
"symbol": "BTCUSDT",
"interval": "1m",
"EMA_period": 28,
"MACD_fast_period": 12,
"MACD_slow_period": 26,
"MACD_signal_period": 9,
"historic_highs": [],
"historic_lows": [],
"historic_closes": [],
"MACD_CrossBool": [None, None],
"in_position": False
}
希望能帮到你。
我正在使用 websockets Python 包并且正在执行的代码在 on_message
函数中,所以我无法将我的变量传递给函数,但我不想使用 global
.
我在定义函数之前设置了一些变量:
Symbol = "BTCUSDT"
interval = "1m"
EMA_period = 28
MACD_fast_period = 12
MACD_slow_period = 26
MACD_signal_period = 9
historic_highs = []
historic_lows = []
historic_closes = []
MACD_CrossBool = [None, None]
in_position = False
在我的 on_message
函数中,我需要在某些 if 语句中使用 in_position
:
def on_message(ws, message):
global in_position
# ------------------------- Unpack JSON message data ------------------------- #
json_message = json.loads(message)
candle = json_message['k'] # Accesses candle data
......[Other code here]......
if MACD_CrossOver == True and trend == 1:
if not in_position:
print("Buy order")
in_position = True
if MACD_CrossUnder == True and trend == -1:
if not in_position:
print("Sell order")
in_position = True
......[Other code here]......
if in_position:
if buy_order['status'] and close <= long_stop_price:
sell_order = margin_order(buy_order['symbol'], SIDE_SELL, buy_order['quantity'], close, ORDER_TYPE_LIMIT)
print("Long stop-loss triggered")
if sell_order['status'] and close >= short_stop_price:
buy_order = margin_order(sell_order['symbol'], SIDE_BUY, sell_order['quantity'], close, ORDER_TYPE_LIMIT)
print("Short stop-loss triggered")
那么有没有一种不同的方法可以让我在函数开始时不必使用 global in_position
来完成这项工作?另外,我无法在每条消息上设置变量,因为这无法满足我的需要。
您可以像 database
dict
database = {
"symbol": "BTCUSDT",
"interval": "1m",
"EMA_period": 28,
"MACD_fast_period": 12,
"MACD_slow_period": 26,
"MACD_signal_period": 9,
"historic_highs": [],
"historic_lows": [],
"historic_closes": [],
"MACD_CrossBool": [None, None],
"in_position": False
}
希望能帮到你。