Class: Iro::Datapoint

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
app/models/iro/datapoint.rb

Overview

Datapoints are at most daily! See Priceitem for intra-day data

Constant Summary collapse

KIND_CRYPTO =
'CRYPTO'
KIND_STOCK =
'STOCK'
KIND_OPTION =

but not PUT or CALL

'OPTION'
KIND_CURRENCY =
'CURRENCY'
KIND_TREASURY =
'TREASURY'
SYMBOL_BTC =

crypto

'BTC'
SYMBOL_ETH =
'ETH'
SYMBOL_JPY =

currencies

'JPY'
SYMBOL_COP =
'COP'
SUMBOL_EUR =
'EUR'
SYMBOL_T1MO =

treasuries

'T1MO'
SYMBOL_T2MO =
'T2MO'
SYMBOL_T3MO =
'T3MO'
SYMBOL_T4MO =
'T4MO'
SYMBOL_T6MO =
'T6MO'
SYMBOL_T1YR =
'T1YR'
SYMBOL_T2YR =
'T2YR'
SYMBOL_T3YR =
'T3YR'
SYMBOL_T5YR =
'T5YR'
SYMBOL_T7YR =
'T7YR'
SYMBOL_T10YR =
'T10YR'
SYMBOL_T20YR =
'T20YR'
SYMBOL_T30YR =
'T30YR'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.import_stock(symbol:, path:) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/models/iro/datapoint.rb', line 164

def self.import_stock symbol:, path:
  csv = CSV.read(path, headers: true)
  csv.each do |row|
    flag = create({
      kind:     KIND_STOCK,
      symbol:   symbol,
      date:     row['date'],
      quote_at: row['date'],

      volume: row['volume'],

      open:  row['open'],
      high:  row['high'],
      low:   row['low'],
      value: row['close'],
    })
    if flag.persisted?
      print '^'
    else
      puts flag.errors.messages
    end
  end
  puts 'ok'
end

.testObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/iro/datapoint.rb', line 115

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


  match = { '$match': {
    'date': {
      '$gte': '2023-12-25',
      '$lte': '2023-12-31',
    }
  } }

  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

.test_0trashObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/iro/datapoint.rb', line 66

def self.test_0trash
  add_fields = { '$addFields':  {
    'date_string': {
      '$dateToString': { 'format': "%Y-%m-%d", 'date': "$created_at" }
    }
  } }
  # group = { '$group': {
  #   '_id': "$date_string",
  #   'my_doc': { '$first': "$$ROOT" }
  # } }
  group = { '$group': {
    '_id': "$date",
    'my_doc': { '$first': "$$ROOT" }
  } }
  lookup = { '$lookup': {
    'from':         'iro_dates',
    'localField':   'date_string',
    'foreignField': 'date',
    'as':           'dates',
  } }
  lookup_merge = { '$replaceRoot': {
    'newRoot': { '$mergeObjects': [
      { '$arrayElemAt': [ "$dates", 0 ] }, "$$ROOT"
    ] }
  } }
  match = { '$match': {
    'kind': 'some-type',
    'created_at': {
      '$gte': '2023-12-01'.to_datetime,
      '$lte': '2023-12-31'.to_datetime,
    }
  } }

  outs = Iro::Datapoint.collection.aggregate([
    add_fields,
    lookup, lookup_merge,
    match,

    { '$sort': { 'date_string': 1 } },
    group,
    # { '$replaceRoot': { 'newRoot': "$my_doc" } },
    # { '$project': { '_id': 0, 'date_string': 1, 'value': 1 } },
  ])

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

Instance Method Details

#closeObject



59
# File 'app/models/iro/datapoint.rb', line 59

def close;    value;    end

#close=(a) ⇒ Object



60
# File 'app/models/iro/datapoint.rb', line 60

def close= a; value= a; end

#openObject

I don’t understand why this was forced to be unique… I want the date to be unique. validates :quote_at, uniqueness: { scope: [ :kind, :symbol ] } ## scope-by-kind is unnecessary here? vp 2024-08-08



54
# File 'app/models/iro/datapoint.rb', line 54

field :open,  type: Float

#quote_atObject

@obsolete, @deprecated, use :date instead?



49
# File 'app/models/iro/datapoint.rb', line 49

field :quote_at, type: DateTime

#symbolObject

ticker, but use ‘symbol’ here



21
# File 'app/models/iro/datapoint.rb', line 21

field :symbol