<?php
namespace Bundles\Instruments\Swap\Entity;
use Bundles\Instruments\Swap\Repository\SwapRepository;
use Doctrine\ORM\Mapping as ORM;
use App\Entity\Counterparty;
use App\Entity\CurrencyPair;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Bundles\Portfolios\Entity\Portfolio;
use Bundles\Instruments\Base\Entity\Forward;
/**
* @ORM\Entity(repositoryClass=SwapRepository::class)
*/
class Swap {
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Portfolio::class)
* @ORM\JoinColumn(nullable=false)
*/
private $Portfolio;
/**
* @ORM\Column(type="string")
*/
private $executionDate;
/**
* @ORM\Column(type="string", length=100, nullable=true)
*/
private $number;
/**
* @ORM\ManyToOne(targetEntity=Counterparty::class)
*/
private $Counterparty;
/**
* return CurrencyPair
* @ORM\ManyToOne(targetEntity=CurrencyPair::class)
*/
private $CurrencyPair;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $comment;
/**
* @ORM\Column(type="decimal", precision=20, scale=4, nullable=true)
*/
private $amountMajor;
/**
* @ORM\Column(type="decimal", precision=20, scale=4, nullable=true)
*/
private $amountMinor;
/**
* @ORM\Column(type="decimal", precision=10, scale=5)
*/
private $rate;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $action;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $currency;
/**
* @ORM\Column(type="string")
*/
private $maturityDate;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $status;
/**
* @ORM\Column(type="string", nullable=true)
*/
public $reference;
/**
* @ORM\ManyToOne(targetEntity=Forward::class)
*/
private $Forward;
private $roi = 0;
private $roiByActionCurrencySpot = 0;
private $roiPercents = 0;
private $coverage = 0;
private $actionAmount = 0;
private $opositeAmount = 0;
private $opositeCurrency = '';
public function getId(): ?int {
return $this->id;
}
public function setId($id): self {
$this->id = $id;
return $this;
}
public function getPortfolio(): ?Portfolio {
return $this->Portfolio;
}
public function setPortfolio(?Portfolio $Portfolio): self {
$this->Portfolio = $Portfolio;
return $this;
}
public function getExecutionDate(): ?string {
return $this->executionDate;
}
public function setExecutionDate($executionDate): self {
$this->executionDate = $executionDate;
return $this;
}
public function getNumber(): ?string {
return $this->number;
}
public function setNumber(?string $number): self {
$this->number = $number;
return $this;
}
public function getCounterparty(): ?Counterparty {
return $this->Counterparty;
}
public function setCounterparty(?Counterparty $Counterparty): self {
$this->Counterparty = $Counterparty;
return $this;
}
public function getComment(): ?string {
return $this->comment;
}
public function setComment(?string $comment): self {
$this->comment = $comment;
return $this;
}
/**
* @return CurrencyPair
*/
public function getCurrencyPair() {
return $this->CurrencyPair;
}
/**
* @param mixed $CurrencyPair
*/
public function setCurrencyPair($CurrencyPair): void {
$this->CurrencyPair = $CurrencyPair;
}
/**
* @return mixed
*/
public function getAmountMajor() {
return $this->amountMajor;
}
/**
* @param mixed $amountMajor
*/
public function setAmountMajor($amountMajor): void {
$this->amountMajor = $amountMajor;
}
/**
* @return mixed
*/
public function getAmountMinor() {
return $this->amountMinor;
}
/**
* @param mixed $amountMinor
*/
public function setAmountMinor($amountMinor): void {
$this->amountMinor = $amountMinor;
}
/**
* @return mixed
*/
public function getRate() {
return $this->rate;
}
/**
* @param mixed $rate
*/
public function setRate($rate): self {
$this->rate = $rate;
return $this;
}
/**
* @return mixed
*/
public function getAction() {
return $this->action;
}
/**
* @param mixed $action
*/
public function setAction($action): void {
$this->action = $action;
}
/**
* @return mixed
*/
public function getCurrency() {
return $this->currency;
}
/**
* @param mixed $currency
*/
public function setCurrency($currency): void {
$this->currency = $currency;
}
/**
* @return mixed
*/
public function getMaturityDate() {
return $this->maturityDate;
}
/**
* @param mixed $maturityDate
*/
public function setMaturityDate($maturityDate): void {
$this->maturityDate = $maturityDate;
}
/**
* @return mixed
*/
public function getStatus() {
return $this->status;
}
/**
* @param mixed $status
*/
public function setStatus($status): void {
$this->status = $status;
}
/**
* @return mixed
*/
public function getReference() {
return $this->reference;
}
/**
* @param mixed $reference
*/
public function setReference($reference): void {
$this->reference = $reference;
}
/**
* @return Forward
*/
public function getForward() {
return $this->Forward;
}
/**
* @param mixed $Forward
*/
public function setForward($Forward): void {
$this->Forward = $Forward;
}
public function calcs() {
$swapAmountMinor = $this->getAmountMinor();
$swapAmountMajor = $this->getAmountMajor();
$swapRate = $this->getRate();
$currMajor = $this->getCurrencyPair()->getCurrencyNameByNumber(1);
$currMinor = $this->getCurrencyPair()->getCurrencyNameByNumber(2);
$this->actionAmount = $swapAmountMinor ;//sell USD
$this->opositeAmount = $swapAmountMajor ;//buy USD
$this->opositeCurrency = $currMajor ; // USD
if ($this->getCurrency() == $currMajor) {//sell EUR
$this->actionAmount = $swapAmountMajor;
$this->opositeAmount = $swapAmountMinor ;//buy USD
$this->opositeCurrency = $currMinor; // EUR
}
if (empty($this->getForward())) {
return false;
}
$forwardAmountMajor = $this->getForward()->getAmount();
$forwardAmountMinor = $this->getForward()->getAmountMinor();
$forwardRate = $this->getForward()->getStrike();
if (!empty($forwardAmountMinor) && !empty($swapAmountMinor)) {
if ($this->getCurrency() == $currMajor) {//sell EUR
$tempAmount1 = $forwardAmountMajor * $swapRate;
$tempAmount2 = $forwardAmountMajor * $forwardRate;
$this->roi = $tempAmount1 - $tempAmount2;
$this->roiPercents = round($this->roi / $swapAmountMinor * 100, 2);
$this->coverage = $forwardAmountMajor/ $swapAmountMajor * 100;
$this->roiByActionCurrencySpot = $this->roi / $swapRate;
} else { // sell USD
$tempAmount1 = $forwardAmountMinor / $swapRate;
$tempAmount2 = $forwardAmountMinor / $forwardRate;
$this->roi = $tempAmount1 - $tempAmount2;
$this->roiPercents = round($this->roi / $swapAmountMajor * 100, 2);
$this->coverage = $forwardAmountMinor / $swapAmountMinor * 100;
$this->roiByActionCurrencySpot = $this->roi * $swapRate;
}
}
}
public function getRoi() {
if (empty($this->roi)) {
$this->calcs();
}
return $this->roi;
}
public function getRoiBySpot() {
if (empty($this->roiByActionCurrencySpot)) {
$this->calcs();
}
return $this->roiByActionCurrencySpot;
}
public function getRoiPercents() {
if (empty($this->roi)) {
$this->calcs();
}
return $this->roiPercents;
}
public function getCoverage() {
if (empty($this->coverage)) {
$this->calcs();
}
return round($this->coverage, 1);
}
public function getActionAmount() {
if (empty($this->actionAmount)) {
$this->calcs();
}
return round($this->actionAmount, 1);
}
public function getOpositeAmount() {
if (empty($this->opositeAmount)) {
$this->calcs();
}
return round($this->opositeAmount, 1);
}
public function getOpositeCurrency() {
if (empty($this->opositeCurrency)) {
$this->calcs();
}
return $this->opositeCurrency;
}
}