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

53
LINES

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

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

Libraryless. Click here for Pure Java version (29173L/183K).

concept JuiceStrategy extends G22TradingStrategy {
  persistable class Position extends G22TradingStrategy.Position {
    settable AbstractJuicer juicer;
  }
  
  srecord ProfitBeforeLeverageJuiceable(Position p) is Juiceable {
    public double juiceValue() { ret p.profitBeforeLeverage(); }
  }
  
  void price(double price) {
    if (currentPrice == price) ret;
    currentPrice = price;
    afterwards { change(); }
    
    for (p : cloneList(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) {
    new Position p;
    p.marginToUse = marginPerPosition;
    addJuicer(p);
    ret openPosition(p, direction);
  }
  
  void addJuicer(Position p) {
    p.juicer(makeJuicer());
    p.juicer.juiceable(new ProfitBeforeLeverageJuiceable(p));
  }
  
  swappable AbstractJuicer makeJuicer() {
    ret new StopLossJuicer;
  }
  
  void recreateJuicersForExistingPositions() {
    for (p : cloneList(openPositions())) {
      cast p to Position;
      addJuicer(p);
    }
  }
}

Author comment

Began life as a copy of #1036259

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036351
Snippet name: JuiceStrategy - just "juices" (automatically closes) a position that was opened manually
Eternal ID of this version: #1036351/22
Text MD5: 24fa62efde9aea06ccb2015eb9f9252a
Transpilation MD5: fd8dc1b3564da97a68aec7152f026f2e
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-11-23 20:19:31
Source code size: 1433 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 45 / 151
Version history: 21 change(s)
Referenced in: [show references]