Margin calculator API has issue

aditya_chauhan
Let me explain you briefly what i am doing in the following trade and how margin calulator API didn't work well

Ideally I want to short the ATM PE & CE but for a given traded_lot without hedging, my net available fund may not be sufficient. so what I do is I will divide the number of lots to be traded (traded_lot) in two parts.

trade_lot = lot1(naked ATM PE & CE sell) + lot2 ( ATM PE & CE with PE & CE hedges)

entry1 from snapshot: I first sold the lot1, it went smoothly as the fund was sufficient. Once lot1 were executed, i checked the available fund

entry2 from snapshot: I understand that for lot2 to execute smoothly, remaining available fund should be higher than the initial amount required. I first place the hedges order and keep checking the order id 's status, if status of order corresponding to hedge order ID is COMPLETE, i place the ATM PE& CE sell. This makes entry2 complete. I thought this was the fully safe entry as hedges were already present so my PE/CE sell will go smoothly but that didn't happen.

exit from snapshot: Once a predefined target/SL is hit, I exit all the trades at the same time.

Manual exit from snapshot: Since my 34900 PE (125 quantity) was rejected, at the exit time, algorithm bought additional 125 quantity of the same and I had to manually exit it once I saw this havoc. It was unfortunate day for me.

What could have gone wrong? Please help me. I will post the function used to estimate the initial and final margin required.



Tagged:
  • aditya_chauhan
    I am attaching a snapshot(copying the text changes the indentation) of the function used to execute the entry2.
    I used the margin API for calculating margin per lot for PE,CE,PE hedge & CE hedge. What ever is the initial and final margin required, i add the additional 1k for safety.




    1.JPG 141.9K
    2.JPG 113.4K
    3.JPG 60.2K
  • SRIJAN
    It might be because you bought CE at :01 and PE at :02. After that,you shorted CE at :02 and PE at :02 . So,there was one second difference between CE LONG and SHORT,so you got margin benefit there. Whereas PE LONG and SHORT happened at the same second,so the margin couldn't be released. I suggest you try to add some delay after buying hedges and then check if this is happening .
  • rakeshr
    Yes, you would need a complete margin amount before the margin benefits kicks in. This thread explains more.
  • aditya_chauhan
    As per the coded logic, I bought PE hedge at :02 and when tracked the status of PE hedge order ID, it said order is COMPLETE. Then only i go ahead and placed the PE sell order. Two cases might have happened ;
    1. PE hedge order ID status is COMPLETE but actual PE hedge order is not executed and hence when PE sell order was placed margin benefit didn't kick in.
    2. PE hedge order ID status is COMPLETE and actual PE hedge order is executed and margin benefit should kick in with delay but before that PE sell order was placed.

    which one happened in my case?
  • aditya_chauhan
    @rakeshr could you please elaborate your statement: you would need a complete margin amount before the margin benefits kicks in

    Example , i have a basket1 in order (1. PE_hedge 2. CE_hedge, 3. PE sell, 4. CE sell)
    i have another basket2 in order ( 1. PE sell, 2. CE sell)

    Lets say Initial amount for basket1 is initial_amount1 and final amount is final_amount1,
    where final_amount1 < initial_amount1

    similarly final_amount2<initial_amount2.

    lets assume i have available fund such that initial_amount1<available fund<initial amount2
  • aditya_chauhan
    Are you suggesting that if i want to execute the basket1 in the mentioned sequence(1->2->3->4), my fund should be higher than initial_amount2?
  • SRIJAN
    SRIJAN edited March 2022
    #2 happened. Just add some delay and it should work. No, your fund need not be higher than initial amount2. Just buy hedges and place short orders after a few seconds delay.
  • aditya_chauhan
    today(11th mar 2022), i tested what you said, i gave 3 second delay after PE hedges's order ID status became COMPLETE and then placed CE/PE sell order. still one of the leg got rejected.

    test entry1 from snapshot: today i tried placing CE/PE with hedges, first I executed the buy hedges and then sell CE/PE and and number of lot traded is available fund/ initial amount.
    it went smoothly


    later my net fund should be availabe amount-final amount of previous trade.

    test entry 2 from snapshot: now i placed the similar order but this time fund used was previous net amount.

    after hedges buy, i am giving 3 second delay before PE/CE is sold. I wonder why 34000 PE got rejected?


    after these trades, i manually exited all the trades.

    Issue still persists. does margin benefit kick in takes more than 3 sec?



  • aditya_chauhan
    Output of the previously used functions. Please not that i have added 3 second delay once PE/CE hedge buy is complete. other than that code remains same.


  • SRIJAN
    Then Idk bro. I thought adding delay would work. Maybe Sujith or Rakesh Sir can clarify.
  • SRIJAN
    I guess even after buying hedge,your margin is actually not sufficient to sell another leg. Not sure though.
  • aditya_chauhan
    If you check the output log attached in previous reply, you can see that for entry2 , available fund is 210134

    initial margin requirement per lot for the entry 2 basket is : 70667 + 1000(safety margin from my side)=71667

    if you see two lots of basket in entry two has been placed , which means, if my hedges has been bought successfully and after 3 seconds, if i have 71667*2 amount, trade should go smoothly but that is not the case. One of the leg got rejected. Margin was more than enough.

    @rakeshr @sujith need your help. Please look into this

    I have pasted the part of routine used to execute these trades.
  • aditya_chauhan
    If you need, i can share the python routine used to execute this trade ...in a txt file .
  • aditya_chauhan
    Yes, you would need a complete margin amount before the margin benefits kicks in. This thread explains more.
    @rakeshr how much time it takes to pass on the margin benefit?

    I want to short a same strike PE & CE and lets assume margins required initial is initial_margin and final margin is final_margin. If i have available_fund which is higher than initial_margin. Once the trade is executed, is there any chance that my net available fund is much less than available_fund - final_margin?
Sign In or Register to comment.