Exemplo n.º 1
0
 def get_open_position(trade_id, connection='benchmark'):
     """
     Returns the open position for the given trade id
     :param trade_id: tradeId for the position
     :param connection: specify the connection account to be used. can be either 'benchmark' or 'lstm'
     :raises ValueError if the position can not be found
     """
     conn = Client.get_connection(connection, True)
     return conn.get_open_position(trade_id)
Exemplo n.º 2
0
 def get_last_n_candles(instrument=cfg.instrument,
                        period=Period.MINUTE_1[0],
                        n=60,
                        connection='benchmark'):
     """
     Returns the last n candles of the given instrument and period
     :param instrument: Currency pair e.g. 'EUR/USD'
     :param period: Candle time interval e.g. 'm1'
     :param n: Number of candles to retrieve
     :param connection: specify the connection account to be used. can be either 'benchmark' or 'lstm'
     :return DataFrame: The last n candles
     """
     return Client.get_connection(connection,
                                  False).get_candles(instrument,
                                                     period=period,
                                                     number=n)
Exemplo n.º 3
0
    def close_all(connection='benchmark'):
        """
        SELL: Closes all open positions
        :param connection: specify the connection account to be used. can be either 'benchmark' or 'lstm'
        :returns Float the gross profit/loss of all closed positions. returns 0 if there are no positions to close
        """
        conn = Client.get_connection(connection, True)

        # Get all open positions
        positions = conn.get_open_positions()

        # Close all open positions and return the sum of their profit
        profit = 0
        for index, position in positions.iterrows():
            position.close()
            profit = profit + position.get_grossPL()
        return profit
Exemplo n.º 4
0
    def buy(amount=0,
            stop_percentage=.95,
            symbol='EUR/USD',
            connection='benchmark'):
        """
        Opens a new buy position for the specified amount
        :param amount: amount to buy. set to 0 to go all in
        :param stop_percentage: the stop limit will be set to stop_percentage*current_market_price
        :param symbol: currency to buy
        :param connection: specify the connection account to be used. can be either 'benchmark' or 'lstm'
        :returns fxcm_position: The new position object returned by FXCM. You can call various methods such as position.get_grossPL() to get the profit or position.get_amount() to get the trade amount
        :raises Exception
        """
        conn = Client.get_connection(connection, True)

        if conn.get_open_positions().T.size == 0:  # No open positions
            # Check account funds
            accounts = conn.get_accounts()
            usable_funds = accounts['usableMargin'][0].T
            if amount == 0:
                # Use all available funds
                buy = usable_funds
            elif amount < usable_funds:
                buy = amount
            else:
                # Insufficient funds!
                raise Exception('Insufficient funds in account')

            # Place the buy order
            order = conn.create_market_buy_order(symbol, buy)

            # Calculate the effective stop loss rate
            trade_id = order.get_tradeId()
            buy_price = order.get_buy()
            stop_rate = stop_percentage * buy_price

            # Set the stop loss rate on the active trade
            conn.change_trade_stop_limit(trade_id,
                                         is_in_pips=False,
                                         is_stop=True,
                                         rate=stop_rate)
            return conn.get_open_position(trade_id)
        else:
            # Positions are already opened!
            raise Exception('This account already has an open position')
Exemplo n.º 5
0
    def close_position(trade_id, connection='benchmark'):
        """
        SELL: Closes the given position and returns the gross profit/loss that was made
        :param trade_id: tradeId for the position
        :param connection: specify the connection account to be used. can be either 'benchmark' or 'lstm'
        :returns fxcm_position: The position object returned by FXCM. You can call various methods such as position.get_grossPL() to get the profit or position.get_amount() to get the trade amount
        :raises ValueError when the position can not be found
        """
        conn = Client.get_connection(connection, True)

        try:
            position = conn.get_open_position(trade_id)
            position.close()
        except ValueError:
            # position not found, stoploss might have been called. try for closed position instead
            position = conn.get_closed_position(trade_id)

        return position
Exemplo n.º 6
0
 def get_candles_from_interval(instrument=cfg.instrument,
                               period=Period.DAY_1[0],
                               start=datetime.datetime(2018, 6, 20),
                               stop=datetime.datetime(2018, 10, 20),
                               connection='benchmark'):
     """
     Returns all candles in the given interval
     https://www.fxcm.com/fxcmpy/02_historical_data.html#Time-Windows
     :param instrument: Currency pair e.g. 'EUR/USD'
     :param period: Candle time interval e.g. 'm1'
     :param start: datetime start date
     :param stop: datetime stop date
     :param connection: specify the connection account to be used. can be either 'benchmark' or 'lstm'
     :return DataFrame: The candles in the given time window
     """
     return Client.get_connection(connection,
                                  False).get_candles(instrument,
                                                     period=period,
                                                     start=start,
                                                     stop=stop)
Exemplo n.º 7
0
 def tearDownClass(cls):
     Client.logout()
Exemplo n.º 8
0
    def test_login(self):
        conn = Client.get_connection()
        self.assertEqual(conn.connection_status, 'established')

        conn = Client.get_connection()
        self.assertEqual(conn.connection_status, 'established')