Skip to content

Commit

Permalink
return more info for stacked imbalance
Browse files Browse the repository at this point in the history
  • Loading branch information
focus1691 committed Nov 30, 2024
1 parent 9b92cd9 commit a940a3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/constants/orderflow/stackedImbalances.ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export enum ImbalanceType {
SELLING_IMBALANCE = 0,
BUYING_IMBALANCE = 1
export enum ImbalanceSide {
SELLING_IMBALANCE = 'sell',
BUYING_IMBALANCE = 'buy'
}
22 changes: 12 additions & 10 deletions src/lib/orderflow/stackedImbalances/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ImbalanceType } from '../../../constants';
import { ImbalanceSide } from '../../../constants';
import { IStackedImbalanceConfig, IStackedImbalancesResult, Imbalance, OrderFlowRow } from '../../../types/orderflow';

const DEFAULT_THRESHOLD = 300.0;
Expand All @@ -22,14 +22,14 @@ export function detectImbalances(data: { [price: number]: OrderFlowRow }, thresh
if (orderFlowRow.volSumBid >= (threshold / 100) * orderFlowRow.volSumAsk) {
imbalances.push({
price: priceNumber,
imbalanceType: ImbalanceType.SELLING_IMBALANCE,
imbalanceSide: ImbalanceSide.SELLING_IMBALANCE,
volSumAsk: orderFlowRow.volSumAsk,
volSumBid: orderFlowRow.volSumBid
});
} else if (orderFlowRow.volSumAsk >= (threshold / 100) * orderFlowRow.volSumBid) {
imbalances.push({
price: priceNumber,
imbalanceType: ImbalanceType.BUYING_IMBALANCE,
imbalanceSide: ImbalanceSide.BUYING_IMBALANCE,
volSumAsk: orderFlowRow.volSumAsk,
volSumBid: orderFlowRow.volSumBid
});
Expand All @@ -46,12 +46,13 @@ export function detectImbalances(data: { [price: number]: OrderFlowRow }, thresh
* @param stackCount - The minimum number of consecutive imbalances required to form a stack.
* @param stackedImbalances - The array of stacked imbalance ranges to add to.
*/
function addStackRangeIfValid(currentStack: Imbalance[], stackCount: number, stackedImbalances: IStackedImbalancesResult[]): void {
function addStackRangeIfValid(currentStack: Imbalance[], stackCount: number, stackedImbalances: IStackedImbalancesResult[], imbalanceSide: ImbalanceSide): void {
if (currentStack.length >= stackCount) {
stackedImbalances.push({
startPrice: currentStack[0].price,
endPrice: currentStack[currentStack.length - 1].price,
count: currentStack.length
imbalanceStartAt: currentStack[0].price,
imbalanceEndAt: currentStack[currentStack.length - 1].price,
stackedCount: currentStack.length,
imbalanceSide
});
}
}
Expand All @@ -73,20 +74,21 @@ export function detectStackedImbalances(data: { [price: number]: OrderFlowRow },
let lastImbalance: Imbalance | null = null;

for (const imbalance of imbalances) {
const { price: imbalancePrice, imbalanceSide } = imbalance;
const isDiagonallyConsecutive =
lastImbalance === null || (Math.abs(imbalance.price - lastImbalance.price) === tickSize && imbalance.imbalanceType === lastImbalance.imbalanceType);
lastImbalance === null || (Math.abs(imbalancePrice - lastImbalance.price) === tickSize && imbalance.imbalanceSide === lastImbalance.imbalanceSide);

if (isDiagonallyConsecutive) {
currentStack.push(imbalance);
} else {
addStackRangeIfValid(currentStack, stackCount, stackedImbalances);
addStackRangeIfValid(currentStack, stackCount, stackedImbalances, imbalanceSide);
currentStack = [imbalance];
}

lastImbalance = imbalance;
}

addStackRangeIfValid(currentStack, stackCount, stackedImbalances);
addStackRangeIfValid(currentStack, stackCount, stackedImbalances, currentStack[0]?.imbalanceSide);

return stackedImbalances;
}
11 changes: 6 additions & 5 deletions src/types/orderflow/stackedImbalances.types.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { ImbalanceType } from '../../constants';
import { ImbalanceSide } from '../../constants';

export interface Imbalance {
price: number;
imbalanceType: ImbalanceType;
imbalanceSide: ImbalanceSide;
volSumAsk: number;
volSumBid: number;
}

export interface IStackedImbalancesResult {
startPrice: number;
endPrice: number;
count: number;
imbalanceStartAt: number;
imbalanceEndAt: number;
stackedCount: number;
imbalanceSide: ImbalanceSide;
}

export interface IStackedImbalanceConfig {
Expand Down

0 comments on commit a940a3b

Please sign in to comment.