Install the OrientDB-Database-Server

The database can be run as single instance or distributed across several cloud units.

We focus on a single server instance and highly recommend to install the database-server on an virtual machine, a lxc/lxd-container or to run it inside a docker image.

Check your installation by visiting the studio app. Serveraddress: https://{ip of orientdb-server}:2480

Prepare the database

Although OrientDB can operate completely schemaless, a rudimentary declaration of links, indices and relations speeds things up.

The database structure is defined in setup_orientdb.rb. Setup.init_database is called during initialisation of the interactive console.

The following hierarchy is implemented:

- - V
  - - ib_account
    - - ib_advisor
      - - ib_demo_advisor
    - - ib_user
      - - ib_demo_user
    - ib_account_value
    - ib_bar
    - ib_contract
    - - ib_bag
      - - ib_spread
      - ib_forex
      - ib_future
      - ib_index
      - ib_option
      - ib_stock
   - ib_contract_detail
   - ib_portfolio_value

Any IB::Model class is a vertex (V). Vertices hold the data. They are connected
via joins (unidirectional) or edges (bidirectional).

  • Vertex - an entity that can be linked with other Vertices and has the following mandatory properties:
    • set of incoming Edges ( through in )
    • set of outgoing Edges ( through out )
  • Edge - an entity that links two Vertices and has the following mandatory properties:
    • link to an incoming Vertex (also known as head)
    • link to an outgoing Vertex (also known as tail)
    • label that defines the type of connection/relationship between head and tail vertex

source

Setup the TimeGrid

Many data received from the TWS are time related. OrientDB Time Graph provides the infrastructure to connect these items to a grid.

This Graph is realized

Jahr -- [MONTH_OF] -- Monat --[DAY_OF]-- Tag --[TIME_OF]-- Stunde

The nodes are crosslinked and any point of the grid is easily accessed.

The library provides .to_tg additions to »Date«, »DateTime« and »String«.

z = "22.3.2003".to_tg
=> #<TG::Tag:0x000000030d79d0 @metadata={"type"=>"d", "class"=>"tag", "version"=>4, "fieldTypes"=>"in_grid_of=g,out_grid_of=g,in_day_of=g", "cluster"=>25, "record"=>294}, @d=nil, @attributes={"value"=>22, "in_grid_of" =>["#49:304"], "out_grid_of"=>["#50:304"], "in_day_of"=>["#41:294"], "created_at"=>Mon, 12 Sep 2016 09:56:41 +0200}> 
z.datum          => Sat, 22 Mar 2003  (returns a Date)
z.next.datum     => Sun, 23 Mar 2003
( z + 3 ).datum  => Tue, 25 Mar 2003 
z.prev.datum     => Fri, 21 Mar 2003
( z - 5 ).datum   => Mon, 17 Mar 2003 
z.move( -20 ).datum => Sun, 02 Mar 2003 
z.environment( 5 ).datum
 => ["18.5.2003", "19.5.2003", "20.5.2003", "21.5.2003", "22.5.2003", "23.5.2003", "24.5.2003", "25.5.2003", "26.5.2003", "27.5.2003", "28.5.2003"] 

datum is a method of the TimeGrid-gem. It has to be replaced by custom methods to access connected data.

Working with intervalls

The time-grid provides a high degree of flexibility to access intervalls.

  • Display any data associated to the first three day of the month August to November in the years 2000, 2005 and 2010
> z= TG::Jahr[2000, 2005,.2010].monat(8 .. 11).tag(1,2,3 ).orient_flatten.datum
  =>  [Tue, 01 Aug 2000, Wed, 02 Aug 2000, Thu, 03 Aug 2000,
       Fri, 01 Sep 2000, Sat, 02 Sep 2000, Sun, 03 Sep 2000,
       Sun, 01 Oct 2000, Mon, 02 Oct 2000, Tue, 03 Oct 2000,
       Wed, 01 Nov 2000, Thu, 02 Nov 2000, Fri, 03 Nov 2000,
      Mon, 01 Aug 2005, Tue, 02 Aug 2005, Wed, 03 Aug 2005,
       Thu, 01 Sep 2005, Fri, 02 Sep 2005, Sat, 03 Sep 2005,
       Sat, 01 Oct 2005, Sun, 02 Oct 2005, Mon, 03 Oct 2005,
       Tue, 01 Nov 2005, Wed, 02 Nov 2005, Thu, 03 Nov 2005,
      Sun, 01 Aug 2010, Mon, 02 Aug 2010, Tue, 03 Aug 2010,
       Wed, 01 Sep 2010, Thu, 02 Sep 2010, Fri, 03 Sep 2010,
       Fri, 01 Oct 2010, Sat, 02 Oct 2010, Sun, 03 Oct 2010,
       Mon, 01 Nov 2010, Tue, 02 Nov 2010, Wed, 03 Nov 2010]
 

orient_flatten flattens the array and leaves ActiveOrient-Objects intact.

Boot-Process

In Strict-Mode (ActiveOrient::Model.keep_models_without_file = false) ActiveOrient only recognizes database classes if a corresponding model file is detected. The files are searched in directories specified in ActiveOrient::Model.model_dir.

The boot-process of IB-OrientDB starts initializing the time-grid. Any database class prefixed by ‘tg_’ is assigned to corresponding ruby classes. Other database-classes are left unallocated. Then the namespace is changed to IB and ‘ib_‘-prefixed classes are initialized. Finally the namespace is changed to HC.

If the interactive console is run, this process is documented:

>>#:~/workspace/ib-orientdb/bin$ ./gateway 

>> IB-OrientDB Interactive Console  <<
---------------------------------------------

 ... preparing environment
13.12.(05:38:23) INFO->Connected to database demo
13.12.(05:38:23) INFO->CREATE CLASS E 
13.12.(05:38:23) INFO->CREATE CLASS V 
13.12.(05:38:23) INFO->Connected to database demo    ### Time-grid initialisation
                                                     ### Its OK that the following classes 
                                                     ### are left  untouchted
13.12.(05:38:23) INFO->TG::HC_D2F -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_GRID -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_HAS_ACCOUNT -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_HAS_PORTFOLIO -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_HAS_POSITION -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_HAS_STRATEGY -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_MY_USER -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HC_P2U -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::HcPortfolio -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbAccount -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbAccountValue -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbAccount -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbBar -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContractDetail -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbAccount -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbAccount -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbPortfolioValue -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbContract -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbUnderlying -->  Class NOT allocated
13.12.(05:38:23) INFO->TG::IbAccount -->  Class NOT allocated
13.12.(05:38:23) INFO->select COUNT(*) from tg_time_of 
13.12.(05:38:24) INFO->Connected to database demo     ### IB-OrientDB initialisation
                                                      ### the list of not allocated 
                                                      ### classes shortens
13.12.(05:38:24) INFO->IB::HC_D2F -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_GRID -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_HAS_ACCOUNT -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_HAS_PORTFOLIO -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_HAS_POSITION -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_HAS_STRATEGY -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_MY_USER -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HC_P2U -->  Class NOT allocated
13.12.(05:38:24) INFO->IB::HcPortfolio -->  Class NOT allocated
Contract already a constant
Underlying already a constant
-------------------- initialize --------------------
13.12.(05:38:24) INFO->Connected to database demo     ### HC- initialisation :: only one class is
                                                      ### left uninitialized. The file has to be 
                                                      ### created if the database class should be used.
13.12.(05:38:24) INFO->HC::HAS_ACCOUNT -->  Class NOT allocated
-----------------------------------------------------------------------------------
----------------------------------- TWS Connect -----------------------------------
SELF: IB::OrientGateway
< ManagedAccounts:
Tags: