Exemplo n.º 1
0
  public static double calculateRealizedPnL(
      Position position, Map<Instrument, HistoryDataKBar> currentPrices, Currency baseCurrency) {

    Double amount = position.getAmount();
    Direction direction = position.getDirection();
    Double openPrice = position.getOpenPrice();
    Double closePrice = position.getClosePrice();
    HistoryDataKBar instrumentPriceBar = currentPrices.get(position.getInstrument());
    HistoryDataKBar conversionPriceBar = null;
    if (baseCurrency != position.getInstrument().getCurrency1()
        && baseCurrency != position.getInstrument().getCurrency2()) {
      Instrument conversionInstrument =
          new Instrument(baseCurrency, position.getInstrument().getCurrency2());
      conversionPriceBar = currentPrices.get(conversionInstrument);
    }

    Double realizedPnL;

    //  Standard PnL calculation.  This is in the second currency of the instrument
    if (direction == Direction.Long) {
      realizedPnL = amount * TradingUtils.getGolbalAmountUnit() * (closePrice - openPrice);
    } else {
      realizedPnL = amount * TradingUtils.getGolbalAmountUnit() * (openPrice - closePrice);
    }

    // simple case -- pips is already in base currency
    if (baseCurrency == instrumentPriceBar.getInstrument().getCurrency2()) return realizedPnL;

    // if the base currency is the first currency of the instrument, we need to
    // divide pips by ask price to get pips in base currency
    if (instrumentPriceBar.getInstrument().getCurrency1() == baseCurrency) {
      realizedPnL = realizedPnL / instrumentPriceBar.getOhlc().getAskClose();
      return realizedPnL;
    }

    if (conversionPriceBar == null)
      throw new IllegalArgumentException(
          "need a conversion price when computing instrument "
              + conversionPriceBar.getInstrument()
              + " into P/L of base currency "
              + baseCurrency);

    if (baseCurrency == conversionPriceBar.getInstrument().getCurrency1()) {
      // use the 'ask'
      realizedPnL = realizedPnL / conversionPriceBar.getOhlc().getAskClose();
    } else {
      // use the 'bid'
      realizedPnL = realizedPnL * conversionPriceBar.getOhlc().getBidClose();
    }
    return realizedPnL;
  }
Exemplo n.º 2
0
  private static double calculateOpenPnL(
      Double amount,
      Direction direction,
      Double openPrice,
      HistoryDataKBar instrumentPriceBar,
      HistoryDataKBar conversionPriceBar,
      Currency baseCurrency) {

    Double openPnL;

    //  Standard PnL calculation.  This is in the second currency of the instrument
    if (direction == Direction.Long) {
      openPnL =
          amount
              * TradingUtils.getGolbalAmountUnit()
              * (instrumentPriceBar.getOhlc().getBidClose() - openPrice);
    } else {
      openPnL =
          amount
              * TradingUtils.getGolbalAmountUnit()
              * (openPrice - instrumentPriceBar.getOhlc().getAskClose());
    }

    // simple case -- pips is already in base currency
    if (baseCurrency == instrumentPriceBar.getInstrument().getCurrency2()) return openPnL;

    // if the base currency is the first currency of the instrument, we need to
    // divide pips by ask price to get pips in base currency
    if (instrumentPriceBar.getInstrument().getCurrency1() == baseCurrency) {
      openPnL = openPnL / instrumentPriceBar.getOhlc().getAskClose();
      return openPnL;
    }

    if (conversionPriceBar == null)
      throw new IllegalArgumentException(
          "need a conversion price when computing instrument "
              + conversionPriceBar.getInstrument()
              + " into P/L of base currency "
              + baseCurrency);

    if (baseCurrency == conversionPriceBar.getInstrument().getCurrency1()) {
      // use the 'ask'
      openPnL = openPnL / conversionPriceBar.getOhlc().getAskClose();
    } else {
      // use the 'bid'
      openPnL = openPnL * conversionPriceBar.getOhlc().getBidClose();
    }
    return openPnL;
  }
Exemplo n.º 3
0
  /**
   * Assumption is the account base currency is USD
   *
   * @param accountLeverage accountLeverage
   * @param closePrice closePrice
   * @param p p
   * @param baseCurrency Now Assumption is the account base currency is USD
   * @return position margin
   */
  public static Double calculateMargin(
      Double accountLeverage, Double closePrice, Position p, Currency baseCurrency) {
    TradingUtils.assertStat(baseCurrency == Currency.USD, "\"The baseCurrency is not USD!\"");
    if (p.getInstrument().getCurrency1() != baseCurrency) {
      if (!getInterestInstrumentList()
          .contains(new Instrument(p.getInstrument().getCurrency1(), baseCurrency))) {
        throw new RuntimeException(
            "No related instrument data found! ("
                + new Instrument(p.getInstrument().getCurrency1(), baseCurrency)
                + ")");
      }

      return closePrice * p.getAmount() * TradingUtils.getGolbalAmountUnit() / accountLeverage;
    } else {
      return p.getAmount() * TradingUtils.getGolbalAmountUnit() / accountLeverage;
    }
  }