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

67
LINES

< > BotCompany Repo | #1036534 // MultiCallbackJuicer - saner version of MultiPullbackJuicer

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

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

persistable sclass MultiCallbackJuicer extends AbstractJuicer {
  // Must have at least one level, otherwise no closing at all
  
  sclass Level {
    // At juice value minJuiceValue, define the callback rate.
    // Callback rates are interpolated between levels.
    settable double minJuiceValue;
    settable double callback;
    
    toString { ret "Callback " + callback + "% starting at " + formatDouble2(minJuiceValue); }
  }
  
  settableWithVar new L<Level> levels;
  
  // Highest juice value seen
  settableWithVar double crest = -infinity();
  
  settableWithVar double currentCallback;
  
  {
    onCalculatingCloseSignals(signals -> {
      if (juiceValue > crest)
        crest(juiceValue);
        
      currentCallback(callbackForJuiceValue(crest));

      var signal = new CloseSignal().createdBy(this).reason(formatDouble2(currentCallback) + "% Callback");
      signal.strength(doubleRatio(crest-juiceValue, currentCallback)*100);
      signals.add(signal);
    });
  }
  
  double callbackForJuiceValue(double juice) {
    for (int i = l(levels)-1; i >= 0; i--) {
      Level level = levels.get(i);
      if (juice < level.minJuiceValue)
        continue;
        
      Level nextLevel = get(levels, i+1);
      if (nextLevel == null)
        ret level.callback;
      
      ret blend(level.callback, nextLevel.callback,
        transformToZeroToOne(juice, level.minJuiceValue, nextLevel.minJuiceValue));
    }
    
    ret empty(levels) ? 1000 : first(levels).callback;
  }
  
  void addLevel(double minJuiceValue, double callback) {
    levels.add(new Level().minJuiceValue(minJuiceValue).callback(callback));
  }
  
  toString {
    ret commaCombine(
      shortClassName(this),
      levels,
      crest == -infinity() ? null : "Highest profit seen: " + formatDouble2(crest),
      "Current callback: " + formatDouble2(currentCallback),
    );
  }
  
  void copyTransientValuesFrom(AbstractJuicer juicer) {
    if (juicer cast MultiCallbackJuicer)
      crest(juicer.crest);
  }
}

Author comment

Began life as a copy of #1036356

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036534
Snippet name: MultiCallbackJuicer - saner version of MultiPullbackJuicer
Eternal ID of this version: #1036534/6
Text MD5: a24a630c506af79bbb848db2d812cccd
Transpilation MD5: 251fb15583ad1a10ff2fac0023f867db
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-24 17:57:46
Source code size: 2077 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 181 / 242
Version history: 5 change(s)
Referenced in: [show references]