Esempio n. 1
0
    def is_market_open(self):

        today_date = datetime.datetime.today().strftime("%Y-%m-%d")

        IsMarketOpen = True
        if not IsMarketOpen:
            oanda_logger.info(
                "{}'s forex market is closed!".format(today_date))
        return IsMarketOpen
Esempio n. 2
0
    def get_trade_instrument(self, day_buy, day_sell):
        trade_instruments_tuple = []
        for instrument in day_buy:
            trade_instruments_tuple.append((instrument, 'buy'))

        for instrument in day_sell:
            trade_instruments_tuple.append((instrument, 'sell'))

        time = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        oanda_logger.info("Trading decision: {}, Time: {}".format(
            trade_instruments_tuple, time))
        return trade_instruments_tuple
Esempio n. 3
0
    def get_close_out_instrument(self, day_buy, day_sell, strategy='s1'):
        print("=============get_close_out_instrument===============")
        instrument_list = []
        unit_list = []
        line_id_list = []
        # if strategy == 's2':
        #     # close out the sell trade
        #     for instrument in day_buy:
        #         for instrument_pos, buy_or_sell in self.pos_detail_dict.items():
        #             if instrument == instrument_pos and buy_or_sell == 'sell':
        #                 instruments.append(instrument_pos)
        #     # close out the buy trade
        #     for instrument in day_sell:
        #         for instrument_pos, buy_or_sell in self.pos_detail_dict.items():
        #             oanda_logger.debug("instrument: {}, instrument_pos:{}, buy_or_sell:{}"
        #                                .format(instrument, instrument_pos, buy_or_sell))
        #             if instrument == instrument_pos and buy_or_sell == 'buy':
        #                 instruments.append(instrument_pos)

        if strategy == 's1':
            today_str = self.date_today.strftime("%Y-%m-%d")
            with open(self.close_out_file_path, 'r') as f:
                for i, line in enumerate(f):
                    if line == '\n':
                        continue
                    line_list = line.split(',')
                    date_str = line_list[1]
                    if today_str != date_str:
                        continue
                    else:
                        instrument = line_list[0]
                        units = str(line_list[2])
                        instrument_list.append(instrument)
                        unit_list.append(units)
                        line_id_list.append(i)
            # date_today_str = self.date_today.strftime("%Y-%m-%d")
            # strategy1_file_path = self.strategy1_file_path
            # with open (strategy1_file_path, 'r', encoding = 'utf-8') as f:
            #     strategy1_dict = json.load(f)
            # if strategy1_dict.get(date_today_str):
            #     instruments = strategy1_dict[date_today_str]['close_out']
            # else:
            #     instruments = []

        #instruments = ['EUR_USD']
        time = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        oanda_logger.info(
            "Close_out instruments: {}, units: {}, Time: {}".format(
                instrument_list, unit_list, time))
        return instrument_list, unit_list, line_id_list
Esempio n. 4
0
    def get_day_buy_sell(self, ga_classifier_result_dict):
        #
        is_1_day_buy = self.is_1_day_buy
        is_1_day_sell = self.is_1_day_sell
        is_3_day_buy = self.is_3_day_buy
        is_3_day_sell = self.is_3_day_sell
        is_7_day_buy = self.is_7_day_buy
        is_7_day_sell = self.is_7_day_sell
        #
        buy_list = []
        sell_list = []
        buy_set = set()
        sell_set = set()
        # buy
        if is_1_day_buy:
            buy_list.extend(ga_classifier_result_dict['1_day_buy'])
        if is_3_day_buy:
            buy_list.extend(ga_classifier_result_dict['3_day_buy'])
        if is_7_day_buy:
            buy_list.extend(ga_classifier_result_dict['7_day_buy'])

        buy_set = set(buy_list)

        # sell
        if is_1_day_sell:
            sell_list.extend(ga_classifier_result_dict['1_day_sell'])
        if is_3_day_sell:
            sell_list.extend(ga_classifier_result_dict['3_day_sell'])
        if is_7_day_sell:
            sell_list.extend(ga_classifier_result_dict['7_day_sell'])

        sell_set = set(sell_list)

        # if forex appear in both set, delete it
        buy_set_complete = buy_set.copy()
        sell_set_complete = sell_set.copy()
        buy_set -= sell_set_complete
        sell_set -= buy_set_complete
        day_buy = list(buy_set)
        day_sell = list(sell_set)
        oanda_logger.info("day_buy: {}".format(day_buy))
        oanda_logger.info("day_sell: {}".format(day_sell))
        return day_buy, day_sell
Esempio n. 5
0
def main_loop():
    print("Oanda Trading Start!")
    # (0.) see if market is open
    is_market_open = oanda_trading.is_market_open()
    if not is_market_open:
        print("Market is closed today!")
        oanda_trading.modify_close_out_date()
        sys.exit()

    oanda_logger.info(
        "===============adopted strategy: {}===============".format(strategy))
    # (1.) Update Forex Data
    read_up_to_date_forex_data()

    # (2.) get ga_classifier_result_dict
    ga_classifier_result_dict = get_ga_classifier_result_dict()

    # (3.) get day_buy/day_sell
    if strategy == 's2':
        day_buy, day_sell = oanda_trading.get_day_buy_sell(
            ga_classifier_result_dict)
    elif strategy == 's1':
        day_buy, day_sell = oanda_trading.s1_get_day_buy_sell(
            ga_classifier_result_dict)

    # TODO reinforcement learning, action outputs close order date, units, which shoule be the input of close_out and trade

    # (4.) close out
    oanda_trading.close_out2(strategy=strategy)
    time.sleep(3)

    # (5.) buy
    oanda_trading.trade(trading_params, day_buy, day_sell)
    time.sleep(3)

    # (6.) show position and archive positions
    oanda_trading.get_all_positions()

    # (7.)
    oanda_trading.isEnd = False
Esempio n. 6
0
    def trade(self, trading_params, day_buy, day_sell):
        time = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        # trade_instruments_tuple: (('EUR_USD', 'buy'), ('EUR_USD', 'sell')...)
        trade_instruments_tuple = self.get_trade_instrument(day_buy, day_sell)
        for trade_instrument, sell_or_buy in trade_instruments_tuple:

            if not trade_instrument:
                # return logging
                oanda_logger.info(
                    "=============================Return Trading============================="
                )
                oanda_logger.info("Trading Time: {}".format(time))
                oanda_logger.info("Day buy: {}".format(day_buy))
                oanda_logger.info("Day sell: {}".format(day_sell))
                oanda_logger.info("No instrument chosen")
                oanda_logger.info(
                    "=============================Return Trading END==========================\n"
                )
                return

            body = self.body
            headers = self.headers
            url = self.order_url
            body['order']['instrument'] = trade_instrument
            body['order']['units'] = '50'

            # sell or buy
            if sell_or_buy == 'sell':
                body['order']['units'] = str(int(body['order']['units']) * -1)
            else:
                pass
            response = requests.post(url, headers=headers, json=body)
            response_content = response.json()
            status_code = response.status_code
            last_transaction_id = response_content['lastTransactionID']
            # TODO update close_out_order.txt
            if status_code == '201':
                closed_out_date = self.s1_get_close_out_date()
                self.update_close_out_order(trade_instrument, sell_or_buy,
                                            closed_out_date,
                                            last_transaction_id)

            # logging
            oanda_logger.info(
                "=============================Oanda Trading============================="
            )
            oanda_logger.info("Trading Time: {}".format(time))
            oanda_logger.info("Day buy: {}".format(day_buy))
            oanda_logger.info("Day sell: {}".format(day_sell))
            oanda_logger.info("Instrument chosen: {}".format(trade_instrument))
            oanda_logger.info("status_code: {}".format(status_code))
            oanda_logger.info("response_content:\n {}".format(
                pprint.pformat(response_content)))
            oanda_logger.info(
                "=============================Oanda Trading END==========================\n"
            )
Esempio n. 7
0
    def close_out2(self, strategy='s2'):
        oanda_logger.info(
            "=============================Oanda Close Out============================="
        )
        # get close out id
        close_out_id_list = []
        with open(self.close_out_file_path, 'r') as f:
            for line in f:
                if line == '\n':
                    continue
                line_list = line.strip().split(',')
                date = line_list[1]
                id = line_list[-1]
                date_temp = time.strptime(date, '%Y-%m-%d')
                date_obj = datetime.datetime(*date_temp[:3]).date()
                if self.date_today == date_obj:
                    close_out_id_list.append(id)

        if not close_out_id_list:
            oanda_logger.info("No close out forex for {}".format(date))

        for i, trade_id in enumerate(close_out_id_list):

            url = self.close_out_url.format(self.account_id, trade_id)
            response = requests.put(url, headers=self.headers)
            response_content = response.json()
            status_code = response.status_code
            oanda_logger.info("trade_id: ", trade_id)
            oanda_logger.info("response_content: ", response_content)
            oanda_logger.info("status_code: ", status_code)
            # ===============================================================================
            # delete the close_out_order line if close out is done succesfully
            # TODO change status_code
            new_file_list = []
            if status_code == 200:
                with open(self.close_out_file_path, 'r') as f:
                    for line in f:
                        line_list = line.strip().split(',')
                        f_trade_id = line_list[-1]
                        date = line_list[1]
                        date_temp = time.strptime(date, '%Y-%m-%d')
                        date_obj = datetime.datetime(*date_temp[:3]).date()
                        if f_trade_id == trade_id and self.date_today == date_obj:
                            continue
                        else:
                            new_file_list.append(line)

                with open(self.close_out_file_path, 'w') as f:
                    for new_line in new_file_list:
                        f.write(new_line)
                oanda_logger.info(
                    "Close out trade_id {} succesfully!".format(trade_id))
            else:
                oanda_logger.info(
                    "trade_id {} does not hold any position!".format(trade_id))
            # ===============================================================================
        oanda_logger.info(
            "=============================Oanda Close Out END=============================\n"
        )
Esempio n. 8
0
    def close_out(self, trading_params, day_buy, day_sell, strategy='s2'):
        def get_trade_id(instrument):
            url = self.get_trade_id_url.format(self.account_id, instrument)
            response = requests.get(url, headers=self.headers)
            trade_id = dict(response.json())['trades'][0]["id"]
            return trade_id

        # ::: close_out
        time = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        instrument_in_pos = self.get_all_positions()
        print("instrument_in_pos", instrument_in_pos)
        # ('EUR_USD', 'USD_JPY')
        close_out_instruments, units_list, line_id_list = self.get_close_out_instrument(
            day_buy, day_sell, strategy=strategy)
        close_out_instruments = set(close_out_instruments) & set(
            instrument_in_pos)
        if not close_out_instruments:
            # return logging
            oanda_logger.info(
                "=============================No Close Out============================="
            )
            oanda_logger.info("Time: {}".format(time))
            oanda_logger.info("Day buy: {}".format(day_buy))
            oanda_logger.info("Day sell: {}".format(day_sell))
            oanda_logger.info("Hold position: {}".format(instrument_in_pos))
            oanda_logger.info("No close_out!")
            oanda_logger.info(
                "=============================Return Trading END==========================\n"
            )
            return

        # get close out id
        close_out_id_list = []
        with open(self.close_out_file_path, 'r') as f:
            for line in f:
                if line == '\n':
                    continue
                line_list = line.strip().split(',')
                date = line_list[1]
                id = line_list[-1]
                date_temp = time.strptime(date, '%Y%m%d')
                date_obj = datetime.datetime(*date_temp[:3])
                if self.date_today == date_obj:
                    close_out_id_list.append(id)

        if not close_out_id_list:
            print("No close out forex for {}".format())

        for i, trade_id in enumerate(close_out_id_list):

            url = self.close_out_url.format(self.account_id, trade_id)
            response = requests.put(url, headers=self.headers, json=body)
            response_content = response.json()
            status_code = response.status_code

            # ===============================================================================
            # delete the close_out_order line if close out is done succesfully
            # TODO change status_code
            if status_code == '200':
                delete_line_id = line_id_list[i]
                with open(self.close_out_file_path, 'r') as f:
                    file_lines = f.readlines()
                new_file_lines = [
                    x for i, x in enumerate(file_lines) if i != delete_line_id
                ]
                with open(self.close_out_file_path, 'w') as f:
                    for new_line in new_file_lines:
                        f.write(new_line)
                        f.write('\n')
            # ===============================================================================

            # close_out logging
            oanda_logger.info(
                "=============================Oanda Close Out============================="
            )
            oanda_logger.info("Close Out Time: {}".format(time))
            oanda_logger.info("Close Out trade_id: {}".format(trade_id))
            oanda_logger.info("status_code: {}".format(status_code))
            oanda_logger.info("response_content:\n {}".format(
                pprint.pformat(response_content)))
            oanda_logger.info(
                "=============================Oanda Trading END==========================\n"
            )
Esempio n. 9
0
    def get_all_positions(self):
        time = datetime.datetime.today().strftime("%Y-%m-%d %H:%M:%S")
        url = self.get_all_positions_url
        response = requests.get(url, headers=self.headers)
        positions_list = dict(response.json())['positions']
        pprint.pprint(positions_list)
        all_pos_list = []
        for positions_dict in positions_list:
            instrument = positions_dict['instrument']
            all_pos_list.append(instrument)

            # get the pos detail(buy/sell)
            long_units = int(positions_dict['long']['units'])
            short_units = int(positions_dict['short']['units'])
            if short_units != 0 and long_units == 0:
                if short_units > 0:
                    buy_or_sell = 'buy'
                elif short_units < 0:
                    buy_or_sell = 'sell'
            elif long_units != 0 and short_units == 0:
                if long_units > 0:
                    buy_or_sell = 'buy'
                elif long_units < 0:
                    buy_or_sell = 'sell'
            else:
                oanda_logger.error(
                    "ERROR HAPPEND AT get_all_positions! long_unit, short unit"
                )
                sys.exit(0)
            self.pos_detail_dict[instrument] = buy_or_sell

        #   archive positions
        date = datetime.datetime.today().strftime("%Y-%m-%d")
        folder_name = 'position_archive'
        date_file_name = date + '_positions.txt'
        date_file_path = os.path.join(folder_name, date_file_name)
        archive_time = datetime.datetime.today().strftime("%Y-%m-%d-%H-%M-%S")

        with open(date_file_path, 'w') as f:
            f.write("archive_time: {}\n".format(archive_time))
            for detail in positions_list:
                f.write(str(detail))
                f.write("\n")
        #

        # logging
        oanda_logger.info(
            "=============================All positions============================="
        )
        oanda_logger.info("Time: {}".format(time))
        oanda_logger.info("All_pos_list: {}".format(all_pos_list))
        oanda_logger.info("Pos details:\n {}".format(
            pprint.pformat(response.json())))
        oanda_logger.info("Buy/Sell details:\n {}".format(
            pprint.pformat(dict(self.pos_detail_dict))))
        oanda_logger.info(
            "=============================All positions END==========================\n"
        )
        return all_pos_list
Esempio n. 10
0
def get_ga_classifier_result_dict():
    oanda_logger.info(
        "======================GETTING FOREX RETURN START======================"
    )
    import random
    today = datetime.datetime.today()
    today = datetime.date(year=today.year, month=today.month, day=today.day)
    ga_classifier_result_dict = collections.defaultdict(lambda: [])
    chromosome_strategy_chosen_path = os.path.join(
        code_main_folder, 'pyoanda', 'chromosome_strategy_chosen.txt')
    with open(chromosome_strategy_chosen_path, 'r', encoding='utf-8') as f:
        for line in f:
            chromosome_type = re.findall(r'#([A-Za-z0-9_]+)#', line)[0]
            chromosome = re.findall(r':chromosome#([0-9]+)#END', line)[0]
            chromosome_bits = list(chromosome)
            # convert str to int
            chromosome_bits = [int(x) for x in chromosome_bits]
            print("len_chromosome: ", len(chromosome_bits))
            #chromosome_bits = [random.sample([0,1],1)[0] for x in range(len(chromosome_bits))]

            oanda_logger.info("chromosome_bits: {}".format(''.join(
                [str(x) for x in chromosome_bits])))
            #(chromosome_bits, chromosome_type, parameter_path, data_path, output_path, trading = False
            # cls_result_today: (datetime.date(2017, 2, 17), 'GBP_USD')
            cls_result = get_single_chromo_cls_result(
                chromosome_bits,
                chromosome_type,
                oanda_main_parameter_json__path,
                oanda_forex_trading_data_path,
                trading=True)

            print("cls_result: ", cls_result)

            oanda_logger.debug("chromosome_type: {}, cls_result :{}".format(
                chromosome_type, cls_result))
            # test whether this chromosome has return any forex for any date
            if cls_result == None:
                oanda_logger.info("{} has no forex return for any date".format(
                    chromosome_type))
            else:
                cls_result_today = cls_result[-1]
                # test whether the fetech result is today
                date_cls = cls_result_today[0]
                if date_cls == today:
                    ga_classifier_result_dict[chromosome_type].append(
                        cls_result_today[1])
                    oanda_logger.info("{} has return forex {} for {}".format(
                        chromosome_type, cls_result_today, today))
                else:
                    oanda_logger.info("{} has no forex return for {}".format(
                        chromosome_type, today))

    print("ga_classifier_result_dict: ", dict(ga_classifier_result_dict))
    oanda_logger.info(
        "======================GETTING FOREX RETURN END======================")
    return ga_classifier_result_dict