@gbozee/ultimate 0.0.2-106 → 0.0.2-108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -78,6 +78,46 @@ export declare function computeSellZones(payload: {
78
78
  exit: number;
79
79
  zones?: number;
80
80
  }): number[];
81
+ export type RawPosition = {
82
+ entry: number;
83
+ quantity: number;
84
+ price_places: string;
85
+ decimal_places?: string;
86
+ };
87
+ export type ConfigOptionType = {
88
+ reduce_ratio: number | string;
89
+ profit_percent: number | string;
90
+ };
91
+ /**
92
+ * This function determines the take profit price for a given position based of the profit percent and the reduce ratio
93
+ * @param payload
94
+ * @returns {
95
+ * tp_price: number;
96
+ * pnl: number;
97
+ * quantity: number;
98
+ * reduce_quantity: number;
99
+ * expected_loss: number;
100
+ * }
101
+ */
102
+ export declare function determineTPSl(payload: {
103
+ sell_ratio?: number;
104
+ positions: {
105
+ long: RawPosition;
106
+ short: RawPosition;
107
+ };
108
+ configs: {
109
+ long: ConfigOptionType;
110
+ short: ConfigOptionType;
111
+ };
112
+ kind: "long" | "short";
113
+ decimal_places?: string;
114
+ }): {
115
+ tp_price: number;
116
+ pnl: number;
117
+ quantity: number;
118
+ reduce_quantity: number;
119
+ expected_loss: number;
120
+ };
81
121
  export type SignalConfigType = {
82
122
  focus: number;
83
123
  budget: number;
@@ -411,6 +451,7 @@ export declare function computeProfitDetail(payload: {
411
451
  max_reward_factor: number;
412
452
  risk: number;
413
453
  };
454
+ pnl: number;
414
455
  reduce_position?: {
415
456
  kind: "long" | "short";
416
457
  entry: number;
@@ -152,8 +152,7 @@ class Signal {
152
152
  support: kind === "long" ? _stop_loss : this.support
153
153
  };
154
154
  const instance = new Signal(derivedConfig);
155
- if (kind === "short") {
156
- }
155
+ if (kind === "short") {}
157
156
  let result = instance.get_bulk_trade_zones({ current_price, kind });
158
157
  return result;
159
158
  return result?.filter((x) => {
@@ -371,8 +370,7 @@ class Signal {
371
370
  kind = "long",
372
371
  raw
373
372
  }) {
374
- if (raw) {
375
- }
373
+ if (raw) {}
376
374
  const margin_range = this.get_margin_range(current_price, kind);
377
375
  let margin_zones = this.get_margin_zones({ current_price });
378
376
  let remaining_zones = margin_zones.filter((x) => JSON.stringify(x) != JSON.stringify(margin_range));
@@ -583,8 +581,7 @@ class Signal {
583
581
  return true;
584
582
  });
585
583
  let total_orders = limit_trades.concat(market_trades);
586
- if (kind === "short") {
587
- }
584
+ if (kind === "short") {}
588
585
  if (this.minimum_size && total_orders.length > 0) {
589
586
  let payload = total_orders;
590
587
  let greater_than_min_size = total_orders.filter((o) => o ? o.quantity >= this.minimum_size : true);
@@ -670,8 +667,7 @@ class Signal {
670
667
  });
671
668
  const multiplier = start - index;
672
669
  const incurred_fees = fees.reduce((a, b) => a + b, 0) + previous_risks.reduce((a, b) => a + b, 0);
673
- if (index === 0) {
674
- }
670
+ if (index === 0) {}
675
671
  let quantity = determine_position_size({
676
672
  entry,
677
673
  stop,
@@ -837,8 +833,7 @@ function extractValue(_param, condition) {
837
833
  try {
838
834
  let value2 = JSON.parse(_param || "[]");
839
835
  param = value2.map((o) => parseFloat(o));
840
- } catch (error) {
841
- }
836
+ } catch (error) {}
842
837
  } else {
843
838
  param = parseFloat(_param);
844
839
  }
@@ -853,8 +848,7 @@ function asCoins(symbol) {
853
848
  if (symbol.toLowerCase().includes("-")) {
854
849
  result = result.split("-")[0];
855
850
  }
856
- if (symbol.toLowerCase() == "usdt-usd") {
857
- }
851
+ if (symbol.toLowerCase() == "usdt-usd") {}
858
852
  let result2 = _type == "usdt" ? symbol.split(result)[0] : result;
859
853
  if (result.includes("-")) {
860
854
  result2 = result;
@@ -1153,6 +1147,47 @@ function computeSellZones(payload) {
1153
1147
  const spread = factor - 1;
1154
1148
  return Array.from({ length: zones }, (_, i) => entry * Math.pow(1 + spread, i));
1155
1149
  }
1150
+ function determineTPSl(payload) {
1151
+ const { sell_ratio = 1, kind, positions, configs, decimal_places } = payload;
1152
+ const long_settings = configs.long;
1153
+ const short_settings = configs.short;
1154
+ const settings = kind === "long" ? long_settings : short_settings;
1155
+ const reverse_kind = kind === "long" ? "short" : "long";
1156
+ const _position = positions[kind];
1157
+ const reverse_position = positions[reverse_kind];
1158
+ const reduce_ratio = settings.reduce_ratio;
1159
+ const notion = _position.entry * _position.quantity;
1160
+ const profit_percent = settings.profit_percent;
1161
+ const places = decimal_places || reverse_position.decimal_places;
1162
+ if (profit_percent) {
1163
+ let quantity = to_f(_position.quantity * sell_ratio, places);
1164
+ const as_float = parseFloat(profit_percent) * sell_ratio;
1165
+ const pnl = to_f(as_float * notion / 100, "%.2f");
1166
+ const diff = pnl / quantity;
1167
+ const tp_price = to_f(kind === "long" ? _position.entry + diff : _position.entry - diff, _position.price_places);
1168
+ const expected_loss = to_f(parseFloat(reduce_ratio) * pnl, "%.2f");
1169
+ let reduce_quantity = 0;
1170
+ if (reverse_position.quantity > 0) {
1171
+ const total_loss = Math.abs(tp_price - reverse_position.entry) * reverse_position.quantity;
1172
+ const ratio = expected_loss / total_loss;
1173
+ reduce_quantity = to_f(reverse_position.quantity * ratio, places);
1174
+ }
1175
+ return {
1176
+ tp_price,
1177
+ pnl,
1178
+ quantity,
1179
+ reduce_quantity,
1180
+ expected_loss
1181
+ };
1182
+ }
1183
+ return {
1184
+ tp_price: 0,
1185
+ pnl: 0,
1186
+ quantity: 0,
1187
+ reduce_quantity: 0,
1188
+ expected_loss: 0
1189
+ };
1190
+ }
1156
1191
  // src/helpers/shared.ts
1157
1192
  function buildConfig(app_config, {
1158
1193
  take_profit,
@@ -1202,8 +1237,7 @@ function buildConfig(app_config, {
1202
1237
  return [];
1203
1238
  }
1204
1239
  const condition = (kind === "long" ? entry > app_config.support : entry >= app_config.support) && stop >= app_config.support * 0.999;
1205
- if (kind === "short") {
1206
- }
1240
+ if (kind === "short") {}
1207
1241
  const result = entry === stop ? [] : condition ? instance.build_entry({
1208
1242
  current_price: entry,
1209
1243
  stop_loss: stop,
@@ -1766,24 +1800,14 @@ function computeProfitDetail(payload) {
1766
1800
  const {
1767
1801
  focus_position,
1768
1802
  strategy,
1803
+ pnl,
1769
1804
  price_places = "%.1f",
1770
1805
  reduce_position,
1771
1806
  decimal_places,
1772
1807
  reverse_position
1773
1808
  } = payload;
1774
1809
  let reward_factor = strategy.reward_factor;
1775
- let risk = strategy.risk;
1776
- if (strategy.max_reward_factor === 0) {
1777
- reward_factor = strategy.reward_factor;
1778
- }
1779
- if (focus_position.avg_qty >= focus_position.quantity && strategy.max_reward_factor) {
1780
- reward_factor = to_f(focus_position.quantity * strategy.max_reward_factor / focus_position.avg_qty, "%.4f");
1781
- } else {
1782
- reward_factor = strategy.reward_factor;
1783
- }
1784
- const full_pnl = reward_factor * risk;
1785
- const profit_percent = to_f(full_pnl * 100 / (focus_position.avg_price * focus_position.avg_qty), "%.4f");
1786
- const pnl = to_f(focus_position.entry * focus_position.quantity * profit_percent / 100, "%.2f");
1810
+ const profit_percent = to_f(pnl * 100 / (focus_position.avg_price * focus_position.avg_qty), "%.4f");
1787
1811
  const diff = pnl / focus_position.quantity;
1788
1812
  const sell_price = to_f(focus_position.kind === "long" ? focus_position.entry + diff : focus_position.entry - diff, price_places);
1789
1813
  let loss = 0;
@@ -2340,6 +2364,7 @@ class Strategy {
2340
2364
  avg_price: focus_position.avg_price,
2341
2365
  avg_qty: focus_position.avg_qty
2342
2366
  },
2367
+ pnl: this.pnl(kind),
2343
2368
  strategy: {
2344
2369
  reward_factor,
2345
2370
  max_reward_factor,
@@ -2441,6 +2466,7 @@ export {
2441
2466
  determine_average_entry_and_size,
2442
2467
  determine_amount_to_sell2 as determine_amount_to_sell,
2443
2468
  determine_amount_to_buy,
2469
+ determineTPSl,
2444
2470
  determineRewardFactor,
2445
2471
  determineOptimumReward,
2446
2472
  createGapPairs,