Class PositionsApi

java.lang.Object
markets.alpaca.client.openapi.trading.api.PositionsApi

public class PositionsApi extends Object
Generated client for PositionsApi operations.

This class is generated from the configured OpenAPI spec. Operation method Javadocs include OAS summaries, descriptions, parameters, return types, response details, and external documentation links when present in the spec.

Operations

  • deleteAllOpenPositions: Close All Positions
  • deleteOpenPosition: Close a Position
  • getAllOpenPositions: All Open Positions
  • getOpenPosition: Get an Open Position
  • optionDoNotExercise: Do Not Exercise an Options Position
  • optionExercise: Exercise an Options Position
  • Constructor Details

    • PositionsApi

      public PositionsApi()
    • PositionsApi

      public PositionsApi(ApiClient apiClient)
  • Method Details

    • getApiClient

      public ApiClient getApiClient()
    • setApiClient

      public void setApiClient(ApiClient apiClient)
    • getHostIndex

      public int getHostIndex()
    • setHostIndex

      public void setHostIndex(int hostIndex)
    • getCustomBaseUrl

      public String getCustomBaseUrl()
    • setCustomBaseUrl

      public void setCustomBaseUrl(String customBaseUrl)
    • deleteAllOpenPositionsCall

      public okhttp3.Call deleteAllOpenPositionsCall(@Nullable Boolean cancelOrders, ApiCallback _callback) throws ApiException
      Build call for deleteAllOpenPositions
      Parameters:
      cancelOrders - If true is specified, cancel all open orders before liquidating all positions. (optional)
      _callback - Callback for upload/download progress
      Returns:
      Call to execute
      Throws:
      ApiException - If fail to serialize the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      207 Multi-Status with body. an array of PositionClosed responses -
      500 Failed to liquidate -
    • deleteAllOpenPositions

      public List<PositionClosedReponse> deleteAllOpenPositions(@Nullable Boolean cancelOrders) throws ApiException
      Close All Positions Closes (liquidates) all of the account's open long and short positions. A response will be provided for each order that is attempted to be cancelled. If an order is no longer cancelable, the server will respond with status 500 and reject the request.
      Parameters:
      cancelOrders - If true is specified, cancel all open orders before liquidating all positions. (optional)
      Returns:
      List<PositionClosedReponse>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      207 Multi-Status with body. an array of PositionClosed responses -
      500 Failed to liquidate -
    • deleteAllOpenPositionsWithHttpInfo

      public ApiResponse<List<PositionClosedReponse>> deleteAllOpenPositionsWithHttpInfo(@Nullable Boolean cancelOrders) throws ApiException
      Close All Positions Closes (liquidates) all of the account's open long and short positions. A response will be provided for each order that is attempted to be cancelled. If an order is no longer cancelable, the server will respond with status 500 and reject the request.
      Parameters:
      cancelOrders - If true is specified, cancel all open orders before liquidating all positions. (optional)
      Returns:
      ApiResponse<List<PositionClosedReponse>>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      207 Multi-Status with body. an array of PositionClosed responses -
      500 Failed to liquidate -
    • deleteAllOpenPositionsAsync

      public okhttp3.Call deleteAllOpenPositionsAsync(@Nullable Boolean cancelOrders, ApiCallback<List<PositionClosedReponse>> _callback) throws ApiException
      Close All Positions (asynchronously) Closes (liquidates) all of the account's open long and short positions. A response will be provided for each order that is attempted to be cancelled. If an order is no longer cancelable, the server will respond with status 500 and reject the request.
      Parameters:
      cancelOrders - If true is specified, cancel all open orders before liquidating all positions. (optional)
      _callback - The callback to be executed when the API call finishes
      Returns:
      The request call
      Throws:
      ApiException - If fail to process the API call, e.g. serializing the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      207 Multi-Status with body. an array of PositionClosed responses -
      500 Failed to liquidate -
    • deleteOpenPositionCall

      public okhttp3.Call deleteOpenPositionCall(@Nonnull String symbolOrAssetId, @Nullable BigDecimal qty, @Nullable BigDecimal percentage, ApiCallback _callback) throws ApiException
      Build call for deleteOpenPosition
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      qty - the number of shares to liquidate. Can accept up to 9 decimal points. Cannot work with percentage (optional)
      percentage - percentage of position to liquidate. Must be between 0 and 100. Would only sell fractional if position is originally fractional. Can accept up to 9 decimal points. Cannot work with qty (optional)
      _callback - Callback for upload/download progress
      Returns:
      Call to execute
      Throws:
      ApiException - If fail to serialize the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response Returns the order created to close out this position -
    • deleteOpenPosition

      public Order deleteOpenPosition(@Nonnull String symbolOrAssetId, @Nullable BigDecimal qty, @Nullable BigDecimal percentage) throws ApiException
      Close a Position Closes (liquidates) the account's open position for the given symbol. Works for both long and short positions.
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      qty - the number of shares to liquidate. Can accept up to 9 decimal points. Cannot work with percentage (optional)
      percentage - percentage of position to liquidate. Must be between 0 and 100. Would only sell fractional if position is originally fractional. Can accept up to 9 decimal points. Cannot work with qty (optional)
      Returns:
      Order
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response Returns the order created to close out this position -
    • deleteOpenPositionWithHttpInfo

      public ApiResponse<Order> deleteOpenPositionWithHttpInfo(@Nonnull String symbolOrAssetId, @Nullable BigDecimal qty, @Nullable BigDecimal percentage) throws ApiException
      Close a Position Closes (liquidates) the account's open position for the given symbol. Works for both long and short positions.
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      qty - the number of shares to liquidate. Can accept up to 9 decimal points. Cannot work with percentage (optional)
      percentage - percentage of position to liquidate. Must be between 0 and 100. Would only sell fractional if position is originally fractional. Can accept up to 9 decimal points. Cannot work with qty (optional)
      Returns:
      ApiResponse<Order>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response Returns the order created to close out this position -
    • deleteOpenPositionAsync

      public okhttp3.Call deleteOpenPositionAsync(@Nonnull String symbolOrAssetId, @Nullable BigDecimal qty, @Nullable BigDecimal percentage, ApiCallback<Order> _callback) throws ApiException
      Close a Position (asynchronously) Closes (liquidates) the account's open position for the given symbol. Works for both long and short positions.
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      qty - the number of shares to liquidate. Can accept up to 9 decimal points. Cannot work with percentage (optional)
      percentage - percentage of position to liquidate. Must be between 0 and 100. Would only sell fractional if position is originally fractional. Can accept up to 9 decimal points. Cannot work with qty (optional)
      _callback - The callback to be executed when the API call finishes
      Returns:
      The request call
      Throws:
      ApiException - If fail to process the API call, e.g. serializing the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response Returns the order created to close out this position -
    • getAllOpenPositionsCall

      public okhttp3.Call getAllOpenPositionsCall(ApiCallback _callback) throws ApiException
      Build call for getAllOpenPositions
      Parameters:
      _callback - Callback for upload/download progress
      Returns:
      Call to execute
      Throws:
      ApiException - If fail to serialize the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getAllOpenPositions

      public List<Position> getAllOpenPositions() throws ApiException
      All Open Positions The positions API provides information about an account's current open positions. The response will include information such as cost basis, shares traded, and market value, which will be updated live as price information is updated. Once a position is closed, it will no longer be queryable through this API Retrieves a list of the account's open positions
      Returns:
      List<Position>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getAllOpenPositionsWithHttpInfo

      public ApiResponse<List<Position>> getAllOpenPositionsWithHttpInfo() throws ApiException
      All Open Positions The positions API provides information about an account's current open positions. The response will include information such as cost basis, shares traded, and market value, which will be updated live as price information is updated. Once a position is closed, it will no longer be queryable through this API Retrieves a list of the account's open positions
      Returns:
      ApiResponse<List<Position>>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getAllOpenPositionsAsync

      public okhttp3.Call getAllOpenPositionsAsync(ApiCallback<List<Position>> _callback) throws ApiException
      All Open Positions (asynchronously) The positions API provides information about an account's current open positions. The response will include information such as cost basis, shares traded, and market value, which will be updated live as price information is updated. Once a position is closed, it will no longer be queryable through this API Retrieves a list of the account's open positions
      Parameters:
      _callback - The callback to be executed when the API call finishes
      Returns:
      The request call
      Throws:
      ApiException - If fail to process the API call, e.g. serializing the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getOpenPositionCall

      public okhttp3.Call getOpenPositionCall(@Nonnull String symbolOrAssetId, ApiCallback _callback) throws ApiException
      Build call for getOpenPosition
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      _callback - Callback for upload/download progress
      Returns:
      Call to execute
      Throws:
      ApiException - If fail to serialize the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getOpenPosition

      public Position getOpenPosition(@Nonnull String symbolOrAssetId) throws ApiException
      Get an Open Position Retrieves the account's open position for the given symbol or assetId.
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      Returns:
      Position
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getOpenPositionWithHttpInfo

      public ApiResponse<Position> getOpenPositionWithHttpInfo(@Nonnull String symbolOrAssetId) throws ApiException
      Get an Open Position Retrieves the account's open position for the given symbol or assetId.
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      Returns:
      ApiResponse<Position>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • getOpenPositionAsync

      public okhttp3.Call getOpenPositionAsync(@Nonnull String symbolOrAssetId, ApiCallback<Position> _callback) throws ApiException
      Get an Open Position (asynchronously) Retrieves the account's open position for the given symbol or assetId.
      Parameters:
      symbolOrAssetId - symbol or assetId (required)
      _callback - The callback to be executed when the API call finishes
      Returns:
      The request call
      Throws:
      ApiException - If fail to process the API call, e.g. serializing the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful response -
    • optionDoNotExerciseCall

      public okhttp3.Call optionDoNotExerciseCall(@Nonnull UUID symbolOrContractId, ApiCallback _callback) throws ApiException
      Build call for optionDoNotExercise
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      _callback - Callback for upload/download progress
      Returns:
      Call to execute
      Throws:
      ApiException - If fail to serialize the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Do-not-exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient or no position found. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionDoNotExercise

      public void optionDoNotExercise(@Nonnull UUID symbolOrContractId) throws ApiException
      Do Not Exercise an Options Position This endpoint enables users to submit a do-not-exercise (DNE) instruction for a held option contract, preventing automatic exercise at expiry. By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry. This endpoint allows users to override that behavior. To override this behavior and submit an exercise instruction, please contact our support team.
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Do-not-exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient or no position found. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionDoNotExerciseWithHttpInfo

      public ApiResponse<Void> optionDoNotExerciseWithHttpInfo(@Nonnull UUID symbolOrContractId) throws ApiException
      Do Not Exercise an Options Position This endpoint enables users to submit a do-not-exercise (DNE) instruction for a held option contract, preventing automatic exercise at expiry. By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry. This endpoint allows users to override that behavior. To override this behavior and submit an exercise instruction, please contact our support team.
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      Returns:
      ApiResponse<Void>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Do-not-exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient or no position found. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionDoNotExerciseAsync

      public okhttp3.Call optionDoNotExerciseAsync(@Nonnull UUID symbolOrContractId, ApiCallback<Void> _callback) throws ApiException
      Do Not Exercise an Options Position (asynchronously) This endpoint enables users to submit a do-not-exercise (DNE) instruction for a held option contract, preventing automatic exercise at expiry. By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry. This endpoint allows users to override that behavior. To override this behavior and submit an exercise instruction, please contact our support team.
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      _callback - The callback to be executed when the API call finishes
      Returns:
      The request call
      Throws:
      ApiException - If fail to process the API call, e.g. serializing the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Do-not-exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient or no position found. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionExerciseCall

      public okhttp3.Call optionExerciseCall(@Nonnull UUID symbolOrContractId, ApiCallback _callback) throws ApiException
      Build call for optionExercise
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      _callback - Callback for upload/download progress
      Returns:
      Call to execute
      Throws:
      ApiException - If fail to serialize the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionExercise

      public void optionExercise(@Nonnull UUID symbolOrContractId) throws ApiException
      Exercise an Options Position This endpoint enables users to exercise a held option contract, converting it into the underlying asset based on the specified terms. All available held shares of this option contract will be exercised. By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry. Exercise requests will be processed immediately once received. Exercise requests submitted between market close and midnight will be rejected to avoid any confusion about when the exercise will settle. To cancel an exercise request or to submit a Do-not-exercise (DNE) instruction, you can use the do-not-exercise endpoint or contact our support team.
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionExerciseWithHttpInfo

      public ApiResponse<Void> optionExerciseWithHttpInfo(@Nonnull UUID symbolOrContractId) throws ApiException
      Exercise an Options Position This endpoint enables users to exercise a held option contract, converting it into the underlying asset based on the specified terms. All available held shares of this option contract will be exercised. By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry. Exercise requests will be processed immediately once received. Exercise requests submitted between market close and midnight will be rejected to avoid any confusion about when the exercise will settle. To cancel an exercise request or to submit a Do-not-exercise (DNE) instruction, you can use the do-not-exercise endpoint or contact our support team.
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      Returns:
      ApiResponse<Void>
      Throws:
      ApiException - If fail to call the API, e.g. server error or cannot deserialize the response body
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient. -
      422 Invalid Parameters. One or more parameters provided are invalid. -
    • optionExerciseAsync

      public okhttp3.Call optionExerciseAsync(@Nonnull UUID symbolOrContractId, ApiCallback<Void> _callback) throws ApiException
      Exercise an Options Position (asynchronously) This endpoint enables users to exercise a held option contract, converting it into the underlying asset based on the specified terms. All available held shares of this option contract will be exercised. By default, Alpaca will automatically exercise in-the-money (ITM) contracts at expiry. Exercise requests will be processed immediately once received. Exercise requests submitted between market close and midnight will be rejected to avoid any confusion about when the exercise will settle. To cancel an exercise request or to submit a Do-not-exercise (DNE) instruction, you can use the do-not-exercise endpoint or contact our support team.
      Parameters:
      symbolOrContractId - Option contract symbol or ID. (required)
      _callback - The callback to be executed when the API call finishes
      Returns:
      The request call
      Throws:
      ApiException - If fail to process the API call, e.g. serializing the request body object
      HTTP Response Details:
      Response Details
      Status Code Description Response Headers
      200 Successful Response Exercise instruction successfully submitted. -
      403 Forbidden Available position quantity is not sufficient. -
      422 Invalid Parameters. One or more parameters provided are invalid. -