              :refer [>! <! >!! <!! go chan buffer close! thread
                      alts! alts!! timeout go-loop]])
   [:import com.ib.controller.ApiController
-           (com.ib.controller ApiController$TopMktDataAdapter)
+           (com.ib.controller ApiController$TopMktDataAdapter NewContract)
            ])
 
 

                                        ))))


(defn create-row [c]
  (reify
    com.ib.controller.ApiController$ITopMktDataHandler
    (tickPrice [this tick-type p auto-execute?]
      ;(println (.name tick-type) "price: " p)
      (>!! c [(.name tick-type) p])
      )
    (tickSize [this tick-type size]
      ;(println (.name tick-type) "size: " size)
      (>!! c [(.name tick-type) size])
      )
    (tickString [this tick-type val]
      ;(println (.name tick-type) "LastTime: " val)
      (>!! c [(.name tick-type) val])
      )
    (marketDataType [this data-type]
      (println "Frozen? " (= data-type com.ib.controller.Types$MktDataType/Frozen)))
    (tickSnapshotEnd [this] (println "tick snapshot end"))
    ))


(defn start []
  (let [api (api-ctrl)
        contract (create-contract "VXX")
        c (chan)
        row (create-row c)]
    (.connect api "localhost" 7497 5)
    (.reqTopMktData api contract "" false row)

    (go-loop []
             (println (<! c))
             (recur))

    (Thread/sleep 100000)
    (.cancelTopMktData api row)
    (.disconnect api)))




    ))

(defn create-contract [symbol type]
  (doto (com.ib.controller.NewContract.)
    (.symbol symbol)
    (.secType type)
    (.expiry "")
    (.strike 0.0)
    (.right com.ib.controller.Types$Right/None)
