更新日:2024/1/23
JSTrader型定義
/* 下記はインジケーターの変更と同期して更新される */
type ItemIndicatorNameTypeMap =Record<string,[Record<string,unknown>,Record<string,unknown>]>
type ContainerIndicatorNameTypeMap =Record<string,[Record<string,unknown>,Record<string,unknown>]>
type ItemIndicatorNames = keyof ItemIndicatorNameTypeMap;
type ContainerIndicatorNames = keyof ContainerIndicatorNameTypeMap;
interface Number {
toDateString(): string
}
declare class IndicatorBuffer<Entry = unknown> {
get(index: number): number;
set(index: number, value: number, entry?: Entry): void;
getByTime(time: number): number;
reduce(
f: (acc: number, val: number) => number,
startIndex: number,
endIndex: number,
): number;
min(startIndex?: number, endIndex?: number): number;
max(startIndex?: number, endIndex?: number): number;
sum(startIndex?: number, endIndex?: number): number;
avg(startIndex?: number, endIndex?: number): number;
sma(buffer: IndicatorBuffer, maPeriod: number, index: number): number;
ema(
buffer: IndicatorBuffer,
maPeriod: number,
index: number,
offsetIndex?: number,
): number;
getContinuousDataFirstIndex(index: number): number;
}
type CharCode = number;
type CharCodes = CharCode[];
type TextBufferEntry = Omit<TextBufferOption, "fontSize"> & {
text:
| string
| CharCode
| CharCodes /* numberは1文字の文字コード、numbersは文字コードの配列 */;
};
declare class TextBuffer extends IndicatorBuffer<TextBufferEntry> {
set(index: number, val: number, x: TextBufferEntry): void;
charCodeToText(charCode: CharCode | CharCodes[]): string;
getText(index: number): string;
}
type LineBufferOption = {
lineColor?: string;
lineWidth?: number;
lineDash?: number[];
};
type TextBufferOption = {
arrow?: boolean;
backgroundColor?: string;
borderColor?: string;
borderWidth?: number;
color?: string;
fontFamily?: string;
fontSize?:
| number
| { min?: number; max?: number }
| ((currentColumnWidth: number) => number) /*0はauto(columnWidth) */;
padding?: number;
position?: "center" | "top" | "bottom" | "left" | "right";
};
type HistgramBuffrOption = {
backgroundColor?: string;
borderColor?: string;
baseValue?: number;
};
type CandleStickBufferOption = {
bodyColor?: string | [string, string?, string?];
shadowColor?: string | [string, string?, string?];
lineWidth?: number | [number, number?, number?];
shadowWidth?: number | [number, number?, number?];
};
type Period = 60 | 300 | 900 | 1800 | 3600 | 14400 | 43200 | 86400;
declare class MarketSymbol {
name: string;
digit: number;
point: number;
spread: number;
}
declare class ChartManager {
charts: Chart[];
}
declare class Chart {
period: Period;
times: IndicatorBuffer;
opens: IndicatorBuffer;
highs: IndicatorBuffer;
lows: IndicatorBuffer;
closes: IndicatorBuffer;
spreads: IndicatorBuffer;
barWidth: number;
symbol: MarketSymbol;
chartManager: ChartManager;
mainContainerIndicator: AddContainerIndicatorReturn<'builtin.DefaultCandleStick'>;
containerIndicators: ContainerIndicator[];
addContainerIndicator: AddContainerIndicator;
}
declare class IndicatorUtil {
static getParameter<
IndicatorName extends ItemIndicatorNames | ContainerIndicatorNames,
>(
indicatorName: IndicatorName,
): IndicatorName extends ItemIndicatorNames
? ItemIndicatorNameTypeMap[IndicatorName][0]
: IndicatorName extends ContainerIndicatorNames
? ContainerIndicatorNameTypeMap[IndicatorName][0]
: never;
}
declare class OpenPosition {
direction: "buy" | "sell";
lot: number;
}
type OnChartChangeParams = {
index: number;
times: IndicatorBuffer;
opens: IndicatorBuffer;
highs: IndicatorBuffer;
lows: IndicatorBuffer;
closes: IndicatorBuffer;
spreads: IndicatorBuffer;
isBarChanged: boolean;
};
type TradingTool<
Params extends Record<string, any> = {},
ThisProps extends Record<string, any> = {},
> = {
type: "i" | "c" | "s";
name: string;
onInit(): void;
params?: Params;
} & ThisType<
{
name: string;
shortName: string;
params: Params;
chart: Chart;
symbol: MarketSymbol;
digit: number;
zIndex: number;
onChartChange(params: OnChartChangeParams): void;
} & ThisProps
>;
type Indicator<
Params extends Record<string, any> = {},
ThisProps extends Record<string, any> = {},
> = TradingTool<
Params,
{
addLineBuffer(param?: string | LineBufferOption): IndicatorBuffer;
addTextBuffer(param?: TextBufferOption): TextBuffer;
addCandleStickBuffer(
param?: string | [string, string?, string?] | CandleStickBufferOption,
): [IndicatorBuffer, IndicatorBuffer, IndicatorBuffer, IndicatorBuffer];
addHistgramBuffer(param?: HistgramBuffrOption): IndicatorBuffer;
addWorkBuffer(): IndicatorBuffer;
getDisplayData(index: number): { autoDigitScaling?: boolean } & Record<string, string | number | boolean | Array<string | number | boolean>>
getMin(lows?: IndicatorBuffer, spreads?: IndicatorBuffer): number;
getMax(highs?: IndicatorBuffer, spreads?: IndicatorBuffer): number;
} & ThisProps
> & {
type: "i" | "c";
};
type StrictPropertyCheck<T, TExpected> = keyof T extends keyof TExpected
? T
: never;
type PartialRequired<T, K extends keyof T> = Omit<T, K> & {
[P in K]-?: T[P]
}
type ItemIndicatorOverrideParams<IndicatorName extends ItemIndicatorNames> = Partial<
ItemIndicatorNameTypeMap[IndicatorName][0] & {
zIndex?: number
}>
type ContainerIndicatorOverrideParams<IndicatorName extends ContainerIndicatorNames> = Partial<
ContainerIndicatorNameTypeMap[IndicatorName][0] & {
zIndex?: number
}>
type AddIndicatorReturnAddtionalProps = {
addItemIndicator: AddItemIndicator
buffers: IndicatorBuffer[]
getDisplayData(index: number): { autoDigitScaling?: boolean } & Record<string, string | number | boolean | Array<string | number | boolean>>
getMin(lows?: IndicatorBuffer, spreads?: IndicatorBuffer): number;
getMax(highs?: IndicatorBuffer, spreads?: IndicatorBuffer): number;
}
type AddItemIndicator = <
IndicatorName extends ItemIndicatorNames,
OverrideParams extends
ItemIndicatorOverrideParams<IndicatorName>
= ItemIndicatorOverrideParams<IndicatorName>
>(
indicatorName: IndicatorName,
overrideParams?: StrictPropertyCheck<OverrideParams, ItemIndicatorOverrideParams<IndicatorName>>,
) => AddItemIndicatorReturn<IndicatorName>
type AddItemIndicatorReturn<IndicatorName extends ItemIndicatorNames> =
PartialRequired<
ItemIndicator<
ItemIndicatorNameTypeMap[IndicatorName][0],
ItemIndicatorNameTypeMap[IndicatorName][1]
>,
'params'> &
ItemIndicatorNameTypeMap[IndicatorName][1] &
AddIndicatorReturnAddtionalProps
type AddContainerIndicator = <
IndicatorName extends ContainerIndicatorNames,
OverrideParams extends
ContainerIndicatorOverrideParams<IndicatorName>
= ContainerIndicatorOverrideParams<IndicatorName>
>(
indicatorName: IndicatorName,
overrideParams?: StrictPropertyCheck<OverrideParams, ContainerIndicatorOverrideParams<IndicatorName>>,
) => AddContainerIndicatorReturn<IndicatorName>
type AddContainerIndicatorReturn<
IndicatorName extends ContainerIndicatorNames,
> = PartialRequired<
ContainerIndicator<
ContainerIndicatorNameTypeMap[IndicatorName][0],
ContainerIndicatorNameTypeMap[IndicatorName][1]
>, 'params'> &
ContainerIndicatorNameTypeMap[IndicatorName][1] &
AddIndicatorReturnAddtionalProps
type ItemIndicator<
Params extends Record<string, any> = {},
ThisProps extends Record<string, any> = {},
> = Indicator<
Params,
{
addItemIndicator: AddItemIndicator;
} & ThisProps
> & {
type: "i";
};
type ContainerIndicator<
Params extends Record<string, any> = {},
ThisProps extends Record<string, any> = {},
> = Indicator<
Params,
{
isMain: boolean;
initialHeight: number | string;
addItemIndicator: AddItemIndicator;
isShowDefaultRountNumber: boolean;
addPercentageLine(
value: number | number[],
color?: string,
width?: number,
lineDash?: number[],
): void;
} & ThisProps
> & {
type: "c";
};
type EntryOrderProps = {
orderType: "market";
direction: "buy" | "sell";
lot: number;
hedging?: boolean;
limit?: number;
stop?: number;
};
type ExitOrderProps = {
orderType: "market" | "limit" | "stop";
openPosition: OpenPosition;
lot: number;
exitPrice?: number;
};
type Strategy<
Params extends Record<string, any> = {},
ThisProps extends Record<string, any> = {},
> = TradingTool<
Params,
ThisProps & {
entry(props: EntryOrderProps): OpenPosition;
exit(props: ExitOrderProps): void;
}
> & {
type: "s";
};