-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from vananiev/feature-derivatives-market
Срочный рынок Уралсиб
- Loading branch information
Showing
20 changed files
with
352 additions
and
35 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
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
36 changes: 36 additions & 0 deletions
36
src/main/java/ru/portfolio/portfolio/parser/OptionalTableColumn.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,36 @@ | ||
/* | ||
* Portfolio | ||
* Copyright (C) 2020 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ru.portfolio.portfolio.parser; | ||
|
||
import org.apache.poi.ss.usermodel.Row; | ||
|
||
public class OptionalTableColumn implements TableColumn { | ||
|
||
public static TableColumn of(TableColumn column) { | ||
return AnyOfTableColumn.of(column, TableColumn.NOCOLUMN); | ||
} | ||
|
||
private OptionalTableColumn() { | ||
} | ||
|
||
@Override | ||
public int getColumnIndex(int firstColumnForSearch, Row... headerRows) { | ||
return -1; | ||
} | ||
} |
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
79 changes: 79 additions & 0 deletions
79
src/main/java/ru/portfolio/portfolio/parser/uralsib/DerivativeCashFlowTable.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,79 @@ | ||
/* | ||
* Portfolio | ||
* Copyright (C) 2020 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ru.portfolio.portfolio.parser.uralsib; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import ru.portfolio.portfolio.parser.AbstractReportTable; | ||
import ru.portfolio.portfolio.parser.ExcelTable; | ||
import ru.portfolio.portfolio.pojo.CashFlowType; | ||
import ru.portfolio.portfolio.pojo.SecurityEventCashFlow; | ||
|
||
import java.util.Collection; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.singletonList; | ||
import static ru.portfolio.portfolio.parser.uralsib.PaymentsTable.PaymentsTableHeader.*; | ||
|
||
@Slf4j | ||
public class DerivativeCashFlowTable extends AbstractReportTable<SecurityEventCashFlow> { | ||
|
||
private final Pattern contractPattern = Pattern.compile(".*\\sвариационной маржи по\\s(.+)$"); | ||
|
||
public DerivativeCashFlowTable(UralsibBrokerReport report) { | ||
super(report, PaymentsTable.TABLE_NAME, "", PaymentsTable.PaymentsTableHeader.class); | ||
} | ||
|
||
protected Collection<SecurityEventCashFlow> getRow(ExcelTable table, Row row) { | ||
String action = table.getStringCellValue(row, OPERATION); | ||
action = String.valueOf(action).toLowerCase().trim(); | ||
if (!action.equalsIgnoreCase("вариационная маржа")) { | ||
return emptyList(); | ||
} | ||
return singletonList(SecurityEventCashFlow.builder() | ||
.timestamp(convertToInstant(table.getStringCellValue(row, DATE))) | ||
.portfolio(getReport().getPortfolio()) | ||
.value(table.getCurrencyCellValue(row, VALUE)) | ||
.currency(UralsibBrokerReport.convertToCurrency(table.getStringCellValue(row, CURRENCY))) | ||
.eventType(CashFlowType.DERIVATIVE_PROFIT) | ||
.isin(getContract(table, row)) | ||
.build()); | ||
} | ||
|
||
private String getContract(ExcelTable table, Row row) { | ||
String description = table.getStringCellValue(row, DESCRIPTION); | ||
Matcher matcher = contractPattern.matcher(description); | ||
if (matcher.find()) { | ||
return matcher.group(1); | ||
} | ||
throw new RuntimeException("Не могу найти наименование контракта в отчете брокера по событию:" + description); | ||
} | ||
|
||
@Override | ||
protected boolean checkEquality(SecurityEventCashFlow cash1, SecurityEventCashFlow cash2) { | ||
return SecurityEventCashFlow.checkEquality(cash1, cash2); | ||
} | ||
|
||
@Override | ||
protected Collection<SecurityEventCashFlow> mergeDuplicates(SecurityEventCashFlow oldObject, SecurityEventCashFlow newObject) { | ||
return SecurityEventCashFlow.mergeDuplicates(oldObject, newObject); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/ru/portfolio/portfolio/parser/uralsib/DerivativeTransactionTable.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,107 @@ | ||
/* | ||
* Portfolio | ||
* Copyright (C) 2020 Vitalii Ananev <[email protected]> | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package ru.portfolio.portfolio.parser.uralsib; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import ru.portfolio.portfolio.parser.*; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static ru.portfolio.portfolio.parser.uralsib.DerivativeTransactionTable.FortsTableHeader.*; | ||
|
||
@Slf4j | ||
public class DerivativeTransactionTable extends AbstractReportTable<DerivativeTransaction> { | ||
private static final String TABLE_NAME = "СДЕЛКИ С ФЬЮЧЕРСАМИ И ОПЦИОНАМИ"; | ||
private static final String TABLE_END_TEXT = "ДВИЖЕНИЕ ДЕНЕЖНЫХ СРЕДСТВ ЗА ОТЧЕТНЫЙ ПЕРИОД"; | ||
|
||
public DerivativeTransactionTable(UralsibBrokerReport report) { | ||
super(report, TABLE_NAME, TABLE_END_TEXT, FortsTableHeader.class, 2); | ||
} | ||
|
||
@Override | ||
protected Collection<DerivativeTransaction> getRow(ExcelTable table, Row row) { | ||
Long transactionId = SecurityTransactionTable.getTransactionId(table, row, TRANSACTION); | ||
if (transactionId == null) return emptyList(); | ||
|
||
boolean isBuy = table.getStringCellValue(row, DIRECTION).equalsIgnoreCase("покупка"); | ||
int count = table.getIntCellValue(row, COUNT); | ||
BigDecimal valueInPoints = table.getCurrencyCellValue(row, QUOTE).multiply(BigDecimal.valueOf(count)); | ||
BigDecimal value = table.getCurrencyCellValue(row, VALUE); | ||
String valueCurrency = UralsibBrokerReport.convertToCurrency( | ||
table.getStringCellValueOrDefault(row, VALUE_CURRENCY, "RUB")); | ||
if (isBuy) { | ||
value = value.negate(); | ||
valueInPoints = valueInPoints.negate(); | ||
} | ||
BigDecimal commission = table.getCurrencyCellValue(row, MARKET_COMMISSION) | ||
.add(table.getCurrencyCellValue(row, BROKER_COMMISSION)) | ||
.negate(); | ||
List<DerivativeTransaction> transactionInfo = new ArrayList<>(2); | ||
DerivativeTransaction.DerivativeTransactionBuilder builder = DerivativeTransaction.builder() | ||
.timestamp(convertToInstant(table.getStringCellValue(row, DATE_TIME))) | ||
.transactionId(transactionId) | ||
.portfolio(getReport().getPortfolio()) | ||
.contract(table.getStringCellValue(row, CONTRACT)) | ||
.count((isBuy ? 1 : -1) * count); | ||
transactionInfo.add(builder | ||
.value(value) | ||
.commission(commission) | ||
.valueCurrency(valueCurrency) | ||
.commissionCurrency("RUB") // FORTS, only RUB | ||
.build()); | ||
transactionInfo.add(builder | ||
.value(valueInPoints) | ||
.commission(BigDecimal.ZERO) | ||
.valueCurrency(DerivativeTransaction.QUOTE_CURRENCY) | ||
.commissionCurrency("RUB") // FORTS, only RUB | ||
.build()); | ||
return transactionInfo; | ||
} | ||
|
||
@RequiredArgsConstructor | ||
enum FortsTableHeader implements TableColumnDescription { | ||
DATE_TIME("дата расчетов"), | ||
TRANSACTION("номер сделки"), | ||
TYPE("вид контракта"), | ||
CONTRACT("наименование контракта"), | ||
DIRECTION("вид сделки"), | ||
COUNT("количество контрактов"), | ||
QUOTE(AnyOfTableColumn.of( | ||
TableColumnImpl.of("цена фьючерса"), | ||
TableColumnImpl.of("премия по опциону"))), | ||
VALUE("^сумма$"), | ||
VALUE_CURRENCY(OptionalTableColumn.of( | ||
TableColumnImpl.of("валюта суммы"))), // sometime does not exists | ||
MARKET_COMMISSION("комиссия тс"), | ||
BROKER_COMMISSION("комиссия брокера"); | ||
|
||
@Getter | ||
private final TableColumn column; | ||
FortsTableHeader(String ... words) { | ||
this.column = TableColumnImpl.of(words); | ||
} | ||
} | ||
} |
Oops, something went wrong.