0

So in the code, I am sharing multiple stocks data has been downloaded through a user-defined function and user defined function stores the data in CSV format then the code we have to calculate some stats like hit ratio, daily returns, the total number of trades and some other stuff that's not the problem the problem is in the code below instructor has used vars() function it seems each every stocks data frame is being stored in that vars() object and then calling some operation like calculating len of values in the each stock data frame can be done through by adding vars() and operation is applied on each data frame it would be great if someone answering the question could explain var() role in this particular code below in more detail and what is it

I am able to understand the code vars() is unclear

stocks = ['MSFT','IBM', 'GM', 'ACN', 'GOOG']
end=datetime.datetime.now().date() 
start=end-pd.Timedelta(days=365*5) # ONLY FIVE YEARS Of HISTORICAL DATA CAN BE DOWNLOADED FROM 'IEX'

def hist_data(stocks):
    stock_df=web.DataReader(stocks,'iex',start,end)
    stock_df['Name']=stocks
    fileName=stocks+'_data.csv'#this code is actually gaiving adding '_data.csv' to everay stock name in stocks varialble above.by this we will be able to write individual stock data downloaded to csv fromat 
    stock_df.to_csv(fileName) # writing individual stock data to csv format 

with futures.ThreadPoolExecutor(len(stocks)) as executor:
    result=executor.map(hist_data,stocks)
print('completed')

all_stats=[]

for stock in stocks:
    df = pd.read_csv(stock+'_data.csv',index_col=0)
    df.columns
    df['Daily returns'] = df['close'] /df['open'] -1
    vars()['df_'+stock] = df.copy()

    #Calculation of Loss and profit trades
    loss=np.where(vars()['df_'+stock]['Daily returns']<0)# so u see this code is first calling the data frame 'df_'+stock so a stock data is stored in it where we named each stock dataframe
    profit=np.where(vars()['df_'+stock]['Daily returns']>0)

    #Calculation of trade counts
    total_trades = len(vars()['df_' + stock]) # as we are taking a trade evry single day so we can count the lenght of dataframe 
    loss_trades = len(loss[0]) # [0] this argument is there because without this code would return zero 
    profit_trades = len(profit[0])

    #Calculation of hit ratios
    hit_ratio= profit_trades/(loss_trades + profit_trades)
    total_returns=np.cumsum(vars()['df_'+stock]['Daily returns'])    
    vars()['df_'+stock]['Cum Returns']=total_returns 

    stats=[stock,hit_ratio,total_returns[len(total_returns)-1]]
    all_stats.append(stats)
    headings=['Stock Name','Hit Ratio','Final Return']

    #Final Result of all the calculations
    final_result=pd.DataFrame(all_stats, columns=headings)

    plt.plot(vars()['df_'+stock].index.values,vars()['df_'+stock]['Cum Returns'],label=stock)
    plt.legend()

#results are as expected
Sahil Swaroop
  • 81
  • 1
  • 4

1 Answers1

0

Using vars()[variable] allows you to use a variable to name another variable.

Usually, a much superior method both in terms of readability and reliability is to use a dictionary.

tripleee
  • 175,061
  • 34
  • 275
  • 318