@gbozee/ultimate 0.0.2-204 → 0.0.2-207

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.
@@ -376,6 +376,7 @@ class Signal {
376
376
  budget;
377
377
  percent_change = 0.02;
378
378
  price_places = "%.5f";
379
+ distribution_params = {};
379
380
  decimal_places = "%.0f";
380
381
  zone_risk = 1;
381
382
  fee = 0.08 / 100;
@@ -431,11 +432,13 @@ class Signal {
431
432
  kelly_minimum_risk = 0.2,
432
433
  kelly_func = "theoretical",
433
434
  full_distribution,
434
- max_quantity = 0.03
435
+ max_quantity = 0.03,
436
+ distribution_params = {}
435
437
  }) {
436
438
  if (full_distribution) {
437
439
  this.distribution = full_distribution;
438
440
  }
441
+ this.distribution_params = distribution_params;
439
442
  this.symbol = symbol;
440
443
  this.minimum_size = minimum_size;
441
444
  this.first_order_size = first_order_size;
@@ -473,7 +476,8 @@ class Signal {
473
476
  risk,
474
477
  no_of_trades = 1,
475
478
  take_profit,
476
- distribution
479
+ distribution,
480
+ distribution_params = {}
477
481
  }) {
478
482
  let _stop_loss = stop_loss;
479
483
  if (!_stop_loss && stop_percent) {
@@ -497,7 +501,8 @@ class Signal {
497
501
  full_distribution: distribution ? {
498
502
  ...this.distribution,
499
503
  [kind]: distribution
500
- } : undefined
504
+ } : undefined,
505
+ distribution_params
501
506
  };
502
507
  const instance = new Signal(derivedConfig);
503
508
  if (kind === "short") {}
@@ -725,7 +730,8 @@ class Signal {
725
730
  kind: _kind,
726
731
  distribution,
727
732
  risk_reward: this.risk_reward,
728
- price_places: this.price_places
733
+ price_places: this.price_places,
734
+ distribution_params: this.distribution_params
729
735
  });
730
736
  return entries.sort((a, b) => a - b);
731
737
  }
@@ -1606,7 +1612,8 @@ function buildConfig(app_config, {
1606
1612
  kelly_prediction_model = "exponential",
1607
1613
  kelly_func = "theoretical",
1608
1614
  min_avg_size = 0,
1609
- distribution
1615
+ distribution,
1616
+ distribution_params
1610
1617
  }) {
1611
1618
  let fee = app_config.fee / 100;
1612
1619
  let working_risk = risk || app_config.risk_per_trade;
@@ -1637,7 +1644,8 @@ function buildConfig(app_config, {
1637
1644
  kelly_prediction_model: kelly_prediction_model || app_config.kelly?.kelly_prediction_model,
1638
1645
  kelly_func: kelly_func || app_config.kelly?.kelly_func,
1639
1646
  symbol: app_config.symbol,
1640
- max_quantity: app_config.max_quantity
1647
+ max_quantity: app_config.max_quantity,
1648
+ distribution_params: distribution_params || app_config.distribution_params
1641
1649
  };
1642
1650
  const instance = new Signal(config);
1643
1651
  if (raw_instance) {
@@ -1654,7 +1662,8 @@ function buildConfig(app_config, {
1654
1662
  risk: working_risk,
1655
1663
  kind: kind || app_config.kind,
1656
1664
  no_of_trades: trade_no,
1657
- distribution
1665
+ distribution,
1666
+ distribution_params
1658
1667
  }) || [] : [];
1659
1668
  const new_trades = computeTotalAverageForEachTrade(result, config);
1660
1669
  let filtered = new_trades.filter((o) => o.avg_size > min_avg_size);
@@ -1724,7 +1733,8 @@ function get_app_config_and_max_size(config, payload) {
1724
1733
  kelly_minimum_risk: payload.kelly_minimum_risk,
1725
1734
  kelly_prediction_model: payload.kelly_prediction_model,
1726
1735
  kelly_func: payload.kelly_func,
1727
- distribution: payload.distribution
1736
+ distribution: payload.distribution,
1737
+ distribution_params: payload.distribution_params
1728
1738
  });
1729
1739
  const max_size = initialResult[0]?.avg_size;
1730
1740
  const last_value = initialResult[0];
@@ -1763,7 +1773,8 @@ function buildAppConfig(config, payload) {
1763
1773
  kelly_minimum_risk: payload.kelly_minimum_risk,
1764
1774
  kelly_prediction_model: payload.kelly_prediction_model,
1765
1775
  kelly_func: payload.kelly_func,
1766
- distribution: payload.distribution
1776
+ distribution: payload.distribution,
1777
+ distribution_params: payload.distribution_params
1767
1778
  });
1768
1779
  app_config.max_size = max_size;
1769
1780
  app_config.entry = payload.entry || app_config.entry;
@@ -1777,10 +1788,12 @@ function buildAppConfig(config, payload) {
1777
1788
  kelly_prediction_model: payload.kelly_prediction_model,
1778
1789
  kelly_func: payload.kelly_func
1779
1790
  };
1791
+ app_config.distribution = payload.distribution;
1792
+ app_config.distribution_params = payload.distribution_params;
1780
1793
  return app_config;
1781
1794
  }
1782
1795
  function getOptimumStopAndRisk(app_config, params) {
1783
- const { max_size, target_stop, distribution } = params;
1796
+ const { max_size, target_stop, distribution, distribution_params: _distribution_params } = params;
1784
1797
  const isLong = app_config.kind === "long";
1785
1798
  const stopRange = Math.abs(app_config.entry - target_stop) * 0.5;
1786
1799
  let low_stop = isLong ? target_stop - stopRange : Math.max(target_stop - stopRange, app_config.entry);
@@ -1804,7 +1817,8 @@ function getOptimumStopAndRisk(app_config, params) {
1804
1817
  gap: app_config.gap,
1805
1818
  price_places: app_config.price_places,
1806
1819
  decimal_places: app_config.decimal_places,
1807
- distribution
1820
+ distribution,
1821
+ distribution_params: _distribution_params
1808
1822
  });
1809
1823
  if (result.length === 0) {
1810
1824
  if (isLong) {
@@ -1859,7 +1873,8 @@ function getOptimumStopAndRisk(app_config, params) {
1859
1873
  gap: app_config.gap,
1860
1874
  price_places: app_config.price_places,
1861
1875
  decimal_places: app_config.decimal_places,
1862
- distribution
1876
+ distribution,
1877
+ distribution_params: _distribution_params
1863
1878
  });
1864
1879
  if (result.length === 0) {
1865
1880
  high_risk = mid_risk;
@@ -1905,7 +1920,8 @@ function getOptimumStopAndRisk(app_config, params) {
1905
1920
  gap: app_config.gap,
1906
1921
  price_places: app_config.price_places,
1907
1922
  decimal_places: app_config.decimal_places,
1908
- distribution
1923
+ distribution,
1924
+ distribution_params: _distribution_params
1909
1925
  });
1910
1926
  if (result.length === 0)
1911
1927
  continue;
@@ -2126,7 +2142,8 @@ function determineOptimumReward(payload) {
2126
2142
  kind: app_config.kind,
2127
2143
  gap: app_config.gap,
2128
2144
  decimal_places: app_config.decimal_places,
2129
- distribution
2145
+ distribution,
2146
+ distribution_params: payload.distribution_params
2130
2147
  });
2131
2148
  let total = 0;
2132
2149
  let max = -Infinity;
@@ -2315,6 +2332,7 @@ function computeRiskReward(payload) {
2315
2332
  app_config,
2316
2333
  target_loss,
2317
2334
  distribution,
2335
+ distribution_params: payload.distribution_params,
2318
2336
  high_range,
2319
2337
  max_size
2320
2338
  });
@@ -2339,7 +2357,8 @@ function getRiskReward(payload) {
2339
2357
  risk_reward: 30,
2340
2358
  risk,
2341
2359
  symbol: global_config.symbol,
2342
- distribution
2360
+ distribution,
2361
+ distribution_params: payload.distribution_params
2343
2362
  });
2344
2363
  const risk_reward = computeRiskReward({
2345
2364
  app_config,
@@ -2349,6 +2368,7 @@ function getRiskReward(payload) {
2349
2368
  high_range,
2350
2369
  target_loss,
2351
2370
  distribution,
2371
+ distribution_params: payload.distribution_params,
2352
2372
  max_size
2353
2373
  });
2354
2374
  if (force_exact_risk) {
@@ -2358,7 +2378,8 @@ function getRiskReward(payload) {
2358
2378
  risk_reward,
2359
2379
  risk,
2360
2380
  symbol: global_config.symbol,
2361
- distribution
2381
+ distribution,
2382
+ distribution_params: payload.distribution_params
2362
2383
  }, {
2363
2384
  highest_risk: risk * risk_factor
2364
2385
  }).optimal_risk;
@@ -2779,7 +2800,8 @@ function generateOppositeTradeConfig(payload) {
2779
2800
  }
2780
2801
  }
2781
2802
  },
2782
- global_config
2803
+ global_config,
2804
+ distribution_config: {}
2783
2805
  });
2784
2806
  const risk_reward = computeRiskReward({
2785
2807
  app_config,
@@ -2796,7 +2818,7 @@ function generateOppositeTradeConfig(payload) {
2796
2818
  };
2797
2819
  }
2798
2820
  function constructAppConfig(payload) {
2799
- const { account, global_config, kelly_config } = payload;
2821
+ const { account, global_config, kelly_config, distribution_config } = payload;
2800
2822
  const config = account.expand?.b_config;
2801
2823
  if (!config) {
2802
2824
  return null;
@@ -2811,7 +2833,9 @@ function constructAppConfig(payload) {
2811
2833
  use_kelly: kelly_config?.use_kelly ?? kelly?.use_kelly,
2812
2834
  kelly_confidence_factor: kelly_config?.kelly_confidence_factor ?? kelly?.kelly_confidence_factor,
2813
2835
  kelly_minimum_risk: kelly_config?.kelly_minimum_risk ?? kelly?.kelly_minimum_risk,
2814
- kelly_prediction_model: kelly_config?.kelly_prediction_model ?? kelly?.kelly_prediction_model
2836
+ kelly_prediction_model: kelly_config?.kelly_prediction_model ?? kelly?.kelly_prediction_model,
2837
+ distribution: distribution_config?.distribution ?? config?.distribution,
2838
+ distribution_params: distribution_config?.distribution_params ?? config?.distribution_params
2815
2839
  };
2816
2840
  const { entries: _entries, ...appConfig } = buildAppConfig(global_config, options);
2817
2841
  return appConfig;
@@ -2821,7 +2845,8 @@ function generateDangerousConfig(payload) {
2821
2845
  const app_config = constructAppConfig({
2822
2846
  account,
2823
2847
  global_config,
2824
- kelly_config: {}
2848
+ kelly_config: {},
2849
+ distribution_config: {}
2825
2850
  });
2826
2851
  const { optimal_risk, optimal_stop } = getOptimumStopAndRisk(app_config, {
2827
2852
  max_size: config.quantity,
@@ -3344,6 +3369,7 @@ class Strategy {
3344
3369
  function buildTrades(payload) {
3345
3370
  const { appConfig, settings, kind } = payload;
3346
3371
  const kelly_config = settings.kelly;
3372
+ const distribution_params = settings.distribution_params;
3347
3373
  const current_app_config = { ...appConfig[kind] };
3348
3374
  const entryNum = parseFloat(settings.entry);
3349
3375
  const stopNum = parseFloat(settings.stop);
@@ -3353,6 +3379,7 @@ function buildTrades(payload) {
3353
3379
  current_app_config.risk_reward = parseFloat(settings.risk_reward);
3354
3380
  current_app_config.kind = kind;
3355
3381
  current_app_config.kelly = kelly_config;
3382
+ current_app_config.distribution_params = distribution_params;
3356
3383
  const options = {
3357
3384
  take_profit: null,
3358
3385
  entry: current_app_config.entry,
@@ -3470,11 +3497,13 @@ function buildWithOptimumReward({
3470
3497
  const risk = settings.risk;
3471
3498
  const stop_ratio = settings.stop_ratio || 1;
3472
3499
  const distribution = settings.distribution || config?.distribution;
3500
+ const distribution_params = settings.distribution_params || config?.distribution_params;
3473
3501
  const custom_b_config = {
3474
3502
  entry,
3475
3503
  stop,
3476
3504
  risk,
3477
- distribution
3505
+ distribution,
3506
+ distribution_params
3478
3507
  };
3479
3508
  const appConfig = constructAppConfig2({
3480
3509
  config,
@@ -3524,6 +3553,7 @@ function generateOppositeOptimum({
3524
3553
  settings,
3525
3554
  ratio = 1,
3526
3555
  distribution,
3556
+ distribution_params,
3527
3557
  risk_factor = 1
3528
3558
  }) {
3529
3559
  const configKind = config.entry > config.stop ? "long" : "short";
@@ -3544,6 +3574,7 @@ function generateOppositeOptimum({
3544
3574
  stop: settings.stop,
3545
3575
  risk: risk * ratio,
3546
3576
  distribution: distribution || "inverse-exponential",
3577
+ distribution_params: distribution_params || config?.distribution_params,
3547
3578
  risk_factor
3548
3579
  };
3549
3580
  const appConfig = constructAppConfig2({
@@ -3630,11 +3661,13 @@ function increaseTradeHelper({
3630
3661
  entry,
3631
3662
  position: position2,
3632
3663
  stop_ratio = 1,
3633
- distribution: default_distribution
3664
+ distribution: default_distribution,
3665
+ distribution_params: default_distribution_params
3634
3666
  }) {
3635
3667
  const symbol_config = global_config;
3636
3668
  const kind = config.entry > config.stop ? "long" : "short";
3637
3669
  const distribution = default_distribution || config.distribution || "inverse-exponential";
3670
+ const distribution_params = default_distribution_params || config.distribution_params;
3638
3671
  const appConfig = constructAppConfig2({
3639
3672
  config,
3640
3673
  global_config
@@ -3661,7 +3694,8 @@ function increaseTradeHelper({
3661
3694
  entry,
3662
3695
  stop,
3663
3696
  risk: style === "minimum" ? Math.abs(neg_pnl) : optimal_risk,
3664
- distribution
3697
+ distribution,
3698
+ distribution_params
3665
3699
  };
3666
3700
  const { result, trades, summary } = helperFuncToBuildTrades({
3667
3701
  custom_b_config,
@@ -3730,7 +3764,8 @@ function generatePositionIncreaseTrade({
3730
3764
  config,
3731
3765
  global_config,
3732
3766
  style = "minimum",
3733
- distribution = "inverse-exponential"
3767
+ distribution = "inverse-exponential",
3768
+ distribution_params
3734
3769
  }) {
3735
3770
  const kind = config.entry > config.stop ? "long" : "short";
3736
3771
  const target_max_quantity = kind === "long" ? account.short.quantity : account.long.quantity;
@@ -3745,10 +3780,46 @@ function generatePositionIncreaseTrade({
3745
3780
  stop,
3746
3781
  style,
3747
3782
  increase_qty,
3748
- distribution
3783
+ distribution,
3784
+ distribution_params
3749
3785
  });
3750
3786
  }
3787
+ function determineHedgeTradeToPlace({
3788
+ position: position2,
3789
+ config,
3790
+ global_config,
3791
+ profit_risk = 200,
3792
+ allowable_loss = 1000
3793
+ }) {
3794
+ const diff = profit_risk / position2.quantity;
3795
+ const kind = position2.kind === "long" ? "short" : "long";
3796
+ const tp_price = position2.kind === "long" ? diff + position2.entry : position2.entry - diff;
3797
+ const loss_diff = allowable_loss / position2.quantity;
3798
+ const loss_price = position2.kind === "long" ? position2.entry - loss_diff : position2.entry + loss_diff;
3799
+ const entry = kind === "short" ? loss_price : tp_price;
3800
+ const stop = kind === "short" ? tp_price : loss_price;
3801
+ const result = buildWithOptimumReward({
3802
+ config: {
3803
+ ...config,
3804
+ entry,
3805
+ stop
3806
+ },
3807
+ global_config,
3808
+ force_exact: true,
3809
+ settings: {
3810
+ entry,
3811
+ stop,
3812
+ risk: profit_risk,
3813
+ distribution: config.distribution
3814
+ }
3815
+ });
3816
+ return {
3817
+ opposite: result,
3818
+ take_profit: to_f(tp_price, global_config.price_places)
3819
+ };
3820
+ }
3751
3821
  var compoundAPI = {
3822
+ determineHedgeTradeToPlace,
3752
3823
  buildWithOptimumReward,
3753
3824
  constructAppConfig: constructAppConfig2,
3754
3825
  generateOppositeOptimum,