@gbozee/ultimate 0.0.2-next.0 → 0.0.2-next.4

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.
@@ -170,6 +170,16 @@ export type SignalConfigType = {
170
170
  stdDevFactor?: number;
171
171
  lambda?: number;
172
172
  };
173
+ use_progressive_risk?: boolean;
174
+ risk_distribution?: {
175
+ enabled: boolean;
176
+ total_risk_budget: number;
177
+ risk_reward: number;
178
+ buckets: Array<{
179
+ zone_percentage: number;
180
+ risk_percentage: number;
181
+ }>;
182
+ };
173
183
  };
174
184
  declare class Signal {
175
185
  focus: number;
@@ -207,8 +217,18 @@ declare class Signal {
207
217
  long: GetEntriesParams["distribution"];
208
218
  short: GetEntriesParams["distribution"];
209
219
  };
220
+ use_progressive_risk?: boolean;
221
+ risk_distribution?: {
222
+ enabled: boolean;
223
+ total_risk_budget: number;
224
+ risk_reward: number;
225
+ buckets: Array<{
226
+ zone_percentage: number;
227
+ risk_percentage: number;
228
+ }>;
229
+ };
210
230
  max_quantity: number;
211
- constructor({ focus, symbol, budget, percent_change, price_places, decimal_places, zone_risk, fee, support, risk_reward, resistance, risk_per_trade, increase_size, additional_increase, minimum_pnl, take_profit, increase_position, minimum_size, first_order_size, gap, max_size, use_kelly, kelly_prediction_model, kelly_confidence_factor, kelly_minimum_risk, kelly_func, full_distribution, max_quantity, distribution_params, }: SignalConfigType);
231
+ constructor({ focus, symbol, budget, percent_change, price_places, decimal_places, zone_risk, fee, support, risk_reward, resistance, risk_per_trade, increase_size, additional_increase, minimum_pnl, take_profit, increase_position, minimum_size, first_order_size, gap, max_size, use_kelly, kelly_prediction_model, kelly_confidence_factor, kelly_minimum_risk, kelly_func, full_distribution, max_quantity, distribution_params, use_progressive_risk, risk_distribution, }: SignalConfigType);
212
232
  build_entry({ current_price, stop_loss, pnl, stop_percent, kind, risk, no_of_trades, take_profit, distribution, distribution_params, }: {
213
233
  take_profit?: number;
214
234
  no_of_trades?: number;
@@ -223,6 +243,11 @@ declare class Signal {
223
243
  curveFactor?: number;
224
244
  stdDevFactor?: number;
225
245
  lambda?: number;
246
+ use_progressive?: boolean;
247
+ buckets?: Array<{
248
+ zone_percentage: number;
249
+ risk_percentage: number;
250
+ }>;
226
251
  };
227
252
  }): any;
228
253
  get risk(): number;
@@ -273,6 +298,14 @@ declare class Signal {
273
298
  kind?: "long" | "short";
274
299
  }): number[][];
275
300
  get_margin_range(current_price: number, kind?: string): number[];
301
+ /**
302
+ * Helper function to calculate zone-specific risk based on progressive risk distribution
303
+ * @param index - The index of the current zone
304
+ * @param totalZones - Total number of zones
305
+ * @param account - Account configuration with risk settings
306
+ * @returns The risk amount for the specific zone
307
+ */
308
+ private getZoneRisk;
276
309
  process_orders({ current_price, stop_loss, trade_zones, kind, }: {
277
310
  current_price: number;
278
311
  stop_loss: number;
@@ -572,9 +605,15 @@ export type ExtendConfigType = {
572
605
  curveFactor?: number;
573
606
  stdDevFactor?: number;
574
607
  lambda?: number;
608
+ buckets?: Array<{
609
+ zone_percentage: number;
610
+ risk_percentage: number;
611
+ }>;
612
+ use_progressive?: boolean;
575
613
  };
614
+ use_progressive_risk?: boolean;
576
615
  };
577
- export declare function buildConfig(app_config: AppConfig, { take_profit, entry, stop, raw_instance, risk, no_of_trades, min_profit, risk_reward, kind, increase, gap, rr, price_places, decimal_places, use_kelly, kelly_confidence_factor, kelly_minimum_risk, kelly_prediction_model, kelly_func, min_avg_size, distribution, distribution_params, }: ExtendConfigType): any[] | Signal;
616
+ export declare function buildConfig(app_config: AppConfig, { take_profit, entry, stop, raw_instance, risk, no_of_trades, min_profit, risk_reward, kind, increase, gap, rr, price_places, decimal_places, use_kelly, kelly_confidence_factor, kelly_minimum_risk, kelly_prediction_model, kelly_func, min_avg_size, distribution, distribution_params, use_progressive_risk, }: ExtendConfigType): any[] | Signal;
578
617
  export declare function buildAvg({ _trades, kind, }: {
579
618
  _trades: any[];
580
619
  kind: "long" | "short";
@@ -403,6 +403,8 @@ class Signal {
403
403
  long: "arithmetic",
404
404
  short: "geometric"
405
405
  };
406
+ use_progressive_risk;
407
+ risk_distribution;
406
408
  max_quantity = 0.03;
407
409
  constructor({
408
410
  focus,
@@ -433,12 +435,16 @@ class Signal {
433
435
  kelly_func = "theoretical",
434
436
  full_distribution,
435
437
  max_quantity = 0.03,
436
- distribution_params = {}
438
+ distribution_params = {},
439
+ use_progressive_risk,
440
+ risk_distribution
437
441
  }) {
438
442
  if (full_distribution) {
439
443
  this.distribution = full_distribution;
440
444
  }
441
445
  this.distribution_params = distribution_params;
446
+ this.use_progressive_risk = use_progressive_risk;
447
+ this.risk_distribution = risk_distribution;
442
448
  this.symbol = symbol;
443
449
  this.minimum_size = minimum_size;
444
450
  this.first_order_size = first_order_size;
@@ -477,7 +483,13 @@ class Signal {
477
483
  no_of_trades = 1,
478
484
  take_profit,
479
485
  distribution,
480
- distribution_params = {}
486
+ distribution_params = {
487
+ buckets: [
488
+ { zone_percentage: 0.25, risk_percentage: 0.1 },
489
+ { zone_percentage: 0.5, risk_percentage: 0.4 },
490
+ { zone_percentage: 0.25, risk_percentage: 0.5 }
491
+ ]
492
+ }
481
493
  }) {
482
494
  let _stop_loss = stop_loss;
483
495
  if (!_stop_loss && stop_percent) {
@@ -488,12 +500,20 @@ class Signal {
488
500
  let _resistance = current_price * Math.pow(1 + percent_change, 1);
489
501
  const simple_support = Math.min(current_price, stop_loss);
490
502
  const simple_resistance = Math.max(current_price, stop_loss);
503
+ const risk_per_trade = risk / this.risk_reward;
504
+ const use_progressive = distribution_params.use_progressive || this.use_progressive_risk;
505
+ const risk_distribution = use_progressive ? {
506
+ enabled: true,
507
+ total_risk_budget: risk,
508
+ risk_reward: this.risk_reward,
509
+ buckets: distribution_params.buckets || []
510
+ } : undefined;
491
511
  const derivedConfig = {
492
512
  ...this,
493
513
  percent_change,
494
514
  focus: current_price,
495
515
  resistance: distribution ? simple_resistance : _resistance,
496
- risk_per_trade: risk / this.risk_reward,
516
+ risk_per_trade,
497
517
  minimum_pnl: pnl,
498
518
  risk_reward: _no_of_trades,
499
519
  take_profit: take_profit || this.take_profit,
@@ -502,7 +522,9 @@ class Signal {
502
522
  ...this.distribution,
503
523
  [kind]: distribution
504
524
  } : undefined,
505
- distribution_params
525
+ use_progressive_risk: use_progressive,
526
+ distribution_params,
527
+ risk_distribution
506
528
  };
507
529
  const instance = new Signal(derivedConfig);
508
530
  if (kind === "short") {}
@@ -867,6 +889,24 @@ class Signal {
867
889
  }
868
890
  return null;
869
891
  }
892
+ getZoneRisk(index, totalZones, account) {
893
+ if (!account.risk_distribution?.enabled) {
894
+ return account.risk_per_trade;
895
+ }
896
+ const { buckets, total_risk_budget } = account.risk_distribution;
897
+ const reversedBuckets = [...buckets].reverse();
898
+ let zoneStart = 0;
899
+ for (const bucket of reversedBuckets) {
900
+ const zoneEnd = zoneStart + Math.floor(totalZones * bucket.zone_percentage);
901
+ if (index < zoneEnd) {
902
+ const zonesInBucket = zoneEnd - zoneStart;
903
+ const bucket_total = total_risk_budget * bucket.risk_percentage;
904
+ return bucket_total / zonesInBucket;
905
+ }
906
+ zoneStart = zoneEnd;
907
+ }
908
+ return account.risk_per_trade;
909
+ }
870
910
  process_orders({
871
911
  current_price,
872
912
  stop_loss,
@@ -929,7 +969,8 @@ class Signal {
929
969
  }
930
970
  const defaultStopLoss = i === 0 ? stop_loss : _base;
931
971
  const new_stop = kind === "long" ? this.support : stop_loss;
932
- let risk_to_use = risk_per_trade;
972
+ let risk_to_use = this.getZoneRisk(i, limit_orders.length, this);
973
+ console.log("index: ", i, " risk: ", risk_to_use);
933
974
  if (this.use_kelly) {
934
975
  const func = this.kelly_func === "theoretical" ? calculateTheoreticalKelly : this.kelly_func === "position_based" ? calculatePositionBasedKelly : calculateTheoreticalKellyFixed;
935
976
  const theoretical_kelly = func({
@@ -1613,7 +1654,8 @@ function buildConfig(app_config, {
1613
1654
  kelly_func = "theoretical",
1614
1655
  min_avg_size = 0,
1615
1656
  distribution,
1616
- distribution_params
1657
+ distribution_params,
1658
+ use_progressive_risk
1617
1659
  }) {
1618
1660
  let fee = app_config.fee / 100;
1619
1661
  let working_risk = risk || app_config.risk_per_trade;
@@ -1645,7 +1687,8 @@ function buildConfig(app_config, {
1645
1687
  kelly_func: kelly_func || app_config.kelly?.kelly_func,
1646
1688
  symbol: app_config.symbol,
1647
1689
  max_quantity: app_config.max_quantity,
1648
- distribution_params: distribution_params || app_config.distribution_params
1690
+ distribution_params: distribution_params || app_config.distribution_params,
1691
+ use_progressive_risk
1649
1692
  };
1650
1693
  const instance = new Signal(config);
1651
1694
  if (raw_instance) {
@@ -1793,7 +1836,12 @@ function buildAppConfig(config, payload) {
1793
1836
  return app_config;
1794
1837
  }
1795
1838
  function getOptimumStopAndRisk(app_config, params) {
1796
- const { max_size, target_stop, distribution, distribution_params: _distribution_params } = params;
1839
+ const {
1840
+ max_size,
1841
+ target_stop,
1842
+ distribution,
1843
+ distribution_params: _distribution_params
1844
+ } = params;
1797
1845
  const isLong = app_config.kind === "long";
1798
1846
  const stopRange = Math.abs(app_config.entry - target_stop) * 0.5;
1799
1847
  let low_stop = isLong ? target_stop - stopRange : Math.max(target_stop - stopRange, app_config.entry);