src/Bundles/Instruments/Swap/Entity/Swap.php line 17

Open in your IDE?
  1. <?php
  2. namespace Bundles\Instruments\Swap\Entity;
  3. use Bundles\Instruments\Swap\Repository\SwapRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use App\Entity\Counterparty;
  6. use App\Entity\CurrencyPair;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Bundles\Portfolios\Entity\Portfolio;
  10. use Bundles\Instruments\Base\Entity\Forward;
  11. /**
  12. * @ORM\Entity(repositoryClass=SwapRepository::class)
  13. */
  14. class Swap {
  15. /**
  16. * @ORM\Id
  17. * @ORM\GeneratedValue
  18. * @ORM\Column(type="integer")
  19. */
  20. private $id;
  21. /**
  22. * @ORM\ManyToOne(targetEntity=Portfolio::class)
  23. * @ORM\JoinColumn(nullable=false)
  24. */
  25. private $Portfolio;
  26. /**
  27. * @ORM\Column(type="string")
  28. */
  29. private $executionDate;
  30. /**
  31. * @ORM\Column(type="string", length=100, nullable=true)
  32. */
  33. private $number;
  34. /**
  35. * @ORM\ManyToOne(targetEntity=Counterparty::class)
  36. */
  37. private $Counterparty;
  38. /**
  39. * return CurrencyPair
  40. * @ORM\ManyToOne(targetEntity=CurrencyPair::class)
  41. */
  42. private $CurrencyPair;
  43. /**
  44. * @ORM\Column(type="string", length=255, nullable=true)
  45. */
  46. private $comment;
  47. /**
  48. * @ORM\Column(type="decimal", precision=20, scale=4, nullable=true)
  49. */
  50. private $amountMajor;
  51. /**
  52. * @ORM\Column(type="decimal", precision=20, scale=4, nullable=true)
  53. */
  54. private $amountMinor;
  55. /**
  56. * @ORM\Column(type="decimal", precision=10, scale=5)
  57. */
  58. private $rate;
  59. /**
  60. * @ORM\Column(type="string", nullable=true)
  61. */
  62. private $action;
  63. /**
  64. * @ORM\Column(type="string", nullable=true)
  65. */
  66. private $currency;
  67. /**
  68. * @ORM\Column(type="string")
  69. */
  70. private $maturityDate;
  71. /**
  72. * @ORM\Column(type="boolean", nullable=true)
  73. */
  74. private $status;
  75. /**
  76. * @ORM\Column(type="string", nullable=true)
  77. */
  78. public $reference;
  79. /**
  80. * @ORM\ManyToOne(targetEntity=Forward::class)
  81. */
  82. private $Forward;
  83. private $roi = 0;
  84. private $roiByActionCurrencySpot = 0;
  85. private $roiPercents = 0;
  86. private $coverage = 0;
  87. private $actionAmount = 0;
  88. private $opositeAmount = 0;
  89. private $opositeCurrency = '';
  90. public function getId(): ?int {
  91. return $this->id;
  92. }
  93. public function setId($id): self {
  94. $this->id = $id;
  95. return $this;
  96. }
  97. public function getPortfolio(): ?Portfolio {
  98. return $this->Portfolio;
  99. }
  100. public function setPortfolio(?Portfolio $Portfolio): self {
  101. $this->Portfolio = $Portfolio;
  102. return $this;
  103. }
  104. public function getExecutionDate(): ?string {
  105. return $this->executionDate;
  106. }
  107. public function setExecutionDate($executionDate): self {
  108. $this->executionDate = $executionDate;
  109. return $this;
  110. }
  111. public function getNumber(): ?string {
  112. return $this->number;
  113. }
  114. public function setNumber(?string $number): self {
  115. $this->number = $number;
  116. return $this;
  117. }
  118. public function getCounterparty(): ?Counterparty {
  119. return $this->Counterparty;
  120. }
  121. public function setCounterparty(?Counterparty $Counterparty): self {
  122. $this->Counterparty = $Counterparty;
  123. return $this;
  124. }
  125. public function getComment(): ?string {
  126. return $this->comment;
  127. }
  128. public function setComment(?string $comment): self {
  129. $this->comment = $comment;
  130. return $this;
  131. }
  132. /**
  133. * @return CurrencyPair
  134. */
  135. public function getCurrencyPair() {
  136. return $this->CurrencyPair;
  137. }
  138. /**
  139. * @param mixed $CurrencyPair
  140. */
  141. public function setCurrencyPair($CurrencyPair): void {
  142. $this->CurrencyPair = $CurrencyPair;
  143. }
  144. /**
  145. * @return mixed
  146. */
  147. public function getAmountMajor() {
  148. return $this->amountMajor;
  149. }
  150. /**
  151. * @param mixed $amountMajor
  152. */
  153. public function setAmountMajor($amountMajor): void {
  154. $this->amountMajor = $amountMajor;
  155. }
  156. /**
  157. * @return mixed
  158. */
  159. public function getAmountMinor() {
  160. return $this->amountMinor;
  161. }
  162. /**
  163. * @param mixed $amountMinor
  164. */
  165. public function setAmountMinor($amountMinor): void {
  166. $this->amountMinor = $amountMinor;
  167. }
  168. /**
  169. * @return mixed
  170. */
  171. public function getRate() {
  172. return $this->rate;
  173. }
  174. /**
  175. * @param mixed $rate
  176. */
  177. public function setRate($rate): self {
  178. $this->rate = $rate;
  179. return $this;
  180. }
  181. /**
  182. * @return mixed
  183. */
  184. public function getAction() {
  185. return $this->action;
  186. }
  187. /**
  188. * @param mixed $action
  189. */
  190. public function setAction($action): void {
  191. $this->action = $action;
  192. }
  193. /**
  194. * @return mixed
  195. */
  196. public function getCurrency() {
  197. return $this->currency;
  198. }
  199. /**
  200. * @param mixed $currency
  201. */
  202. public function setCurrency($currency): void {
  203. $this->currency = $currency;
  204. }
  205. /**
  206. * @return mixed
  207. */
  208. public function getMaturityDate() {
  209. return $this->maturityDate;
  210. }
  211. /**
  212. * @param mixed $maturityDate
  213. */
  214. public function setMaturityDate($maturityDate): void {
  215. $this->maturityDate = $maturityDate;
  216. }
  217. /**
  218. * @return mixed
  219. */
  220. public function getStatus() {
  221. return $this->status;
  222. }
  223. /**
  224. * @param mixed $status
  225. */
  226. public function setStatus($status): void {
  227. $this->status = $status;
  228. }
  229. /**
  230. * @return mixed
  231. */
  232. public function getReference() {
  233. return $this->reference;
  234. }
  235. /**
  236. * @param mixed $reference
  237. */
  238. public function setReference($reference): void {
  239. $this->reference = $reference;
  240. }
  241. /**
  242. * @return Forward
  243. */
  244. public function getForward() {
  245. return $this->Forward;
  246. }
  247. /**
  248. * @param mixed $Forward
  249. */
  250. public function setForward($Forward): void {
  251. $this->Forward = $Forward;
  252. }
  253. public function calcs() {
  254. $swapAmountMinor = $this->getAmountMinor();
  255. $swapAmountMajor = $this->getAmountMajor();
  256. $swapRate = $this->getRate();
  257. $currMajor = $this->getCurrencyPair()->getCurrencyNameByNumber(1);
  258. $currMinor = $this->getCurrencyPair()->getCurrencyNameByNumber(2);
  259. $this->actionAmount = $swapAmountMinor ;//sell USD
  260. $this->opositeAmount = $swapAmountMajor ;//buy USD
  261. $this->opositeCurrency = $currMajor ; // USD
  262. if ($this->getCurrency() == $currMajor) {//sell EUR
  263. $this->actionAmount = $swapAmountMajor;
  264. $this->opositeAmount = $swapAmountMinor ;//buy USD
  265. $this->opositeCurrency = $currMinor; // EUR
  266. }
  267. if (empty($this->getForward())) {
  268. return false;
  269. }
  270. $forwardAmountMajor = $this->getForward()->getAmount();
  271. $forwardAmountMinor = $this->getForward()->getAmountMinor();
  272. $forwardRate = $this->getForward()->getStrike();
  273. if (!empty($forwardAmountMinor) && !empty($swapAmountMinor)) {
  274. if ($this->getCurrency() == $currMajor) {//sell EUR
  275. $tempAmount1 = $forwardAmountMajor * $swapRate;
  276. $tempAmount2 = $forwardAmountMajor * $forwardRate;
  277. $this->roi = $tempAmount1 - $tempAmount2;
  278. $this->roiPercents = round($this->roi / $swapAmountMinor * 100, 2);
  279. $this->coverage = $forwardAmountMajor/ $swapAmountMajor * 100;
  280. $this->roiByActionCurrencySpot = $this->roi / $swapRate;
  281. } else { // sell USD
  282. $tempAmount1 = $forwardAmountMinor / $swapRate;
  283. $tempAmount2 = $forwardAmountMinor / $forwardRate;
  284. $this->roi = $tempAmount1 - $tempAmount2;
  285. $this->roiPercents = round($this->roi / $swapAmountMajor * 100, 2);
  286. $this->coverage = $forwardAmountMinor / $swapAmountMinor * 100;
  287. $this->roiByActionCurrencySpot = $this->roi * $swapRate;
  288. }
  289. }
  290. }
  291. public function getRoi() {
  292. if (empty($this->roi)) {
  293. $this->calcs();
  294. }
  295. return $this->roi;
  296. }
  297. public function getRoiBySpot() {
  298. if (empty($this->roiByActionCurrencySpot)) {
  299. $this->calcs();
  300. }
  301. return $this->roiByActionCurrencySpot;
  302. }
  303. public function getRoiPercents() {
  304. if (empty($this->roi)) {
  305. $this->calcs();
  306. }
  307. return $this->roiPercents;
  308. }
  309. public function getCoverage() {
  310. if (empty($this->coverage)) {
  311. $this->calcs();
  312. }
  313. return round($this->coverage, 1);
  314. }
  315. public function getActionAmount() {
  316. if (empty($this->actionAmount)) {
  317. $this->calcs();
  318. }
  319. return round($this->actionAmount, 1);
  320. }
  321. public function getOpositeAmount() {
  322. if (empty($this->opositeAmount)) {
  323. $this->calcs();
  324. }
  325. return round($this->opositeAmount, 1);
  326. }
  327. public function getOpositeCurrency() {
  328. if (empty($this->opositeCurrency)) {
  329. $this->calcs();
  330. }
  331. return $this->opositeCurrency;
  332. }
  333. }