需要帮助创建一个使用正确参数调用函数的循环

时间:2016-11-28 10:53:19

标签: python python-2.7

nsetools是用于获取股票报价的库

from nsetools import Nse
nse = Nse()

#Names of stocks
stocks = {
    "APLAPOLLO" : 878.2,
    "AVANTIFEED": 488.95,
    "BALAMINES": 308.95
}
#Quantities of stocks
qty = {
    "APLAPOLLO": 10,
    "AVANTIFEED": 10,
    "BALAMINES": 10
}

def get_closing(stock):
    """
    Function to obtain closePrice of stocks
    """
    return nse.get_quote(stock)['closePrice']

#The function I am having problem with
def expenses_calc(buy, sell, qty):
    stt = 0.10
    ttc = 0.00325
    service_tax = 15.0
    sebi_charges = 0.0002
    swacch_bharat_cess = 0.02
    stamp_duty = 0.01

    turnover = (buy * qty) + (sell * qty)
    stt_total = (stt / 100) * turnover
    total_tran_charge = (ttc / 100) * turnover
    service_tax = (service_tax / 100) * total_tran_charge
    sebi_charges = (sebi_charges / 100) * turnover
    stamp_duty = (stamp_duty / 100) * turnover
    total_tax_and_charges = stt_total + total_tran_charge + service_tax + sebi_charges + stamp_duty
    total_investment = buy * qty
    current_value = sell * qty
    net_profit = (sell * qty) - (buy * qty) - total_tax_and_charges

如何创建一个调用expense_calc function& amp;以买入价格替换参数。来自相应字典的数量,并用字典中每个股票的get_closing函数获得的收盘价替换卖价?

#expenses_calc(buy price from stocks dict, 
#sell price from get_closing func, qty from qty dict) 
#in a loop for each stock in the dictionary

1 个答案:

答案 0 :(得分:0)

我认为您股票的开盘价在股票字典中列出。在这种情况下,您希望迭代dict.keys()

for key in stocks.keys():
    buy_price = stocks[key]
    sell_price = get_closing(key)
    stock_qty = qty[key]

    expenses_calc(buy_price, sell_price, stock_qty)

我不确定你的expenses_calc函数应该做什么 - 返回一些东西,或者打印出来 - 但是在适当的时候添加一个变量赋值。

此代码使用stocks字典键从该字典,qty字典和get_closing()函数中提取数据,假设所有内容都键入相同的股票名称。< / p>