Advertisement
DIMENSI0N

Trading Simulation

Jan 28th, 2022
1,747
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trading_fees = 0.075 / 100
  2. futures_fees = 0.04 / 100
  3.  
  4. class Position:
  5.  
  6.     def __init__(self, value, leverage, price, type):
  7.         self.type = type
  8.         self.value = value
  9.         self.price = price
  10.         self.leverage = leverage
  11.  
  12.  
  13.     def liquidated(self, price):
  14.         return self.get_value(price) <= 0
  15.  
  16.  
  17.     def get_value(self, price):
  18.         evolution = (price - self.price) / self.price
  19.  
  20.         if self.type == 'long':
  21.             return self.value + self.leverage * evolution * self.value
  22.         elif self.type == 'short':
  23.             return self.value - self.leverage * evolution * self.value
  24.         else:
  25.             return 0
  26.  
  27.  
  28. class Wallet:
  29.  
  30.     def __init__(self, balance):
  31.         self.usdt = balance
  32.         self.btc = 0
  33.         self.positions = []
  34.  
  35.  
  36.     def buy(self, percentage, price):
  37.         self.usdt -= self.usdt * percentage * trading_fees
  38.         self.btc += (self.usdt * percentage) / price
  39.         self.usdt -= self.usdt * percentage
  40.  
  41.  
  42.     def sell(self, percentage, price):
  43.         self.btc -= self.btc * percentage * trading_fees
  44.         self.usdt += self.btc * percentage * price
  45.         self.btc -= self.btc * percentage + self.btc * percentage * trading_fees
  46.  
  47.  
  48.     def open_position(self, percentage, leverage, price, type):
  49.         self.usdt -= percentage * self.usdt * futures_fees
  50.         self.positions.append(Position(percentage * self.usdt, leverage, price, type))
  51.         self.usdt -= percentage * self.usdt + percentage * self.usdt * futures_fees
  52.  
  53.  
  54.     def close_positions(self, price):
  55.         for position in self.positions:
  56.             self.usdt += position.get_value(price)
  57.             self.usdt -= ((position.value * position.price) / price) * futures_fees
  58.             self.positions.remove(position)
  59.             self.close_positions(price)
  60.             return
  61.  
  62.  
  63.     def update_positions(self, price):
  64.         for position in self.positions:
  65.             if position.liquidated(price):
  66.                 self.positions.remove(position)
  67.                 self.update_positions(price)
  68.                 return
  69.  
  70.  
  71.     def get_balance(self, price):
  72.         self.close_positions(price)
  73.         self.sell(1, price)
  74.         return self.usdt
  75.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement