All files / src/utils ib.helper.ts

100% Statements 30/30
100% Branches 18/18
100% Functions 7/7
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 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 1119x   9x                 9x                                                                       9x                     8x   8x   8x 49x 7x 7x 1x   7x 7x 1x   7x 6x           8x 2x           9x 9x 10x 1x     9x     9x 2x   9x 8x     9x         25x      
import * as IB from "@stoqey/ib";
import {MapExt} from "@waytrade/microservice-core";
import {AccountSummary} from "../models/account-summary.model";
import {MarketData} from "../models/market-data.model";
 
/**
 * Account summary tag values, make suer this is in sync with
 * AccountSummary model.
 *
 * Ecented AccountSummary model if you extend this!!
 */
export const ACCOUNT_SUMMARY_TAGS = [
  "AccountType",
  "NetLiquidation",
  "TotalCashValue",
  "SettledCash",
  "AccruedCash",
  "BuyingPower",
  "EquityWithLoanValue",
  "PreviousEquityWithLoanValue",
  "GrossPositionValue",
  "RegTEquity",
  "RegTMargin",
  "InitMarginReq",
  "SMA",
  "InitMarginReq",
  "MaintMarginReq",
  "AvailableFunds",
  "ExcessLiquidity",
  "Cushion",
  "FullInitMarginReq",
  "FullMaintMarginReq",
  "FullAvailableFunds",
  "FullExcessLiquidity",
  "LookAheadNextChange",
  "LookAheadInitMarginReq",
  "LookAheadMaintMarginReq",
  "LookAheadAvailableFunds",
  "LookAheadExcessLiquidity",
  "HighestSeverity",
  "DayTradesRemaining",
  "Leverage",
];
 
/**
 * Collection of helper functions used by IBApiService.
 */
export class IBApiServiceHelper {
  /**
   * Collect account summary tag values to map of account ids and
   * AccountSummary models.
   */
  static colllectAccountSummaryTagValues(
    accountId: string,
    tagValues: IB.AccountSummaryTagValues,
    baseCurrency: string,
    all: MapExt<string, AccountSummary>,
  ): void {
    const accountSummary = all.getOrAdd(
      accountId,
      () => new AccountSummary({account: accountId}),
    );
    tagValues.forEach((summaryValues, key) => {
      if (ACCOUNT_SUMMARY_TAGS.find(v => v === key)) {
        let value = summaryValues.get(baseCurrency);
        if (!value) {
          value = summaryValues.get("");
        }
        let val: number | string | undefined = Number(value?.value);
        if (isNaN(val)) {
          val = value?.value;
        }
        if (val !== undefined) {
          ((<unknown>accountSummary) as Record<string, unknown>)[
            key[0].toLowerCase() + key.substr(1)
          ] = val;
        }
      }
    });
    if (Object.keys(accountSummary).length === 1) {
      all.delete(accountId);
    }
  }
 
  /** Convert IB.MarketDataTicks to MarketData model.  */
  static marketDataTicksToModel(data: IB.MarketDataTicks): MarketData {
    const result: Record<string, number | undefined> = {};
    data.forEach((value, type) => {
      if (!value.value) {
        return;
      }
      let propName =
        type > IB.IBApiNextTickType.API_NEXT_FIRST_TICK_ID
          ? IB.IBApiNextTickType[type]
          : IB.IBApiTickType[type];
      if (propName?.startsWith("DELAYED_")) {
        propName = propName.substr("DELAYED_".length);
      }
      if (propName) {
        result[propName] = value.value;
      }
    });
    return result;
  }
 
  /** Format a possition id. */
  static formatPositionId(accountId: string, conId?: number): string {
    return `${accountId}:${conId}`;
  }
}