new_order_leg.order_leg_price(price=112.50)

# Define the QUANTITY - CAN ONLY BE A INTEGER.
new_order_leg.order_leg_quantity(quantity=10)

# Define the ASSET to be traded - ENUM EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
new_order_leg.order_leg_asset(asset_type=ORDER_ASSET_TYPE.EQUITY, symbol='MSFT')

# Define the ASSET to be traded - STRING EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
new_order_leg.order_leg_asset(asset_type='EQUITY', symbol='MSFT')

# Order Legs can be copied so you have a template to build off of.
copied_order_leg = new_order_leg.copy()

# Once we have built our order leg, we can add it to our OrderObject.
new_order.add_order_leg(order_leg=new_order_leg)

# Print it out so you can see it, normally this method is called by the TD API when using the td.client object.
print("YOU HAVE {} ORDER(S) IN THE ORDER LEGS COLLECTION.".format(new_order.order_legs_count))
print('-'*80)

# We could also add a copied order to our order leg collection.
new_order.add_order_leg(order_leg=copied_order_leg)

# Print it out so we can see both now.
print("YOU HAVE {} ORDER(S) IN THE ORDER LEGS COLLECTION.".format(new_order.order_legs_count))
print('-'*80)


#
#  SOME ORDERS, USUALLY THE MORE COMPLEX ONES, HAVE A CHILD ORDER.
# Define the ORDER INSTRUCTION - ENUM EXAMPLE.
new_order_leg.order_leg_instruction(instruction=ORDER_INSTRUCTIONS.SELL)

# Define the PRICE - CAN ONLY BE A FLOAT.
new_order_leg.order_leg_price(price=112.50)

# Define the QUANTITY - CAN ONLY BE A INTEGER.
new_order_leg.order_leg_quantity(quantity=10)

# Define the ASSET to be traded - ENUM EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
new_order_leg.order_leg_asset(asset_type=ORDER_ASSET_TYPE.EQUITY,
                              symbol='MSFT')

# Once we have built our order leg, we can add it to our OrderObject.
new_order.add_order_leg(order_leg=new_order_leg)

# Create a new session
td_session = TDClient(
    account_number=ACCOUNT_NUMBER,
    account_password=ACCOUNT_PASSWORD,
    consumer_id=CONSUMER_ID,
    redirect_uri=REDIRECT_URI,
    json_path=r"C:\Users\Alex\OneDrive\Desktop\TDAmeritradeState.json")

td_session.login()

# # Place the Order
# td_session.place_order(account = '11111', order= new_order)

# Create the Order.
Exemplo n.º 3
0
class TDSession(TestCase):
    """Will perform a unit test for the TD session."""
    def setUp(self) -> None:
        """Set up the Robot."""

        # Grab configuration values.
        config = ConfigParser()
        config.read('config/config.ini')

        CLIENT_ID = config.get('main', 'CLIENT_ID')
        REDIRECT_URI = config.get('main', 'REDIRECT_URI')
        JSON_PATH = config.get('main', 'JSON_PATH')
        ACCOUNT_NUMBER = config.get('main', 'ACCOUNT_NUMBER')

        # Initalize the session.
        self.td_session = TDClient(client_id=CLIENT_ID,
                                   redirect_uri=REDIRECT_URI,
                                   credentials_path=JSON_PATH,
                                   account_number=ACCOUNT_NUMBER)

        self.td_order = Order()
        self.td_order_leg = OrderLeg()

    def test_creates_instance_of_session(self):
        """Create an instance and make sure it's a robot."""

        self.assertIsInstance(self.td_session, TDClient)
        self.assertIsInstance(self.td_order, Order)
        self.assertIsInstance(self.td_order_leg, OrderLeg)

    def test_define_simple_order(self):
        """Test creating a simple order."""

        # Add the Order session.
        self.td_order.order_session(session=td_enums.ORDER_SESSION.NORMAL)

        # Add the Order duration.
        self.td_order.order_duration(
            duration=td_enums.DURATION.GOOD_TILL_CANCEL)

        # Add the Order Leg Instruction.
        self.td_order_leg.order_leg_instruction(
            instruction=td_enums.ORDER_INSTRUCTIONS.SELL)

        # Add the Order Leg price.
        self.td_order_leg.order_leg_price(price=112.50)

        # Add the Order Leg quantity.
        self.td_order_leg.order_leg_quantity(quantity=10)

        # Add the Order Leg Asset.
        self.td_order_leg.order_leg_asset(
            asset_type=td_enums.ORDER_ASSET_TYPE.EQUITY, symbol='MSFT')

        # Add the Order Leg.
        self.td_order.add_order_leg(order_leg=self.td_order_leg)

        correct_dict = {
            "session":
            "NORMAL",
            "duration":
            "GOOD_TILL_CANCEL",
            "orderLegCollection": [{
                "instruction": "SELL",
                "price": 112.5,
                "quantity": 10,
                "instrument": {
                    "assetType": "EQUITY",
                    "symbol": "MSFT"
                }
            }]
        }

        self.assertDictEqual(correct_dict, self.td_order._grab_order())

    def tearDown(self):
        """Clean Up."""

        self.td_session = None
        self.td_order = None
        self.td_order_leg = None
Exemplo n.º 4
0
new_order.order_session(session=ORDER_SESSION.NORMAL)

# Define the DURATION of the Order - ENUM EXAMPLE.
new_order.order_duration(duration=DURATION.GOOD_TILL_CANCEL)

# Define a new OrderLeg Object.
new_order_leg = OrderLeg()

# Define the ORDER INSTRUCTION - ENUM EXAMPLE.
new_order_leg.order_leg_instruction(instruction=ORDER_INSTRUCTIONS.SELL)

# Define the PRICE - CAN ONLY BE A FLOAT.
new_order_leg.order_leg_price(price=112.50)

# Define the QUANTITY - CAN ONLY BE A INTEGER.
new_order_leg.order_leg_quantity(quantity=10)

# Define the ASSET to be traded - ENUM EXAMPLE -- SYMBOL MUST ALWAYS BE A STRING.
new_order_leg.order_leg_asset(asset_type=ORDER_ASSET_TYPE.EQUITY,
                              symbol='MSFT')

# Once we have built our order leg, we can add it to our OrderObject.
new_order.add_order_leg(order_leg=new_order_leg)

# Create a new session
td_session = TDClient(account_number=ACCOUNT_NUMBER,
                      account_password=ACCOUNT_PASSWORD,
                      consumer_id=CONSUMER_ID,
                      redirect_uri=REDIRECT_URI)

td_session.place_order(account='11111', order=new_order)