moomoo SG Help Center-Catch exceptions to improve your strategies
English
Back
Log in to access Online Inquiry
Back to the Top

Catch exceptions to improve your strategies

How to catch error code exceptions

When exceptions occur during the calling of algo functions, an APIException is thrown. This exception contains an err_code attribute that provides specific error code information corresponding to different error scenarios.

 

Error Code

Scenario

ExceedReqLimit

Requests too frequent

ReqTimeout

Request to the interface timed out

NoQuoteRight

No permission to access quotes

InvalidArgument

Invalid parameter (parameter validation failed)

ReqFailed

Interface request failed

NoDataAvailable

No data (returned data is NA)

EmptySymbol

The parameter symbol is empty

Unknow

Unknown error

 

To strengthen your strategy, you can catch error codes at critical points in the strategy and execute custom handling procedures.

Example: To improve the reliability of order placements, consider using a custom-coded card that attempts to resubmit the order until it succeeds in the event of an initial failure.

while True:
    try:
        place_limit(symbol=self.trigger_symbol 1, price=2, qty=100, side=OrderSide.BUY, time_in_force=TimeInForce.DAY)
        break  # Order placed successfully. Exit the loop.
    except APIException as ex:
        print('Order placement failed. Retry in 2 seconds.')
        time.sleep(2)
        continue  # Order placement failed. Retry.

Any images provided are not current and any securities or inputs shown are for illustrative purposes only and are not recommendations.

 

How to use a Custom-Coded Card vs Global Exception Settings

Using a custom-coded card for exception handling overrides the global exception settings.

 

Example 1: Passive exception catching

Use a standard card without custom coding to catch exception error codes. Make sure to change the default settings:

  • Set Exception Settings to Skip the Current Round

Any images provided are not current and any securities or inputs shown are for illustrative purposes only and are not recommendations.

When the strategy runs and reaches the No Retry on Order Failure card, it will follow the exception settings and skip this round if the order placement fails. Therefore, the Mark as Complete card will not be executed. The No Retry on Order Failure card will run again when the next signal triggers the strategy.

 

Example 2: Active exception catching

Use a custom-coded card to actively catch exception error codes and retry submitting the order until it is successful. Make sure to change the default settings:

  • Set Exception Settings to Skip the Current Round

Any images provided are not current and any securities or inputs shown are for illustrative purposes only and are not recommendations.

When the strategy runs and reaches the Retry Order Placement on Exception card, you assume the first attempt to place an order fails.

According to the steps shown in the image, when an error code is detected, the system will keep trying to resubmit the order until it succeeds. Afterward, the Mark as Complete card will be run next.

Custom-coded cards designed to catch error code exceptions will capture any exceptions first. Once an exception is caught by the card, the global exception settings will not be triggered.

This presentation is for informational and educational use only and is not a recommendation or endorsement of any particular investment or investment strategy. Investment information provided in this content is general in nature, strictly for illustrative purposes, and may not be appropriate for all investors.  It is provided without respect to individual investors’ financial sophistication, financial situation, investment objectives, investing time horizon, or risk tolerance. You should consider the appropriateness of this information having regard to your relevant personal circumstances before making any investment decisions. Past investment performance does not indicate or guarantee future success. Returns will vary, and all investments carry risks, including loss of principal. Moomoo makes no representation or warranty as to its adequacy, completeness, accuracy or timeliness for any particular purpose of the above content.