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



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

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



113
114
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
# File 'app/models/iro/datapoint.rb', line 113

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



64
65
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
# File 'app/models/iro/datapoint.rb', line 64

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



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

def close;    value;    end

#close=(a) ⇒ Object



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

def close= a; value= a; end

#quote_atObject

scope-by-kind is unnecessary here? vp 2024-08-08



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

validates :quote_at, uniqueness: { scope: [ :kind, :symbol ] }

#symbolObject

ticker, but use ‘symbol’ here



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

field :symbol