quantengine
Version:
QuantEngine SDK Definition
630 lines (441 loc) • 14.3 kB
TypeScript
import {AxiosInstance} from "axios";
import {client, w3cwebsocket} from "websocket";
// import {Telegraf as Tgf} from 'telegraf';
import CryptoJS = require ("crypto-js");
import DecimalTS from "decimal.js";
import Web3DT from "web3";
import momentDT from "moment";
import EE = require("events");
// import * as Decimal from 'decimal.js';
// declare module 'Decimal' {}
//
// export as namespace Decimal;
// export {
// // Decimal
// };
declare class TCommasAPI {
constructor(opts: { url?: string, apiKey: string, apiSecret: string, forcedMode?: "real" });
public generateSignature(requestUri, reqData): string;
public makeRequest(method, path, params): Promise<any>;
/**
* Deals methods
*/
public getDeals(params?): Promise<any>;
public dealUpdateMaxSafetyOrders(deal_id, max_safety_orders): Promise<any>;
public dealPanicSell(deal_id): Promise<any>;
public dealCancel(deal_id): Promise<any>;
public dealUpdateTp(deal_id, new_take_profit_percentage): Promise<any>;
public getDeal(deal_id): Promise<any>;
public getDealSafetyOrders(deal_id): Promise<any>;
public dealAddFunds(params?): Promise<any>;
/**
* Bots methods
*/
public getBotsBlackList(): Promise<any>;
public botsUpdateBlackList(params?): Promise<any>;
public botCreate(params?): Promise<any>;
public getBots(params?): Promise<any>;
public getBotsStats(params?): Promise<any>;
public botUpdate(params?): Promise<any>;
public botDisable(bot_id): Promise<any>;
public botEnable(bot_id): Promise<any>;
public botStartNewDeal(params?): Promise<any>;
public botDelete(bot_id): Promise<any>;
public botPaniceSellAllDeals(bot_id): Promise<any>;
public botCancelAllDeals(bot_id): Promise<any>;
public botShow(bot_id): Promise<any>;
/**
* Smart Trades methods
*/
public smartTradesCreateSimpleSell(params?): Promise<any>;
public smartTradesCreateSimpleBuy(params?): Promise<any>;
public smartTradesCreateSmartSell(params?): Promise<any>;
public smartTradesCreateSmartCover(params?): Promise<any>;
public smartTradesCreateSmartTrade(params?): Promise<any>;
public smartTrades(params?): Promise<any>;
public smartTradesV2(params?): Promise<any>;
public smartTradesStepPanicSell(params?): Promise<any>;
public smartTradesUpdate(params?): Promise<any>;
public smartTradesCancel(smart_trade_id): Promise<any>;
public smartTradesPanicSell(smart_trade_id): Promise<any>;
public smartTradesForceProcess(smart_trade_id): Promise<any>;
/**
* Accounts methods
*/
public accountsNew(params?): Promise<any>;
public accounts(): Promise<any>;
public accountsMarketList(): Promise<any>;
public accountsCurrencyRates(): Promise<any>;
public accountSellAllToUsd(account_id): Promise<any>;
public accountSellAllToBtc(account_id): Promise<any>;
public accountLoadBalances(account_id): Promise<any>;
public accountRename(params?): Promise<any>;
public accountPieChartData(account_id): Promise<any>;
public accountTableData(account_id): Promise<any>;
public accountRemove(account_id): Promise<any>;
}
declare class Log {
public tags(...tags: string []): Log
public info(...args: any []): void;
public warn(...args: any []): void;
public error(...args: any []): void;
public success(...args: any []): void;
public debug(...args: any []): void;
}
interface UserStorage {
Set(key: string, value: string): Promise<boolean>;
Get(key: string): Promise<string | null>;
}
interface StrategyStorage {
Set(key: string, value: string): Promise<boolean>;
Get(key: string): Promise<string | null>;
}
interface Table {
Update(): Promise<boolean>;
}
interface BotWebhook {
// Get(channel?: number): Promise<any>;
OnMessage(callback: (message: string, channel: number) => void, channel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9): boolean;
}
interface StrategyWebhook {
OnMessage(callback: (message: string, channel: number) => void, channel?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9): boolean;
}
interface BaseExchange {
currentContract: ContractData;
getTime(): Promise<number>
//////////////////////////// 行情API ////////////////////////////
getTicker(symbol?: string): Promise<TickerData>
getDepth(limit?: number, symbol?: string): Promise<Array<DepthData>>
getTrades(symbol?: string): Promise<Array<TradeData>>
getKlines(limit?: number, interval?: string, symbol?: string): Promise<Array<KlineData>>
getHistoryKlines(start: number, end: number, interval?: string, symbol?: string): Promise<Array<KlineData>>
getPeriod(): Promise<string>
setMaxBarLen(len?: number): Promise<boolean>
//////////////////////////// 交易API ////////////////////////////
buy(price: number, amount: number, symbol?: string): Promise<OrderData>
sell(price: number, amount: number, symbol?: string): Promise<OrderData>
cancelOrder(id: number, symbol?: string): Promise<boolean>
cancelOrders(orderIds?: Array<number | string | bigint>, symbol?: string): Promise<boolean>
getOrder(orderId?: number | string | bigint, clientOrderId?: string, symbol?: string): Promise<OrderData>
getOrders(symbol?: string): Promise<Array<OrderData>>
// getOpenOrder(orderId?: number, clientOrderId?: string, symbol?: string): Promise<OrderData>
// getOpenOrders(symbol?: string): Promise<Array<OrderData>>
getHistoryTrades(start?: number, end?: number, symbol?: string): Promise<any>
setPrecision(pricePrecision: number, amountPrecision: number): Promise<boolean>
customIO(type: string, httpMethod: string, path: string, params: object, row: string, signed: number): Promise<any>
customAPI(httpMethod: string, path: string, params: object, signed: number): Promise<any>
//////////////////////////// 账号API ////////////////////////////
getAccount(): Promise<AccountData>
getName(): Promise<string>
getSymbol(): Promise<string>
setSymbol(symbol: string): Promise<boolean>
//////////////////////////// 期货API ////////////////////////////
getPosition(symbol?: string): Promise<PositionData>
setLeverageLevel(level: number, symbol?: string): Promise<boolean | number>
setDirection(direction: string): Promise<boolean>
// setContractType(type: string): Promise<boolean>
// getContractType(): Promise<string>
}
interface AbstractNotice {
sendText(content: string): Promise<any>
}
////////////////// 行情相关 //////////////////
export interface TickerData {
Symbol: string,
Open: number,
High: number,
Low: number,
LastPrice: number,
LastTime: number,
LastVolume: number,
Raw?: any
}
export interface KlineData {
Time: number,
Open: number,
High: number,
Low: number,
Close: number,
Volume: number
}
export interface TradeData {
Id: number,
Time: number,
Price: number,
Amount: number,
Type: string,
}
export interface UserTradeData {
Id: number,
OrderId: number,
IsMaker: boolean,
Commission: number,
CommissionAsset: string,
RealizedPnl: number,
Price: number,
Amount: number,
Side: string,
PositionSide: string,
Time: number,
Symbol: string
}
export interface DepthData {
Asks: Array<DepthDetail>,
Bids: Array<DepthDetail>,
Time?: number
}
interface DepthDetail {
Price: number,
Amount: number,
}
export interface ContractData {
Symbol: string,
BaseCurrency: string,
QuoteCurrency: string,
Name: string,
PriceTick: number,
MinVolume: number
}
////////////////// 交易相关 //////////////////
export interface OrderData {
Id: number | string | bigint,
ClientOrderId?: string,
Price: number,
Amount: number,
DealAmount: number
AvgPrice: number,
Status: string,
Type: string,
PositionType: string,
PositionSide: string,
Create?: string,
End?: string,
Commission?: number | null,
CommissionAsset?: string | null,
Raw?: any,
}
////////////////// 账号信息相关 //////////////////
export interface AccountData {
Asset: string,
Balance: number,
AvailableBalance?: number,
Stock?: number,
Freezing?: number,
}
export interface PnlData {
RealizedPnl: number,
UnrealizedPnl: number
Fee: number
}
////////////////// 期货相关 //////////////////
export interface PositionData {
Amount: number,
Price: number,
Profit: number,
Leverage: number,
Type: string,
ContractType: string
}
////////////////// 交易所初始化数据 //////////////////
export interface ExchangeParamData {
name: string,
symbol?: string,
key?: string,
secret?: string,
passphrase?: string,
interval?: string,
limit?: number,
url?: string,
proxyHost?: string,
proxyPort?: number
}
////////////////// 通知初始化参数 //////////////////
export interface NoticeParamData {
name: string,
accessToken?: string,
secret?: string,
chatId?: string,
}
////////////////// Indicators //////////////////
export interface TA {
SMA(arr: Array<number>, n: number)
EMA(arr: Array<number>, n: number)
RSI(arr: Array<number>, n: number)
BOLL(arr: Array<number>, n: number, stdDev: number)
KDJ(arr: Array<number>, n: number, m1: number, m2: number)
}
////////////////// Exchange //////////////////
export interface ExchangeInterface {
createExchange(params: ExchangeParamData): BaseExchange
}
////////////////// Notice //////////////////
export interface NoticeInterface {
createNoticeBot(params: NoticeParamData): AbstractNotice
}
////////////////// Common Tools //////////////////
declare global {
interface BotInfo {
id: string;
user_strategy_name: string;
strategy_id: string;
strategy_version: string;
strategy_version_id: string;
}
//Profit Object Definition
interface Profit {
profit: number
percent: number
tables: ProfitTable []
}
interface LastProfit {
profit: number;
percent: number;
update_time: number;
}
interface ProfitTableCol {
label: string
name: string
}
interface ProfitTable {
title: string
cols: ProfitTableCol []
rows: any []
}
//Strategy Base Class
abstract class StrategyBase {
//BotWebhookSwitch
// bot_webhook?: boolean;
//StrategyWebhookSwitch
// strategy_webhook?: boolean;
// config_template: any [];
// @ts-ignore
async main(): Promise<any> {
}
// @ts-ignore
async profit(last_profit?: LastProfit): Promise<Profit> {
}
// // @ts-ignore
// async test(): Promise<any> {
// }
//
// // @ts-ignore
// async cancel(): Promise<any> {
// }
//
// // @ts-ignore
// async stop(): Promise<any> {
// }
}
const BotInfo: BotInfo;
//Axios Http Tool
const axios: AxiosInstance;
const Web3: typeof Web3DT;
//WebsocketClient
const WebSocketClient: typeof client
//W3CWebSocket
const W3CWebSocket: typeof w3cwebsocket
//Decimal Calc Tool
export type Decimal = DecimalTS;
export const Decimal: typeof DecimalTS;
//Moment 时间工具
export const moment: typeof momentDT;
//Sleep Function : Async
function Sleep(time: number): Promise<void>;
// Repeat Method
function Repeat(func, ...args): Promise<any>;
// Line Cross Index Function
function Cross(arr1, arr2): Promise<number>;
//Stop The Program
function Stop(): void;
//SendLog To List
const Log: Log;
//UserConfigObject
const Config: any;
//UserStorageUtil
const UserStorage: UserStorage;
//StrategyStorageUtil
const StrategyStorage: StrategyStorage;
//Bot Scoped Webhook Util
const BotWebhook: BotWebhook;
//Strategy Scoped Webhook Util
const StrategyWebhook: StrategyWebhook;
//TelegramBot
// const Telegraf: typeof Tgf;
//TableUtil
const Table: Table;
const BaseExchange: BaseExchange;
const ExchangeParamData: ExchangeParamData;
const BaseNotice: AbstractNotice;
const NoticeParamData: NoticeParamData;
//Create Exchange
function CreateExchange(params: ExchangeParamData): BaseExchange
// Create Notice bot
function CreateNotice(params: NoticeParamData): AbstractNotice;
const TA: TA;
const Utils: {
Repeat(func, ...args): Promise<void>;
Cross(arr1, arr2): number;
Crossover(arr1, arr2): boolean;
Crossunder(arr1, arr2): boolean;
}
const Exchange: ExchangeInterface
const Notice: NoticeInterface
const ThreeCommasAPI: typeof TCommasAPI;
const EventEmitter: typeof EE;
//ETH Input Data Decoder
type NestedArray<T> = T | NestedArray<T>[];
interface InputData {
method: string | null;
types: string[];
inputs: any[];
names: NestedArray<string>[];
}
class InputDataDecoder {
constructor(abi: any);
decodeConstructor(data: any): InputData;
decodeData(data: any): InputData;
}
const iconv: typeof import("iconv-lite");
interface AbiMethodDateParamItem {
name: string;
value: string;
type: string
}
interface AbiMethodData {
name: string,
params: AbiMethodDateParamItem []
}
interface AbiLogsData {
name: string;
events: string;
address: string;
}
const abiDecoder: {
getABIs(): any [];
addABI(abiArray: any []): any [];
getMethodIDs(): any;
decodeMethod(data: string): AbiMethodData;
decodeLogs(logs: any []): AbiLogsData [];
removeABI(abiArray: any []): void;
}
//系统对象
const Q: {
System: {
Stop: typeof Stop,
Sleep: typeof Sleep
},
Table: typeof Table
Storage: {
User: UserStorage,
Strategy: StrategyStorage
},
Util: {
iconv: typeof import("iconv-lite"),
abiDecoder: typeof abiDecoder
},
Webhook: {
Bot: BotWebhook,
Strategy: StrategyWebhook
}
}
}