sample program for testing risk management

Imran
'''
hello traders ..
this is a sample program to test risk reward...by randomly buying selling
edit the sl and tp
note: trade at your own risk
'''
import logging
from kiteconnect import KiteConnect
from kiteconnect import KiteTicker

import datetime,time,os,random;


trd_portfolio ={325121:"AMBUJACEM",40193:"APOLLOHOSP",41729:"APOLLOTYRE",49409:"ARVIND",54273:"ASHOKLEY",60417:"ASIANPAINT",1510401:"AXISBANK",1214721:"BANKINDIA",94977:"BATAINDIA",108033:"BHARATFORG",2714625:"BHARTIARTL",558337:"BOSCHLTD",134657:"BPCL",2763265:"CANBK",320001:"CASTROLIND",3905025:"CEATLTD",177665:"CIPLA",5215745:"COALINDIA",3876097:"COLPAL",197633:"DABUR",3513601:"DCBBANK",261889:"FEDERALBNK",1207553:"GAIL",340481:"HDFC",341249:"HDFCBANK",345089:"HEROMOTOCO"}

kws = "";
kite = "";

is_buy_sell_done = {};
for x in trd_portfolio:
is_buy_sell_done[x] = False;

def get_login(api_k,api_s):
global kws,kite;
kite = KiteConnect(api_key=api_k)

print("[*] Generate access Token : ",kite.login_url())
request_tkn = input("[*] Enter Your Request Token Here : ");

data = kite.generate_session(request_tkn, api_secret=api_s)
kite.set_access_token(data["access_token"])
kws = KiteTicker(api_k, data["access_token"])

api_k = "xxxxxxxxxxxxx";
api_s = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

trd_capital = int(input("[*] Enter Trading Capital (eg. 10000, 25000) : "));

get_login(api_k,api_s);

def do_buy_sell(instrument_token,company_data):
global trd_capital,trd_portfolio;
quant = int(trd_capital//company_data['last_price']);
sl = 20/quant;
tp = 50/quant;
number = random.randint(1,2000)%2;

kite.place_order(tradingsymbol=trd_portfolio[instrument_token],price=company_data['last_price'],variety=kite.VARIETY_BO,exchange=kite.EXCHANGE_NSE,transaction_type=kite.TRANSACTION_TYPE_BUY if number==0 else kite.TRANSACTION_TYPE_SELL,quantity=quant,squareoff=tp , stoploss=sl ,order_type=kite.ORDER_TYPE_LIMIT,product=kite.PRODUCT_BO)

print(("BUY" if number==0 else "SELL"),quant," OF ",trd_portfolio[instrument_token]," ON TIME ",company_data['timestamp']," AT PRICE ",ohlc[instrument_token][5][1]);



def on_ticks(ws, ticks):
global is_buy_sell_done;
for company_data in ticks:
if not is_buy_sell_done[company_data['instrument_token']]:
try:
do_buy_sell(company_data['instrument_token'],company_data);#do buy or sell on random number
except Exception as e:
print("EXC ",e);

is_buy_sell_done[company_data['instrument_token']] = True; #buy or sell is done

def on_connect(ws, response):
ws.subscribe([x for x in trd_portfolio])
ws.set_mode(ws.MODE_FULL,[x for x in trd_portfolio])

# Assign the callbacks.
kws.on_ticks = on_ticks
kws.on_connect = on_connect

kws.connect()
Sign In or Register to comment.