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

70
LINES

< > BotCompany Repo | #1036119 // TradingPosition

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

Libraryless. Click here for Pure Java version (30183L/191K).

1  
sclass TradingPosition {
2  
  settable S coin;
3  
  settable bool isLong;
4  
  settable int leverage;
5  
  
6  
  // How much we originally invest in USDT
7  
  settable double marginInvestment;
8  
  
9  
  // How much USDT margin we end up with
10  
  settable double margin;
11  
  
12  
  // How much crypto we bet on
13  
  settable double amount;
14  
  
15  
  // Is position still open?
16  
  settable bool ongoing;
17  
  
18  
  settable long openingTime;
19  
  settable long closingTime;
20  
  
21  
  settable double openingPrice;
22  
  
23  
  // closing or current price
24  
  settable double closingPrice;
25  
  
26  
  settable double openingFees;
27  
  settable double closingFees;
28  
  
29  
  settable double profit = Double.NaN;
30  
  
31  
  // Actual price movement during position
32  
  settable TradingCandle candle;
33  
  
34  
  // Signal we acted on (optional)
35  
  settable TradingSignal afterSignal;
36  
  
37  
  double openingFeePercentage() {
38  
    ret doubleRatio(openingFees, margin*leverage);
39  
  }
40  
  
41  
  double priceDifference() {
42  
    ret closingPrice-openingPrice;
43  
  }
44  
  
45  
  double leveragedPriceDifference() {
46  
    ret priceDifference()*leverage;
47  
  }
48  
  
49  
  // PNL = Profit & Loss
50  
  double expectedPNL aka relativeValue() {
51  
    ret !isNaN(profit) ? profit : (isLong() ? 1 : -1)*leveragedPriceDifference()*amount;
52  
  }
53  
  
54  
  bool isWinner() { ret relativeValue() >= 0; }
55  
  
56  
  bool isOpen() { ret ongoing; }
57  
  
58  
  int direction() { ret isLong() ? 1 : -1; }
59  
  
60  
  static TradingPosition fromStrategy(G22TradingStrategy.Position p) {
61  
    ret new TradingPosition()
62  
      .profit(p.profit())
63  
      .openingTime(p.openingTime())
64  
      .closingTime(p.closingTime())
65  
      .openingPrice(p.openingPrice())
66  
      .closingPrice(p.closingPrice())
67  
      .isLong(p.direction > 0)
68  
      .leverage(iround(p.leverage));
69  
  }
70  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1036119
Snippet name: TradingPosition
Eternal ID of this version: #1036119/12
Text MD5: 1e77967db1d6af89752a34e318727fb3
Transpilation MD5: 43c9efc74c049a2ea8f8db114b530cb0
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-03-12 23:26:39
Source code size: 1748 bytes / 70 lines
Pitched / IR pitched: No / No
Views / Downloads: 159 / 255
Version history: 11 change(s)
Referenced in: [show references]