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

29
LINES

< > BotCompany Repo | #1036355 // StopLossJuicer - simple juicer that closes when juice level goes below a threshold

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

Libraryless. Click here for Pure Java version (15018L/89K).

persistable sclass StopLossJuicer extends AbstractJuicer {
  // How much loss we will accept
  // Can be positive (stop loss in profit) or negative (stop loss with actual loss)
  settable double stopLossLimit = -0.5;
  
  settable bool stopLossEnabled = true;
  
  // Reference threshold to calculate signal below 100%
  // (doesn't impact closing behavior)
  settable double startStopLossSignalAt;
  
  {
    onCalculatingCloseSignals(signals -> {
      // How close are we to our loss limit?
      if (stopLossEnabled && stopLossLimit > -infinity()) {
        var signal = new CloseSignal().createdBy(this).reason("Stop loss");
        if (juiceValue <= stopLossLimit)
          signal.strength(100);
        else {
          double startAt = max(startStopLossSignalAt, 0);
          if (startAt > stopLossLimit)
            signal.strength(doubleRatio(startAt-juiceValue, startAt-stopLossLimit)*100);
        }
        
        signals.add(signal);
      }
    });
  }
}

Author comment

Began life as a copy of #1036353

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036355
Snippet name: StopLossJuicer - simple juicer that closes when juice level goes below a threshold
Eternal ID of this version: #1036355/20
Text MD5: f539869011a4aaec17ea9e39f74fec59
Transpilation MD5: a242e59aa9605b96584e27d1215e55e0
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-12-04 23:54:53
Source code size: 1003 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 48 / 160
Version history: 19 change(s)
Referenced in: [show references]