Package com.quantcomponents.core.model

Examples of com.quantcomponents.core.model.IContract


    positions.clear();
  }
 
  @Override
  public synchronized void onTrade(ITrade trade) {
    IContract contract = trade.getOrder().getContract();
    IContract currency = ContractBean.cash(contract.getCurrency());
    PositionBean contractPosition = positions.get(contract);
    if (contractPosition == null) {
      contractPosition = new PositionBean();
      positions.put(contract, contractPosition);
    }
View Full Code Here


    }
  }
 
  private void executeTrade(OrderInfo orderInfo, int tradeAmount, double executionPrice) {
    double multiplier = 1.0;
    IContract contract = orderInfo.order.getContract();
    if (contract.getMultiplier() != null && contract.getMultiplier() != 0) {
      multiplier = contract.getMultiplier();
    }
    int signedTradeAmount = orderInfo.order.getSide() == OrderSide.BUY ? tradeAmount : -tradeAmount;
    double tradeCommissionAmount = commissionCalculator.calculateCommission(orderInfo.order, tradeAmount, executionPrice);
    double tradeCashFlow = -(signedTradeAmount * multiplier * executionPrice) - tradeCommissionAmount;
    double tradeAveragePrice = Math.abs(tradeCashFlow / multiplier / tradeAmount);
    TradeBean trade = new TradeBean(orderInfo.order, "Simulated", getLastPriceTimestamp(contract), tradeAmount, executionPrice, tradeAveragePrice);
    trades.add(trade);
    positionCalculator.onTrade(trade);
    for (IOrderStatusListener listener : orderStatusListeners) {
      listener.onOrderFilled(Integer.toString(orderInfo.internalId), tradeAmount, orderInfo.order.getAmount() == tradeAmount, executionPrice);
    }
    notifyPositionListeners(contract);
    notifyPositionListeners(ContractBean.cash(contract.getCurrency()));
  }
View Full Code Here

  }

  @Override
  public synchronized void setInputSeries(Collection<ISeries<Date, Double, ? extends ISeriesPoint<Date, Double>>> inputSeries) {
    for (ISeries<Date, Double, ? extends ISeriesPoint<Date, Double>> input : inputSeries) {
      IContract contract = null;
      if (input instanceof IStockDataCollection) {
        contract = ((IStockDataCollection) input).getContract();
      }
      SeriesListener seriesListener = new SeriesListener(contract);
      input.addSeriesListener(seriesListener);
View Full Code Here

    TableViewerColumn viewColId = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColId.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        return c.getBrokerID();
      }});
    TableColumn columnId = viewColId.getColumn();
    columnId.setText("Broker ID");
    columnId.setWidth(100);
    columnId.setResizable(true);

    TableViewerColumn viewColDesc = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColDesc.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        return c.getContractDescription().getLongName();
      }});
    TableColumn columnDesc = viewColDesc.getColumn();
    columnDesc.setText("Description");
    columnDesc.setWidth(140);
    columnDesc.setResizable(true);

    TableViewerColumn viewColTicker = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColTicker.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        return c.getSymbol();
      }});
    TableColumn columnTicker = viewColTicker.getColumn();
    columnTicker.setText("Ticker");
    columnTicker.setWidth(50);
    columnTicker.setResizable(true);
   
    TableViewerColumn viewColType = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColType.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        return c.getSecurityType().name();
      }});
    TableColumn columnType = viewColType.getColumn();
    columnType.setText("Type");
    columnType.setWidth(40);
    columnType.setResizable(true);
   
    TableViewerColumn viewColCcy = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColCcy.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        return c.getCurrency().getCurrencyCode();
      }});
    TableColumn columnCcy = viewColCcy.getColumn();
    columnCcy.setText("Ccy");
    columnCcy.setWidth(40);
    columnCcy.setResizable(true);
   
    TableViewerColumn viewColExchange = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColExchange.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        return c.getExchange();
      }});
    TableColumn columnExchange = viewColExchange.getColumn();
    columnExchange.setText("Exchange");
    columnExchange.setWidth(100);
    columnExchange.setResizable(true);
   
    TableViewerColumn viewColExpiry = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColExpiry.setLabelProvider(new ColumnLabelProvider() {
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        BareDate expiryDate = c.getExpiryDate();
        if (expiryDate != null) {
          return sdf.format(expiryDate.getDate(TimeZone.getDefault()));
        } else {
          return "";
        }
      }});
    TableColumn columnExpiry = viewColExpiry.getColumn();
    columnExpiry.setText("Expiry");
    columnExpiry.setWidth(90);
    columnExpiry.setResizable(true);
   
    TableViewerColumn viewColStrike = new TableViewerColumn(contractListViewer, SWT.NONE);
    viewColStrike.setLabelProvider(new ColumnLabelProvider() {
      private DecimalFormat doubleFormat = new DecimalFormat("0.000");
      @Override
      public String getText(Object element) {
        IContract c = (IContract)element;
        Double strike = c.getStrike();
        if (strike != null) {
          return doubleFormat.format(strike);
        } else {
          return "";
        }
      }});
    TableColumn columnStrike = viewColStrike.getColumn();
    columnStrike.setText("Strike");
    columnStrike.setWidth(80);
    columnStrike.setResizable(true);
   
    Table table = contractListViewer.getTable();
    table.setHeaderVisible(true);
    table.setLinesVisible(true);
    table.setSortColumn(columnDesc);
    table.setSortDirection(SORT_ASCENDING);
   
    contractListViewer.setContentProvider(new ArrayContentProvider());
    contractListViewer.addSelectionChangedListener(new ISelectionChangedListener() {
      @Override
      public void selectionChanged(SelectionChangedEvent event) {
        selection = (IStructuredSelection)event.getSelection();
        if (selection != null) {
          Object firstElement = selection.getFirstElement();
          if (firstElement != null && firstElement instanceof IContract) {
            selectedContract = (IContract)firstElement;
            setPageComplete(true);
          }
        }
      }
    });
    contractListViewer.setComparator(new ViewerComparator() {
      @Override
      public int compare(Viewer viewer, Object e1, Object e2) {
        IContract c1 = (IContract) e1;
        IContract c2 = (IContract) e2;
        int cmp = c1.getContractDescription().getLongName().compareToIgnoreCase(c2.getContractDescription().getLongName());
        if (cmp != 0)  {
          return cmp;
        }
        if (c1.getExpiryDate() != null && c2.getExpiryDate() != null) {
          cmp = c1.getExpiryDate().compareTo(c2.getExpiryDate());
          if (cmp != 0) {
            return cmp;
          }
        }
        if (c1.getExchange() != null && c2.getExchange() != null) {
          cmp = c1.getExchange().compareTo(c2.getExchange());
          if (cmp != 0) {
            return cmp;
          }
        }
        return 0;
View Full Code Here

    stockDbMultiMap.clear();
  }
 
  @Override
  public synchronized void addStockDatabase(IStockDatabase stockDatabase) {
    IContract contract = stockDatabase.getContract();
    Set<IStockDatabase> set = stockDbMultiMap.get(contract);
    if (set == null) {
      set = new CopyOnWriteArraySet<IStockDatabase>();
      IContract key = new ImmutableContractBean(contract); // since IContract instances can be mutable, make a snapshot of it
      stockDbMultiMap.put(key, set);
    }
    set.add(stockDatabase);
    stockDbByID.put(stockDatabase.getPersistentID(), stockDatabase);
  }
View Full Code Here

    final Map<IContract, PositionPriceUpdater> positionUpdaters = new ConcurrentHashMap<IContract, PositionPriceUpdater>();
   
    @Override
    public void updatePortfolio(Contract iBContract, int positionAmt, double marketPrice, double marketValue, double averageCost, double unrealizedPNL, double realizedPNL, String accountName) {
      if (accountName.equals(accountId)) {
        final IContract contract = ContractBean.copyOf(new IBContract(iBContract, constantTranslator));
        final PositionBean position = new PositionBean(new Date(), positionAmt, marketPrice, marketValue, averageCost, unrealizedPNL, realizedPNL);
       
        if (!positionUpdaters.containsKey(contract)) { // setup price listener for that contract: since IB sends too few updates, we update automatically based on price changes
          PositionPriceUpdater positionPriceUpdater = new PositionPriceUpdater();
          positionPriceUpdater.position = position;
          final ITickListener tickListener = new ITickListener() {
            @Override
            public void onTick(ITickPoint tick) {
              if (tick.getDataType() == DataType.TRADES || tick.getDataType() == DataType.MIDPOINT && SecurityType.CASH==contract.getSecurityType()) {
                PositionPriceUpdater pu = positionUpdaters.get(contract);
                if (pu != null) {
                  synchronized (pu) { // update position with current price
                    pu.position.setMarketPrice(tick.getValue());
                    pu.position.setMarketValue(tick.getValue() * pu.position.getSignedAmount());
 
View Full Code Here

TOP

Related Classes of com.quantcomponents.core.model.IContract

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.