mybudget-ws / mybudget

Мой бюджет - домашняя бухгалтерия
https://2.mybudget.ws
Open Software License 3.0
4 stars 0 forks source link

Implement logic for real orders by signals via api #280

Closed blackchestnut closed 1 year ago

blackchestnut commented 2 years ago

Для закрытия открытых ордеров можно использовать вот это (пример):

S::Deal.last(3).select { |v| v.start_order.completed? }.map { |v| [v.id, v.signal.symbol.name, v.start_order.id] }
blackchestnut commented 2 years ago
  1. client.share_by id_type: InvestTinkoff::V2::InstrumentIdType::TICKER, id: 'MAC', class_code: 'SPBXM' a. Пример для сигнала: b. client.share_by id_type: InvestTinkoff::V2::InstrumentIdType::TICKER, id: s.symbol.name, class_code: 'SPBXM'
  2. цена из стакана на покупку - client.order_book(figi: 'BBG000BL9C59', depth: 1).payload['bids'][0]
  3. создание заявки - client.create_order account_id: '20000xxxxx', figi: 'BBG000BL9C59', quantity: 1, price: 11.35, direction: InvestTinkoff::V2::OrderDirection::BUY, order_type: InvestTinkoff:: V2::OrderType::LIMIT, order_id: s.id.to_s

Чтобы проверить, что с ордером:

client.order_state account_id: '20000xxxxx', order_id: '422315101350'
# => 
{"orderId"=>"422315101350",
   "executionReportStatus"=>"EXECUTION_REPORT_STATUS_NEW",
   "lotsRequested"=>"1",
   "lotsExecuted"=>"0",
   "initialOrderPrice"=>{"currency"=>"usd", "units"=>"11", "nano"=>350000000},
   "executedOrderPrice"=>{"currency"=>"usd", "units"=>"0", "nano"=>0},
   "totalOrderAmount"=>{"currency"=>"usd", "units"=>"11", "nano"=>350000000},
   "averagePositionPrice"=>{"currency"=>"usd", "units"=>"0", "nano"=>0},
   "initialCommission"=>{"currency"=>"usd", "units"=>"0", "nano"=>10000000},
   "executedCommission"=>{"currency"=>"usd", "units"=>"0", "nano"=>0},
   "figi"=>"BBG000BL9C59",
   "direction"=>"ORDER_DIRECTION_BUY",
   "initialSecurityPrice"=>{"currency"=>"usd", "units"=>"11", "nano"=>350000000},
   "stages"=>[],
   "serviceCommission"=>{"currency"=>"rub", "units"=>"0", "nano"=>0},
   "currency"=>"USD",
   "orderType"=>"ORDER_TYPE_LIMIT",
   "orderDate"=>"2022-06-06T20:27:16.804920Z"}

Проверка исполнения ордера:

r = client.order_state account_id: '20000xxxxx', order_id: '422315101350'
r.payload['lotsRequested'] == r.payload['lotsExecuted']

Отмена ордера:

client.cancel_order account_id: '20000xxxx', order_id: '422315101350'
# => #<struct InvestTinkoff::V2::Response payload={"time"=>"2022-06-06T17:56:26.710960Z"}, http_code=200>