Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Properly Formatting Pandas Display
#1
Hello everyone!
I please need your help. I get:
Date 2014-01-02 2014-01-03 ... 2017-12-28 2017-12-29
GOOG 0.0 0.0 ... 0.000000 0.000000
positions 0.0 0.0 ... 0.000000 0.000000
cash NaN 10000.0 ... 10253.277405 10253.277405
total NaN 10000.0 ... 10253.277405 10253.277405
after running my code:
from pandas_datareader import data
import numpy as np
import pandas as pd
start_date = '2014-01-01'
end_date = '2018-01-01'
data_values = data.DataReader('GOOG', 'yahoo', start_date, end_date)

dict_result = pd.DataFrame({"Date": [],
                       "Port. Start Value": [],
                         "Entry & Price Paid": [], "Profit": []})
data_signal = pd.DataFrame(index=data_values.index)
data_signal['price'] = data_values['Adj Close']
data_signal['daily_difference'] = data_signal['price'].diff()
data_signal['signal'] = 0.0
data_signal['signal'][:] = np.where(data_signal['daily_difference'][:] > 0, 1.0, 0.0)

data_signal['positions'] = data_signal['signal'].diff()

# Set the initial capital
initial_capital= float(10000.0)

positions = pd.DataFrame(index=data_signal.index).fillna(0.0)
portfolio = pd.DataFrame(index=data_signal.index).fillna(0.0)

positions['GOOG'] = data_signal['signal']
portfolio['positions'] = (positions.multiply(data_signal['price'], axis=0))
portfolio['cash'] = initial_capital - (positions.diff().multiply(data_signal['price'], axis=0)).cumsum()
portfolio['total'] = portfolio['positions'] + portfolio['cash']

values = [positions['GOOG'], portfolio['positions'], portfolio['cash'], portfolio['total']]
backtest = pd.DataFrame(values)
                         
print(backtest)
But what i want to do is passing values in
dict_result = pd.DataFrame({"Date": [],
                       "Port. Start Value": [],
                         "Entry & Price Paid": [], "Profit": []})
of my code. Thank you for your help.
These are the values I'm trying to display in 'dict_result'(individual print out of
positions['GOOG']
,
portfolio['positions']
,
portfolio['cash']
and
portfolio['total']
shown below):
2014-01-02 0.0
2014-01-03 0.0
2014-01-06 1.0
2014-01-07 1.0
2014-01-08 1.0
...
2017-12-22 0.0
2017-12-26 0.0
2017-12-27 0.0
2017-12-28 0.0
2017-12-29 0.0
Name: GOOG, Length: 1007, dtype: float64, Date
2014-01-02 0.000000
2014-01-03 0.000000
2014-01-06 556.573853
2014-01-07 567.303589
2014-01-08 568.484192
...
2017-12-22 0.000000
2017-12-26 0.000000
2017-12-27 0.000000
2017-12-28 0.000000
2017-12-29 0.000000
Name: positions, Length: 1007, dtype: float64, Date
2014-01-02 NaN
2014-01-03 10000.000000
2014-01-06 9443.426147
2014-01-07 9443.426147
2014-01-08 9443.426147
...
2017-12-22 10253.277405
2017-12-26 10253.277405
2017-12-27 10253.277405
2017-12-28 10253.277405
2017-12-29 10253.277405
Name: cash, Length: 1007, dtype: float64, Date
2014-01-02 NaN
2014-01-03 10000.000000
2014-01-06 10000.000000
2014-01-07 10010.729736
2014-01-08 10011.910339
...
2017-12-22 10253.277405
2017-12-26 10253.277405
2017-12-27 10253.277405
2017-12-28 10253.277405
2017-12-29 10253.277405
Name: total, Length: 1007, dtype: float64]
Quote
#2
Hi All! I got the solution to my problem, it amounted to directly passing values obtained in positions['GOOG'], portfolio['positions'], portfolio['cash'] and portfolio['total']directly to the data frame as follows (with a renaming of the columns):
backtest_result = pd.DataFrame({"Signal": positions['GOOG'],
                       "Price Paid": portfolio['positions'],
                         "Position Value": portfolio['cash'], "PnL": portfolio['total']})
Reworked -and working- version of code:
from pandas_datareader import data
import numpy as np
import pandas as pd
start_date = '2014-01-01'
end_date = '2018-01-01'
data_values = data.DataReader('GOOG', 'yahoo', start_date, end_date)

data_signal = pd.DataFrame(index=data_values.index)
data_signal['price'] = data_values['Adj Close']
data_signal['daily_difference'] = data_signal['price'].diff()
data_signal['signal'] = 0.0
data_signal['signal'][:] = np.where(data_signal['daily_difference'][:] > 0, 1.0, 0.0)

data_signal['positions'] = data_signal['signal'].diff()

# Set the initial capital
initial_capital= float(10000.0)


positions = pd.DataFrame(index=data_signal.index).fillna(0.0)
portfolio = pd.DataFrame(index=data_signal.index).fillna(0.0)


positions['GOOG'] = data_signal['signal']
portfolio['positions'] = (positions.multiply(data_signal['price'], axis=0))
portfolio['cash'] = initial_capital - (positions.diff().multiply(data_signal['price'], axis=0)).cumsum()
portfolio['total'] = portfolio['positions'] + portfolio['cash']

pd.set_option("display.max_rows", None, "display.max_columns", None)
backtest_result = pd.DataFrame({"Signal": positions['GOOG'],
                       "Price Paid": portfolio['positions'],
                         "Position Value": portfolio['cash'], "PnL": portfolio['total']})

print(backtest_result)

Quote
#3
Glad you figured it out, and thank you for coming back to post the solution.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  python pandas to html with formatting slangswinger 0 118 Mar-24-2020, 05:38 PM
Last Post: slangswinger
  Is there any way to properly load fixed width file into a dataframe using Pandas? vicky53 1 820 Mar-29-2019, 06:04 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)