-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New actual portfolio CSV + New tickers
- Loading branch information
1 parent
a46da02
commit 9a6a1b2
Showing
11 changed files
with
175 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/main/java/com/quant/backtest/multi/strategy/enums/ActualPortfolioHeader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.quant.backtest.multi.strategy.enums; | ||
|
||
public enum ActualPortfolioHeader { | ||
|
||
Company("Company"), | ||
Ticker("Ticker"), | ||
MarketValue("Market Value"); | ||
|
||
private String value; | ||
|
||
private ActualPortfolioHeader(String value) { | ||
this.value = value; | ||
} | ||
|
||
public String getValue() { | ||
return this.value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
public class Defaults { | ||
|
||
public static final int SCALE = 1; | ||
public static final int SCALE = 2; | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/quant/backtest/multi/strategy/utils/PortfolioUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.quant.backtest.multi.strategy.utils; | ||
|
||
import java.math.BigDecimal; | ||
import java.math.RoundingMode; | ||
import java.text.NumberFormat; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import org.apache.commons.lang.StringUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.quant.backtest.multi.strategy.enums.ActualPortfolioHeader; | ||
|
||
@Component | ||
public class PortfolioUtils { | ||
|
||
@Autowired | ||
private CsvUtils csvUtils; | ||
|
||
private static final String SEPARATOR = "."; | ||
private final NumberFormat numberFormat = NumberFormat.getInstance(); | ||
private final String BBG_TICKER_APPENDER = " CN EQUITY"; | ||
|
||
private Function<String, String> bbgTickerFinderFunction = a -> StringUtils.substringAfter(a, SEPARATOR).length() == 0 ? new StringBuilder(a).append(BBG_TICKER_APPENDER).toString() | ||
: StringUtils.substringAfter(a, SEPARATOR).length() == 1 ? | ||
new StringBuilder(StringUtils.substringBefore(a, SEPARATOR)).append("/").append(StringUtils.substringAfter(a, SEPARATOR)).append(BBG_TICKER_APPENDER).toString() : | ||
new StringBuilder(StringUtils.substringBefore(a, SEPARATOR)).append("-").append(StringUtils.substringAfter(a, SEPARATOR).charAt(0)).append(BBG_TICKER_APPENDER).toString(); | ||
|
||
|
||
public Map<String, BigDecimal> createActualPortfolio(String filePath) throws Exception { | ||
Set<Map<String, Object>> actualPortfolioTickers = csvUtils.fetchActualPortfolioFromCsv(filePath); | ||
Double totalMarketValue = csvUtils.getTotalMarketValue(); | ||
Map<String, BigDecimal> actualPortfolio = new HashMap<>(); | ||
for (Map<String, Object> actualPortfolioTicker : actualPortfolioTickers) { | ||
String portfolioMarketValue = (String) actualPortfolioTicker.get(ActualPortfolioHeader.MarketValue.getValue()); | ||
BigDecimal value = null; | ||
if (StringUtils.isNotBlank(portfolioMarketValue)) { | ||
Double portfolioTicker = (numberFormat.parse(portfolioMarketValue).doubleValue()/totalMarketValue)*100; | ||
value = new BigDecimal(portfolioTicker).setScale(Defaults.SCALE, RoundingMode.HALF_EVEN); | ||
|
||
} else | ||
value = new BigDecimal(0d).setScale(Defaults.SCALE, RoundingMode.HALF_EVEN); | ||
actualPortfolio.put((String) actualPortfolioTicker.get(ActualPortfolioHeader.Ticker.getValue()), value); | ||
|
||
} | ||
return actualPortfolio; | ||
} | ||
|
||
public String findBloombergTicker(String optimalPortfolioTicker) { | ||
return bbgTickerFinderFunction.apply(optimalPortfolioTicker); | ||
} | ||
|
||
public Double getTotalMarketValueOfPortfolio() { | ||
return csvUtils.getTotalMarketValue(); | ||
} | ||
} |
Oops, something went wrong.