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

sclass TradingPosition {
  settable S coin;
  settable bool isLong;
  settable int leverage;
  
  // How much we originally invest in USDT
  settable double marginInvestment;
  
  // How much USDT margin we end up with
  settable double margin;
  
  // How much crypto we bet on
  settable double amount;
  
  // Is position still open?
  settable bool ongoing;
  
  settable long openingTime;
  settable long closingTime;
  
  settable double openingPrice;
  
  // closing or current price
  settable double closingPrice;
  
  settable double openingFees;
  settable double closingFees;
  
  settable double profit = Double.NaN;
  
  // Actual price movement during position
  settable TradingCandle candle;
  
  // Signal we acted on (optional)
  settable TradingSignal afterSignal;
  
  double openingFeePercentage() {
    ret doubleRatio(openingFees, margin*leverage);
  }
  
  double priceDifference() {
    ret closingPrice-openingPrice;
  }
  
  double leveragedPriceDifference() {
    ret priceDifference()*leverage;
  }
  
  // PNL = Profit & Loss
  double expectedPNL aka relativeValue() {
    ret !isNaN(profit) ? profit : (isLong() ? 1 : -1)*leveragedPriceDifference()*amount;
  }
  
  bool isWinner() { ret relativeValue() >= 0; }
  
  bool isOpen() { ret ongoing; }
  
  int direction() { ret isLong() ? 1 : -1; }
  
  static TradingPosition fromStrategy(G22TradingStrategy.Position p) {
    ret new TradingPosition()
      .profit(p.profit())
      .openingTime(p.openingTime())
      .closingTime(p.closingTime())
      .openingPrice(p.openingPrice())
      .closingPrice(p.closingPrice())
      .isLong(p.direction > 0)
      .leverage(iround(p.leverage));
  }
}

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: 87 / 157
Version history: 11 change(s)
Referenced in: [show references]