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

64
LINES

< > BotCompany Repo | #1036461 // JuiceStrategy Include - just "juices" (automatically closes) a position that was opened manually

JavaX fragment (include)

1  
  persistable class Position extends G22TradingStrategy.Position {
2  
    settable AbstractJuicer juicer;
3  
    
4  
    toString {
5  
      ret super.toString()
6  
        + (juicer == null ? "" : ". " + commaCombine(juicer.currentSignals()));
7  
    }
8  
  }
9  
  
10  
  srecord ProfitBeforeLeverageJuiceable(Position p) is Juiceable {
11  
    public double juiceValue() { ret p.profitBeforeLeverage(); }
12  
  }
13  
  
14  
  void juiceStrategy_price(double price) {
15  
    if (currentPrice == price) ret;
16  
    currentPrice = price;
17  
    afterwards { afterStep(); change(); }
18  
    juiceStrategy_newPrice(price);
19  
  }
20  
    
21  
  void juiceStrategy_newPrice(double price) {
22  
    for (p : openPositions()) {
23  
      cast p to Position;
24  
      
25  
      if (p.juicer != null) {
26  
        var signals = p.juicer.calculateCloseSignals();
27  
        var strongest = highestBy(signals, s -> s.strength());
28  
        if (strongest != null && strongest.isTrigger()) {
29  
          p.close(strongest);
30  
        }
31  
      }
32  
    }
33  
  }
34  
  
35  
  Position openShort() { ret openPosition(-1); }
36  
  Position openLong() { ret openPosition(1); }
37  
  
38  
  Position openPosition(int direction, O openReason default null) {
39  
    new Position p;
40  
    p.marginToUse = marginToUseForNewPosition();
41  
    addJuicer(p);
42  
    ret openPosition(p, direction, openReason);
43  
  }
44  
  
45  
  void addJuicer(Position p) {
46  
    var j = makeJuicer();
47  
    if (j == null) ret;
48  
    j.copyTransientValuesFrom(p.juicer);
49  
    j.juiceable(new ProfitBeforeLeverageJuiceable(p));
50  
    p.juicer(j);
51  
  }
52  
  
53  
  void recreateJuicersForExistingPositions aka updateJuicers() {
54  
    for (p : openPositions()) {
55  
      cast p to Position;
56  
      addJuicer(p);
57  
    }
58  
  }
59  
  
60  
  L<? extends Position> openPositions() { ret (L) super.openPositions(); }
61  
  
62  
  L<AbstractJuicer> allJuicers aka juicers() {
63  
    ret map(openPositions(), -> .juicer());
64  
  }

Author comment

Began life as a copy of #1036351

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036461
Snippet name: JuiceStrategy Include - just "juices" (automatically closes) a position that was opened manually
Eternal ID of this version: #1036461/19
Text MD5: d575c0d36ee2e5e595754ef811164077
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-04-03 17:04:52
Source code size: 1853 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 133 / 193
Version history: 18 change(s)
Referenced in: [show references]