JSFiddle - React, Tailwind, and code Playground

by thorst

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

JavaScript

let twBP = {
  // Tastyworks
  //https://support.tastyworks.com/support/solutions/articles/43000435195-naked-short-call
  //https://support.tastyworks.com/support/solutions/articles/43000435177-naked-short-put


  f1: function(curPrice, premCollected, cntContracts, strike, isCall, rate) {
    let outOfMoneyAmount = isCall ? curPrice - strike : strike - curPrice,
      final = (rate * curPrice + outOfMoneyAmount + premCollected) * cntContracts * 100;
    return _.round(final, 2);
  },
  f2: function(curPrice, premCollected, cntContracts, strike, isCall) {
    let multiplier = isCall ? curPrice : strike,
      final = (0.1 * multiplier + premCollected) * cntContracts * 100;
    return _.round(final, 2);
  },
  f3: function(cntContracts) {
    let final = 2.5 * cntContracts * 100;
    return _.round(final, 2);
  },
  greatest: function(curPrice, premCollected, cntContracts, strike, isCall, rate) {
    let results = [
      this.f1(curPrice, premCollected, cntContracts, strike, isCall, rate),
      this.f2(curPrice, premCollected, cntContracts, strike, isCall),
      this.f3(cntContracts)
    ];

    let margiReq = Math.max(...results);
    let bpEffect = _.round(margiReq - (premCollected * 100), 2);

    return bpEffect;
  }
};


let final = twBP.greatest(47.5, .5, 1, 45, false, .2);
console.log("Final margin req is:", final);