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).

1  
persistable sclass PullbackToProfitAnalysisBase {
2  
  settable double pullbackLimit = 1.0;
3  
  settable double adversity = 0.2;
4  
  settable boolean isShort;
5  
  settable TickerSequence ticker;
6  
  settable long startTime;
7  
  settable L<PullbackToProfit> ptpList;
8  
  settable double openingPrice;
9  
  
10  
  double priceToProfit(double price) {
11  
    var profit = asPercentIncrease(price, openingPrice());
12  
    if (isShort()) profit = neg(profit);
13  
    ret profit-adversity();
14  
  }
15  
  
16  
  run {
17  
    if (notNull(ptpList())) ret;
18  
    if (startTime == 0) startTime = ticker().startTime();
19  
    var idx1 = ticker().indexOfTimestamp(startTime());
20  
    openingPrice(ticker().getPrice(idx1));
21  
    var crest = 0.0; //negativeInfinity
22  
    var maxPullback = 0.0;
23  
      
24  
    ptpList = new L;
25  
    
26  
    int n = ticker().size();
27  
    for (int idx = idx1; idx < n; idx++)  {
28  
      var price = ticker().getPrice(idx);
29  
      var time = ticker().getTimestamp(idx);
30  
      var profit = priceToProfit(price);
31  
            
32  
      crest = max(crest, profit);
33  
      var pullback = crest-profit;
34  
      maxPullback = max(maxPullback, pullback);
35  
      
36  
      if (maxPullback >= pullbackLimit()) {
37  
        printConcat("Ending at pullback ", maxPullback, " (", formatMinutes(time-startTime()), ")");
38  
        break;
39  
      }
40  
          
41  
      var result = new PullbackToProfit().pullback(maxPullback).profit(crest-maxPullback);
42  
      
43  
      var prev = last(ptpList());
44  
      if (prev != null)
45  
        if (prev.replaces(result))
46  
          continue;
47  
        else if (result.replaces(prev))
48  
          popLast(ptpList);
49  
          
50  
      ptpList.add(result
51  
        .isShort(isShort)
52  
        .timestamp(time)
53  
        .startTime(startTime()));
54  
    }
55  
  }
56  
  
57  
  double maxCleanness() {
58  
    run();
59  
    ret doubleMax(map(ptpList(), ptp -> ptp.cleanness());
60  
  }
61  
  
62  
  PullbackToProfit cleanestPTP() {
63  
    run();
64  
    ret highestBy(ptpList(), ptp -> ptp.cleanness());
65  
  }
66  
}

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: 108 / 222
Version history: 17 change(s)
Referenced in: [show references]