Skip to main content

Method: openPosition

The openPosition method is used to open a trading position with various parameters such as position side, take profit, stop loss, leverage, and maximum time to open the position. There are multiple overloads of this method to accommodate different trading scenarios.


Overloads

  1. openPosition(PositionSide positionSide, double? takeProfit, double? stopLoss)
  2. openPosition(PositionSide positionSide, double? takeProfit, int leverage)
  3. openPosition(PositionSide positionSide, double takeProfit, double stopLoss, string openMaxTime)

1. openPosition(PositionSide positionSide, double? takeProfit, double? stopLoss)

Description

Opens a trading position specifying the side (long or short), with optional take profit and stop loss levels.

Parameters

  • positionSide (PositionSide): The direction of the position to open.

    • PositionSide.BUY: Indicates a long position.
    • PositionSide.SELL: Indicates a short position.
  • takeProfit (double?): (Optional) The price level at which to take profit. Set to null if no take profit is desired.

  • stopLoss (double?): (Optional) The price level at which to stop loss. Set to null if no stop loss is desired.

Usage Example

// Open a long position with take profit at 1.1500 and stop loss at 1.1200
openPosition(PositionSide.BUY, 1.1500, 1.1200);

// Open a short position without take profit or stop loss
openPosition(PositionSide.SELL, null, null);

Notes

  • If both takeProfit and stopLoss are omitted (null), the position will be opened without any profit or loss targets.
  • Use this overload when leverage is not a concern or defaults to the account's standard leverage.

2. openPosition(PositionSide positionSide, double? takeProfit, int leverage)

Description

Opens a trading position specifying the side, optional take profit, and the leverage to apply.

Parameters

  • positionSide (PositionSide): The direction of the position to open (BUY or SELL).

  • takeProfit (double?): (Optional) The price level at which to take profit. Set to null if no take profit is desired.

  • leverage (int): The leverage factor to apply to the position.

Usage Example

// Open a long position with take profit at 1.1500 and leverage of 10x
openPosition(PositionSide.BUY, 1.1500, 10);

// Open a short position with leverage of 20x without take profit
openPosition(PositionSide.SELL, null, 20);

Notes

  • The leverage parameter allows you to control the position size relative to your account balance.
  • Ensure that the specified leverage complies with your broker's or exchange's regulations.

3. openPosition(PositionSide positionSide, double takeProfit, double stopLoss, string openMaxTime)

Description

Opens a trading position specifying the side, take profit, stop loss, and the maximum time within which the position should be opened.

Parameters

  • positionSide (PositionSide): The direction of the position to open (BUY or SELL).

  • takeProfit (double): The price level at which to take profit.

  • stopLoss (double): The price level at which to stop loss.

  • openMaxTime (string): The maximum allowable time to open the position, formatted as "hours:minutes".

    • Example formats:
      • "0:30" for 30 minutes.
      • "2:15" for 2 hours and 15 minutes.

Usage Example

// Open a long position with take profit and stop loss, to be opened within 1 hour
openPosition(PositionSide.BUY, 1.1500, 1.1200, "1:00");

// Open a short position with take profit and stop loss, to be opened within 30 minutes
openPosition(PositionSide.SELL, 1.1300, 1.1600, "0:30");

Notes

  • The openMaxTime parameter ensures the position is opened within the specified time window. If the time elapses before the position is opened, the request may be cancelled.
  • The time format must strictly follow "hours:minutes". Invalid formats may cause errors.

Common Parameter Definitions

  • PositionSide: An enumeration indicating the position direction.

    public enum PositionSide
    {
    BUY,
    SELL
    }
  • double?: A nullable double. Can hold a double value or null.

  • double: A double-precision floating-point number.

  • int: A 32-bit integer.

  • string: A sequence of characters.


General Usage Notes

  • Take Profit and Stop Loss: Setting these parameters helps in automating exit points for your positions, aiding in risk management.

  • Leverage: Utilize leverage cautiously, as it can amplify both gains and losses.

  • Open Max Time: Useful in time-sensitive trading strategies where positions need to be opened within a specific timeframe.

  • Error Handling: Always validate parameter values before calling the method to prevent runtime errors or unintended trading actions.


Additional Examples

Example 1: Opening a Long Position with Default Leverage

// Open a long position with only a take profit target
openPosition(PositionSide.BUY, 1.1550, null);

Example 2: Opening a Position with Custom Leverage

// Open a short position with 5x leverage
openPosition(PositionSide.SELL, null, 5);

Example 3: Opening a Position with Time Constraint

// Open a long position that must be opened within 45 minutes
openPosition(PositionSide.Long, 1.1600, 1.1400, "0:45");

Best Practices

  • Input Validation: Ensure that the input values are within acceptable ranges (e.g., positive numbers for prices, valid times).

  • Exception Handling: Implement try-catch blocks around the method calls to handle any potential exceptions.

  • Logging: Keep logs of method calls with parameters for auditing and debugging purposes.

  • Concurrency: If dealing with multi-threaded applications, ensure thread safety when accessing shared resources.


Conclusion

The openPosition method provides flexibility for opening trading positions with various parameters tailored to different trading strategies. Understanding each overload and its parameters allows for precise control over how and when positions are opened.