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)

  persistable class Position extends G22TradingStrategy.Position {
    settable AbstractJuicer juicer;
    
    toString {
      ret super.toString()
        + (juicer == null ? "" : ". " + commaCombine(juicer.currentSignals()));
    }
  }
  
  srecord ProfitBeforeLeverageJuiceable(Position p) is Juiceable {
    public double juiceValue() { ret p.profitBeforeLeverage(); }
  }
  
  void juiceStrategy_price(double price) {
    if (currentPrice == price) ret;
    currentPrice = price;
    afterwards { afterStep(); change(); }
    juiceStrategy_newPrice(price);
  }
    
  void juiceStrategy_newPrice(double price) {
    for (p : openPositions()) {
      cast p to Position;
      
      if (p.juicer != null) {
        var signals = p.juicer.calculateCloseSignals();
        var strongest = highestBy(signals, s -> s.strength());
        if (strongest != null && strongest.isTrigger()) {
          p.close(strongest);
        }
      }
    }
  }
  
  Position openShort() { ret openPosition(-1); }
  Position openLong() { ret openPosition(1); }
  
  Position openPosition(int direction, O openReason default null) {
    new Position p;
    p.marginToUse = marginToUseForNewPosition();
    addJuicer(p);
    ret openPosition(p, direction, openReason);
  }
  
  void addJuicer(Position p) {
    var j = makeJuicer();
    if (j == null) ret;
    j.copyTransientValuesFrom(p.juicer);
    j.juiceable(new ProfitBeforeLeverageJuiceable(p));
    p.juicer(j);
  }
  
  void recreateJuicersForExistingPositions aka updateJuicers() {
    for (p : openPositions()) {
      cast p to Position;
      addJuicer(p);
    }
  }
  
  L<? extends Position> openPositions() { ret (L) super.openPositions(); }
  
  L<AbstractJuicer> allJuicers aka juicers() {
    ret map(openPositions(), -> .juicer());
  }

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: 130 / 188
Version history: 18 change(s)
Referenced in: [show references]