first init
This commit is contained in:
Коммит
d02f01ae6a
Двоичный файл не отображается.
|
@ -0,0 +1,23 @@
|
|||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio 15
|
||||
VisualStudioVersion = 15.0.27130.2003
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "portfolioPerf", "portfolioPerf\portfolioPerf.pyproj", "{A9A2E519-01E9-4960-A2D4-6B01ED7DE4E1}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A9A2E519-01E9-4960-A2D4-6B01ED7DE4E1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A9A2E519-01E9-4960-A2D4-6B01ED7DE4E1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {DFA9180E-8E80-42C4-8516-5EF179715B1C}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
Двоичный файл не отображается.
Двоичный файл не отображается.
|
@ -0,0 +1,6 @@
|
|||
asset,currency
|
||||
AT0000A18XM4 SW,CHF
|
||||
BE0974268972 BB,EUR
|
||||
US0527691069 US,USD
|
||||
DE0007164600 GR,EUR
|
||||
US6092071058 US,USD
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,115 @@
|
|||
import numpy as np
|
||||
import pandas as pd
|
||||
import datetime
|
||||
|
||||
|
||||
weights = pd.read_csv('weights.csv',sep=',', encoding='utf8', parse_dates=['date'], dayfirst=False, index_col='date')
|
||||
prices = pd.read_csv('prices.csv',sep=',', encoding='utf8', parse_dates=['date'], dayfirst=False, index_col='date')
|
||||
currencies = pd.read_csv('currencies.csv',sep=',', encoding='utf8', dayfirst=False, index_col='asset')
|
||||
exchanges = pd.read_csv('exchanges.csv',sep=',', encoding='utf8', parse_dates=['date'], dayfirst=False, index_col='date')
|
||||
|
||||
|
||||
class PortfolioPerformance:
|
||||
def calculate_asset_performance(start_date, end_date):
|
||||
start_date = pd.Timestamp(start_date)
|
||||
end_date = pd.Timestamp(end_date)
|
||||
next_start_date = start_date + datetime.timedelta(days=1)
|
||||
|
||||
price = prices[start_date : end_date]
|
||||
weight = weights[next_start_date : end_date]
|
||||
|
||||
price = price.fillna(method='pad')
|
||||
price = price.fillna(method='bfill')
|
||||
weight = weight.fillna(method='pad')
|
||||
weight = weight.fillna(method='bfill')
|
||||
|
||||
Ri = pd.DataFrame(columns = price.columns)
|
||||
P = pd.Series(1., index = pd.date_range(start_date, end_date))
|
||||
|
||||
for i in range(len(price) - 1):
|
||||
Ri.loc[i] = price.apply(lambda row: (row.iloc[i+1] - row.iloc[i]) / row.iloc[i])
|
||||
Ri.index = pd.date_range(next_start_date, end_date)
|
||||
|
||||
Ri = Ri.mul(weight)
|
||||
R = Ri.sum(axis=1)
|
||||
|
||||
for i in range(len(P) - 1):
|
||||
P.iloc[i+1] = P.iloc[i]*(1+R.iloc[i])
|
||||
|
||||
print("Asset Performance:"+ '\n',P)
|
||||
return (P)
|
||||
|
||||
|
||||
def calculate_currency_performance(start_date, end_date):
|
||||
start_date = pd.Timestamp(start_date)
|
||||
end_date = pd.Timestamp(end_date)
|
||||
next_start_date = start_date + datetime.timedelta(days=1)
|
||||
|
||||
exchange = exchanges[start_date : end_date]
|
||||
weight = weights[next_start_date : end_date]
|
||||
|
||||
exchange = exchange.fillna(method='pad')
|
||||
exchange = exchange.fillna(method='bfill')
|
||||
weight = weight.fillna(method='pad')
|
||||
weight = weight.fillna(method='bfill')
|
||||
|
||||
ex = pd.DataFrame(columns = currencies.index)
|
||||
CRi = pd.DataFrame(columns = currencies.index)
|
||||
CP = pd.Series(1., index = pd.date_range(start_date, end_date))
|
||||
|
||||
for i in range(len(exchange.columns)):
|
||||
for j in range(len(currencies)):
|
||||
if ((exchange.columns[i] == currencies.iloc[j]).bool()):
|
||||
ex.iloc[ :, j] = exchange.iloc[ :, i]
|
||||
|
||||
for i in range(len(exchange) - 1):
|
||||
CRi.loc[i] = ex.apply(lambda row: (row.iloc[i+1] - row.iloc[i]) / row.iloc[i])
|
||||
CRi.index = pd.date_range(next_start_date, end_date)
|
||||
|
||||
CRi = CRi.mul(weight)
|
||||
CR = CRi.sum(axis = 1)
|
||||
|
||||
for i in range(len(CP) - 1):
|
||||
CP.iloc[i+1] = CP.iloc[i] * (1 + CR.iloc[i])
|
||||
|
||||
print("Currency Performance:"+ '\n', CP)
|
||||
return (CP)
|
||||
|
||||
def calculate_total_performance(start_date, end_date):
|
||||
start_date = pd.Timestamp(start_date)
|
||||
end_date = pd.Timestamp(end_date)
|
||||
next_start_date = start_date + datetime.timedelta(days=1)
|
||||
|
||||
price = prices[start_date : end_date]
|
||||
exchange = exchanges[start_date : end_date]
|
||||
weight = weights[next_start_date : end_date]
|
||||
|
||||
price = price.fillna(method='pad')
|
||||
price = price.fillna(method='bfill')
|
||||
exchange = exchange.fillna(method='pad')
|
||||
exchange = exchange.fillna(method='bfill')
|
||||
weight = weight.fillna(method='pad')
|
||||
weight = weight.fillna(method='bfill')
|
||||
|
||||
ex = pd.DataFrame(columns = currencies.index)
|
||||
TRi = pd.DataFrame(columns = currencies.index)
|
||||
TP = pd.Series(1., index = pd.date_range(start_date, end_date))
|
||||
|
||||
for i in range(len(exchange.columns)):
|
||||
for j in range(len(currencies)):
|
||||
if ((exchange.columns[i] == currencies.iloc[j]).bool()):
|
||||
ex.iloc[ :, j] = exchange.iloc[ :, i]
|
||||
ex = ex.mul(price)
|
||||
|
||||
for i in range(len(exchange) - 1):
|
||||
TRi.loc[i] = ex.apply(lambda row: (row.iloc[i+1] - row.iloc[i]) / row.iloc[i])
|
||||
TRi.index = pd.date_range(next_start_date, end_date)
|
||||
|
||||
TRi = TRi.mul(weight)
|
||||
TR = TRi.sum(axis = 1)
|
||||
|
||||
for i in range(len(TP) - 1):
|
||||
TP.iloc[i+1] = TP.iloc[i] * (1 + TR.iloc[i])
|
||||
|
||||
print("Total Performance:"+ '\n', TP)
|
||||
return (TP)
|
|
@ -0,0 +1,38 @@
|
|||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
<ProjectGuid>a9a2e519-01e9-4960-a2d4-6b01ed7de4e1</ProjectGuid>
|
||||
<ProjectHome>.</ProjectHome>
|
||||
<StartupFile>portfolioPerf.py</StartupFile>
|
||||
<SearchPath>
|
||||
</SearchPath>
|
||||
<WorkingDirectory>.</WorkingDirectory>
|
||||
<OutputPath>.</OutputPath>
|
||||
<Name>portfolioPerf</Name>
|
||||
<RootNamespace>portfolioPerf</RootNamespace>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<EnableUnmanagedDebugging>false</EnableUnmanagedDebugging>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="portfolioPerf.py" />
|
||||
<Compile Include="test_portfolioPerf.py">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
|
||||
<!-- Uncomment the CoreCompile target to enable the Build command in
|
||||
Visual Studio and specify your pre- and post-build commands in
|
||||
the BeforeBuild and AfterBuild targets below. -->
|
||||
<!--<Target Name="CoreCompile" />-->
|
||||
<Target Name="BeforeBuild">
|
||||
</Target>
|
||||
<Target Name="AfterBuild">
|
||||
</Target>
|
||||
</Project>
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -0,0 +1,19 @@
|
|||
import unittest
|
||||
import pandas as pd
|
||||
from portfolioPerf import PortfolioPerformance
|
||||
|
||||
class Test_test_portfolioPerf(unittest.TestCase):
|
||||
def test_asset_performance(self):
|
||||
P = pd.Series([1.000000, 1.007755, 1.015940, 1.019302], index = pd.date_range('20140113', '20140116'))
|
||||
self.assertTrue((PortfolioPerformance.calculate_asset_performance('20140113', '20140116') == P).bool)
|
||||
|
||||
def test_currency_performance(self):
|
||||
CP = pd.Series([1.000000, 0.999568, 0.997234, 0.997369], index = pd.date_range('20140113', '20140116'))
|
||||
self.assertTrue((PortfolioPerformance.calculate_currency_performance('20140113', '20140116') == CP).bool)
|
||||
|
||||
def test_total_performance(self):
|
||||
TP = pd.Series([1.000000, 0.999568, 0.997234, 0.997369], index = pd.date_range('20140113', '20140116'))
|
||||
self.assertTrue((PortfolioPerformance.calculate_total_performance('20140113', '20140116') == TP).bool)
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
Загрузка…
Ссылка в новой задаче