Skip to content

Instantly share code, notes, and snippets.

@PVSS
Last active October 1, 2022 16:58
Show Gist options
  • Save PVSS/6ce1728f62ce42128aa72bfeb8493378 to your computer and use it in GitHub Desktop.
Save PVSS/6ce1728f62ce42128aa72bfeb8493378 to your computer and use it in GitHub Desktop.
Combine Stop Loss with Trailing Stop Loss
from time import sleep
from pushbullet import PushBullet
from pywebio.input import *
from pywebio.output import *
from pywebio.session import *
import requests
MONTH='SEP'
YEAR='22'
strike_price=18000
purchase_price=530
csl=purchase_price*1.2
PUSHBULLET_ACCESS_TOKEN='XXXXXXXXXXX'
class KiteApp:
def __init__(self, enctoken):
self.headers = {"Authorization": f"enctoken {enctoken}"}
self.session = requests.session()
self.root_url = "https://api.kite.trade"
self.session.get(self.root_url, headers=self.headers)
def ltp(self, instruments):
data = self.session.get(f"{self.root_url}/quote/ltp", params={"i": instruments}, headers=self.headers).json()["data"]
return data
def get_CE_PE_ltp(strike_price):
ce_tradingsymbol='NFO:NIFTY'+YEAR+MONTH+str(strike_price)+'CE'
pe_tradingsymbol='NFO:NIFTY'+YEAR+MONTH+str(strike_price)+'PE'
res= kite.ltp([ce_tradingsymbol,pe_tradingsymbol])
return res[ce_tradingsymbol]['last_price'],res[pe_tradingsymbol]['last_price']
kite=KiteApp(enctoken=enc_token)
pb = PushBullet(PUSHBULLET_ACCESS_TOKEN)
# CSL with TSL logic with pushbullet
current_low = purchase_price
while True:
ce_ltp,pe_ltp=get_CE_PE_ltp(strike_price)
current_price=ce_ltp+pe_ltp
print(current_price)
if current_price > csl:
push = pb.push_note('Stop Loss Hit', 'CSL Stop loss Hit')
break
else:
change = current_low-current_price
perc_change=(change/float(current_low))*100
if (current_low-current_price) > 2:
current_low = current_price
csl = current_price*1.2
push = pb.push_note('Trailing Stop Loss to: {}'.format(csl), 'Stop loss Change')
print('Trailing Stop Loss to: {}'.format(csl))
sleep(2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment