Help needed for OHLC data in excel and fire order directly

shrivardhan
How can I get OHLC data in excel and fire orders automatically for "Opening range breakout strategy"? Do i need kiteconnect or just publisher will fine?
  • Imran
    hii @shrivardhan
    you would need kiteconnect for it:


    use can use this code:
    from kiteconnect import WebSocket;
    from kiteconnect import KiteConnect;
    import time,os,datetime,math;


    # orb code with trail
    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",356865:"HINDUNILVR",1346049:"INDUSINDBK",415745:"IOC",424961:"ITC",3001089:"JSWSTEEL",492033:"KOTAKBANK",2061825:"KTKBANK",2939649:"LT",2815745:"MARUTI",4464129:"OIL",633601:"ONGC",636673:"ORIENTBANK",2905857:"PETRONET",3834113:"POWERGRID",738561:"RELIANCE",779521:"SBIN",1887745:"STAR",895745:"TATASTEEL",3050241:"YESBANK"}

    buy_percentage_distribution = []
    sell_percentage_distribution = []
    trd_tkn1 = [];
    live_open = {}
    live_high = {}
    live_low = {}
    live_close = {}
    buy_count = {}
    sell_count = {}

    for x in trd_portfolio:
    trd_tkn1.append(x)
    live_open[x] = 0
    live_high[x] = 0
    live_low[x] = 0
    live_close[x] = 0
    buy_count[x] = 0
    sell_count[x] = 0

    #--- Taking input credentials from user to make connection with ZERODHA API
    c_id = "xxxxx"
    ak = "xxxxxxxxxxx"
    asecret = "xxxxxxxxxxxxxxxxxxxx"

    try:
    trd_capital = int(input("[*] Enter Trading Capital (eg. 10000, 25000) : "));
    trd_tp = float(input("[*] Enter your Reward (eg.2500, 4500, 6000 ) : "))
    trd_sl = float(input("[*] Enter your Risk (eg.1200, 1500, 1800 ) : "))
    trd_inc_dec = float(input("[*] Enter Percentage Increment Decrement (eg. 0.25%,0.5%,1%,2%) : "))/100
    time_frame = int(input("[*] Enter timeframe (eg. 5, 15) : "));


    print("------------------------ ENTER BUY PRICE PERCENTAGE DISTRIBUTION ------------------------")
    total_percentage = 0;
    while total_percentage is not 100:
    print("\t[+] Please make total percentage 100")
    total_percentage = 0;
    buy_percentage_distribution = []
    for x in range(1,5):
    buy_percentage_distribution.append(int(input("\t"+str(x)+": ")))
    total_percentage+=buy_percentage_distribution[x-1]


    print("------------------------ ENTER SELL PRICE PERCENTAGE DISTRIBUTION ------------------------")
    total_percentage = 0;
    while total_percentage is not 100:
    print("\t[+] Please make total percentage 100")
    total_percentage = 0;
    sell_percentage_distribution = []
    for x in range(1,5):
    sell_percentage_distribution.append(int(input("\t"+str(x)+": ")))
    total_percentage+=sell_percentage_distribution[x-1]

    except Exception as e:
    print(e)
    print("[!] Please enter correct data")
    i = input("[!] PRESS ENTER TO EXIT... ")
    exit();


    kite = KiteConnect(api_key=ak)
    print("[*] Generate access Token : ",kite.login_url())
    request_tkn = input("[*] Enter Your Request Token Here : ")[-32:];
    data = kite.request_access_token(request_tkn, secret=asecret)
    kite.set_access_token(data["access_token"])
    kws = WebSocket(ak, data["public_token"], c_id)


    # VARIABLE DECLARATION
    is_ohlc_calculation_done = False;

    def calculate_ohlc(tick):
    global live_open,live_high,live_low,live_close;
    for company_data in tick:
    live_open[company_data['instrument_token']] = company_data['ohlc']['open']
    live_high[company_data['instrument_token']] = company_data['ohlc']['high']
    live_low[company_data['instrument_token']] = company_data['ohlc']['low']
    live_close[company_data['instrument_token']] = company_data['ohlc']['close']

    def do_buy_sell_operation(tick):
    global live_high,live_low,buy_count,sell_count,trd_inc_dec
    global trd_capital,buy_percentage_distribution,sell_percentage_distribution
    global trd_tp,trd_sl;

    for company_data in tick:
    var_x1 = round(live_high[company_data['instrument_token']]+company_data['ohlc']['open']*(trd_inc_dec*buy_count[company_data['instrument_token']]),2);

    if company_data['last_price'] >= var_x1 and buy_count[company_data['instrument_token']]<4:
    quant = int((trd_capital*(buy_percentage_distribution[buy_count[company_data['instrument_token']]]/100))//company_data['last_price']);
    stoploss = round((trd_tp*buy_percentage_distribution[buy_count[company_data['instrument_token']]]/100)/quant,2)
    takeprofit = round((trd_sl*buy_percentage_distribution[buy_count[company_data['instrument_token']]]/100)/quant,2)

    kite.order_place(tradingsymbol=trd_portfolio[company_data['instrument_token']], exchange="NSE", quantity=quant , transaction_type="BUY",order_type="LIMIT", price=company_data['last_price'], squareoff_value=takeprofit, stoploss_value=stoploss, variety="bo",validity="DAY");

    print((buy_count[company_data['instrument_token']]+1),". BUY : ",trd_portfolio[company_data['instrument_token']]," AT ",company_data['last_price']," => ",var_x1," QUANTITY : ",quant)

    buy_count[company_data['instrument_token']]+=1;



    var_x2 = round(live_low[company_data['instrument_token']]-company_data['ohlc']['open']*(trd_inc_dec*buy_count[company_data['instrument_token']]),2);

    if company_data['last_price'] <= var_x2 and sell_count[company_data['instrument_token']]<4:
    quant = int((trd_capital*(sell_percentage_distribution[sell_count[company_data['instrument_token']]]/100))//company_data['last_price']);
    stoploss = round((trd_tp*sell_percentage_distribution[sell_count[company_data['instrument_token']]]/100)/quant,2)
    takeprofit = round((trd_sl*sell_percentage_distribution[sell_count[company_data['instrument_token']]]/100)/quant,2)

    kite.order_place(tradingsymbol=trd_portfolio[company_data['instrument_token']], exchange="NSE", quantity=quant , transaction_type="SELL",order_type="LIMIT", price=company_data['last_price'], squareoff_value=takeprofit, stoploss_value=stoploss, variety="bo",validity="DAY");

    print((sell_count[company_data['instrument_token']]+1),". SELL : ",trd_portfolio[company_data['instrument_token']]," AT ",company_data['last_price']," => ",var_x2," QUANTITY : ",quant)

    sell_count[company_data['instrument_token']]+=1;



    def on_tick(tick, ws):
    # ACCESSING GLOBAL VARIABLES
    global is_ohlc_calculation_done;

    if is_ohlc_calculation_done:
    do_buy_sell_operation(tick);

    if not is_ohlc_calculation_done:
    is_ohlc_calculation_done = True;
    calculate_ohlc(tick);

    def on_connect(ws):
    ws.subscribe(trd_tkn1)
    ws.set_mode(ws.MODE_FULL, trd_tkn1)


    kws.on_tick = on_tick
    kws.on_connect = on_connect
    kws.enable_reconnect(reconnect_interval=5, reconnect_tries=50)

    time_to_go = "09"+str(15+time_frame)+"00";

    while int(time.strftime("%H%M%S")) != int(time_to_go):
    print(time_to_go," : ",time.strftime("%H%M%S"))

    kws.connect()
  • shrivardhan
    Thanks a lot Imran.
    Is there any tutorial available to understand these in detail...
Sign In or Register to comment.