Skip to content
Open
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
6 changes: 3 additions & 3 deletions src/dealing/DealingAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ export interface PositionCreateRequest {
}

export interface PositionCloseRequest {
dealId?: string;
dealId: string;
direction: Direction;
epic?: string;
expiry: string;
level: number;
expiry?: string;
level?: number;
orderType: PositionOrderType;
quoteId?: string;
size: number;
Expand Down
8 changes: 3 additions & 5 deletions src/demo/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ async function main(): Promise<void> {

const closePositionRequest: PositionCloseRequest = {
dealId: confirmSession.dealId,
direction: Direction.SELL,
expiry: createPositionRequest.expiry,
level: 860.4,
orderType: createPositionRequest.orderType,
size: createPositionRequest.size,
direction: Direction.SELL, //ensure opposite direction to close the position
orderType: PositionOrderType.MARKET, //seemingly have to use market otherwise it'll wait for the level
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When providing a different type than MARKET does it mean you will have to specify a level then?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been trying to work this out myself based on the documentation and testing.

It's awkward to test as it isn't 100% clear.

The docs state

"Limit orders get executed at the price seen by IG at the moment of booking a trade. A limit determines the level at which the order or the remainder of the order will be rejected."

I wasn't getting any response when using LIMIT, with various test levels, however i don't know if the close would happen at that particular level. That seems logical, but then a stop level would do the same thing and i don't think IG supports closing at a predefined level any other way.

I made this change off the back of looking at this implementation which always passes "market"

https://github.com/gfiocco/node-ig-api/blob/master/index.js and this discussion here ig-python/trading-ig#181

My assumption was that Market should always be used for the order type when closing. Although the documentation constraints suggest you can enter a LIMIT, it isn't really clear when that close should trigger?

[Constraint: If epic is defined, then set expiry]
[Constraint: If orderType equals LIMIT, then DO NOT set quoteId]
[Constraint: If orderType equals LIMIT, then set level]
[Constraint: If orderType equals MARKET, then DO NOT set level,quoteId]
[Constraint: If orderType equals QUOTE, then set level,quoteId]
[Constraint: Set only one of {dealId,epic}]

size: createPositionRequest.size, //size of existing position / partial size if you want to partially close
};

const closePositionSession = await client.rest.dealing.closePosition(closePositionRequest);
Expand Down