<commit_before>class TradeData

    constructor: ->
        @type = null
        @id = null
        @timestamp = null
        @quantity = null
        @price = null
        @cost = 0.0
        @collateral
        @status = null # possible values: canceled, unconfirmed, confirmed, placed

    invert: ->
        td = new TradeData()
        td.type = @type
        td.id = @id
        td.status = @status
        td.timestamp = @timestamp
        td.quantity = @cost
        td.cost = @quantity
        td.collateral = @collateral
        #td.price = if @price and @price > 0.0 then 1.0 / @price else 0.0
        td.price = 1.0 / @price
        return td

    touch: ->
        @timestamp = Date.now()
    expired: ->
        return Date.now() - @timestamp > 20000


class Market

    constructor: ->
        @actual_market = null
        @name = ''
        @quantity_symbol = ''
        @quantity_asset = null
        @quantity_precision = 0
        @base_symbol = ''
        @base_asset = null
        @base_precision = 0
        @inverted = true
        @url = ''
        @inverted_url = ''
        @price_symbol = ''
        @bid_depth = 0.0
        @ask_depth = 0.0
        @avg_price_24h = 0.0
        @median_price = 0.0
        @assets_by_id = null
        @shorts_available = false
        @error = {title: null, text: null}

    get_actual_market: ->
        return @ if !@inverted
        return @actual_market if @actual_market
        m = new Market()
        m.name = "#{@base_symbol}:#{@quantity_symbol}"
        m.quantity_symbol = @base_symbol
        m.quantity_asset = @base_asset
        m.quantity_precision = @base_precision
        m.base_symbol = @quantity_symbol
        m.base_asset = @quantity_asset
        m.base_precision = @quantity_precision
        m.shorts_available = m.base_asset.id == 0
        m.inverted = null
        m.url = @inverted_url
        m.inverted_url = @url
        m.price_symbol = "#{@quantity_symbol}/#{@base_symbol}"
        m.bid_depth = @bid_depth
        m.ask_depth = @ask_depth
        m.avg_price_24h = @avg_price_24h
        m.median_price = @median_price
        m.assets_by_id = @assets_by_id
        m.error = @error
        @actual_market = m
        return @actual_market


class MarketHelper

    get_array_element_by_id: (array, id) ->
        for index, value of array
            return value if value.id == id
        return null

    remove_array_element_by_id: (array, id) ->
        for index, value of array
            if value.id == id
                array.splice(index, 1)
                break

    ratio_to_price: (value, assets) ->
        return 0.0 if value.base_asset_id == 0 and value.quote_asset_id == 0
        ba = assets[value.base_asset_id]
        qa = assets[value.quote_asset_id]
        return value.ratio * (ba.precision / qa.precision)

    read_market_data: (market, data, assets) ->
        ba = assets[data.base_id]
        market.bid_depth = data.bid_depth / ba.precision
        market.ask_depth = data.ask_depth / ba.precision
        market.avg_price_24h = @ratio_to_price(data.avg_price_24h, assets)
        market.avg_price_24h = 1.0 / market.avg_price_24h if market.inverted and market.avg_price_24h > 0
        if data.last_error
            market.error.title = data.last_error.message
        else
            market.error.text = market.error.title = null


    order_to_trade_data: (order, qa, ba, invert_price, invert_assets, invert_order_type) ->
        #invert_assets = !invert_assets if order.type == "cover_order"
        td = new TradeData()
        td.type = if invert_order_type then @invert_order_type(order.type) else order.type
        td.type = "margin_order" if order.type == "cover_order"
        td.id = order.market_index.owner
        price = order.market_index.order_price.ratio * (ba.precision / qa.precision)
        td.price = if invert_price then 1.0 / price else price
        if order.type == "cover_order"
            td.cost = order.state.balance / qa.precision
            td.quantity = td.cost * price
            td.collateral = order.collateral / ba.precision
            td.status = "cover"
#        if order.type == "cover"
#            td.cost = order.state.balance / qa.precision
#            td.quantity = if invert_price then td.cost * td.price else td.cost / td.price
#            td.collateral = order.collateral / ba.precision
#            #console.log "--------- cover order: ", order, td
        else
            td.quantity = order.state.balance / ba.precision
            td.cost = td.quantity * price<commit_msg>fixes cover order quantity display<commit_after>class TradeData

    constructor: ->
        @type = null
        @id = null
        @timestamp = null
        @quantity = null
        @price = null
        @cost = 0.0
        @collateral
        @status = null # possible values: canceled, unconfirmed, confirmed, placed

    invert: ->
        td = new TradeData()
        td.type = @type
        td.id = @id
        td.status = @status
        td.timestamp = @timestamp
        td.quantity = @cost
        td.cost = @quantity
        td.collateral = @collateral
        #td.price = if @price and @price > 0.0 then 1.0 / @price else 0.0
        td.price = 1.0 / @price
        return td

    touch: ->
        @timestamp = Date.now()
    expired: ->
        return Date.now() - @timestamp > 20000


class Market

    constructor: ->
        @actual_market = null
        @name = ''
        @quantity_symbol = ''
        @quantity_asset = null
        @quantity_precision = 0
        @base_symbol = ''
        @base_asset = null
        @base_precision = 0
        @inverted = true
        @url = ''
        @inverted_url = ''
        @price_symbol = ''
        @bid_depth = 0.0
        @ask_depth = 0.0
        @avg_price_24h = 0.0
        @median_price = 0.0
        @assets_by_id = null
        @shorts_available = false
        @error = {title: null, text: null}

    get_actual_market: ->
        return @ if !@inverted
        return @actual_market if @actual_market
        m = new Market()
        m.name = "#{@base_symbol}:#{@quantity_symbol}"
        m.quantity_symbol = @base_symbol
        m.quantity_asset = @base_asset
        m.quantity_precision = @base_precision
        m.base_symbol = @quantity_symbol
        m.base_asset = @quantity_asset
        m.base_precision = @quantity_precision
        m.shorts_available = m.base_asset.id == 0
        m.inverted = null
        m.url = @inverted_url
        m.inverted_url = @url
        m.price_symbol = "#{@quantity_symbol}/#{@base_symbol}"
        m.bid_depth = @bid_depth
        m.ask_depth = @ask_depth
        m.avg_price_24h = @avg_price_24h
        m.median_price = @median_price
        m.assets_by_id = @assets_by_id
        m.error = @error
        @actual_market = m
        return @actual_market


class MarketHelper

    get_array_element_by_id: (array, id) ->
        for index, value of array
            return value if value.id == id
        return null

    remove_array_element_by_id: (array, id) ->
        for index, value of array
            if value.id == id
                array.splice(index, 1)
                break

    ratio_to_price: (value, assets) ->
        return 0.0 if value.base_asset_id == 0 and value.quote_asset_id == 0
        ba = assets[value.base_asset_id]
        qa = assets[value.quote_asset_id]
        return value.ratio * (ba.precision / qa.precision)

    read_market_data: (market, data, assets) ->
        ba = assets[data.base_id]
        market.bid_depth = data.bid_depth / ba.precision
        market.ask_depth = data.ask_depth / ba.precision
        market.avg_price_24h = @ratio_to_price(data.avg_price_24h, assets)
        market.avg_price_24h = 1.0 / market.avg_price_24h if market.inverted and market.avg_price_24h > 0
        if data.last_error
            market.error.title = data.last_error.message
        else
            market.error.text = market.error.title = null


    order_to_trade_data: (order, qa, ba, invert_price, invert_assets, invert_order_type) ->
        td = new TradeData()
        td.type = if invert_order_type then @invert_order_type(order.type) else order.type
        td.type = "margin_order" if order.type == "cover_order"
        td.id = order.market_index.owner
        price = order.market_index.order_price.ratio * (ba.precision / qa.precision)
        td.price = if invert_price then 1.0 / price else price
        if order.type == "cover_order"
            cover_price = 1.0 / price
            td.cost = order.state.balance / qa.precision
            td.quantity = td.cost * cover_price
            td.collateral = order.collateral / ba.precision
            td.status = "cover"
        else
            td.quantity = order.state.balance / ba.precision
            td.cost = td.quantity * price
{'fraction_non_alphanumeric': 0.10150674068199841, 'fraction_numerical': 0.012008609946754277, 'mean_word_length': 1.9125701088749587, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1671900', 'n_tokens_mistral': 2878, 'n_tokens_neox': 2696, 'n_words': 622}