我正在使用云平台运行程序,当程序在try/except块中遇到错误时会崩溃。我不知道是不是由于平台的问题,但我需要一种方法来避免程序崩溃。
try:
r = self.http.request('GET', 'https://www.alphavantage.co/query?function=TIME_SERIES_INTADAY&symbol=VIX&interval=1min&apikey=apikey')
data = json.loads(r.data)
if 'Time Series (1min)' in data.keys():
self.VIX = Decimal(data['Time Series (1min)'][list(data['Time Series (1min)'].keys())[0]]['4. close'])
else:
raise Exception("key")
except Exception as e:
self.Debug('VIX Error: ' + str(e))
try:
r = self.http.request('GET', 'https://www.google.com/finance/getprices?q=VIX&i=60&p=1d&f=c') #f=d,o,h,l,c,v'
s = (r.data).decode('utf-8')
l = list(s.splitlines())
self.VIX = Decimal(l[-1])
except Exception as e:
self.Debug('VIX Error: ' + str(e)) #change after last deployment
if (type(self.VIX) is Decimal) == False:
self.VIX = 0
LiveTradingRealTimeHandler.Run(): 在QuantConnect.Scheduling.ScheduledEvent中有一个错误,错误是UnicodeDecodeError: 'utf-8'编解码器无法解码字节0xa0,在位置57360:无效的起始字节。在main.py:line 417的OnData中的GetVix位置57405处,运行时错误:UnicodeDecodeError:'utf-8'编解码器无法解码字节0xa0,在位置57405处的无效起始字节,UnicodeDecodeError:'utf-8'编解码器无法解码字节0xa0,在位置57405处的无效起始字节Stack Trace:System.Exception:UnicodeDecodeError:'utf-8'编解码器无法解码字节0xa0,在位置57405处:无效的起始字节在main.py:line 417的OnData中的GetVix位置458---> Python.Runtime.PythonException:UnicodeDecodeError:'utf-8'编解码器无法解码字节0xa0,在位置57405处:无效的起始字节在Python.Runtime.PyObject.Invoke(Python.Runtime.PyTuple args,Python.Runtime.PyDict kw) [0x00033] in <7ada479175184ff388929ece541bbdb4>:0 at Python.Runtime.PyObject.InvokeMethod(System.String name,Python.Runtime.PyTuple args,Python.Runtime.PyDict kw) [0x00007] in <7ada479175184ff388929ece541bbdb4>:0 在<7ada479175184ff388929ece541bbdb4>:0的Python.Runtime.PyObject.TryInvokeMember(System.Dynamic.InvokeMemberBinder binder,System.Object[] args,System.Object& result) [0x0003e] at (wrapper dynamic-method) System.Object:CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,object,QuantConnect.Data.Slice) 在QuantConnect.AlgorithmFactory.Python.Wrappers.AlgorithmPythonWrapper.OnData(QuantConnect.Data.Slice slice) [0x00088] in <4d2bac9c1f474180a99afaeed3fe5b8a>:0 at QuantConnect.Lean.Engine.AlgorithmManager.Run(QuantConnect.Packets.AlgorithmNodePacket job,QuantConnect.Interfaces.IAlgorithm algorithm,QuantConnect.Lean.Engine.DataFeeds.IDataFeed feed,QuantConnect.Lean.Engine.TransactionHandlers.ITransactionHandler transactions,QuantConnect.Lean.Engine.Results.IResultHandler results,QuantConnect.Lean.Engine.RealTime.IRealTimeHandler realtime,QuantConnect.Lean.Engine.Server.ILeanManager leanManager,QuantConnect.Lean.Engine.Alpha.IAlphaHandler alphas,System.Threading.CancellationToken token) [0x013e5] in <7d8df6f15cb44723bde6b3f6cea9f521>:0---内部异常堆栈跟踪结束---
if (type(self.VIX) is Decimal) == False:
而不是if not isinstance(self.VIX, Decimal):
? - Edward Minnixif not isinstance(self.VIX, Decimal):
。我不是专业的编程人员,所以通常只是找到一个可行的方法。 - Pier-Olivier Marquis