Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

fix current price of currency #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/tinky.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,7 @@ def total_amount(positions) # rubocop:disable Metrics/AbcSize
def exchange_rates(positions)
# calculate exchange rate in RUB by currency
currencies(positions).reduce({}) do |result, c|
last_currency_candle = client.market_candles(candles_params(c[:figi]))
rate = last_currency_candle.dig(:payload, :candles).last[:c]

# get currency code by ticker
currency = currency_by_ticker(c[:ticker])

result.merge(currency => rate)
result.merge(currency_by_ticker(c[:ticker]) => exchange_rate(c))
end
end

Expand Down Expand Up @@ -192,6 +186,10 @@ def sell_price(item)
end

def row_data(item)
if item[:instrumentType] == 'Currency'
item[:averagePositionPrice] = { currency: 'RUB', value: exchange_rate(item) }
end

[
item[:instrumentType].upcase,
decorate_name(item[:name]),
Expand Down Expand Up @@ -262,6 +260,13 @@ def print_timestamp
puts "\nLast updated: #{Time.now}\n\n"
end

def exchange_rate(position)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

есть смысл передавать сюда просто figi

client
.market_candles(candles_params(position[:figi]))
.dig(:payload, :candles)
.last[:c]
end

def currency_by_ticker(ticker)
CURRENCIES.select { |_, v| v[:ticker] == ticker }.keys.first
end
Expand Down