Lee Chang Jun

How can I access whether certain trade closed with either targetprofit or stoploss?

Programming


I looked at the documents for querying orders but I couldn't find how I can query closed orders. I need to know whether my orders closed with target profit or stop loss. May I know how I can access this information? Thank you.
 
admin

Hello Chang Jun, the current version of Web API doesn't support info querying for closed orders.

For your case, it needs to create a temporary variable to manage and check the order fill status.

Here's a working example. Please refer to line #29 - #52 for the checking logic.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest

class AlgoEvent:
    def __init__(self):
        self.isFirstTrade = False
        self.tmp = {}
        
    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)
        self.evt.start()

    def on_marketdatafeed(self, md, ab):
        # trade only 1 time for demo purpose
        if not self.isFirstTrade:
            self.isFirstTrade = True

            order = AlgoAPIUtil.OrderObject(
                instrument = md.instrument,
                openclose = 'open', 
                buysell = 1,    # buy
                ordertype = 0,  # market order
                volume = 0.01,
                takeProfitLevel = md.lastPrice*1.1,
                stopLossLevel = md.lastPrice*0.9
            )
            self.evt.sendOrder(order)

    def on_orderfeed(self, of):
        if of.status=="success":
            if of.openclose=="open":
                self.tmp[of.tradeID] = {
                    "buysell": of.buysell,
                    "openprice": of.fill_price, 
                    "closeprice": None, 
                    "pl_side": None
                }
            elif of.openclose=="close":

                self.tmp[of.tradeID]['closeprice'] = of.fill_price

                # check whether it is profitable or lossing trade
                if self.tmp[of.tradeID]["buysell"]>0:
                    if self.tmp[of.tradeID]['closeprice']>self.tmp[of.tradeID]['openprice']:
                        self.tmp[of.tradeID]['pl_side'] = "tp"
                    else:
                        self.tmp[of.tradeID]['pl_side'] = "sl"

                elif self.tmp[of.tradeID]["buysell"]<0:
                    if self.tmp[of.tradeID]['closeprice']>self.tmp[of.tradeID]['openprice']:
                        self.tmp[of.tradeID]['pl_side'] = "sl"
                    else:
                        self.tmp[of.tradeID]['pl_side'] = "tp"

                # log result
                self.evt.consoleLog(self.tmp)

 
admin
We will supplement the Web API for already closed orders. 
 
Lee Chang Jun
Original Posted by - b'admin':

Hello Chang Jun, the current version of Web API doesn't support info querying for closed orders.

For your case, it needs to create a temporary variable to manage and check the order fill status.

Here's a working example. Please refer to line #29 - #52 for the checking logic.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from AlgoAPI import AlgoAPIUtil, AlgoAPI_Backtest

class AlgoEvent:
    def __init__(self):
        self.isFirstTrade = False
        self.tmp = {}
        
    def start(self, mEvt):
        self.evt = AlgoAPI_Backtest.AlgoEvtHandler(self, mEvt)
        self.evt.start()

    def on_marketdatafeed(self, md, ab):
        # trade only 1 time for demo purpose
        if not self.isFirstTrade:
            self.isFirstTrade = True

            order = AlgoAPIUtil.OrderObject(
                instrument = md.instrument,
                openclose = 'open', 
                buysell = 1,    # buy
                ordertype = 0,  # market order
                volume = 0.01,
                takeProfitLevel = md.lastPrice*1.1,
                stopLossLevel = md.lastPrice*0.9
            )
            self.evt.sendOrder(order)

    def on_orderfeed(self, of):
        if of.status=="success":
            if of.openclose=="open":
                self.tmp[of.tradeID] = {
                    "buysell": of.buysell,
                    "openprice": of.fill_price, 
                    "closeprice": None, 
                    "pl_side": None
                }
            elif of.openclose=="close":

                self.tmp[of.tradeID]['closeprice'] = of.fill_price

                # check whether it is profitable or lossing trade
                if self.tmp[of.tradeID]["buysell"]>0:
                    if self.tmp[of.tradeID]['closeprice']>self.tmp[of.tradeID]['openprice']:
                        self.tmp[of.tradeID]['pl_side'] = "tp"
                    else:
                        self.tmp[of.tradeID]['pl_side'] = "sl"

                elif self.tmp[of.tradeID]["buysell"]<0:
                    if self.tmp[of.tradeID]['closeprice']>self.tmp[of.tradeID]['openprice']:
                        self.tmp[of.tradeID]['pl_side'] = "sl"
                    else:
                        self.tmp[of.tradeID]['pl_side'] = "tp"

                # log result
                self.evt.consoleLog(self.tmp)

Thank you!

 
Kumar
Original Posted by - b'admin': We will supplement the Web API for already closed orders. 
I am also looking to query closed order info.