Esempio n. 1
0
def do_math_for_single_stock(all_simulations, moving_day_average, number_of_days, stock, mode):

    print('Mode: ' + mode)

    net_profits = []

    # The index for each day. We start it at day 5.
    todays_index = len(stock.get_data()) - 0 - 1 - 4 - (len(stock.get_data()) - number_of_days)
    tomorrows_index = len(stock.get_data()) - 0 - 1 - 5 - (len(stock.get_data()) - number_of_days)
    yesterdays_index = len(stock.get_data()) - 0 - 1 - 3 - (len(stock.get_data()) - number_of_days)
    counter_index = 0

    while tomorrows_index != 0:

        debug.logger.log('Doing math for day: ' + str(stock.get_data()[todays_index].get_date()))
        #debug.logger.log('Tomorrow is: ' + str(stock.get_data()[tomorrows_index].get_date()))

        # Calculate the moving average.
        close_moving_average = 0.0
        for i in range(0, moving_day_average):

            current_index = todays_index + i

            close_moving_average += stock.get_data()[current_index].get_close()
            debug.logger.log(stock.get_data()[current_index].to_string())
        close_moving_average /= 5

        debug.logger.log('The moving average is: ' + str(close_moving_average))

        if mode == 'LONG':
            # Check if today's close is above the moving average. If it is then we do the math.
            debug.logger.log('I am checking if today\'s close is above the moving average.')
            if stock.get_data()[todays_index].get_close() > close_moving_average:
                debug.logger.log('Today\'s close is larger than the close moving average. Doing the math...')
                do_sub_math_for_single_day(net_profits, yesterdays_index, todays_index, tomorrows_index, stock, mode)
        elif mode == 'SHORT':
            debug.logger.log('I am checking if today\'s close is below the moving average.')
            # Check if today's close is below the moving average. If it is then we do the math.
            if stock.get_data()[todays_index].get_close() < close_moving_average:
                debug.logger.log('Today\'s close is below the moving average. Doing the math...')
                do_sub_math_for_single_day(net_profits, yesterdays_index, todays_index, tomorrows_index, stock, mode)
        else:
            if stock.get_data()[todays_index].get_close() > close_moving_average:
                do_sub_math_for_single_day(net_profits, yesterdays_index, todays_index, tomorrows_index, stock, 'LONG')
            elif stock.get_data()[todays_index].get_close() < close_moving_average:
                do_sub_math_for_single_day(net_profits, yesterdays_index, todays_index, tomorrows_index, stock, 'SHORT')

        counter_index += 1
        todays_index = len(stock.get_data()) - 0 - 1 - 4 - counter_index - (len(stock.get_data()) - number_of_days)
        tomorrows_index = len(stock.get_data()) - 0 - 1 - 5 - counter_index - (len(stock.get_data()) - number_of_days)
        yesterdays_index = len(stock.get_data()) - 0 - 1 - 3 - counter_index - (len(stock.get_data()) - number_of_days)

    # Print out % profits.
    if len(net_profits) != 0:
        print('Net profits: ' + str(stock_math.average_fast(net_profits)))

    if len(net_profits) != 0:
        all_simulations.attempt_to_add_best(stock.get_name(), stock_math.average_fast(net_profits))
Esempio n. 2
0
def do_sub_math_for_single_day(net_profits, yesterdays_index, todays_index, tomorrows_index, stock, mode):
    # Calculate a list of the percentage difference between yesterday's close price and today's low.
    percentage_difference_between_yesterdays_close_and_todays_low = []
    percentage_difference_between_yesterdays_close_and_todays_high = []
    for i in range(0, 5):
        current_index = len(stock.get_data()) - i - 1
        past_index = len(stock.get_data()) - i - 2

        local_low_calculation = stock_math.percentage_difference_fast(stock.get_data()[past_index].get_close(), stock.get_data()[current_index].get_low())
        local_high_calculation = stock_math.percentage_difference_fast(stock.get_data()[past_index].get_close(), stock.get_data()[current_index].get_high())

        percentage_difference_between_yesterdays_close_and_todays_low.append(local_low_calculation)
        percentage_difference_between_yesterdays_close_and_todays_high.append(local_high_calculation)

    average_for_low = stock_math.average_fast(percentage_difference_between_yesterdays_close_and_todays_low)
    average_for_high = stock_math.average_fast(percentage_difference_between_yesterdays_close_and_todays_high)

    #print('Close moving average: ' + '\t' + str(close_moving_average))
    buy_price = stock_math.buy_price_fast(average_for_low, stock.get_data()[todays_index].get_close())
    short_price = stock_math.short_price_fast(average_for_high, stock.get_data()[todays_index].get_close())

    debug.logger.log('Buy price: ' + str(buy_price))
    debug.logger.log('Short price: ' + str(short_price))

    if mode == 'LONG':
        # If the buy price is above tomorrow's low, then we buy the stock tomorrow.
        if buy_price > stock.get_data()[tomorrows_index].get_low():
            #net_profits.append(stock_math.generic_percenatage_difference(buy_price, stock.get_data()[tomorrows_index].get_close()))
            local_net_profit = stock_math.generic_percenatage_difference(buy_price, stock.get_data()[tomorrows_index].get_close())
            if local_net_profit <= -4.0:
                net_profits.append(-4.0)
            else:
                net_profits.append(local_net_profit)
    elif mode == 'SHORT':

        # If the short price is below tomorrow's
        #local_net_profit = stock_math.generic_percenatage_difference(stock.get_data()[tomorrows_index].get_close(), short_price)

        if short_price < stock.get_data()[tomorrows_index].get_high():
            local_net_profit = stock_math.generic_percenatage_difference(stock.get_data()[tomorrows_index].get_close(), short_price)
            if local_net_profit <= -4.0:
                net_profits.append(-4.0)
            else:
                net_profits.append(local_net_profit)
    else:
        if buy_price > stock.get_data()[tomorrows_index].get_low():
            local_net_profit = stock_math.generic_percenatage_difference(buy_price, stock.get_data()[tomorrows_index].get_close())
            if local_net_profit <= -4.0:
                net_profits.append(-4.0)
            else:
                net_profits.append(local_net_profit)
        elif short_price < stock.get_data()[tomorrows_index].get_high():
            local_net_profit = stock_math.generic_percenatage_difference(stock.get_data()[tomorrows_index].get_close(), short_price)
            if local_net_profit <= 4.0:
                net_profits.append(-4.0)
            else:
                net_profits.append(stock_math.generic_percenatage_difference(stock.get_data()[tomorrows_index].get_close(), short_price))