It looks like you're new here. If you want to get involved, click one of these buttons!
Public Function GetTrailMTM (
TrdSym As String,
MTM As Double,
BaseMTM As Double,
Optional StgyCode As String = "ZZ"
) As Double
Parameters
TrdSym
Type: System.String
TrdSym
MTM
Type: System.Double
MTM
BaseMTM
Type: System.Double
BaseMTM
StgyCode (Optional)
Type: System.String
StgyCode
Return Value
Type: Double
Trail MTM
This function is purely meant for Excel and it has no connection with Kite API.
Let illustrate with example.
Assume you bought 100 Qty of Stock 'AXISBANK'
and want to exit when your loss reaches -300 and also want to move your max-loss for every Rs300 movement in your favor..
Cell A1 = "NSE"
B1 = "AXISBANK"
C1 = LTP 'GetRTD(A1,B1,"LAST")
D1 = MTM 'GetRTD(A1,B1,"MTM") This cell will display your real-time mtm
E1 = GetTrailMTM(B1,D1,300,"ZZ") 'BaseMtm is nothing but how much MTM you want to trail
This E1 cell will initially display -300 i.e your max loss.
Assume your MTM has crossed 300 i.e. D1 = 320, now E1 will display 0 i.e. Max-loss trailed from -300 to 0. When your MTM crosses 600, E1 will display 300.
What is the use of E1?
In another cell you can write a simple IF function to place a opposite order and close your position when MTM was below TrailMTM.
F1 = IF(D1<=E1,PlaceOrder(........),"TrailMTM not hit")
In the above assumption, your MTM has crossed 600 and your trail MTM is now 300, the moment your real-time bounces below trail MTM i.e. 300, your IF condition's will be triggered and close your position.
The above is just example, you need make robust code to achieve the above without any error and multiple firing.
Just you need to modify the PlaceOrder UDF to place order only once for a signal..
You can refer this link for example
https://howutrade.in/2017/08/31/restrict-multiple-order-firing-excel-upstox-api/