예제 #1
0
def get_order_string(scrip, action, price, stoploss_buy, stoploss_sell):
    outstring = ""
    
    if (price == None):
        return None

    if action == "BUY":
        trigger_price = float(utils.get_floating_value(price)) +  float(0.10)
        price = trigger_price
        target = float(utils.get_floating_value(float(trigger_price) * float(config_dict['target_lock'])))
        if (stoploss_buy == None):
            stoploss = float(utils.get_floating_value(float(trigger_price) * float(config_dict['stoploss_lock'])))
        else:
            stoploss = float(utils.get_floating_value(float(price) - float(stoploss_buy)))
        live_price = utils.get_floating_value(float(price) - float(float(price) * 0.001))
        outstring = scrip + " " + action + " " + str(trigger_price) + " " + str(price) + " " + str(target) + " " + str(stoploss) + " " + str(live_price) + "\n"

    if action == "SELL":
        trigger_price = float(utils.get_floating_value(price)) -  float(0.10)
        price = trigger_price
        target = float(utils.get_floating_value(float(trigger_price) * float(config_dict['target_lock'])))
        if (stoploss_sell == None):
            stoploss = float(utils.get_floating_value(float(trigger_price) * float(config_dict['stoploss_lock'])))
        else:
            stoploss = float(utils.get_floating_value(float(stoploss_sell) - float(price)))

        live_price = utils.get_floating_value(float(price) + float(float(price) * 0.001))
        outstring = scrip + " " + action + " " + str(trigger_price) + " " + str(price) + " " + str(target) + " " + str(stoploss) + " " + str(live_price) +"\n"

    return outstring
예제 #2
0
def get_order_string(scrip, action, price):
    outstring = ""

    if action == "BUY":
        trigger_price = float(utils.get_floating_value(price)) + float(0.10)
        price = trigger_price
        target = float(
            utils.get_floating_value(
                float(trigger_price) * float(config_dict['target_lock'])))
        stoploss = float(
            utils.get_floating_value(
                float(trigger_price) * float(config_dict['stoploss_lock'])))
        outstring = scrip + " " + action + " " + str(
            trigger_price) + " " + str(price) + " " + str(target) + " " + str(
                stoploss) + "\n"

    if action == "SELL":
        trigger_price = float(utils.get_floating_value(price)) - float(0.10)
        price = trigger_price
        target = float(
            utils.get_floating_value(
                float(trigger_price) * float(config_dict['target_lock'])))
        stoploss = float(
            utils.get_floating_value(
                float(trigger_price) * float(config_dict['stoploss_lock'])))
        outstring = scrip + " " + action + " " + str(
            trigger_price) + " " + str(price) + " " + str(target) + " " + str(
                stoploss) + "\n"

    return outstring
예제 #3
0
def calculate_stats(scrip, buy, sell, ohlc):
    outstring = scrip
    if float(ohlc['high']) > float(buy):
        diff_buy = float(
            utils.get_floating_value(float(ohlc['high']) - float(buy)))
        buy_percentage = float(
            utils.get_floating_value(
                float(float(diff_buy) * float(100)) / float(buy)))
        diff_stoploss = float(
            utils.get_floating_value(float(buy) - float(ohlc['low'])))
        buy_stoploss_percentage = float(
            utils.get_floating_value(
                float(float(diff_stoploss) * float(100)) / float(buy)))
        outstring = outstring + " Buy " + str(diff_buy) + " " + str(
            buy_percentage) + " "
        outstring = outstring + str(diff_stoploss) + " " + str(
            buy_stoploss_percentage) + "\n"
        print outstring

    if float(ohlc['low']) < float(sell):
        outstring = ""
        outstring = scrip
        diff_sell = float(
            utils.get_floating_value(float(sell) - float(ohlc['low'])))
        sell_percentage = float(
            utils.get_floating_value(
                float(float(diff_sell) * float(100)) / float(sell)))
        diff_stoploss = float(
            utils.get_floating_value(float(ohlc['high']) - float(sell)))
        sell_stoploss_percentage = float(
            utils.get_floating_value(
                float(float(diff_stoploss) * float(100)) / float(sell)))
        outstring = outstring + " Sell " + str(diff_sell) + " " + str(
            sell_percentage) + " "
        outstring = outstring + str(diff_stoploss) + " " + str(
            sell_stoploss_percentage) + "\n"
        print outstring
def get_overlapping_candle(fno):
    global bullish_engulfing
    global bearing_engulfing
    global open_low, open_high, close_low, close_high
    global bar, close_wicks

    for each in fno:
        # Green candle
        # 0 - open lower than low of previous, close higher than high of previous
        if float(fno[each]['open'][0]) <= float(fno[each]['low'][1]):
            if float(fno[each]['close'][0]) >= float(fno[each]['high'][1]):
                bullish_engulfing.append(each)

        if float(fno[each]['open'][0]) >= float(fno[each]['high'][1]):
            if float(fno[each]['close'][0]) <= float(fno[each]['low'][1]):
                bearing_engulfing.append(each)

        if float(fno[each]['open'][0]) == float(fno[each]['high'][0]):
            low_wick = utils.get_floating_value(
                float(fno[each]['low'][0]) * float(0.005))
            if (float(fno[each]['close'][0]) -
                    float(fno[each]['low'][0])) <= float(low_wick):
                open_high.append(each)

        if float(fno[each]['open'][0]) == float(fno[each]['low'][0]):
            high_wick = utils.get_floating_value(
                float(fno[each]['high'][0]) * float(0.005))
            if (float(fno[each]['high'][0]) -
                    float(fno[each]['close'][0])) <= float(high_wick):
                open_low.append(each)

        if float(fno[each]['open'][0]) > float(fno[each]['close'][0]):
            low_wick = utils.get_floating_value(
                float(fno[each]['low'][0]) * float(0.005))
            if (float(fno[each]['close'][0]) -
                    float(fno[each]['low'][0])) <= float(low_wick):
                close_wicks.append(each)

        if float(fno[each]['open'][0]) < float(fno[each]['close'][0]):
            high_wick = utils.get_floating_value(
                float(fno[each]['high'][0]) * float(0.005))
            if (float(fno[each]['high'][0]) -
                    float(fno[each]['close'][0])) <= float(high_wick):
                close_wicks.append(each)

        if float(fno[each]['close'][0]) == float(fno[each]['low'][0]):
            close_low.append(each)

        if float(fno[each]['close'][0]) == float(fno[each]['high'][0]):
            close_high.append(each)

        if float(fno[each]['open'][0]) == float(fno[each]['low'][0]):
            if float(fno[each]['close'][0]) == float(fno[each]['high'][0]):
                bar.append(each)

        if float(fno[each]['open'][0]) == float(fno[each]['high'][0]):
            if float(fno[each]['close'][0]) == float(fno[each]['low'][0]):
                bar.append(each)

    print "---------------------------------------------------------------------------"
    print "Bar\n"
    print bar

    print "---------------------------------------------------------------------------"
    print "Open high and close within 0.5%"
    print open_high

    print "---------------------------------------------------------------------------"
    print "Open low and close within 0.5%"
    print open_low

    print "---------------------------------------------------------------------------"
    print "Bullish Engulfing"
    print bullish_engulfing

    print "---------------------------------------------------------------------------"
    print "Bearish Engulfing"
    print bearing_engulfing

    print "---------------------------------------------------------------------------"
    print "close == low"
    print close_low

    print "---------------------------------------------------------------------------"
    print "close == high"
    print close_high

    print "---------------------------------------------------------------------------"
    print "Close near 0.5% wicks"
    print close_wicks
    print "---------------------------------------------------------------------------"
예제 #5
0
def main():
    print "Executing CBO Algo for Equities"
    print "-------------------------------"

    global fno_dict
    global base_dict
    global config_dict
    global orders
    global sub_list
    global fno_mapping
    global order_dict
    #TODO: Add argparser for validating input
    if len(sys.argv) < NO_OF_PARAMS:
        print "Invalid number of params"
        return

    # read config file
    config_dict = utils.read_config_file()

    # get list of fno
    fno_dict = utils.get_fno_dict()

    # get yesterdays high low
    base_dict = get_yesterdays_fno_ohlc(sys.argv[1])
    #simulate(sys.argv[2])

    #open kite connection
    if len(sys.argv) == int(NO_OF_PARAMS) + int(1):
        request_token = sys.argv[2]
    else:
        request_token = None

    #api_key, access_token, kite = kite_utils.kite_login(request_token)

    # get instrument list
    quote_list = []
    data = kite.instruments("NFO")
    for each in data:
        if each['instrument_type'] != 'FUT':
            continue

        if config_dict['contract_str'] not in each['tradingsymbol']:
            continue

        entry = "NFO:" + str(each['tradingsymbol'])
        quote_list.append(entry)
        sub_list.append(int(each['instrument_token']))
        fno_mapping[int(each['instrument_token'])] = str(each['tradingsymbol'])

    print "=============================="
    print fno_mapping
    print "=============================="

    # open file to write buy/sell orders
    fp = open(config_dict['cbo_fno_seed_file'], "w")
    count = int(0)
    quotes = kite.quote(quote_list)
    for each in quotes:

        scrip = each.split(":")[1].strip("\n")
        m = re.search("\d", scrip)
        if m:
            scrip = scrip[:m.start()]

        if float(quotes[each]["ohlc"]["open"]) < float(
                config_dict['start_price']):
            continue

        if float(quotes[each]["ohlc"]["open"]) > float(
                config_dict['end_price']):
            continue

        count = int(count) + int(1)
        if scrip in base_dict:
            scrip_fno = scrip + "18MARFUT"
            print scrip_fno
            buy, sell = generate_orders(scrip, base_dict[scrip],
                                        quotes[each]['ohlc']['open'])
            if (buy != None):
                buy_dict = {}
                each = buy.split(" ")
                buy_dict['price'] = each[2]
                buy_dict['target'] = float(
                    utils.get_floating_value(float(each[2]) + float(each[4])))
                buy_dict['stoploss'] = float(
                    utils.get_floating_value(float(each[2]) - float(each[5])))
                buy_dict['trade_active'] = False
                order_dict[scrip_fno] = {}
                order_dict[scrip_fno]['buy'] = buy_dict
                fp.write(buy)
            if (sell != None):
                sell_dict = {}
                each = sell.split(" ")
                sell_dict['price'] = each[2]
                sell_dict['target'] = float(
                    utils.get_floating_value(float(each[2]) - float(each[4])))
                sell_dict['stoploss'] = float(
                    utils.get_floating_value(float(each[2]) + float(each[5])))
                sell_dict['trade_active'] = False
                order_dict[scrip_fno]['sell'] = sell_dict
                fp.write(sell)
    fp.close()
    print "-------------------------------------------------------"
    print order_dict
    print "-------------------------------------------------------"

    kws = KiteTicker(api_key, access_token, debug=False)
    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.on_error = on_error
    kws.on_noreconnect = on_noreconnect
    kws.on_reconnect = on_reconnect
    kws.on_order_update = on_order_update
    kws.connect()