|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import common.config |
| 5 | +import common.view |
| 6 | +import common.args |
| 7 | +from order.view import print_order_create_response_transactions |
| 8 | + |
| 9 | + |
| 10 | +def main(): |
| 11 | + """ |
| 12 | + Close an open Trade in an Account |
| 13 | + """ |
| 14 | + |
| 15 | + parser = argparse.ArgumentParser() |
| 16 | + |
| 17 | + # |
| 18 | + # Add the command line argument to parse to the v20 config |
| 19 | + # |
| 20 | + common.config.add_argument(parser) |
| 21 | + |
| 22 | + parser.add_argument( |
| 23 | + "instrument", |
| 24 | + type=common.args.instrument, |
| 25 | + help=( |
| 26 | + "The Instrument of the Position to close. If prepended " |
| 27 | + "with an '@', this will be interpreted as a client Trade ID" |
| 28 | + ) |
| 29 | + ) |
| 30 | + |
| 31 | + parser.add_argument( |
| 32 | + "--long-units", |
| 33 | + default=None, |
| 34 | + help=( |
| 35 | + "The amount of the long Position to close. Either the string 'ALL' " |
| 36 | + "indicating a full Position close, the string 'NONE', or the " |
| 37 | + "number of units of the Position to close" |
| 38 | + ) |
| 39 | + ) |
| 40 | + |
| 41 | + parser.add_argument( |
| 42 | + "--short-units", |
| 43 | + default=None, |
| 44 | + help=( |
| 45 | + "The amount of the short Position to close. Either the string " |
| 46 | + "'ALL' indicating a full Position close, the string 'NONE', or the " |
| 47 | + "number of units of the Position to close" |
| 48 | + ) |
| 49 | + ) |
| 50 | + |
| 51 | + args = parser.parse_args() |
| 52 | + |
| 53 | + account_id = args.config.active_account |
| 54 | + |
| 55 | + # |
| 56 | + # Create the api context based on the contents of the |
| 57 | + # v20 config file |
| 58 | + # |
| 59 | + api = args.config.create_context() |
| 60 | + |
| 61 | + response = api.position.close( |
| 62 | + account_id, |
| 63 | + args.instrument, |
| 64 | + longUnits=args.long_units, |
| 65 | + shortUnits=args.short_units |
| 66 | + ) |
| 67 | + |
| 68 | + print( |
| 69 | + "Response: {} ({})\n".format( |
| 70 | + response.status, |
| 71 | + response.reason |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + print_order_create_response_transactions(response) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == "__main__": |
| 79 | + main() |
0 commit comments