Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sreenivasdoosa
GitHub Repository: sreenivasdoosa/sdoosa-algo-trade-python
Path: blob/master/src/trademgmt/Trade.py
301 views
1
import logging
2
3
from trademgmt.TradeState import TradeState
4
from models.ProductType import ProductType
5
6
from utils.Utils import Utils
7
8
class Trade:
9
def __init__(self, tradingSymbol = None):
10
self.exchange = "NSE"
11
self.tradeID = Utils.generateTradeID() # Unique ID for each trade
12
self.tradingSymbol = tradingSymbol
13
self.strategy = ""
14
self.direction = ""
15
self.productType = ProductType.MIS
16
self.isFutures = False # Futures trade
17
self.isOptions = False # Options trade
18
self.optionType = None # CE/PE. Applicable only if isOptions is True
19
self.placeMarketOrder = False # True means place the entry order with Market Order Type
20
self.intradaySquareOffTimestamp = None # Can be strategy specific. Some can square off at 15:00:00 some can at 15:15:00 etc.
21
self.requestedEntry = 0 # Requested entry
22
self.entry = 0 # Actual entry. This will be different from requestedEntry if the order placed is Market order
23
self.qty = 0 # Requested quantity
24
self.filledQty = 0 # In case partial fill qty is not equal to filled quantity
25
self.initialStopLoss = 0 # Initial stop loss
26
self.stopLoss = 0 # This is the current stop loss. In case of trailing SL the current stopLoss and initialStopLoss will be different after some time
27
self.target = 0 # Target price if applicable
28
self.cmp = 0 # Last traded price
29
30
self.tradeState = TradeState.CREATED # state of the trade
31
self.timestamp = None # Set this timestamp to strategy timestamp if you are not sure what to set
32
self.createTimestamp = Utils.getEpoch() # Timestamp when the trade is created (Not triggered)
33
self.startTimestamp = None # Timestamp when the trade gets triggered and order placed
34
self.endTimestamp = None # Timestamp when the trade ended
35
self.pnl = 0 # Profit loss of the trade. If trade is Active this shows the unrealized pnl else realized pnl
36
self.pnlPercentage = 0 # Profit Loss in percentage terms
37
self.exit = 0 # Exit price of the trade
38
self.exitReason = None # SL/Target/SquareOff/Any Other
39
40
self.entryOrder = None # Object of Type ordermgmt.Order
41
self.slOrder = None # Object of Type ordermgmt.Order
42
self.targetOrder = None # Object of Type ordermgmt.Order
43
44
def equals(self, trade): # compares to trade objects and returns True if equals
45
if trade == None:
46
return False
47
if self.tradeID == trade.tradeID:
48
return True
49
if self.tradingSymbol != trade.tradingSymbol:
50
return False
51
if self.strategy != trade.strategy:
52
return False
53
if self.direction != trade.direction:
54
return False
55
if self.productType != trade.productType:
56
return False
57
if self.requestedEntry != trade.requestedEntry:
58
return False
59
if self.qty != trade.qty:
60
return False
61
if self.timestamp != trade.timestamp:
62
return False
63
return True
64
65
def __str__(self):
66
return "ID=" + str(self.tradeID) + ", state=" + self.tradeState + ", symbol=" + self.tradingSymbol \
67
+ ", strategy=" + self.strategy + ", direction=" + self.direction \
68
+ ", productType=" + self.productType + ", reqEntry=" + str(self.requestedEntry) \
69
+ ", stopLoss=" + str(self.stopLoss) + ", target=" + str(self.target) \
70
+ ", entry=" + str(self.entry) + ", exit=" + str(self.exit) \
71
+ ", profitLoss" + str(self.pnl)
72
73
74