Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

82
LINES

< > BotCompany Repo | #1036214 // GeometricPriceCells - infinitely many price cells at a constant ratio between adjacent prices

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (9677L) is out of date.

1  
persistable sclass GeometricPriceCells is PriceCells {
2  
  // cell size in percent
3  
  settable double cellSizeInPercent;
4  
  
5  
  // one of the cell limits
6  
  settable double basePrice = 1000;
7  
  
8  
  *(double *cellSizeInPercent) {}
9  
  *(double *basePrice, double *cellSizeInPercent) {}
10  
  
11  
  double ratio() {
12  
    ret 1+cellSizeInPercent/100;
13  
  }
14  
15  
  double toLogScale(double price) {
16  
    ret log(price, ratio());
17  
  }
18  
  
19  
  double fromLogScale(double logPrice) {
20  
    ret pow(ratio(), logPrice);
21  
  }
22  
23  
  double logBasePrice() {
24  
    ret toLogScale(basePrice);
25  
  }
26  
27  
  double remainder(double price) {
28  
    ret remainder(toLogScale(price));
29  
  }
30  
31  
  double remainderLog(double logPrice) {
32  
    ret frac(logPrice-logBasePrice());
33  
  }
34  
35  
  // TODO: fix this logic's rounding problems
36  
  
37  
  public bool isCellLimit(double price) {
38  
    ret remainder(price) == 0;
39  
  }
40  
  
41  
  public double nextCellLimitLog(double logPrice) {
42  
    double r = remainderLog(logPrice);
43  
    ret logPrice + 1-r;
44  
  }
45  
  
46  
  public double nextCellLimit(double price) {
47  
    ret fromLogScale(nextCellLimitLog(toLogScale(price)));
48  
  }
49  
  
50  
  public double previousCellLimitLog(double logPrice) {
51  
    double r = remainderLog(logPrice);
52  
    ret logPrice - (r == 0 ? 1 : r);
53  
  }
54  
  
55  
  public double previousCellLimit(double price) {
56  
    ret fromLogScale(previousCellLimitLog(toLogScale(price)));
57  
  }
58  
  
59  
  public double nCellLimitsDown(double price, int n) {
60  
    double logPrice = toLogScale(price);
61  
    logPrice = previousCellLimitLog(logPrice)-(n-1);
62  
    ret fromLogScale(logPrice);
63  
  }
64  
  
65  
  public double nCellLimitsUp(double price, int n) {
66  
    double logPrice = toLogScale(price);
67  
    logPrice = nextCellLimitLog(logPrice)+n;
68  
    ret fromLogScale(logPrice);
69  
  }
70  
  
71  
  public double priceToCellNumber(double price) {
72  
    ret toLogScale(price)-logBasePrice();
73  
  } 
74  
  
75  
  public double cellNumberToPrice(double cellNumber) {
76  
    ret fromLogScale(cellNumber+logBasePrice());
77  
  }
78  
  
79  
  toString {
80  
    ret formatDouble2(cellSizeInPercent) + "% cells with C0=" + formatPrice(basePrice);
81  
  }
82  
}

Author comment

Began life as a copy of #1036206

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036214
Snippet name: GeometricPriceCells - infinitely many price cells at a constant ratio between adjacent prices
Eternal ID of this version: #1036214/15
Text MD5: 87fee66aea226691d333e5706413c284
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-12 03:10:22
Source code size: 2096 bytes / 82 lines
Pitched / IR pitched: No / No
Views / Downloads: 108 / 194
Version history: 14 change(s)
Referenced in: [show references]