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

66
LINES

< > BotCompany Repo | #1036515 // PullbackToProfitAnalysisBase

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

Libraryless. Click here for Pure Java version (17064L/97K).

persistable sclass PullbackToProfitAnalysisBase {
  settable double pullbackLimit = 1.0;
  settable double adversity = 0.2;
  settable boolean isShort;
  settable TickerSequence ticker;
  settable long startTime;
  settable L<PullbackToProfit> ptpList;
  settable double openingPrice;
  
  double priceToProfit(double price) {
    var profit = asPercentIncrease(price, openingPrice());
    if (isShort()) profit = neg(profit);
    ret profit-adversity();
  }
  
  run {
    if (notNull(ptpList())) ret;
    if (startTime == 0) startTime = ticker().startTime();
    var idx1 = ticker().indexOfTimestamp(startTime());
    openingPrice(ticker().getPrice(idx1));
    var crest = 0.0; //negativeInfinity
    var maxPullback = 0.0;
      
    ptpList = new L;
    
    int n = ticker().size();
    for (int idx = idx1; idx < n; idx++)  {
      var price = ticker().getPrice(idx);
      var time = ticker().getTimestamp(idx);
      var profit = priceToProfit(price);
            
      crest = max(crest, profit);
      var pullback = crest-profit;
      maxPullback = max(maxPullback, pullback);
      
      if (maxPullback >= pullbackLimit()) {
        printConcat("Ending at pullback ", maxPullback, " (", formatMinutes(time-startTime()), ")");
        break;
      }
          
      var result = new PullbackToProfit().pullback(maxPullback).profit(crest-maxPullback);
      
      var prev = last(ptpList());
      if (prev != null)
        if (prev.replaces(result))
          continue;
        else if (result.replaces(prev))
          popLast(ptpList);
          
      ptpList.add(result
        .isShort(isShort)
        .timestamp(time)
        .startTime(startTime()));
    }
  }
  
  double maxCleanness() {
    run();
    ret doubleMax(map(ptpList(), ptp -> ptp.cleanness());
  }
  
  PullbackToProfit cleanestPTP() {
    run();
    ret highestBy(ptpList(), ptp -> ptp.cleanness());
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036515
Snippet name: PullbackToProfitAnalysisBase
Eternal ID of this version: #1036515/18
Text MD5: ae29d1420498e607b8773fbafb764946
Transpilation MD5: c1a8c8e22f2a0a0d089851cc6afa2479
Author: stefan
Category: javax / trading
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-01-21 23:35:04
Source code size: 1961 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 186 / 321
Version history: 17 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1036519 - LivePullbackToProfitAnalysis