Sha256: 155d06ad4ac93256ecb85eef43d840bd62ac902fccf97667a7f1962e9d404b93

Contents?: true

Size: 1.92 KB

Versions: 11

Compression:

Stored size: 1.92 KB

Contents

##
## specifically Option or Stock priceitem?
##
class Iro::Priceitem
  include Mongoid::Document
  include Mongoid::Timestamps
  store_in collection: 'iro_price_items'

  ## PUT, CALL, STOCK
  field :putCall,         type: String
  field :symbol,          type: String
  field :ticker,          type: String
  # belongs_to :stock, inverse_of: :priceitems

  field :bid,             type: Float
  field :bidSize,         type: Integer
  field :ask,             type: Float
  field :askSize,         type: Integer
  field :last,            type: Float

  field :openPrice,       type: Float
  field :lowPrice,        type: Float
  field :highPrice,       type: Float
  field :closePrice,      type: Float

  field :quoteTimeInLong, type: Integer
  field :timestamp,       type: Integer
  field :totalVolume,     type: Integer
  field :mark,            type: Float
  field :exchangeName,    type: String
  field :volatility,      type: Float

  def self.my_find props={}
    lookup = { '$lookup': {
      'from':         'iro_price_items',
      'localField':   'date',
      'foreignField': 'date',
      'pipeline': [
        { '$sort': { 'value': -1 } },
      ],
      'as':           'dates',
    } }
    lookup_merge = { '$replaceRoot': {
      'newRoot': { '$mergeObjects': [
        { '$arrayElemAt': [ "$dates", 0 ] }, "$$ROOT"
      ] }
    } }


    match = { '$match': {
      'date': {
        '$gte': props[:begin_on],
        '$lte': props[:end_on],
      }
    } }

    group = { '$group': {
      '_id': "$date",
      'my_doc': { '$first': "$$ROOT" }
    } }

    outs = Iro::Date.collection.aggregate([
      match,

      lookup,
      lookup_merge,

      group,
      { '$replaceRoot': { 'newRoot': "$my_doc" } },
      # { '$replaceRoot': { 'newRoot': "$my_doc" } },


      { '$project': { '_id': 0, 'date': 1, 'value': 1 } },
      { '$sort': { 'date': 1 } },
    ])

    puts! 'result'
    pp outs.to_a
    # puts! outs.to_a, 'result'
  end

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
iron_warbler-2.0.7.37 app/models/iro/priceitem.rb
iron_warbler-2.0.7.36 app/models/iro/priceitem.rb
iron_warbler-2.0.7.35 app/models/iro/priceitem.rb
iron_warbler-2.0.7.34 app/models/iro/priceitem.rb
iron_warbler-2.0.7.33 app/models/iro/priceitem.rb
iron_warbler-2.0.7.32 app/models/iro/priceitem.rb
iron_warbler-2.0.7.31 app/models/iro/priceitem.rb
iron_warbler-2.0.7.30 app/models/iro/priceitem.rb
iron_warbler-2.0.7.29 app/models/iro/priceitem.rb
iron_warbler-2.0.7.28 app/models/iro/priceitem.rb
iron_warbler-2.0.7.27 app/models/iro/priceitem.rb