Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: watchlist/get service #70

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
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
13 changes: 13 additions & 0 deletions src/client/mockWsJsonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@
LookupAlertsResponse,
} from "./types/alertTypes";
import { MarketDepthResponse } from "./services/marketDepthMessageHandler";
import { GetWatchlistResponse } from "./services/getWatchlistMessageHandler";

export default class MockWsJsonClient implements WsJsonClient {
async *accountPositions(_: string): AsyncIterable<PositionsResponse> {

Check warning on line 37 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 37 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 37 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
return yield {
service: "positions",
positions: [],
Expand All @@ -44,14 +45,14 @@
return Promise.resolve(null);
}

cancelAlert(_: number): Promise<CancelAlertResponse> {

Check warning on line 48 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 48 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 48 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
return Promise.resolve({
service: "alerts/cancel",
alerts: [],
});
}

cancelOrder(_: number): Promise<CancelOrderResponse> {

Check warning on line 55 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 55 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 55 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
return Promise.resolve({
service: "cancel_order",
orderId: 123,
Expand All @@ -60,7 +61,7 @@
});
}

async *chart(_: ChartRequestParams): AsyncIterable<ChartResponse> {

Check warning on line 64 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 64 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 64 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
return yield {
service: "chart",
symbol: "",
Expand All @@ -68,7 +69,7 @@
};
}

createAlert(_: CreateAlertRequestParams): Promise<CreateAlertResponse> {

Check warning on line 72 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 72 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 72 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
return Promise.resolve({
service: "alerts/create",
alerts: [],
Expand Down Expand Up @@ -96,7 +97,7 @@
};
}

optionChain(_: string): Promise<OptionChainResponse> {

Check warning on line 100 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 100 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 100 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
return Promise.resolve({
series: [],
service: "optionSeries",
Expand All @@ -104,7 +105,7 @@
}

optionChainDetails(
_: OptionChainDetailsRequest

Check warning on line 108 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 108 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 108 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
): Promise<OptionChainDetailsResponse> {
return Promise.resolve({
service: "option_chain/get",
Expand All @@ -113,7 +114,7 @@
}

async *optionChainQuotes(
_: string

Check warning on line 117 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 117 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 117 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
): AsyncIterable<OptionSeriesQuotesResponse> {
return yield {
series: [],
Expand All @@ -122,7 +123,7 @@
}

async *optionQuotes(
_: OptionQuotesRequestParams

Check warning on line 126 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 126 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 126 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
): AsyncIterable<OptionQuotesResponse> {
return yield {
items: [],
Expand All @@ -131,7 +132,7 @@
}

placeOrder(
_: PlaceLimitOrderRequestParams

Check warning on line 135 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (16.x)

'_' is defined but never used

Check warning on line 135 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (18.x)

'_' is defined but never used

Check warning on line 135 in src/client/mockWsJsonClient.ts

View workflow job for this annotation

GitHub Actions / build (19.x)

'_' is defined but never used
): Promise<PlaceOrderSnapshotResponse> {
return Promise.resolve({
service: "place_order",
Expand Down Expand Up @@ -201,4 +202,16 @@
service: "market_depth",
};
}

watchlist(watchlistId: number): Promise<GetWatchlistResponse> {
return Promise.resolve({
service: "watchlist/get",
watchlist: {
id: watchlistId,
name: "foo",
type: "STATIC",
symbols: ["AAPL", "GOOG", "MSFT"],
},
});
}
}
11 changes: 11 additions & 0 deletions src/client/realWsJsonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ import {
CreateAlertResponse,
LookupAlertsResponse,
} from "./types/alertTypes";
import GetWatchlistMessageHandler, {
GetWatchlistResponse,
} from "./services/getWatchlistMessageHandler";

export const CONNECTION_REQUEST_MESSAGE = {
ver: "27.*.*",
Expand Down Expand Up @@ -111,6 +114,7 @@ const messageHandlers: WebSocketApiMessageHandler<never, any>[] = [
new LoginMessageHandler(),
new SubmitOrderMessageHandler(),
new MarketDepthMessageHandler(),
new GetWatchlistMessageHandler(),
];

export default class RealWsJsonClient implements WsJsonClient {
Expand Down Expand Up @@ -282,6 +286,13 @@ export default class RealWsJsonClient implements WsJsonClient {
return this.dispatchHandler(CancelOrderMessageHandler, orderId).promise();
}

watchlist(watchlistId: number): Promise<GetWatchlistResponse> {
return this.dispatchHandler(
GetWatchlistMessageHandler,
watchlistId
).promise();
}

userProperties(): Promise<UserPropertiesResponse> {
return this.dispatchHandler(
UserPropertiesMessageHandler,
Expand Down
1 change: 1 addition & 0 deletions src/client/services/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export type ApiService =
| "alerts/lookup"
| "optionSeries/quotes"
| "market_depth"
| "watchlist/get"
| "fake"; // testing only
95 changes: 95 additions & 0 deletions src/client/services/getWatchlistMessageHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import WebSocketApiMessageHandler, {
newPayload,
} from "./webSocketApiMessageHandler";
import { RawPayloadRequest, RawPayloadResponse } from "../tdaWsJsonTypes";
import { ApiService } from "./apiService";
import { isObject } from "lodash";

export type GetWatchListResponseItem = {
id: number;
name: string;
type?: string;
symbols?: string[];
};

export type GetWatchlistSnapshotResponse = {
watchlist: GetWatchListResponseItem;
service: "watchlist/get";
};

export type GetWatchlistPatchResponse = {
patches: {
op: string;
path: string;
value: { watchlist: GetWatchListResponseItem } | number | string | string[];
}[];
service: "watchlist/get";
};

export type GetWatchlistResponse = GetWatchlistSnapshotResponse;

export default class GetWatchlistMessageHandler
implements WebSocketApiMessageHandler<number, GetWatchlistResponse | null>
{
buildRequest(watchlistId: number): RawPayloadRequest {
return newPayload({
header: {
service: "watchlist/get",
id: "watchlist/get",
ver: 0,
},
params: { watchlistId },
});
}

parseResponse(message: RawPayloadResponse): GetWatchlistResponse | null {
const [{ header, body }] = message.payload;
switch (header.type) {
case "snapshot": {
if ("watchlist" in body) {
const { watchlist } = body as { watchlist: GetWatchListResponseItem };
return { watchlist, service: "watchlist/get" };
} else {
console.warn(
"Unexpected watchlist/get snapshot response with missing `watchlist` object",
message
);
return null;
}
}
case "patch": {
return parseGetWatchlistPatchResponse(
body as GetWatchlistPatchResponse
);
}
default:
console.warn("Unexpected watchlist/get response", message);
return null;
}
}

service: ApiService = "watchlist/get";
}

function parseGetWatchlistPatchResponse(
body: GetWatchlistPatchResponse
): GetWatchlistResponse {
const { patches } = body as GetWatchlistPatchResponse;
const { path, value } = patches[0];
if (path === "" && isObject(value)) {
const { watchlist } = value as {
watchlist: GetWatchListResponseItem;
};
return { watchlist, service: "watchlist/get" };
}
const watchlist: Record<string, any> = {};
patches.forEach(({ path, value }) => {
const pathParts = path.split("/");
const key = pathParts[pathParts.length - 1];
watchlist[key] = value;
});
return {
watchlist: watchlist as GetWatchListResponseItem,
service: "watchlist/get",
};
}
3 changes: 3 additions & 0 deletions src/client/wsJsonClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
LookupAlertsResponse,
} from "./types/alertTypes";
import { MarketDepthResponse } from "./services/marketDepthMessageHandler";
import { GetWatchlistResponse } from "./services/getWatchlistMessageHandler";

export interface WsJsonClient {
authenticate(): Promise<RawLoginResponseBody | null>;
Expand Down Expand Up @@ -83,4 +84,6 @@ export interface WsJsonClient {
disconnect(): void;

marketDepth(symbol: string): AsyncIterable<MarketDepthResponse>;

watchlist(watchlistId: number): Promise<GetWatchlistResponse>;
}
9 changes: 8 additions & 1 deletion src/example/testApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class TestApp {
logger("bid quotes: %O", stateUpdater.bidQuotes);
}
}

async getWatchlist(watchlistId: number) {
logger(" --- getWatchlist() get watchlist ---");
const watchlist = await this.client.watchlist(watchlistId);
logger("getWatchlist() %O", watchlist);
}
}

async function run() {
Expand All @@ -170,7 +176,8 @@ async function run() {
);
const { client } = await authClient.authenticateWithRetry(token);
const app = new TestApp(client);
await app.marketDepth("/NQ");
await app.getWatchlist(-3);
await app.getWatchlist(126216147);
}

run().catch(console.error);