예제 #1
0
  public void contractDetailsEnd(int reqId) {
    consoleMsg("contractDetailsEnd: " + reqId);

    try {
      synchronized (m_mutex) {
        if (m_status == Status.SecDefFMF) {

          if (reqId != 0) {
            // ignore details end for leg requests
            return;
          }

          if (m_frontMonthFuture == null) {
            error("Could not find front month future for hedging");
            return;
          }

          consoleMsg("using " + m_frontMonthFuture.conid() + " for hedging");

          m_contract.underComp().conid(m_frontMonthFuture.conid());

          /*
           * And finally submit RFQ
           */
          submitRfq();
        }
      }
    } catch (Exception e) {
      // will update status and notify main thread
      error(e.toString());
    }
  }
  void onBtnUnderComp() {

    UnderCompDlg underCompDlg = new UnderCompDlg(m_underComp, this);

    // show delta neutral dialog
    underCompDlg.setVisible(true);
    if (underCompDlg.ok()) {
      m_contract.m_underComp = m_underComp;
    } else if (underCompDlg.reset()) {
      m_contract.m_underComp = null;
    }
  }
예제 #3
0
  private void submitRfq() {
    consoleMsg("REQ: rfq " + m_rfqId);

    m_status = m_contract.underComp() != null ? Status.Rfq : Status.Ticks;

    client().placeOrder(m_rfqId, m_contract, new RfqOrder(m_clientId, m_rfqId, 1));
  }
예제 #4
0
 private void goButtonActionPerformed(
     java.awt.event.ActionEvent evt) { // GEN-FIRST:event_goButtonActionPerformed
   Contract contract = new Contract();
   contract.m_symbol = symbolInTextBox.getText().toUpperCase();
   contract.m_secType = "FUT";
   contract.m_exchange = exchangeTextBox.getText().toUpperCase();
   contract.m_includeExpired = true;
   contractInfos.setParams(this, contract);
   Thread ciThread = new Thread(contractInfos);
   ciThread.run();
   // Trader trader = Dispatcher.getTrader();
   // trader.setContractDetailsConnection(this);
   // Strategy strategy = new DownloaderStrategy(); // a dummy strategy, needed for the framework
   // trader.getAssistant().addStrategy(strategy); //Updates id for TWS
   // int requestId = strategy.getId();
   // trader.getAssistant().requestContractDetails(requestId, contract);
 } // GEN-LAST:event_goButtonActionPerformed
예제 #5
0
  public void deltaNeutralValidation(int reqId, DeltaNeutralContract underComp) {
    consoleMsg("deltaNeutralValidation:" + reqId);

    synchronized (m_mutex) {
      if (m_status == Status.Rfq) {
        if (reqId != m_rfqId) {
          // unexpected dn validation
          return;
        }

        // update underComp
        m_contract.underComp(underComp);
        m_status = Status.Ticks;
      }
    }
  }
예제 #6
0
  public void testOrder() throws Exception {
    int clientId = 2;
    connect(clientId);

    if (client() != null && client().isConnected()) {
      try {
        synchronized (m_mutex) {
          if (client().serverVersion() < 42) {
            error("Sample will not work with TWS older that 877");
          }

          while (m_status != Status.Done && m_status != Status.Error) {

            if (m_status == Status.None) {
              obtainContract();
              if (m_status != Status.Error && m_status != Status.SecDef) {
                submitRfq();
              }
            }
            m_mutex.wait();
          }
        }
      } finally {
        disconnect();
      }

      if (m_status == Status.Done) {
        String msg =
            "Done, bid=" + m_bidSize + "@" + m_bidPrice + " ask=" + m_askSize + "@" + m_askPrice;

        DeltaNeutralContract underComp = m_contract.underComp();
        if (underComp != null) {
          msg +=
              " DN: conId="
                  + underComp.conid()
                  + " price="
                  + underComp.price()
                  + " delta="
                  + underComp.delta();
        }
        consoleMsg(msg);
      }
    }
  }
예제 #7
0
  public void contractDetails(int reqId, ContractDetails contractDetails) {
    consoleMsg("contractDetails: " + reqId);

    try {
      synchronized (m_mutex) {
        if (m_status == Status.SecDef) {
          /*
           * Note: we are requesting SecDefs only if we need Combo's
           */

          int legId = reqId - 1;

          ComboLeg comboLeg =
              new ComboLeg(
                  contractDetails.contract().conid(), /* ratio */
                  1,
                  (reqId == 1 ? "BUY" : "SELL"),
                  m_contract.exchange(),
                  0);

          m_contract.comboLegs().set(legId, comboLeg);

          /*
           * Do we have all legs?
           */
          for (int i = 0; i < m_contract.comboLegs().size(); ++i) {
            if (i == legId) continue;
            if (m_contract.comboLegs().get(i) == null) return;
          }

          if (m_contract.underComp() != null) {
            /*
             * Store underConId if needed
             */
            if (m_underConId == 0) {
              m_underConId = contractDetails.underConid();
            }

            /*
             * Do we need to request front month future for hedging?
             */

            if (m_needFrontMonthFuture) {
              m_status = Status.SecDefFMF;

              Contract futContract =
                  new FutContract(
                      contractDetails.contract().symbol(),
                      /* all expirations */ "",
                      contractDetails.contract().currency());

              submitSecDef(0, futContract);
              return;
            }

            consoleMsg("using " + m_underConId + " for hedging");
            m_contract.underComp().conid(m_underConId);
          }

          /*
           * And finally submit RFQ
           */
          submitRfq();
        } else if (m_status == Status.SecDefFMF) {
          /*
           * Ignore unknown reqId's
           */
          if (reqId != 0) {
            return;
          }

          /*
           * Ignore secDefs with different underConId
           */
          if (contractDetails.underConid() != m_underConId) {
            return;
          }

          Contract contract = contractDetails.contract();

          /*
           * Check if we have a better match
           */
          int contractLastTradeDate = Integer.parseInt(contract.lastTradeDateOrContractMonth());
          int contractMult = Integer.parseInt(contract.multiplier());

          if (m_frontMonthFuture != null) {
            if (m_frontMonthFutureLastTradeDate <= contractLastTradeDate) {
              return;
            }
            if (m_frontMonthFutureLastTradeDate == contractLastTradeDate
                && m_frontMonthFutureMult <= contractMult) {
              return;
            }
          }

          m_frontMonthFuture = contract;
          m_frontMonthFutureLastTradeDate = contractLastTradeDate;
          m_frontMonthFutureMult = contractMult;
        }
      }
    } catch (Exception e) {
      // will update status and notify main thread
      error(e.toString());
    }
  }
예제 #8
0
  private void obtainContract() {
    switch (m_mode) {
      case 0:
        m_contract = new StkContract("IBM");
        m_contract.currency("EUR");
        break;
      case 1:
        m_contract = new FutContract("IBM", "200809");
        break;
      case 2:
        m_contract = new OptContract("IBM", "200809", 120, "CALL");
        break;
      case 3:
        m_contract = new OptContract("Z", "LIFFE", "200809", 54.75, "CALL");
        m_contract.currency("GBP");
        break;
      case 4:
        m_contract = new ComboContract("Z", "GBP", "LIFFE");
        m_contract.comboLegs(new ArrayList<ComboLeg>(2));
        {
          Contract l1 = new OptContract("Z", "LIFFE", "200809", 54.75, "CALL");
          l1.currency("GBP");
          submitSecDef(1, l1);
        }
        {
          Contract l2 = new OptContract("Z", "LIFFE", "200810", 55.00, "CALL");
          l2.currency("GBP");
          submitSecDef(2, l2);
        }
        m_status = Status.SecDef;
        break;
      case 5:
        m_contract = new ComboContract("IBM");
        m_contract.comboLegs(new ArrayList<ComboLeg>(1));

        m_contract.underComp(new DeltaNeutralContract());
        // m_contract.m_underComp.m_delta = 0.8;
        // m_contract.m_underComp.m_price = 120;
        {
          Contract l1 = new OptContract("IBM", "200809", 120, "CALL");
          submitSecDef(1, l1);
        }

        m_status = Status.SecDef;
        break;
      case 6:
        m_contract = new ComboContract("RUT");
        m_contract.comboLegs(new ArrayList<ComboLeg>(1));

        m_contract.underComp(new DeltaNeutralContract());
        m_needFrontMonthFuture = true;
        {
          Contract l1 = new OptContract("RUT", "200809", 740, "CALL");
          submitSecDef(1, l1);
        }

        m_status = Status.SecDef;
        break;
      case 7:
        m_contract = new ComboContract("Z", "GBP", "LIFFE");
        m_contract.comboLegs(new ArrayList<ComboLeg>(1));

        m_contract.underComp(new DeltaNeutralContract());
        m_needFrontMonthFuture = true;
        {
          Contract l1 = new OptContract("Z", "LIFFE", "200808", 55.00, "CALL");
          l1.currency("GBP");
          submitSecDef(1, l1);
        }

        m_status = Status.SecDef;
        break;
    }
  }
  void onOk() {
    m_rc = false;

    try {
      // set id
      m_id = Integer.parseInt(m_Id.getText());

      // set contract fields
      m_contract.m_conId = ParseInt(m_conId.getText(), 0);
      m_contract.m_symbol = m_symbol.getText();
      m_contract.m_secType = m_secType.getText();
      m_contract.m_expiry = m_expiry.getText();
      m_contract.m_strike = ParseDouble(m_strike.getText(), 0.0);
      m_contract.m_right = m_right.getText();
      m_contract.m_multiplier = m_multiplier.getText();
      m_contract.m_exchange = m_exchange.getText();
      m_contract.m_primaryExch = m_primaryExch.getText();
      m_contract.m_currency = m_currency.getText();
      m_contract.m_localSymbol = m_localSymbol.getText();
      m_contract.m_tradingClass = m_tradingClass.getText();
      try {
        int includeExpired = Integer.parseInt(m_includeExpired.getText());
        m_contract.m_includeExpired = (includeExpired == 1);
      } catch (NumberFormatException ex) {
        m_contract.m_includeExpired = false;
      }
      m_contract.m_secIdType = m_secIdType.getText();
      m_contract.m_secId = m_secId.getText();

      // set order fields
      m_order.m_action = m_action.getText();
      m_order.m_totalQuantity = Integer.parseInt(m_totalQuantity.getText());
      m_order.m_orderType = m_orderType.getText();
      m_order.m_lmtPrice = parseStringToMaxDouble(m_lmtPrice.getText());
      m_order.m_auxPrice = parseStringToMaxDouble(m_auxPrice.getText());
      m_order.m_goodAfterTime = m_goodAfterTime.getText();
      m_order.m_goodTillDate = m_goodTillDate.getText();

      m_order.m_faGroup = m_faGroup;
      m_order.m_faProfile = m_faProfile;
      m_order.m_faMethod = m_faMethod;
      m_order.m_faPercentage = m_faPercentage;

      // set historical data fields
      m_backfillEndTime = m_BackfillEndTime.getText();
      m_backfillDuration = m_BackfillDuration.getText();
      m_barSizeSetting = m_BarSizeSetting.getText();
      m_useRTH = Integer.parseInt(m_UseRTH.getText());
      m_whatToShow = m_WhatToShow.getText();
      m_formatDate = Integer.parseInt(m_FormatDate.getText());
      m_exerciseAction = Integer.parseInt(m_exerciseActionTextField.getText());
      m_exerciseQuantity = Integer.parseInt(m_exerciseQuantityTextField.getText());
      m_override = Integer.parseInt(m_overrideTextField.getText());
      ;

      // set market depth rows
      m_marketDepthRows = Integer.parseInt(m_marketDepthRowTextField.getText());
      m_genericTicks = m_genericTicksTextField.getText();
      m_snapshotMktData = m_snapshotMktDataTextField.isSelected();

      m_marketDataType = m_marketDataTypeCombo.getSelectedIndex() + 1;
    } catch (Exception e) {
      Main.inform(this, "Error - " + e);
      return;
    }

    m_rc = true;
    setVisible(false);
  }
예제 #10
0
  // This places orders and listens to orders status
  public void run() {
    try {
      // Make connection, wait for nextOrderId
      connectToTWS();

      while (nextOrderId == -1) snooze(1000);

      puts("\nFind Contract Definitions for GOOG option");

      Contract con1 = new Contract();
      con1.m_symbol = "GOOG";
      con1.m_secType = "OPT";
      con1.m_expiry = "201301";
      con1.m_strike = 500.0;
      con1.m_right = "C";
      con1.m_multiplier = "100";
      con1.m_exchange = "SMART";
      con1.m_currency = "USD";

      client.reqContractDetails(110, con1);

      puts("Request sent");
      snooze(4000);

      puts("\nFind Contract Definitions for APPLE stock");

      Contract aapl = new Contract();
      aapl.m_symbol = "AAPL";
      aapl.m_secType = "STK";

      client.reqContractDetails(111, aapl);

      puts("Request sent");
      snooze(4000);

      puts("\nSee? No reply...");

      snooze(4000);

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      disconnectFromTWS();
    }
  }
예제 #11
0
  public Contract getContract() {
    Contract c = new Contract();
    c.m_conId = m_conid;
    c.m_symbol = m_symbol;
    c.m_secType = m_secType.toString();
    c.m_expiry = m_expiry;
    c.m_strike = m_strike;
    c.m_right = m_right.getApiString();
    c.m_multiplier = m_multiplier;
    c.m_exchange = m_exchange;
    c.m_currency = m_currency;
    c.m_localSymbol = m_localSymbol;
    c.m_tradingClass = m_tradingClass;
    c.m_primaryExch = m_primaryExch;
    c.m_secIdType = m_secIdType.getApiString();
    c.m_secId = m_secId;

    if (m_underComp != null) {
      c.m_underComp = new UnderComp();
      c.m_underComp.m_conId = m_underComp.conid();
      c.m_underComp.m_delta = m_underComp.delta();
      c.m_underComp.m_price = m_underComp.price();
    }

    c.m_comboLegs = new Vector<ComboLeg>();
    for (NewComboLeg leg : m_comboLegs) {
      c.m_comboLegs.add(leg.getComboLeg());
    }

    return c;
  }