xlwings图表模拟器-无法使用模拟图表复制宏工作表 我从xlwings下载了带有模拟图表的工作表,以及excel调用运行模拟的.py Python脚本 复制Excel工作簿上的工作表 保存其他.py脚本并将所有引用重新链接到复制的工作表 使用新名称在Alt+F11上创建了新函数,以调用重复的.py脚本 检查是否存在引用错误,但由于.py脚本中的错误行,复制的excel选项卡无法对图表运行模拟,所有错误均保持不变:
sht.charts['Chart 5'].设置源数据(sht.range((1,15),(num\u timesteps+2,19)) 具体来说,它似乎突出了“图表5”中的问题。我不知道在不对代码进行重大修改的情况下有什么解决方法。完整错误描述如下:xlwings图表模拟器-无法使用模拟图表复制宏工作表 我从xlwings下载了带有模拟图表的工作表,以及excel调用运行模拟的.py Python脚本 复制Excel工作簿上的工作表 保存其他.py脚本并将所有引用重新链接到复制的工作表 使用新名称在Alt+F11上创建了新函数,以调用重复的.py脚本 检查是否存在引用错误,但由于.py脚本中的错误行,复制的excel选项卡无法对图表运行模拟,所有错误均保持不变:,python,excel,vba,charts,xlwings,Python,Excel,Vba,Charts,Xlwings,sht.charts['Chart 5'].设置源数据(sht.range((1,15),(num\u timesteps+2,19)) 具体来说,它似乎突出了“图表5”中的问题。我不知道在不对代码进行重大修改的情况下有什么解决方法。完整错误描述如下: 错误 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“GBM_2.py”,第26行,主 短程图表['Chart 5'].设置源数据(短程范围((1,15),(num\u timesteps+2,19))) 文件“C:\Users\D
错误 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 文件“GBM_2.py”,第26行,主 短程图表['Chart 5'].设置源数据(短程范围((1,15),(num\u timesteps+2,19))) 文件“C:\Users\Darius\Miniconda2\lib\site packages\xlwings\main.py”,第78行,位于getitem 返回自我(键) 文件“C:\Users\Darius\Miniconda2\lib\site packages\xlwings\main.py”,第49行,在调用中 返回self.\u wrap(impl=self.impl(名称或索引)) 文件“C:\Users\Darius\Miniconda2\lib\site packages\xlwings\u xlwindows.py”,第1122行,在调用中 升起钥匙错误(钥匙) 关键错误:“图表5” Actual.py脚本:
from __future__ import division
import sys
import numpy as np
import xlwings as xw
def main():
sht = xw.Book.caller().sheets('GBM2')
# User Inputs
num_simulations = sht.range('E3').options(numbers=int).value
time = sht.range('E4').value
num_timesteps = sht.range('E5').options(numbers=int).value
dt = time/num_timesteps # Length of time period
sig = sht.range('E7').value
mu = np.log(1 + sht.range('E6').value) # Drift
starting_price = sht.range('E8').value
perc_selection = [1, 50, 99] # percentiles (hardcoded for now)
# Animation
animate = sht.range('E9').value.lower() == 'yes'
# On Excel: clear output, write out initial values of percentiles/sample path and set chart source
# and x-axis values
sht.range('O2').expand().clear_contents()
sht.range('P2').value = [starting_price, starting_price, starting_price, starting_price]
sht.charts['Chart 5'].set_source_data(sht.range((1, 15), (num_timesteps + 2, 19)))
sht.range('O2').value = np.round(np.linspace(0, time, num_timesteps + 1).reshape(-1, 1), 2)
# Preallocation
price = np.zeros((num_timesteps + 1, num_simulations))
percentiles = np.zeros((num_timesteps + 1, 3))
# Set initial values
price[0,:] = starting_price
percentiles[0,:] = starting_price
# Simulation at each time step
for t in range(1, num_timesteps + 1):
rand_nums = np.random.randn(num_simulations)
price[t,:] = price[t-1,:] * np.exp((mu - 0.5 * sig**2) * dt + sig * rand_nums * np.sqrt(dt))
percentiles[t, :] = np.percentile(price[t, :], perc_selection)
if animate:
sht.range((t+2, 16)).value = percentiles[t, :]
sht.range((t+2, 19)).value = price[t, 0] # Sample path
if sys.platform.startswith('win'):
sht.book.app.screen_updating = True
if not animate:
sht.range('P2').value = percentiles
sht.range('S2').value = price[:, :1] # Sample path