=begin
#BitMEX API

### REST API for the BitMEX Trading Platform  _If you are building automated tools, please subscribe to the_ _[BitMEX API RSS Feed](https://blog.bitmex.com/api_announcement/feed/) for changes. The feed will be updated_ _regularly and is the most reliable way to get downtime and update announcements._  [View Changelog](/app/apiChangelog)  -  #### Getting Started  Base URI: [https://www.bitmex.com/api/v1](/api/v1)  ##### Fetching Data  All REST endpoints are documented below. You can try out any query right from this interface.  Most table queries accept `count`, `start`, and `reverse` params. Set `reverse=true` to get rows newest-first.  Additional documentation regarding filters, timestamps, and authentication is available in [the main API documentation](/app/restAPI).  _All_ table data is available via the [Websocket](/app/wsAPI). We highly recommend using the socket if you want to have the quickest possible data without being subject to ratelimits.  ##### Return Types  By default, all data is returned as JSON. Send `?_format=csv` to get CSV data or `?_format=xml` to get XML data.  ##### Trade Data Queries  _This is only a small subset of what is available, to get you started._  Fill in the parameters and click the `Try it out!` button to try any of these queries.  - [Pricing Data](#!/Quote/Quote_get)  - [Trade Data](#!/Trade/Trade_get)  - [OrderBook Data](#!/OrderBook/OrderBook_getL2)  - [Settlement Data](#!/Settlement/Settlement_get)  - [Exchange Statistics](#!/Stats/Stats_history)  Every function of the BitMEX.com platform is exposed here and documented. Many more functions are available.  ##### Swagger Specification  [⇩ Download Swagger JSON](swagger.json)  -  ## All API Endpoints  Click to expand a section. 

OpenAPI spec version: 1.2.0
Contact: support@bitmex.com
Generated by: https://github.com/swagger-api/swagger-codegen.git
Swagger Codegen version: 2.4.42-SNAPSHOT

=end

require 'date'

module SwaggerClient
  # Summary of Open and Closed Positions
  class Position
    attr_accessor :account

    attr_accessor :symbol

    attr_accessor :currency

    attr_accessor :underlying

    attr_accessor :quote_currency

    attr_accessor :commission

    attr_accessor :init_margin_req

    attr_accessor :maint_margin_req

    attr_accessor :risk_limit

    attr_accessor :leverage

    attr_accessor :cross_margin

    attr_accessor :deleverage_percentile

    attr_accessor :rebalanced_pnl

    attr_accessor :prev_realised_pnl

    attr_accessor :prev_unrealised_pnl

    attr_accessor :opening_qty

    attr_accessor :open_order_buy_qty

    attr_accessor :open_order_buy_cost

    attr_accessor :open_order_buy_premium

    attr_accessor :open_order_sell_qty

    attr_accessor :open_order_sell_cost

    attr_accessor :open_order_sell_premium

    attr_accessor :current_qty

    attr_accessor :current_cost

    attr_accessor :current_comm

    attr_accessor :realised_cost

    attr_accessor :unrealised_cost

    attr_accessor :gross_open_premium

    attr_accessor :is_open

    attr_accessor :mark_price

    attr_accessor :mark_value

    attr_accessor :risk_value

    attr_accessor :home_notional

    attr_accessor :foreign_notional

    attr_accessor :pos_state

    attr_accessor :pos_cost

    attr_accessor :pos_cross

    attr_accessor :pos_comm

    attr_accessor :pos_loss

    attr_accessor :pos_margin

    attr_accessor :pos_maint

    attr_accessor :init_margin

    attr_accessor :maint_margin

    attr_accessor :realised_pnl

    attr_accessor :unrealised_pnl

    attr_accessor :unrealised_pnl_pcnt

    attr_accessor :unrealised_roe_pcnt

    attr_accessor :avg_cost_price

    attr_accessor :avg_entry_price

    attr_accessor :break_even_price

    attr_accessor :margin_call_price

    attr_accessor :liquidation_price

    attr_accessor :bankrupt_price

    attr_accessor :timestamp

    # Attribute mapping from ruby-style variable name to JSON key.
    def self.attribute_map
      {
        :'account' => :'account',
        :'symbol' => :'symbol',
        :'currency' => :'currency',
        :'underlying' => :'underlying',
        :'quote_currency' => :'quoteCurrency',
        :'commission' => :'commission',
        :'init_margin_req' => :'initMarginReq',
        :'maint_margin_req' => :'maintMarginReq',
        :'risk_limit' => :'riskLimit',
        :'leverage' => :'leverage',
        :'cross_margin' => :'crossMargin',
        :'deleverage_percentile' => :'deleveragePercentile',
        :'rebalanced_pnl' => :'rebalancedPnl',
        :'prev_realised_pnl' => :'prevRealisedPnl',
        :'prev_unrealised_pnl' => :'prevUnrealisedPnl',
        :'opening_qty' => :'openingQty',
        :'open_order_buy_qty' => :'openOrderBuyQty',
        :'open_order_buy_cost' => :'openOrderBuyCost',
        :'open_order_buy_premium' => :'openOrderBuyPremium',
        :'open_order_sell_qty' => :'openOrderSellQty',
        :'open_order_sell_cost' => :'openOrderSellCost',
        :'open_order_sell_premium' => :'openOrderSellPremium',
        :'current_qty' => :'currentQty',
        :'current_cost' => :'currentCost',
        :'current_comm' => :'currentComm',
        :'realised_cost' => :'realisedCost',
        :'unrealised_cost' => :'unrealisedCost',
        :'gross_open_premium' => :'grossOpenPremium',
        :'is_open' => :'isOpen',
        :'mark_price' => :'markPrice',
        :'mark_value' => :'markValue',
        :'risk_value' => :'riskValue',
        :'home_notional' => :'homeNotional',
        :'foreign_notional' => :'foreignNotional',
        :'pos_state' => :'posState',
        :'pos_cost' => :'posCost',
        :'pos_cross' => :'posCross',
        :'pos_comm' => :'posComm',
        :'pos_loss' => :'posLoss',
        :'pos_margin' => :'posMargin',
        :'pos_maint' => :'posMaint',
        :'init_margin' => :'initMargin',
        :'maint_margin' => :'maintMargin',
        :'realised_pnl' => :'realisedPnl',
        :'unrealised_pnl' => :'unrealisedPnl',
        :'unrealised_pnl_pcnt' => :'unrealisedPnlPcnt',
        :'unrealised_roe_pcnt' => :'unrealisedRoePcnt',
        :'avg_cost_price' => :'avgCostPrice',
        :'avg_entry_price' => :'avgEntryPrice',
        :'break_even_price' => :'breakEvenPrice',
        :'margin_call_price' => :'marginCallPrice',
        :'liquidation_price' => :'liquidationPrice',
        :'bankrupt_price' => :'bankruptPrice',
        :'timestamp' => :'timestamp'
      }
    end

    # Attribute type mapping.
    def self.swagger_types
      {
        :'account' => :'Integer',
        :'symbol' => :'String',
        :'currency' => :'String',
        :'underlying' => :'String',
        :'quote_currency' => :'String',
        :'commission' => :'Float',
        :'init_margin_req' => :'Float',
        :'maint_margin_req' => :'Float',
        :'risk_limit' => :'Integer',
        :'leverage' => :'Float',
        :'cross_margin' => :'BOOLEAN',
        :'deleverage_percentile' => :'Float',
        :'rebalanced_pnl' => :'Integer',
        :'prev_realised_pnl' => :'Integer',
        :'prev_unrealised_pnl' => :'Integer',
        :'opening_qty' => :'Integer',
        :'open_order_buy_qty' => :'Integer',
        :'open_order_buy_cost' => :'Integer',
        :'open_order_buy_premium' => :'Integer',
        :'open_order_sell_qty' => :'Integer',
        :'open_order_sell_cost' => :'Integer',
        :'open_order_sell_premium' => :'Integer',
        :'current_qty' => :'Integer',
        :'current_cost' => :'Integer',
        :'current_comm' => :'Integer',
        :'realised_cost' => :'Integer',
        :'unrealised_cost' => :'Integer',
        :'gross_open_premium' => :'Integer',
        :'is_open' => :'BOOLEAN',
        :'mark_price' => :'Float',
        :'mark_value' => :'Integer',
        :'risk_value' => :'Integer',
        :'home_notional' => :'Float',
        :'foreign_notional' => :'Float',
        :'pos_state' => :'String',
        :'pos_cost' => :'Integer',
        :'pos_cross' => :'Integer',
        :'pos_comm' => :'Integer',
        :'pos_loss' => :'Integer',
        :'pos_margin' => :'Integer',
        :'pos_maint' => :'Integer',
        :'init_margin' => :'Integer',
        :'maint_margin' => :'Integer',
        :'realised_pnl' => :'Integer',
        :'unrealised_pnl' => :'Integer',
        :'unrealised_pnl_pcnt' => :'Float',
        :'unrealised_roe_pcnt' => :'Float',
        :'avg_cost_price' => :'Float',
        :'avg_entry_price' => :'Float',
        :'break_even_price' => :'Float',
        :'margin_call_price' => :'Float',
        :'liquidation_price' => :'Float',
        :'bankrupt_price' => :'Float',
        :'timestamp' => :'DateTime'
      }
    end

    # Initializes the object
    # @param [Hash] attributes Model attributes in the form of hash
    def initialize(attributes = {})
      return unless attributes.is_a?(Hash)

      # convert string to symbol for hash key
      attributes = attributes.each_with_object({}) { |(k, v), h| h[k.to_sym] = v }

      if attributes.has_key?(:'account')
        self.account = attributes[:'account']
      else
        self.account = 0
      end

      if attributes.has_key?(:'symbol')
        self.symbol = attributes[:'symbol']
      end

      if attributes.has_key?(:'currency')
        self.currency = attributes[:'currency']
      end

      if attributes.has_key?(:'underlying')
        self.underlying = attributes[:'underlying']
      end

      if attributes.has_key?(:'quoteCurrency')
        self.quote_currency = attributes[:'quoteCurrency']
      end

      if attributes.has_key?(:'commission')
        self.commission = attributes[:'commission']
      else
        self.commission = 0.0
      end

      if attributes.has_key?(:'initMarginReq')
        self.init_margin_req = attributes[:'initMarginReq']
      else
        self.init_margin_req = 0.0
      end

      if attributes.has_key?(:'maintMarginReq')
        self.maint_margin_req = attributes[:'maintMarginReq']
      else
        self.maint_margin_req = 0.0
      end

      if attributes.has_key?(:'riskLimit')
        self.risk_limit = attributes[:'riskLimit']
      else
        self.risk_limit = 0
      end

      if attributes.has_key?(:'leverage')
        self.leverage = attributes[:'leverage']
      else
        self.leverage = 0.0
      end

      if attributes.has_key?(:'crossMargin')
        self.cross_margin = attributes[:'crossMargin']
      end

      if attributes.has_key?(:'deleveragePercentile')
        self.deleverage_percentile = attributes[:'deleveragePercentile']
      else
        self.deleverage_percentile = 0.0
      end

      if attributes.has_key?(:'rebalancedPnl')
        self.rebalanced_pnl = attributes[:'rebalancedPnl']
      else
        self.rebalanced_pnl = 0
      end

      if attributes.has_key?(:'prevRealisedPnl')
        self.prev_realised_pnl = attributes[:'prevRealisedPnl']
      else
        self.prev_realised_pnl = 0
      end

      if attributes.has_key?(:'prevUnrealisedPnl')
        self.prev_unrealised_pnl = attributes[:'prevUnrealisedPnl']
      else
        self.prev_unrealised_pnl = 0
      end

      if attributes.has_key?(:'openingQty')
        self.opening_qty = attributes[:'openingQty']
      else
        self.opening_qty = 0
      end

      if attributes.has_key?(:'openOrderBuyQty')
        self.open_order_buy_qty = attributes[:'openOrderBuyQty']
      else
        self.open_order_buy_qty = 0
      end

      if attributes.has_key?(:'openOrderBuyCost')
        self.open_order_buy_cost = attributes[:'openOrderBuyCost']
      else
        self.open_order_buy_cost = 0
      end

      if attributes.has_key?(:'openOrderBuyPremium')
        self.open_order_buy_premium = attributes[:'openOrderBuyPremium']
      else
        self.open_order_buy_premium = 0
      end

      if attributes.has_key?(:'openOrderSellQty')
        self.open_order_sell_qty = attributes[:'openOrderSellQty']
      else
        self.open_order_sell_qty = 0
      end

      if attributes.has_key?(:'openOrderSellCost')
        self.open_order_sell_cost = attributes[:'openOrderSellCost']
      else
        self.open_order_sell_cost = 0
      end

      if attributes.has_key?(:'openOrderSellPremium')
        self.open_order_sell_premium = attributes[:'openOrderSellPremium']
      else
        self.open_order_sell_premium = 0
      end

      if attributes.has_key?(:'currentQty')
        self.current_qty = attributes[:'currentQty']
      else
        self.current_qty = 0
      end

      if attributes.has_key?(:'currentCost')
        self.current_cost = attributes[:'currentCost']
      else
        self.current_cost = 0
      end

      if attributes.has_key?(:'currentComm')
        self.current_comm = attributes[:'currentComm']
      else
        self.current_comm = 0
      end

      if attributes.has_key?(:'realisedCost')
        self.realised_cost = attributes[:'realisedCost']
      else
        self.realised_cost = 0
      end

      if attributes.has_key?(:'unrealisedCost')
        self.unrealised_cost = attributes[:'unrealisedCost']
      else
        self.unrealised_cost = 0
      end

      if attributes.has_key?(:'grossOpenPremium')
        self.gross_open_premium = attributes[:'grossOpenPremium']
      else
        self.gross_open_premium = 0
      end

      if attributes.has_key?(:'isOpen')
        self.is_open = attributes[:'isOpen']
      end

      if attributes.has_key?(:'markPrice')
        self.mark_price = attributes[:'markPrice']
      else
        self.mark_price = 0.0
      end

      if attributes.has_key?(:'markValue')
        self.mark_value = attributes[:'markValue']
      else
        self.mark_value = 0
      end

      if attributes.has_key?(:'riskValue')
        self.risk_value = attributes[:'riskValue']
      else
        self.risk_value = 0
      end

      if attributes.has_key?(:'homeNotional')
        self.home_notional = attributes[:'homeNotional']
      else
        self.home_notional = 0.0
      end

      if attributes.has_key?(:'foreignNotional')
        self.foreign_notional = attributes[:'foreignNotional']
      else
        self.foreign_notional = 0.0
      end

      if attributes.has_key?(:'posState')
        self.pos_state = attributes[:'posState']
      end

      if attributes.has_key?(:'posCost')
        self.pos_cost = attributes[:'posCost']
      else
        self.pos_cost = 0
      end

      if attributes.has_key?(:'posCross')
        self.pos_cross = attributes[:'posCross']
      else
        self.pos_cross = 0
      end

      if attributes.has_key?(:'posComm')
        self.pos_comm = attributes[:'posComm']
      else
        self.pos_comm = 0
      end

      if attributes.has_key?(:'posLoss')
        self.pos_loss = attributes[:'posLoss']
      else
        self.pos_loss = 0
      end

      if attributes.has_key?(:'posMargin')
        self.pos_margin = attributes[:'posMargin']
      else
        self.pos_margin = 0
      end

      if attributes.has_key?(:'posMaint')
        self.pos_maint = attributes[:'posMaint']
      else
        self.pos_maint = 0
      end

      if attributes.has_key?(:'initMargin')
        self.init_margin = attributes[:'initMargin']
      else
        self.init_margin = 0
      end

      if attributes.has_key?(:'maintMargin')
        self.maint_margin = attributes[:'maintMargin']
      else
        self.maint_margin = 0
      end

      if attributes.has_key?(:'realisedPnl')
        self.realised_pnl = attributes[:'realisedPnl']
      else
        self.realised_pnl = 0
      end

      if attributes.has_key?(:'unrealisedPnl')
        self.unrealised_pnl = attributes[:'unrealisedPnl']
      else
        self.unrealised_pnl = 0
      end

      if attributes.has_key?(:'unrealisedPnlPcnt')
        self.unrealised_pnl_pcnt = attributes[:'unrealisedPnlPcnt']
      else
        self.unrealised_pnl_pcnt = 0.0
      end

      if attributes.has_key?(:'unrealisedRoePcnt')
        self.unrealised_roe_pcnt = attributes[:'unrealisedRoePcnt']
      else
        self.unrealised_roe_pcnt = 0.0
      end

      if attributes.has_key?(:'avgCostPrice')
        self.avg_cost_price = attributes[:'avgCostPrice']
      else
        self.avg_cost_price = 0.0
      end

      if attributes.has_key?(:'avgEntryPrice')
        self.avg_entry_price = attributes[:'avgEntryPrice']
      else
        self.avg_entry_price = 0.0
      end

      if attributes.has_key?(:'breakEvenPrice')
        self.break_even_price = attributes[:'breakEvenPrice']
      else
        self.break_even_price = 0.0
      end

      if attributes.has_key?(:'marginCallPrice')
        self.margin_call_price = attributes[:'marginCallPrice']
      else
        self.margin_call_price = 0.0
      end

      if attributes.has_key?(:'liquidationPrice')
        self.liquidation_price = attributes[:'liquidationPrice']
      else
        self.liquidation_price = 0.0
      end

      if attributes.has_key?(:'bankruptPrice')
        self.bankrupt_price = attributes[:'bankruptPrice']
      else
        self.bankrupt_price = 0.0
      end

      if attributes.has_key?(:'timestamp')
        self.timestamp = attributes[:'timestamp']
      end
    end

    # Show invalid properties with the reasons. Usually used together with valid?
    # @return Array for valid properties with the reasons
    def list_invalid_properties
      invalid_properties = Array.new
      if @account.nil?
        invalid_properties.push('invalid value for "account", account cannot be nil.')
      end

      if @symbol.nil?
        invalid_properties.push('invalid value for "symbol", symbol cannot be nil.')
      end

      invalid_properties
    end

    # Check to see if the all the properties in the model are valid
    # @return true if the model is valid
    def valid?
      return false if @account.nil?
      return false if @symbol.nil?
      true
    end

    # Checks equality by comparing each attribute.
    # @param [Object] Object to be compared
    def ==(o)
      return true if self.equal?(o)
      self.class == o.class &&
          account == o.account &&
          symbol == o.symbol &&
          currency == o.currency &&
          underlying == o.underlying &&
          quote_currency == o.quote_currency &&
          commission == o.commission &&
          init_margin_req == o.init_margin_req &&
          maint_margin_req == o.maint_margin_req &&
          risk_limit == o.risk_limit &&
          leverage == o.leverage &&
          cross_margin == o.cross_margin &&
          deleverage_percentile == o.deleverage_percentile &&
          rebalanced_pnl == o.rebalanced_pnl &&
          prev_realised_pnl == o.prev_realised_pnl &&
          prev_unrealised_pnl == o.prev_unrealised_pnl &&
          opening_qty == o.opening_qty &&
          open_order_buy_qty == o.open_order_buy_qty &&
          open_order_buy_cost == o.open_order_buy_cost &&
          open_order_buy_premium == o.open_order_buy_premium &&
          open_order_sell_qty == o.open_order_sell_qty &&
          open_order_sell_cost == o.open_order_sell_cost &&
          open_order_sell_premium == o.open_order_sell_premium &&
          current_qty == o.current_qty &&
          current_cost == o.current_cost &&
          current_comm == o.current_comm &&
          realised_cost == o.realised_cost &&
          unrealised_cost == o.unrealised_cost &&
          gross_open_premium == o.gross_open_premium &&
          is_open == o.is_open &&
          mark_price == o.mark_price &&
          mark_value == o.mark_value &&
          risk_value == o.risk_value &&
          home_notional == o.home_notional &&
          foreign_notional == o.foreign_notional &&
          pos_state == o.pos_state &&
          pos_cost == o.pos_cost &&
          pos_cross == o.pos_cross &&
          pos_comm == o.pos_comm &&
          pos_loss == o.pos_loss &&
          pos_margin == o.pos_margin &&
          pos_maint == o.pos_maint &&
          init_margin == o.init_margin &&
          maint_margin == o.maint_margin &&
          realised_pnl == o.realised_pnl &&
          unrealised_pnl == o.unrealised_pnl &&
          unrealised_pnl_pcnt == o.unrealised_pnl_pcnt &&
          unrealised_roe_pcnt == o.unrealised_roe_pcnt &&
          avg_cost_price == o.avg_cost_price &&
          avg_entry_price == o.avg_entry_price &&
          break_even_price == o.break_even_price &&
          margin_call_price == o.margin_call_price &&
          liquidation_price == o.liquidation_price &&
          bankrupt_price == o.bankrupt_price &&
          timestamp == o.timestamp
    end

    # @see the `==` method
    # @param [Object] Object to be compared
    def eql?(o)
      self == o
    end

    # Calculates hash code according to all attributes.
    # @return [Fixnum] Hash code
    def hash
      [account, symbol, currency, underlying, quote_currency, commission, init_margin_req, maint_margin_req, risk_limit, leverage, cross_margin, deleverage_percentile, rebalanced_pnl, prev_realised_pnl, prev_unrealised_pnl, opening_qty, open_order_buy_qty, open_order_buy_cost, open_order_buy_premium, open_order_sell_qty, open_order_sell_cost, open_order_sell_premium, current_qty, current_cost, current_comm, realised_cost, unrealised_cost, gross_open_premium, is_open, mark_price, mark_value, risk_value, home_notional, foreign_notional, pos_state, pos_cost, pos_cross, pos_comm, pos_loss, pos_margin, pos_maint, init_margin, maint_margin, realised_pnl, unrealised_pnl, unrealised_pnl_pcnt, unrealised_roe_pcnt, avg_cost_price, avg_entry_price, break_even_price, margin_call_price, liquidation_price, bankrupt_price, timestamp].hash
    end

    # Builds the object from hash
    # @param [Hash] attributes Model attributes in the form of hash
    # @return [Object] Returns the model itself
    def build_from_hash(attributes)
      return nil unless attributes.is_a?(Hash)
      self.class.swagger_types.each_pair do |key, type|
        if type =~ /\AArray<(.*)>/i
          # check to ensure the input is an array given that the attribute
          # is documented as an array but the input is not
          if attributes[self.class.attribute_map[key]].is_a?(Array)
            self.send("#{key}=", attributes[self.class.attribute_map[key]].map { |v| _deserialize($1, v) })
          end
        elsif !attributes[self.class.attribute_map[key]].nil?
          self.send("#{key}=", _deserialize(type, attributes[self.class.attribute_map[key]]))
        end # or else data not found in attributes(hash), not an issue as the data can be optional
      end

      self
    end

    # Deserializes the data based on type
    # @param string type Data type
    # @param string value Value to be deserialized
    # @return [Object] Deserialized data
    def _deserialize(type, value)
      case type.to_sym
      when :DateTime
        DateTime.parse(value)
      when :Date
        Date.parse(value)
      when :String
        value.to_s
      when :Integer
        value.to_i
      when :Float
        value.to_f
      when :BOOLEAN
        if value.to_s =~ /\A(true|t|yes|y|1)\z/i
          true
        else
          false
        end
      when :Object
        # generic object (usually a Hash), return directly
        value
      when /\AArray<(?<inner_type>.+)>\z/
        inner_type = Regexp.last_match[:inner_type]
        value.map { |v| _deserialize(inner_type, v) }
      when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
        k_type = Regexp.last_match[:k_type]
        v_type = Regexp.last_match[:v_type]
        {}.tap do |hash|
          value.each do |k, v|
            hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
          end
        end
      else # model
        temp_model = SwaggerClient.const_get(type).new
        temp_model.build_from_hash(value)
      end
    end

    # Returns the string representation of the object
    # @return [String] String presentation of the object
    def to_s
      to_hash.to_s
    end

    # to_body is an alias to to_hash (backward compatibility)
    # @return [Hash] Returns the object in the form of hash
    def to_body
      to_hash
    end

    # Returns the object in the form of hash
    # @return [Hash] Returns the object in the form of hash
    def to_hash
      hash = {}
      self.class.attribute_map.each_pair do |attr, param|
        value = self.send(attr)
        next if value.nil?
        hash[param] = _to_hash(value)
      end
      hash
    end

    # Outputs non-array value in the form of hash
    # For object, use to_hash. Otherwise, just return the value
    # @param [Object] value Any valid value
    # @return [Hash] Returns the value in the form of hash
    def _to_hash(value)
      if value.is_a?(Array)
        value.compact.map { |v| _to_hash(v) }
      elsif value.is_a?(Hash)
        {}.tap do |hash|
          value.each { |k, v| hash[k] = _to_hash(v) }
        end
      elsif value.respond_to? :to_hash
        value.to_hash
      else
        value
      end
    end

  end
end
