Ejemplo n.º 1
0
  @Ignore
  @Test
  public void testGetHistoryBarsFromDukascopyServer() throws InterruptedException {

    String start = "2011.01.01 00:00:00 +0000";
    String end = "2011.01.02 01:00:00 +0000";
    historyMarketDataFeedTestStrategy.setServerBars(kBarsFromDukascopyServer);
    historyMarketDataFeedTestStrategy.setTestBar_start(start);
    historyMarketDataFeedTestStrategy.setTestBar_end(end);
    historyMarketDataFeedTestStrategy.setTestPeriod(Period.TEN_SECS);
    historyMarketDataFeedTestStrategy.setInstrument(Instrument.EURUSD);

    dukascopyHistoryMarketDataFeedClient.setStrategy(historyMarketDataFeedTestStrategy);
    new Thread() {
      public void run() {
        dukascopyHistoryMarketDataFeedClient.start();
      }
    }.start();

    synchronized (kBarsFromDukascopyServer) {
      kBarsFromDukascopyServer.wait();
      logger.info("verify the data in kBarsFromDukascopyServer");
      logger.info("kBarsFromDukascopyServer size is " + kBarsFromDukascopyServer.size());
      for (HistoryDataKBar kbar : kBarsFromDukascopyServer) {
        logger.info(kbar.toString());
      }
    }
  }
Ejemplo n.º 2
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;
  }
Ejemplo n.º 3
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;
  }