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

72
LINES

< > BotCompany Repo | #1036519 // LivePullbackToProfitAnalysis

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

Libraryless. Click here for Pure Java version (10779L/60K).

persistable sclass LivePullbackToProfitAnalysis {
  settable double pullbackLimit = 1.0;
  settable double adversity = 0.2;
  settable boolean isShort;
  settable long startTime;
  settable double openingPrice;
  settable double crest = 0;
  settable double maxPullback;
  settable L<PullbackToProfit> ptpList;
  
  double priceToProfit(double price) {
    var profit = asPercentIncrease(price, openingPrice());
    if (isShort()) profit = neg(profit);
    ret profit-adversity();
  }
  
  void add aka feed(PricePoint pp) {
    if (startTime == 0) {
      startTime(pp.time());
      openingPrice(pp.price());
      ptpList(new L);
    }
    
    var price = pp.price();
    var time = pp.time();
    var profit = priceToProfit(price);
            
    crest = max(crest, profit);
    var pullback = crest-profit;
    maxPullback = max(maxPullback, pullback);
      
    if (done()) ret;
      
    var result = new PullbackToProfit().pullback(maxPullback).profit(crest-maxPullback);
      
    var prev = last(ptpList);
    if (prev != null)
      if (prev.replaces(result))
        ret;
      else if (result.replaces(prev))
        popLast(ptpList);
        
    ptpList.add(result
      .isShort(isShort)
      .timestamp(time)
      .startTime(startTime()));
  }
  
  /*double cleanness() {
    ret ptp == null ? 0 : ptp.cleanness();
  }
  
  double profit() {
    ret ptp == null ? negativeInfinity() : ptp.profit();
  }*/
  
  bool done() {
    ret maxPullback >= pullbackLimit();
  }
  
  toString {
    ret startTime == 0 ? "Unstarted PTP analysis" : joinWithComma(ptpList);
  }
  
  double maxCleanness() {
    ret doubleMax(map(ptpList(), ptp -> ptp.cleanness());
  }
  
  PullbackToProfit cleanestPTP() {
    ret highestBy(ptpList(), ptp -> ptp.cleanness());
  }
}

Author comment

Began life as a copy of #1036515

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036519
Snippet name: LivePullbackToProfitAnalysis
Eternal ID of this version: #1036519/9
Text MD5: b4ac65bbe1e338ea6441c36178bc67b2
Transpilation MD5: 6f455482771afb8e717f58f4d116f7fa
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-22 17:05:23
Source code size: 1848 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 147 / 241
Version history: 8 change(s)
Referenced in: [show references]