def exercise_4():
    """
    :return: nothing, it resolves the exercise 4
    """
    fridges = [Fridge(1, "haceb"), Fridge(2, "lg"), Fridge(3, "ibm"),
               Fridge(4, "haceb"), Fridge(5, "lg"), Fridge(6, "ibm"),
               Fridge(7, "haceb"), Fridge(8, "lg"), Fridge(9, "ibm"),
               Fridge(8, "lg"), Fridge(9, "ibm")]

    orders = [Order("eafit", 10), Order("la14", 2),
              Order("olimpica", 4), Order("exito", 1)]

    manage_fridges(fridges, orders)
Example #2
0
 def initialize(self):
     self.dishes_list = []
     for row in db.fetch():
         self.dishes_list.append(Dish(row[0],row[1],row[2],row[3]))
     self.order_list = []
     self.order_no = []
     for row in db.fetchorder():
         self.order_list.append(Order(row[0],row[1],row[2],row[3],row[4],row[5]))    
         self.order_no.append(str(row[0]))
Example #3
0
class MyTest(unittest.TestCase):
    item = Item()
    order = Order()


    def test_add_item(self):
        result = self.item.add_item('test_item', 'test_type', 100)
        self.assertTrue(result)

    def test_add_item_view1(self):
        result = self.item.add_item('', '', 100)
        self.assertFalse(result)

    def test_add_item_view2(self):
        result = self.item.add_item('test', 'type', 'asd')
        self.assertFalse(result)

    def test_add_item_view3(self):
        result = self.item.add_item('', '', -100)
        self.assertFalse(result)

    def test_show_items(self):
        data = self.item.show_item()
        actual_result = len(data)
        expected_result = 9
        self.assertEqual(expected_result, actual_result)

    def test_search_item(self):
        data = self.item.search_item('momo')
        actual_result = len(data)
        expected_result = 2
        self.assertEqual(expected_result, actual_result)

    def test_search_items1(self):
        data = self.item.search_item('noodle')
        actual_result = len(data)
        expected_result = 0
        self.assertEqual(expected_result, actual_result)

    def test_update_item(self):
        result = self.item.update_item(4, "khana", "non-veg", 220)
        self.assertTrue(result)

    def test_update_item1(self):
        result=self.item.update_item(4, "khana", "veg", 200)
        self.assertFalse(result)

    def test_delete_item(self):
        result=self.item.delete_item(4)
        self.assertFalse(result)

    def test_add_order(self):
        result=self.order.add_order(4[])
        self.assertTrue(result)
Example #4
0
class MyTest(unittest.TestCase):
    item = Item()
    order = Order()

    def test_add_item(self):
        result = self.item.add_item('test_item', 'test_type', 100)
        self.assertTrue(result)

    def test_add_item_view(self):
        result = self.item.add_item('', '', 100)
        self.assertFalse(result)

    def test_add_item_view_1(self):
        result = self.item.add_item('test', 'type', 'asd')
        self.assertFalse(result)

    def test_add_item_view_2(self):
        result = self.item.add_item('', '', -100)
        self.assertFalse(result)

    def test_show_items(self):
        data = self.item.show_item()
        actual_result = len(data)
        expected_result = 9
        self.assertEqual(expected_result, actual_result)

    def test_search_item(self):
        data = self.item.search_item('momo')
        actual_result = len(data)
        expected_result = 2
        self.assertEqual(expected_result, actual_result)

    def test_search_items1(self):
        data = self.item.search_item('noodle')
        actual_result = len(data)
        expected_result = 0
        self.assertEqual(expected_result, actual_result)

    def test_update_item(self):
        result = self.item.update_item(5, "Masu", "veg", 600)
        self.assertTrue(result)

    def test_delete_item(self):
        result = self.item.delete_item(5)
        self.assertTrue(result)

    def test_delete_item_1(self):
        result = self.item.delete_item("aa")
        self.assertFalse(result)

    def test_add_order(self):
        result = self.order.add_order(5, ("burger", "chicken", "150"))
        self.assertTrue(result)
Example #5
0
def main():
    rows, cols, n_drones, deadline, max_load = input_ints()
    """Products"""
    number_of_product_types = input_int()
    product_weights = input_ints()
    """Warehouses"""
    number_of_warehouses = input_int()
    warehouses = []
    for _ in range(number_of_warehouses):
        position = input_intpair()
        product_stock = input_ints()
        warehouses.append(Warehouse(position, product_stock))
    """Drones"""
    drones = [Drone(warehouses[0].pos) for _ in range(n_drones)]
    Drone.capacity = max_load
    """Customer orders"""
    number_of_orders = input_int()
    orders = []
    for _ in range(number_of_orders):
        position = input_intpair()
        number_of_products_ordered = input_int()
        products_ordered = input_ints()
        orders.append(Order(position, products_ordered))

    try:
        print(input())
    except EOFError:
        pass
    else:
        print("There was something left in the input data, exiting")
        exit(1)

    print("Warehouses: {}, Drones: {}, Orders: {}".format(
        len(warehouses), n_drones, len(orders)))
    print("Drone capacity: {}".format(Drone.capacity))

    def get_first_available_drone():
        return sorted(drones, key=lambda d: d.turn)[0]

    def get_closest_order(pos):
        return sorted(orders, key=lambda o: distance_to(o.pos, pos))

    print("First available drone: {}".format(get_first_available_drone()))

    min_turn = 0
    while len(orders) > 0 or deadline < min_turn:
        get_first_available_drone().load()
Example #6
0
def create_order_dictionary(orders_file):
    line_number = 0
    orders_dict = {}

    for line in orders_file:
        line_number += 1
        pieces = line.split(",")

        if len(pieces) != 2:
            raise FormatException(line_number, orders_file.name, line)
        elif pieces[0] != "id":
            try:
                id = int(pieces[0])
                name = pieces[1]
                if name[-1] == "\n":
                    name = name[:-1]
                orders_dict[id] = Order(id, name)
            except:
                raise FormatException(line_number, orders_file.name, line)

    return orders_dict
def checkForMorningBreakoutStrategy(tickers, item, orderQueue, ORDERS, price, time, currentInvested, DTBP, candleTime):
    #checks for 'morning breakout' strategy
    if (tickers[item].currentCandle.closePrice > tickers[item].morningHigh and tickers[item].currentCandle.closePrice < tickers[item].morningHigh * 1.003
        and time <= '7:30:00' and tickers[item].canTrade and not tickers[item].havePosition):
        #[test]
        print(time + '\t' + 'morning breakout in ' + item)
        #[test]

        size = round(10000 / price)

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):
            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : size, 'price' : price}})
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'morning breakout breakout'
            #add order to list
            order = Order(item, candleTime, price, 'morning breakout', size, 'long')
            ORDERS.append(order)

            #[test]
            print('DTBP not exceeded')
            #[test]
Example #8
0
def checkForBearish3BarReversalStrategy(tickers, item, orderQueue, ORDERS,
                                        size, price, time, currentInvested,
                                        DTBP, candleTime):
    #checks for 'bullish 3-bar reversal' strategy
    if (checkLowOfDay(tickers, item, 'bearish')
            and checkTrend(tickers, item, 'bearish')
            and checkLastTwoBars(tickers, item, 'bearish')
            and checkReversalBar(tickers, item, 'bearish')
            and tickers[item].canTrade == True
            and tickers[item].havePosition == False):

        #[test]
        print(time + '\t' + 'bearish 3-bar reversal in' + item)
        #[test]

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            print('DTBP not exceeded')

            #[test]
            orderQueue.append({
                item: {
                    'action': 'SELL_SHORT',
                    'symbol': item,
                    'size': size,
                    'price': price
                }
            })

            #update ticker info
            tickers[item].direction = "short"
            tickers[item].strategy = 'bearish 3-bar reversal'
            tickers[item].breakoutBarHigh = tickers[item].priceHistory[-2].high
            #add order to list
            order = Order(item, candleTime, price, 'bearish 3-bar reversal',
                          size, 'short')
            ORDERS.append(order)

            return True

        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            #[test]
            print('Using smaller size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price

            #[test]
            orderQueue.append({
                item: {
                    'action': 'SELL_SHORT',
                    'symbol': item,
                    'size': smallerSize,
                    'price': price
                }
            })

            #update ticker info
            tickers[item].direction = "short"
            tickers[item].strategy = 'bearish 3-bar reversal'
            tickers[item].breakoutBarHigh = tickers[item].priceHistory[-2].high
            #add order to list
            order = Order(item, candleTime, price, 'bearish 3-bar reversal',
                          smallerSize, 'short')
            ORDERS.append(order)

            return True
    else:
        return False
Example #9
0
class MyTest(unittest.TestCase):
    item = Item()
    order = Order()

    def test_add(self):
        abc = Operations()
        actual_result = abc.add(5, 6)
        expected_result = 11
        self.assertEqual(expected_result, actual_result)

    def test_check_even_no(self):
        op = Operations()
        actual_result = op.check_even_no(6)
        # expected_result = True
        # self.assertEqual(expected_result, actual_result)
        self.assertTrue(actual_result)

    def test_check_even_no(self):
        op = Operations()
        actual_result = op.check_even_no(5)
        self.assertFalse(actual_result)

    def test_add_item(self):
        result = self.item.add_item('test_item', 'test_type', 100)
        self.assertTrue(result)

    def test_add_item_view(self):
        result = self.item.add_item('', '', 100)
        self.assertFalse(result)

    def test_add_item_view(self):
        result = self.item.add_item('test', 'type', 'asd')
        self.assertFalse(result)

    def test_add_item_view(self):
        result = self.item.add_item('', '', -100)
        self.assertFalse(result)

    def test_show_items(self):
        data = self.item.show_item()
        actual_result = len(data)
        expected_result = 9
        self.assertEqual(expected_result, actual_result)

    def test_search_item(self):
        data = self.item.search_item('momo')
        actual_result = len(data)
        expected_result = 2
        self.assertEqual(expected_result, actual_result)

    def test_search_items1(self):
        data = self.item.search_item('noodle')
        actual_result = len(data)
        expected_result = 0
        self.assertEqual(expected_result, actual_result)

    def test_order_items(self):
        listdata = [(1, 5)]
        data = self.order.add_order(5, listdata)
        self.assertTrue(data)

    def test_order_items(self):
        listdata = [(1, 5)]
        data = self.order.add_order("five", listdata)
        self.assertFalse(data)

    def test_order_items(self):
        listdata = [("", "")]
        data = self.order.add_order(5, listdata)
        self.assertFalse(data)

    def test_order_items(self):
        listdata = [(1, 5)]
        data = self.order.add_order("", listdata)
        self.assertFalse(data)

    def test_ordershow_items(self):
        data = self.order.show_orders_by_order_id(3)
        actual_result = len(data)
        expected_result = 3
        self.assertEqual(expected_result, actual_result)

    def test_showallorder(self):
        data = self.order.show_all_orders()
        actual_result = len(data)
        expected_result = 44
        self.assertEqual(expected_result, actual_result)

    def test_showallorder(self):
        data = self.order.show_all_orders()
        actual_result = len(data)
        expected_result = 42
        self.assertNotEqual(expected_result, actual_result)
Example #10
0
    def __init__(self):
        self.window = Tk()
        self.window.title('Orders')
        self.window.geometry('503x500+0+0')

        self.item = Item()
        self.order = Order()

        self.all_items = self.item.show_item()
        self.ordered_item_list = []

        self.label_item = Label(self.window, text='Item')
        self.label_item.grid(row=0, column=0)

        self.combo_item = ttk.Combobox(self.window, state='readonly', width=27)
        self.combo_item.grid(row=0, column=1)
        # self.combo_item['values'] = self.all_items

        self.label_qty = Label(self.window, text='Qty')
        self.label_qty.grid(row=1, column=0)

        self.entry_qty = Entry(self.window, width=30)
        self.entry_qty.grid(row=1, column=1)

        self.btn_add_item = Button(self.window,
                                   text='Add >>',
                                   command=self.on_add_item)
        self.btn_add_item.grid(row=2, column=0)

        self.label_tbl = Label(self.window, text='Table No.')
        self.label_tbl.grid(row=3, column=0)

        self.entry_tbl = Entry(self.window, width=30)
        self.entry_tbl.grid(row=3, column=1)

        self.btn_add_order = Button(self.window,
                                    text='Submit Order',
                                    command=self.on_submit_order)
        self.btn_add_order.grid(row=4, column=0)

        self.btn_reset = Button(self.window,
                                text='Reset',
                                command=self.on_reset)
        self.btn_reset.grid(row=4, column=1)

        self.combo_order = ttk.Combobox(self.window)
        self.combo_order.grid(row=5, column=0)

        self.btn_show_order = Button(self.window,
                                     text='Show Order',
                                     command=self.show_orders_in_tree)
        self.btn_show_order.grid(row=5, column=1)

        self.order_tree = ttk.Treeview(self.window,
                                       column=('tbl', 'name', 'type', 'price',
                                               'qty'))
        self.order_tree.grid(row=6, column=0, columnspan=2)
        self.order_tree['show'] = 'headings'
        self.order_tree.column('tbl', width=100, anchor='center')
        self.order_tree.column('name', width=100, anchor='center')
        self.order_tree.column('type', width=100, anchor='center')
        self.order_tree.column('price', width=100, anchor='center')
        self.order_tree.column('qty', width=100, anchor='center')
        self.order_tree.heading('tbl', text='Table No.')
        self.order_tree.heading('name', text='Name')
        self.order_tree.heading('type', text='Type')
        self.order_tree.heading('price', text='Price')
        self.order_tree.heading('qty', text='Qty')

        self.btn_bill = Button(self.window,
                               text='Generate Bill',
                               command=self.generate_bill)
        self.btn_bill.grid(row=7, column=0)

        self.show_items_in_combo()
        self.show_orders_in_combo()

        self.window.mainloop()
Example #11
0
def checkForReversalStrategy(tickers, item, orderQueue, ORDERS, size, price, time, currentInvested, DTBP, candleTime):
    #checks for 'reversal' strategy without climactic volume
    if (tickers[item].numberOfBarsBelowEMAs >= 9 and tickers[item].currentCandle.openPrice < round(tickers[item].EMA5, 2) and tickers[item].currentCandle.closePrice > round(tickers[item].EMA5, 2)
        and tickers[item].trend == 'downtrend' and tickers[item].percentageOfDownBarsInDowntrend >= 50 and tickers[item].priceHistory[-2].closePrice < tickers[item].priceHistory[-2].openPrice
        and tickers[item].currentCandle.closePrice >= (0.75 * (tickers[item].currentCandle.high - tickers[item].currentCandle.low) + tickers[item].currentCandle.low) 
        and tickers[item].canTrade == True and tickers[item].havePosition == False
        and tickers[item].currentCandle.closePrice >= tickers[item].priceHistory[-2].high - 0.01
        and tickers[item].priceHistory[-2].openPrice <= tickers[item].priceHistory[-2].EMA5 + 0.02
        and (tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].openPrice) <= 0.4 * (tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].low)):

        #[test]
        print(time + '\t' + 'reversal without climactic volume in' + item )

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000
        
        if (item == 'INTC'):
            size = 700

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            print('DTBP not exceeded')

            #[test]
            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : size, 'price' : price}})

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'reversal'
            tickers[item].breakoutBarLow = tickers[item].currentCandle.low
            #add order to list
            order = Order(item, candleTime, price, 'reversal', size, 'long')
            ORDERS.append(order)
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            #[test]
            print('Using smaller size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price

            #[test]
            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : smallerSize, 'price' : price}})

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'reversal'
            tickers[item].breakoutBarLow = tickers[item].currentCandle.low
            #add order to list
            order = Order(item, candleTime, price, 'reversal', smallerSize, 'long')
            ORDERS.append(order)

    #checks for 'reversal' strategy with several consecutive down bars
    elif (tickers[item].numberOfBarsBelowEMAs >= 9
            and tickers[item].trend == 'downtrend' and tickers[item].percentageOfDownBarsInDowntrend >= 50 and tickers[item].numberOfConsecutiveBars >= 6
            and tickers[item].currentCandle.closePrice >= (0.75 * (tickers[item].currentCandle.high - tickers[item].currentCandle.low) + tickers[item].currentCandle.low) 
            and tickers[item].canTrade == True and tickers[item].havePosition == False and tickers[item].currentCandle.closePrice >= tickers[item].priceHistory[-2].high - 0.01
            and tickers[item].priceHistory[-2].closePrice < tickers[item].priceHistory[-2].openPrice
            and tickers[item].priceHistory[-2].openPrice <= tickers[item].priceHistory[-2].EMA5 + 0.02
            and (tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].openPrice) <= 0.4 * (tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].low)):
        
        #[test]
        print(time + '\t' + 'reversal with several consecutive down bars in' + item )

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000
        
        if (item == 'INTC'):
            size = 700
        
        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            #[test]
            print('DTBP not exceeded')

            #[test]
            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : size, 'price' : price}})

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'reversal'
            tickers[item].breakoutBarLow = tickers[item].currentCandle.low

            #add order to list
            order = Order(item, candleTime, price, 'reversal', size, 'long')
            ORDERS.append(order)
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            #[test]
            print('Using smaller size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price

            #[test]
            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : smallerSize, 'price' : price}})

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'reversal'
            tickers[item].breakoutBarLow = tickers[item].currentCandle.low
            #add order to list
            order = Order(item, candleTime, price, 'reversal', smallerSize, 'long')
            ORDERS.append(order)
def checkForFlatTopBreakoutStrategy(tickers, item, orderQueue, ORDERS, size,
                                    price, time, currentInvested, DTBP,
                                    candleTime):
    #checks for 'flat top breakout' strategy
    if (tickers[item].sizeOfFlatTopBreakoutPattern >= 3
            and tickers[item].withinFlatTopBreakoutPattern
            and tickers[item].currentCandle.closePrice >
            tickers[item].intervalUnderConsideration
            and tickers[item].currentCandle.closePrice <
        (tickers[item].intervalUnderConsideration + 0.1)
            and tickers[item].canTrade and not tickers[item].havePosition
            and tickers[item].currentCandle.closePrice >=
        (0.7 *
         (tickers[item].currentCandle.high - tickers[item].currentCandle.low) +
         tickers[item].currentCandle.low
         and tickers[item].currentCandle.openPrice <=
         tickers[item].intervalUnderConsideration)
            and tickers[item].currentCandle.openPrice <=
            tickers[item].intervalUnderConsideration):
        #[test]
        print(time + '\t' + 'flat top breakout in ' + item)
        #[test]

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):
            #[test]
            #orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : size, 'price' : price}})
            #[test]

            #[test]
            #tickers[item].havePosition = True
            #[test]

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'flat top breakout'
            tickers[item].withinFlatTopBreakoutPattern = False
            tickers[item].closedBelow10EMA = False
            tickers[item].sizeOfFlatTopBreakoutPattern = 0
            tickers[item].nextInterval = tickers[
                item].intervalUnderConsideration + 0.25
            tickers[item].stopLossInterval = 0
            #add order to list
            order = Order(item, candleTime, price, 'flat top breakout', size,
                          'long')
            ORDERS.append(order)

            #[test]
            print('The next interval is: ' + str(tickers[item].nextInterval))
            #[test]
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            #[test]
            print('Using smaller size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price

            #[test]
            #orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : smallerSize, 'price' : price}})
            #[test]

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'flat top breakout'
            tickers[item].withinFlatTopBreakoutPattern = False
            tickers[item].closedBelow10EMA = False
            tickers[item].sizeOfFlatTopBreakoutPattern = 0
            tickers[item].nextInterval = tickers[
                item].intervalUnderConsideration + 0.25
            tickers[item].stopLossInterval = 0
            #add order to list
            order = Order(item, candleTime, price, 'flat top breakout',
                          smallerSize, 'long')
            ORDERS.append(order)

            #[test]
            print('The next interval is: ' + str(tickers[item].nextInterval))
            #[test]
    elif (tickers[item].withinFlatTopBreakoutPattern
          and tickers[item].currentCandle.high >
          tickers[item].intervalUnderConsideration):
        #[test]
        print("Flat top breakout in " + item + " no longer considered")
        #[test]
        tickers[item].withinFlatTopBreakoutPattern = False
        tickers[item].closedBelow10EMA = False
        tickers[item].sizeOfFlatTopBreakoutPattern = 0
        tickers[item].nextInterval = 0
        tickers[item].stopLossInterval = 0
Example #13
0
def checkForEMAStrategy(tickers, item, orderQueue, ORDERS, size, price, time,
                        currentInvested, DTBP, candleTime):
    conditionsForEMAStrategyMet = False
    #checks for 'break above 20EMA' strategy
    if (tickers[item].currentCandle.openPrice < round(tickers[item].EMA20, 2)
            and tickers[item].currentCandle.closePrice > round(
                tickers[item].EMA20, 2)
            and ((tickers[item].priceHistory[-1].volume >
                  1.7 * tickers[item].priceHistory[-2].volume
                  and tickers[item].priceHistory[-1].volume <
                  3 * tickers[item].priceHistory[-2].volume)
                 or tickers[item].priceHistory[-1].volume >
                 4 * tickers[item].priceHistory[-2].volume)
            and tickers[item].canUseEMAStrategy == True
            and tickers[item].havePosition == False
            and tickers[item].tradedEMAStrategy == False
            and tickers[item].isInSidewaysTrend == False
            and tickers[item].canTrade == True):

        #[test]
        print(time + '\t' + 'break above 20EMA in' + item)
        #[test]

        conditionsForEMAStrategyMet = True

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000

        if (item == 'INTC'):
            size = 700

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):
            orderQueue.append({
                item: {
                    'action': 'BUY',
                    'symbol': item,
                    'size': size,
                    'price': price
                }
            })
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'break above 20EMA'
            tickers[item].tradedEMAStrategy = True
            #add order to list
            order = Order(item, candleTime, price, 'break above 20EMA', size,
                          'long')
            ORDERS.append(order)

            #[test]
            print('DTBP not exceeded')
            #[test]
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            smallerSize = DTBP - 1000 - currentInvested / price
            orderQueue.append({
                item: {
                    'action': 'BUY',
                    'symbol': item,
                    'size': smallerSize,
                    'price': price
                }
            })
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'break above 20EMA'
            tickers[item].tradedEMAStrategy = True
            #add order to list
            order = Order(item, candleTime, price, 'break above 20EMA',
                          smallerSize, 'long')
            ORDERS.append(order)

            #[test]
            print('Using smaller position size')
            #[test]

    #checks for 'break below 20EMA' strategy
    elif (tickers[item].currentCandle.openPrice > round(
            tickers[item].EMA20, 2)
          and tickers[item].currentCandle.closePrice < round(
              tickers[item].EMA20, 2)
          and ((tickers[item].priceHistory[-1].volume >
                1.7 * tickers[item].priceHistory[-2].volume
                and tickers[item].priceHistory[-1].volume <
                3 * tickers[item].priceHistory[-2].volume)
               or tickers[item].priceHistory[-1].volume >
               4 * tickers[item].priceHistory[-2].volume)
          and tickers[item].canUseEMAStrategy == True
          and tickers[item].havePosition == False
          and tickers[item].tradedEMAStrategy == False
          and tickers[item].isInSidewaysTrend == False
          and tickers[item].canTrade == True):

        #[test]
        print(time + '\t' + 'break below 20EMA in' + item)
        #[test]

        conditionsForEMAStrategyMet = True

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000

        if (item == 'INTC'):
            size = 700

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            #[test]
            print('DTBP not exceeded')
            #[test]

            if (item not in data.HARD_TO_BORROW):
                orderQueue.append({
                    item: {
                        'action': 'SELL_SHORT',
                        'symbol': item,
                        'size': size,
                        'price': price
                    }
                })
                #update ticker info
                tickers[item].direction = "short"
                tickers[item].strategy = 'break below 20EMA'
                tickers[item].tradedEMAStrategy = True
                #add order to list
                order = Order(item, candleTime, price, 'break below 20EMA',
                              size, 'short')
                ORDERS.append(order)

                #[test]
                print('stock can be shorted')
                #[test]
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):

            #[test]
            print('Using smaller position size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price
            if (item not in data.HARD_TO_BORROW):
                orderQueue.append({
                    item: {
                        'action': 'SELL_SHORT',
                        'symbol': item,
                        'size': smallerSize,
                        'price': price
                    }
                })
                #update ticker info
                tickers[item].direction = "short"
                tickers[item].strategy = 'bounce lower off 20EMA'
                tickers[item].tradedEMAStrategy = True
                #add order to list
                order = Order(item, candleTime, price,
                              'bounce lower off 20EMA', smallerSize, 'short')
                ORDERS.append(order)

                #[test]
                print('stock can be shorted')
                #[test]

    #checks for 'bounce lower off 20EMA' strategy
    elif (tickers[item].currentCandle.openPrice < round(
            tickers[item].EMA20, 2)
          and tickers[item].currentCandle.high == round(
              tickers[item].EMA20, 2)
          and ((tickers[item].priceHistory[-1].volume >
                1.7 * tickers[item].priceHistory[-2].volume
                and tickers[item].priceHistory[-1].volume <
                3 * tickers[item].priceHistory[-2].volume)
               or tickers[item].priceHistory[-1].volume >
               4 * tickers[item].priceHistory[-2].volume)
          and tickers[item].currentCandle.closePrice <
          tickers[item].currentCandle.openPrice
          and tickers[item].canUseEMAStrategy == True
          and tickers[item].havePosition == False
          and tickers[item].tradedEMAStrategy == False
          and tickers[item].isInSidewaysTrend == False
          and tickers[item].canTrade == True):

        #[test]
        print(time + '\t' + 'bounce lower off 20EMA in' + item)
        #[test]

        conditionsForEMAStrategyMet = True

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000

        if (item == 'INTC'):
            size = 700

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            #[test]
            print('DTBP not exceeded')
            #[test]

            if (item not in data.HARD_TO_BORROW):
                orderQueue.append({
                    item: {
                        'action': 'SELL_SHORT',
                        'symbol': item,
                        'size': size,
                        'price': price
                    }
                })
                #update ticker info
                tickers[item].direction = "short"
                tickers[item].strategy = 'bounce lower off 20EMA'
                tickers[item].tradedEMAStrategy = True
                #add order to list
                order = Order(item, candleTime, price,
                              'bounce lower off 20EMA', size, 'short')
                ORDERS.append(order)

                #[test]
                print('stock can be shorted')
                #[test]
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):

            #[test]
            print('Using smaller position size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price
            if (item not in data.HARD_TO_BORROW):
                orderQueue.append({
                    item: {
                        'action': 'SELL_SHORT',
                        'symbol': item,
                        'size': smallerSize,
                        'price': price
                    }
                })
                #update ticker info
                tickers[item].direction = "short"
                tickers[item].strategy = 'bounce lower off 20EMA'
                tickers[item].tradedEMAStrategy = True
                #add order to list
                order = Order(item, candleTime, price,
                              'bounce lower off 20EMA', smallerSize, 'short')
                ORDERS.append(order)

                #[test]
                print('stock can be shorted')
                #[test]

    #checks for 'bounce higher off 20EMA' strategy
    elif (tickers[item].currentCandle.openPrice > round(
            tickers[item].EMA20, 2)
          and tickers[item].currentCandle.low == round(tickers[item].EMA20, 2)
          and ((tickers[item].priceHistory[-1].volume >
                1.7 * tickers[item].priceHistory[-2].volume
                and tickers[item].priceHistory[-1].volume <
                3 * tickers[item].priceHistory[-2].volume)
               or tickers[item].priceHistory[-1].volume >
               4 * tickers[item].priceHistory[-2].volume)
          and tickers[item].currentCandle.closePrice >
          tickers[item].currentCandle.openPrice
          and tickers[item].canUseEMAStrategy == True
          and tickers[item].havePosition == False
          and tickers[item].tradedEMAStrategy == False
          and tickers[item].isInSidewaysTrend == False
          and tickers[item].canTrade == True):

        #[test]
        print(time + '\t' + 'bounce higher off 20EMA in' + item)
        #[test]

        conditionsForEMAStrategyMet = True

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000

        if (item == 'INTC'):
            size = 700

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            #[test]
            print('DTBP not exceeded')
            #[test]

            orderQueue.append({
                item: {
                    'action': 'BUY',
                    'symbol': item,
                    'size': size,
                    'price': price
                }
            })
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'bounce higher off 20EMA'
            tickers[item].tradedEMAStrategy = True
            #add order to list
            order = Order(item, candleTime, price, 'bounce higher off 20EMA',
                          size, 'long')
            ORDERS.append(order)
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            smallerSize = DTBP - 1000 - currentInvested / price
            orderQueue.append({
                item: {
                    'action': 'BUY',
                    'symbol': item,
                    'size': smallerSize,
                    'price': price
                }
            })
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'break above 20EMA'
            tickers[item].tradedEMAStrategy = True
            #add order to list
            order = Order(item, candleTime, price, 'break above 20EMA',
                          smallerSize, 'long')
            ORDERS.append(order)

            #[test]
            print('Using smaller position size')
            #[test]

    return conditionsForEMAStrategyMet
def checkForOversoldReversalStrategy(tickers, item, orderQueue, ORDERS, size,
                                     price, time, currentInvested, DTBP,
                                     candleTime):
    #checks for 'oversold reversal' strategy
    if (determineOversold(tickers, item) and checkLowOfDay(tickers, item)
            and checkReversalBar(tickers, item)
            and tickers[item].trend == 'downtrend'
            and tickers[item].percentageOfDownBarsInDowntrend >= 50
            and tickers[item].canTrade == True
            and tickers[item].havePosition == False):

        #[test]
        print(time + '\t' + 'oversold reversal in' + item)
        #[test]

        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            print('DTBP not exceeded')

            #[test]
            orderQueue.append({
                item: {
                    'action': 'BUY',
                    'symbol': item,
                    'size': size,
                    'price': price
                }
            })

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'oversold reversal'
            tickers[item].breakoutBarLow = tickers[item].priceHistory[-2].low
            #add order to list
            order = Order(item, candleTime, price, 'oversold reversal', size,
                          'long')
            ORDERS.append(order)

            return True
        #use smaller position size if DTBP would otherwise be exceeded
        elif ((DTBP - 1000 - currentInvested) / price >= 300):
            #[test]
            print('Using smaller size')
            #[test]

            smallerSize = DTBP - 1000 - currentInvested / price

            #[test]
            orderQueue.append({
                item: {
                    'action': 'BUY',
                    'symbol': item,
                    'size': smallerSize,
                    'price': price
                }
            })

            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'oversold reversal'
            tickers[item].breakoutBarLow = tickers[item].priceHistory[-2].low
            #add order to list
            order = Order(item, candleTime, price, 'oversold reversal',
                          smallerSize, 'long')
            ORDERS.append(order)

            return True
    else:
        return False
Example #15
0
def checkForHighTightFlagStrategy(tickers, item, orderQueue, ORDERS, size, price, time, currentInvested, DTBP, candleTime):
    conditionsForHighTightFlagMet = False
    #checks for 'high tight flag' strategy with 1 down bar
    if (tickers[item].isInSteepUptrend(1) == True and tickers[item].priceHistory[-2].volume < tickers[item].priceHistory[-3].volume
            and tickers[item].priceHistory[-2].high <= tickers[item].priceHistory[-3].closePrice + 0.04
            and ((tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].low) <= 0.8 * (tickers[item].priceHistory[-3].high - tickers[item].priceHistory[-3].low) or (tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].low) <= 0.05)
            and tickers[item].priceHistory[-2].low >= tickers[item].priceHistory[-2].EMA5 - 0.02 
            and tickers[item].priceHistory[-2].closePrice <= tickers[item].priceHistory[-2].openPrice
            and tickers[item].priceHistory[-1].closePrice > tickers[item].priceHistory[-2].high
            and tickers[item].priceHistory[-1].closePrice > tickers[item].priceHistory[-3].closePrice
            and (tickers[item].priceHistory[-1].closePrice <= tickers[item].priceHistory[-3].closePrice + 0.08 or tickers[item].priceHistory[-1].closePrice <= tickers[item].priceHistory[-2].high + 0.08)
            and tickers[item].priceHistory[-1].closePrice >= (tickers[item].priceHistory[-1].low + (0.8 * (tickers[item].priceHistory[-1].high - tickers[item].priceHistory[-1].low)))
            and tickers[item].priceHistory[-3].closePrice >= (tickers[item].priceHistory[-3].low + (0.7 * (tickers[item].priceHistory[-3].high - tickers[item].priceHistory[-3].low)))
            and tickers[item].canTrade == True and tickers[item].havePosition == False):

        #[test]
        print(time + '\t' + 'high tight flag with 1 down bar in ' + item)
        #[test]

        conditionsForHighTightFlagMet = True

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000
        
        if (item == 'INTC'):
            size = 700
        
        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            #[test]
            print('DTBP not exceeded')
            #[test]

            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : size, 'price' : price}})
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'high tight flag'
            tickers[item].breakoutBarLow = tickers[item].currentCandle.low
            #add order to list
            order = Order(item, candleTime, price, 'high tight flag', size, 'long')
            ORDERS.append(order)

    #checks for 'high tight flag' strategy with 2 down bars
    elif (tickers[item].isInSteepUptrend(2) == True and tickers[item].priceHistory[-3].volume < tickers[item].priceHistory[-4].volume
            and tickers[item].priceHistory[-3].high <= tickers[item].priceHistory[-4].closePrice + 0.04
            and (tickers[item].priceHistory[-3].high - tickers[item].priceHistory[-3].low) <= 0.8 * (tickers[item].priceHistory[-4].high - tickers[item].priceHistory[-4].low)
            and tickers[item].priceHistory[-3].low >= tickers[item].priceHistory[-3].EMA5 - 0.02 
            and tickers[item].priceHistory[-2].volume < tickers[item].priceHistory[-4].volume
            and tickers[item].priceHistory[-2].volume < 2 * tickers[item].priceHistory[-3].volume
            and (tickers[item].priceHistory[-2].high - tickers[item].priceHistory[-2].low) <= 1.2 * (tickers[item].priceHistory[-3].high - tickers[item].priceHistory[-3].low)
            and tickers[item].priceHistory[-2].high <= tickers[item].priceHistory[-3].high
            and tickers[item].priceHistory[-2].high <= tickers[item].priceHistory[-4].high
            and tickers[item].priceHistory[-2].low >= tickers[item].priceHistory[-2].EMA5 - 0.03
            and tickers[item].priceHistory[-2].closePrice < tickers[item].priceHistory[-2].openPrice
            and tickers[item].priceHistory[-3].closePrice < tickers[item].priceHistory[-3].openPrice
            and tickers[item].priceHistory[-1].closePrice > tickers[item].priceHistory[-2].high
            and tickers[item].priceHistory[-1].closePrice > tickers[item].priceHistory[-3].high
            and tickers[item].priceHistory[-1].closePrice > tickers[item].priceHistory[-4].closePrice
            and (tickers[item].priceHistory[-1].closePrice <= tickers[item].priceHistory[-4].closePrice + 0.07 or tickers[item].priceHistory[-1].closePrice <= tickers[item].priceHistory[-3].high + 0.07)
            and tickers[item].priceHistory[-1].closePrice >= (tickers[item].priceHistory[-1].low + (0.8 * (tickers[item].priceHistory[-1].high - tickers[item].priceHistory[-1].low)))
            and tickers[item].priceHistory[-4].closePrice >= (tickers[item].priceHistory[-4].low + (0.7 * (tickers[item].priceHistory[-4].high - tickers[item].priceHistory[-4].low)))
            and tickers[item].canTrade == True and tickers[item].havePosition == False):

        #[test]
        print(time + '\t' + 'high tight flag with 2 down bars in ' + item)
        #[test]

        conditionsForHighTightFlagMet = True

        if (item == 'M'):
            size = 1500

        if (item == 'AMAT' or item == 'MU'):
            size = 800

        if (item == 'C'):
            size = 1000
        
        if (item == 'INTC'):
            size = 700
        
        #checks if DTBP is exceeded
        if (currentInvested + (price * size) < DTBP - 1000):

            #[test]
            print('DTBP not exceeded')
            #[test]

            orderQueue.append({item : {'action' : 'BUY', 'symbol' : item, 'size' : size, 'price' : price}})
            #update ticker info
            tickers[item].direction = "long"
            tickers[item].strategy = 'high tight flag'
            tickers[item].breakoutBarLow = tickers[item].currentCandle.low
            #add order to list
            order = Order(item, candleTime, price, 'high tight flag', size, 'long')
            ORDERS.append(order)

    return conditionsForHighTightFlagMet