/**
   * @param candleSeries
   * @param startPeriod
   * @return
   * @throws ClassNotFoundException
   * @throws InstantiationException
   * @throws IllegalAccessException
   * @throws NoSuchMethodException
   * @throws InvocationTargetException
   * @throws IOException
   * @throws PersistentModelException
   * @throws StrategyRuleException
   */
  public Candle getPreviousDayCandleFromDb(CandleSeries candleSeries, ZonedDateTime startPeriod)
      throws ClassNotFoundException, InstantiationException, IllegalAccessException,
          NoSuchMethodException, InvocationTargetException, IOException, PersistentModelException,
          StrategyRuleException {
    Candle prevCandle = null;

    this.tradePersistentModel =
        (PersistentModel)
            ClassFactory.getServiceForInterface(PersistentModel._persistentModel, this);

    long days = 1;
    if (DayOfWeek.MONDAY.equals((startPeriod.getDayOfWeek()))) {
      days = 3;
    }

    ZonedDateTime newStartPeriod = this.getTradestrategy().getTradingday().getOpen();
    List<Candle> candleList =
        this.tradePersistentModel.findCandlesByContractDateRangeBarSize(
            candleSeries.getContract().getIdContract(), newStartPeriod.minusDays(days),
            newStartPeriod.minusDays(days), candleSeries.getBarSize());

    if (candleList.size() > 0) {
      prevCandle = candleList.get(0);
    }

    return prevCandle;
  }
Exemplo n.º 2
0
  /**
   * Method persistCandleSeries.
   *
   * @param candleSeries CandleSeries
   * @throws Exception
   */
  public synchronized void persistCandleSeries(CandleSeries candleSeries) throws Exception {
    Candle transientInstance = null;
    try {
      if (candleSeries.isEmpty()) return;

      EntityManager entityManager = EntityManagerHelper.getEntityManager();
      entityManager.getTransaction().begin();
      Tradingday tradingday = null;
      Contract contract = findContractById(candleSeries.getContract().getIdContract());
      for (int i = 0; i < candleSeries.getItemCount(); i++) {

        CandleItem candleItem = (CandleItem) candleSeries.getDataItem(i);
        if (null != candleItem.getCandle().getIdCandle()) {
          Candle instance = entityManager.find(Candle.class, candleItem.getCandle().getIdCandle());
          if (instance.equals(candleItem.getCandle())) {
            continue;
          } else {
            // This should never happen.
            throw new Exception(
                "Count: "
                    + i
                    + " Symbol: "
                    + candleSeries.getSymbol()
                    + "candleid: "
                    + candleItem.getCandle().getIdCandle()
                    + " open: "
                    + candleItem.getCandle().getStartPeriod());
          }
        }

        if (!candleItem.getCandle().getTradingday().equals(tradingday)) {

          if (null == candleItem.getCandle().getTradingday().getIdTradingDay()) {
            tradingday =
                findTradingdayByDate(
                    candleItem.getCandle().getTradingday().getOpen(),
                    candleItem.getCandle().getTradingday().getClose());
          } else {
            tradingday =
                findTradingdayById(candleItem.getCandle().getTradingday().getIdTradingDay());
          }

          if (null == tradingday) {
            entityManager.persist(candleItem.getCandle().getTradingday());
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
            tradingday = candleItem.getCandle().getTradingday();
          } else {
            Integer idTradingday = tradingday.getIdTradingDay();
            Integer idContract = contract.getIdContract();
            Integer barSize = candleSeries.getBarSize();
            String hqlDelete =
                "delete Candle where idContract = :idContract and idTradingday = :idTradingday and barSize = :barSize";
            entityManager
                .createQuery(hqlDelete)
                .setParameter("idContract", idContract)
                .setParameter("idTradingday", idTradingday)
                .setParameter("barSize", barSize)
                .executeUpdate();
            entityManager.getTransaction().commit();
            entityManager.getTransaction().begin();
          }
        }

        transientInstance = candleItem.getCandle();
        transientInstance.setTradingday(tradingday);
        transientInstance.setContract(contract);
        entityManager.persist(transientInstance);

        // Commit every 50 rows
        if ((Math.floor(i / 50d) == (i / 50d)) && (i > 0)) {
          entityManager.getTransaction().commit();
          entityManager.getTransaction().begin();
        }
      }
      entityManager.getTransaction().commit();
    } catch (RuntimeException re) {
      EntityManagerHelper.logError("Error persistCandleSeries failed :" + re.getMessage(), re);
      EntityManagerHelper.rollback();
      throw re;
    } finally {
      EntityManagerHelper.close();
    }
  }