Пример #1
0
    def execute_orders(self, trade_obj: Trade) -> dict:
        """Executes a Trade Object.
        Overview:
        ----
        The `execute_orders` method will execute trades as they're signaled. When executed,
        the `Trade` object will have the order response saved to it, and the order response will
        be saved to a JSON file for further analysis.
        Arguments:
        ----
        trade_obj {Trade} -- A trade object with the `order` property filled out.
        Returns:
        ----
        {dict} -- An order response dicitonary.
        """

        # Execute the order.
        order_dict = self.session.place_order(account=self.trading_account,
                                              order=trade_obj.order)

        # Store the order.
        trade_obj._order_response = order_dict

        # Process the order response.
        trade_obj._process_order_response()

        return order_dict
Пример #2
0
 def create_trade(self,
                  trade_id: str,
                  enter_or_exit: str,
                  long_or_short: str,
                  order_type: str = 'mkt',
                  price: float = 0.0,
                  stop_limit_price: float = 0.0) -> Trade:
     '''
     Init a new instance of the trade object.
     The point is to simplify the process of building an order
     '''
     trade = Trade()
     trade.new_trade(trade_id=trade_id,
                     order_type=order_type,
                     enter_or_exit=enter_or_exit,
                     side=long_or_short,
                     price=price,
                     stop_limit_price=stop_limit_price)
     self.trades[trade_id] = trade
     return trade
Пример #3
0
    def create_trade(self,
                     trade_id: str,
                     enter_or_exit: str,
                     long_or_short: str,
                     order_type: str = 'mkt',
                     price: float = 0.0,
                     stop_limit_price=0.0) -> Trade:
        """Initalizes a new instance of a Trade Object.

        This helps simplify the process of building an order by using pre-built templates that can be
        easily modified to incorporate more complex strategies.

        Arguments:
        ----
        trade_id {str} -- The ID associated with the trade, this can then be used to access the trade during runtime.

        enter_or_exit {str} -- Defines whether this trade will be used to enter or exit a position.
            If used to enter, specify `enter`. If used to exit, speicfy `exit`.

        long_or_short {str} -- Defines whether this trade will be used to go long or short a position.
            If used to go long, specify `long`. If used to go short, speicfy `short`.

        Keyword Arguments:
        ----
        order_type {str} -- Defines the type of order to initalize. Possible values
            are `'mkt', 'lmt', 'stop', 'stop-lmt', 'trailign-stop'` (default: {'mkt'})

        price {float} -- The Price to be associate with the order. If the order type is `stop` or `stop-lmt` then 
            it is the stop price, if it is a `lmt` order then it is the limit price, and `mkt` is the market 
            price.(default: {0.0})

        stop_limit_price {float} -- Only used if the order is a `stop-lmt` and represents the limit price of
            the `stop-lmt` order. (default: {0.0})

        Usage:
        ----
            >>> trading_robot = PyRobot(
                client_id=CLIENT_ID, 
                redirect_uri=REDIRECT_URI, 
                credentials_path=CREDENTIALS_PATH
            )
            >>> new_trade = trading_robot_portfolio.create_trade(
                trade_id='long_1',
                enter_or_exit='enter',
                long_or_short='long',
                order_type='mkt'
            )
            >>> new_trade

            >>> new_market_trade = trading_robot_portfolio.create_trade(
                trade_id='long_2',
                enter_or_exit='enter',
                long_or_short='long',
                order_type='mkt',
                price=12.00
            )
            >>> new_market_trade

            >>> new_stop_trade = trading_robot_portfolio.create_trade(
                trade_id='long_3',
                enter_or_exit='enter',
                long_or_short='long',
                order_type='stop',
                price=2.00
            )
            >>> new_stop_trade

            >>> new_stop_limit_trade = trading_robot_portfolio.create_trade(
                trade_id='long_4',
                enter_or_exit='enter',
                long_or_short='long',
                order_type='stop-lmt',
                price=2.00,
                stop_limit_price=1.90
            )
            >>> new_stop_limit_trade

        Returns:
        ----
        Trade -- A pyrobot.Trade object with the specified template.
        """

        # Initalize a new trade object.
        trade = Trade()

        # Create a new trade.
        trade.new_trade(trade_id=trade_id,
                        order_type=order_type,
                        side=long_or_short,
                        enter_or_exit=enter_or_exit,
                        price=price,
                        stop_limit_price=stop_limit_price)

        self.trades[trade_id] = trade

        return trade