Trading System API  3.0
Library for Simulating and Deploying Trading and Investment Strategies
TSAOrder.h
1 /* ===================================================================
2 *
3 * T R A D I N G S Y S T E M A P I ™
4 * Copyright © 1999 - 2014 by Peter Ritter ( TradingSystemAPI.com )
5 * A L L R I G H T S R E S E R V E D
6 *
7 * Consult your license regarding permissions and restrictions.
8 * You may obtain a copy of the License at:
9 * http://www.TradingSystemAPI.com/licenses/LICENSE-TSAPI-3.0.html
10 *
11 * ====================================================================
12 */
13 
14 #ifndef TSA_ORDER__INCLUDED
15 #define TSA_ORDER__INCLUDED
16 
17 #include <ostream>
18 
19 #include "TSAPOS.h"
20 #include "TSATime.h"
21 #include "TSAString.h"
22 #include "TSASupport.h"
23 
24 namespace tsa {
25 
26  class position_manager;
27  class instrument_price_bar;
28  class transaction;
29  class order;
30  class instrument;
31 
36  class order_ref;
37 
38  /*
39  ** ====================================================
40  ** >>> class order <<<
41  ** ====================================================
42  */
43 
49  class dll_export order : public object, public ref_count {
50  tsa_declare_testable
51  friend class order_manager;
52  friend class order_ref;
53  static const std::string class_name;
54  public:
55 
57  enum status {
58 
60  created = 0,
61 
64  pending_submit = 1, /*active*/
65 
68  pre_submitted = 2, /*active*/
69 
71  submitted = 3, /*active*/
72 
75  pending_cancel = 4, /*active*/
76 
78  pending_cancel_replace = 5, /*active*/
79 
81  paused = 6, /*active*/
82 
84  filled = 7,
85 
87  cancelled = 8,
88 
90  rejected = 9
91  };
92 
94  enum action {
95  buy,
96  sell,
97  oa_undefined
98  };
99 
101  enum type {
105  ot_undefined
106  };
107 
122 
126  ei_undefined // internal use.
127  };
128 
134  gtc,
135  day,
136  user_defined_tif
137  };
138 
140  struct order_info {
143  };
144  public:
146  static std::string to_string(order::status);
148  static std::string to_string(order::time_in_force);
150  static std::string to_string(order::action);
152  static std::string to_string(order::type);
154  static std::string to_string(order::exec_interval);
155  protected:
156  order::exec_interval m_exec_interval = order::ei_undefined;
157  order::time_in_force m_time_in_force = order::gtc;
158 
159  instrument* m_instrument_ptr = nullptr;
160  order::status m_status = order::created;
161  order::status m_prev_status = order::created;
164  date_time m_expiry_timepoint;
165  date_time m_creation_timestamp;
166  date_time m_last_fill_timestamp;
167  std::string m_symbol;
168  std::string m_exchange;
169  identifier_t m_order_id = tsa::undefined_id;
170  identifier_t m_external_order_id = tsa::undefined_id;
171  identifier_t m_permanent_order_id = tsa::undefined_id;
172  quantity_t m_quantity = 0;
173  quantity_t m_min_fill_quantity = 0;
174  double m_price = 0.0;
175  double m_aux_price = 0.0;
176  bool m_is_stop_limit = false;
177  double m_limit_fill_proviso = 0.0;
178  bool m_is_marked_for_deletion = false;
179  bool m_is_partially_filled = false;
180  quantity_t m_filled_quantity = 0;
181  variant m_fixed_fill_price;//leave as 'undefined'
182  double m_estimated_transaction_price = (-std::numeric_limits<double>::max());
183  double m_abs_price_diff_between_open_and_estimated_exec_price = (-std::numeric_limits<double>::max());
184  bool m_is_smart = false;
185  variant m_last_fill_price;
186  std::string m_user_message;
187  int64_t m_created_on_bar = 0;
188  private:
189  std::vector<order::order_info>* m_orders_to_cancel_ptr = nullptr;
190  public:
191  virtual void calculate_estimated_transaction_price(const instrument_price_bar& price_bar) = 0;
192  double estimated_transaction_price(void);
193  double abs_price_diff_between_open_and_estimated_exec_price(void) const;
194  bool is_assumed_to_be_executed_before(const order* order_ptr) const;
195  virtual bool TRY_FILL(const instrument_price_bar& price_bar, transaction&);
196  public:
197  virtual bool try_fill__long(const instrument_price_bar& price_bar, double& transact_price, bool& had_slippage) = 0;
198  virtual bool try_fill__short(const instrument_price_bar& price_bar, double& transact_price, bool& had_slippage) = 0;
199  private:
200  order(const order&) = delete;
201  public:
202  order(void);
203  virtual ~order(void);
204  public:
205  void set_instrument_ptr(instrument*);
206  instrument* instrument_ptr(void) const;
207  public:
208  void add_order_to_cancel_on_fill(identifier_t order_id, identifier_t contract_id);
209  bool has_orders_to_cancel_on_fill(void) const;
210  const std::vector<order::order_info>& get_orders_to_cancel_on_fill(void) const;
211  public:
212  static identifier_t instrument_id_from_order_id(identifier_t order_id);
213  public:
214  void set_symbol(const std::string& s);
215  const std::string& get_symbol(void) const;
216  void set_exchange(const std::string& s);
217  const std::string& get_exchange(void) const;
218  identifier_t get_contract_id(void) const;
219  void set_id(identifier_t);
220  identifier_t get_id(void) const;
221  void set_id_on_platform(identifier_t);
222  identifier_t get_id_on_platform(void) const;
223  void set_permanent_id(tsa::identifier_t);
224  tsa::identifier_t get_permanent_id(void) const;
225  void set_quantity(quantity_t);
226  quantity_t get_quantity(void) const;
227  void set_remaining_quantity(quantity_t);
228  quantity_t get_remaining_quantity(void) const;
229  void set_min_fill_quantity(quantity_t);
230  quantity_t get_min_fill_quantity(void) const;
231  void set_tag(const std::string&);
232  const std::string& get_tag(void) const;
233  void set_create_timestamp(const date_time&);
234  const date_time& get_create_timestamp(void) const;
235  void set_create_bar(int64_t);
236  int64_t get_create_bar(void) const;
237  int64_t get_age_in_bars(void) const;
238  duration get_age(void) const;
239  void set_status(order::status);
240  order::status get_status(void) const;
241  order::status get_prev_status(void) const;
242  void set_action(order::action);
243  order::action get_action(void) const;
244  void set_type(order::type);
245  order::type get_type(void) const;
246  void set_exec_interval(order::exec_interval);
247  order::exec_interval get_exec_interval(void) const;
248  order::time_in_force get_time_in_force(void) const;
249  const date_time& get_expiry_time_point(void) const;
250  void set_time_in_force(order::time_in_force);
251  void set_time_in_force_time_point(const date_time&);
252  void set_price(double);
253  double get_price(void) const;
254  void set_aux_price(double);
255  double get_aux_price(void) const;
256  void set_last_fill_price(double);
257  double get_last_fill_price(void);
258  void set_fixed_fill_price(double);
259  double get_fixed_fill_price(void);
260  void set_is_stop_limit(bool);
261  bool get_is_stop_limit(void) const;
262  void set_limit_fill_proviso(double);
263  double get_limit_fill_proviso(void) const;
264  bool get_is_filled(void) const;
265  void set_is_partial_fill(bool);
266  bool get_is_partial_fill(void) const;
267  void set_filled_quantity(quantity_t);
268  quantity_t get_filled_quantity(void) const;
269  void apply_fill(quantity_t quantity, const date_time& timestamp);
270  void set_last_fill_timestamp(const date_time&);
271  const date_time& get_last_fill_timestamp(void) const;
272  void set_is_smart(bool);
273  bool get_is_smart(void) const;
274  void set_is_marked_for_deletion(bool);
275  bool get_is_marked_for_deletion(void) const;
276  bool get_is_eod(void) const;
277  bool get_is_cancelled(void) const;
278  bool get_is_active(void) const;
279  public:
280  static bool order_status_is_active(tsa::order::status);
281  public:
282  virtual void print(std::ostream& stream) const;
283  public:
284  quantity_t calc_rounded_order_quantity_for_ccy_amount(double unit_cost, double amount);
285  quantity_t calculate_initial_order_size(double price);
286  public://*** STOP orders helper function ***
287  static bool try_fill__long_stop
288  (double stop_price, double& transaction_price, double o, double h, double l);
289  static bool try_fill__short_stop
290  (double stop_price, double& transaction_price, double o, double h, double l);
291  public://*** LIMIT orders helper function ***
292  static bool try_fill__long_limit
293  (double limit_price, double& transaction_price, double o, double h, double l);
294  static bool try_fill__short_limit
295  (double limit_price, double& transaction_price, double o, double h, double l);
296  };
297 
298  /*
299  ** ====================================================
300  ** >>> class order_ref <<<
301  ** ====================================================
302  */
303 
422  class dll_export order_ref : public object {
423  tsa_declare_testable;
424  friend class instrument;
425  public:
426  order* order_ptr(void) const;
427  private:
428  order* m_order_ptr;
429  void verify_init(const char* hint) const;
430  size_t ref_count(void) const;
431  void verify_inactive() const;
432  public:
433  //===========================
436  //===========================
437 
439  order_ref(void);
440 
446  order_ref(order* order_ptr);
447 
453  order_ref(const order_ref& other);
454 
463  void operator= (const order_ref& other);
464 
466  ~order_ref(void);
467 
468  public:
469  //===========================
472  //===========================
473 
476  void send();
477 
482  void cancel(void);
483 
489  void update_price(double new_price);
490 
497  void update_price(double new_price, double new_aux_price);
498 
504  void update_quantity(quantity_t new_quantity);
505 
510  void release(void);
511 
517  order* get(void)const;
518 
523  void print(std::ostream& stream) const;
524 
525  //=================
527  //=================
528  public:
529  //===========================
532  //===========================
533 
538  const std::string& symbol(void) const;
539 
544  const std::string& exchange(void) const;
545 
551  identifier_t id(void) const;
552 
557  identifier_t instrument_id(void) const;
558 
566  order_ref tag(const std::string& tag_string);
567 
571  const std::string& tag(void) const;
572  //=================
574  //=================
575 
576  public:
577  //===========================
580  //===========================
581 
591  void time_in_force(order::time_in_force tif, const date_time& expiry_time_point = date_time());
592 
593 
597  order::time_in_force time_in_force(void) const;
598 
604  date_time expiry_time_point(void) const;
605 
606 
611  double price(void) const;
612 
620  void fixed_fill_price(double fixed_fill_price);
621 
626  double fixed_fill_price(void)const;
627 
632  quantity_t quantity(void) const;
633 
638  quantity_t remaining(void) const;
639 
644  quantity_t size(void) const;
645 
650  quantity_t min_fill_quantity(void) const;
651 
658  void min_fill_quantity(quantity_t quantity);
659 
664  order::action action(void) const;
665 
670  order::type type(void) const;
671 
682  order::exec_interval exec_interval(void) const;
683 
687  double aux_price(void) const;
688 
692  bool is_stop_limit(void) const;
693 
699  quantity_t filled_quantity(void) const;
700 
706  date_time last_fill_timestamp(void) const;
707 
711  date_time create_timestamp(void) const;
712 
718  int64_t create_bar_ordinal(void) const;
719 
725  int64_t age_in_bars(void) const;
726 
732  duration age(void) const;
733 
734  //=================
736  //=================
737 
738  public:
739  //===========================
742  //===========================
743 
748  bool defined(void) const;
749 
755  operator bool(void)const;
756 
757  // Same as is_filled().
758  //bool filled(void) const;
759 
764  bool is_filled(void) const;
765 
766  // Same as order_ref::is_active()
767  bool active(void) const;
768 
779  bool is_active(void) const;
780 
781  // obsolete
782  //bool partially_filled(void) const;
783 
788  bool is_partially_filled(void) const;
789 
793  bool cancelled(void) const;
794 
798  bool is_cancelled(void)const;
799 
804  bool marked_for_deletion(void) const;
805 
809  order::status status(void) const;
810 
815  order::status prev_status(void) const;
816 
822  bool is_pending_submit(void);
823 
828  bool is_pre_submitted(void);
829 
834  bool is_acknowledged(void);
835 
840  bool is_submitted(void);
841 
847  bool is_pending_cancel(void);
848 
853  bool is_cancelled(void);
854 
855  //=================
857  //=================
858 
859  public:
860  // internal use
861  void mark_for_deletion(void);
862 
863  //===========================
866  //===========================
867 
868  // internal
869  void update_status(order::status);
870 
871  // internal
872  void external_id(identifier_t);
873 
878  identifier_t external_id(void)const;
879 
884  identifier_t permanent_id(void)const;
885 
886  //=================
888  //=================
889 
890  public:
891  //===========================
894  //===========================
895 
899  void one_cancels_other(order_ref& other_order);
900 
904  void oco(order_ref& other);
905 
911  void on_fill_cancel(const order_ref& other_order);
912 
919  void on_fill_cancel(identifier_t order_id, identifier_t instrument_id);
920 
924  bool has_orders_to_cancel_on_fill(void) const;
925 
929  const std::vector<order::order_info>& orders_to_cancel(void) const;
930 
931  //=================
933  //=================
934 
935  public:
936  //===========================
939  //===========================
940 
944  void exec_interval(order::exec_interval execution_interval);
945 
946  //=================
948  //=================
949  };
950 
952  bool operator==(const order_ref& lhs, const order_ref& rhs);
953 
955  bool operator!=(const order_ref& lhs, const order_ref& rhs);
956 
957  /*
958  ** ====================================================
959  ** >>> internal <<<
960  ** ====================================================
961  */
962 
963  // internal use
964  class dll_export order__on_stop : public order {
965  public:
966  order__on_stop(order::action, double price);
967  order__on_stop(order::action, double price, double auxPrice);
968  virtual ~order__on_stop();
969  virtual bool try_fill__long(const instrument_price_bar& price_bar,
970  double& transaction_price, bool& incurred_slippage)override;
971  virtual bool try_fill__short(const instrument_price_bar& price_bar,
972  double& transaction_price, bool& incurred_slippage)override;
973  // for calculating order fill precedence in simulations
974  virtual void calculate_estimated_transaction_price(const instrument_price_bar& price_bar)override;
975  };
976 
977  // internal use
978  class dll_export order__at_limit : public order {
979  public:
980  order__at_limit(order::action, double price);
981  virtual ~order__at_limit(void);
982  virtual bool try_fill__long(const instrument_price_bar& price_bar,
983  double& transaction_price, bool& incurred_slippage)override;
984  virtual bool try_fill__short(const instrument_price_bar& price_bar,
985  double& transaction_price, bool& incurred_slippage)override;
986  virtual void calculate_estimated_transaction_price(const instrument_price_bar& price_bar)override;
987  };
988 
989  // internal use
990  class dll_export order__at_market : public order {
991  public:
992  order__at_market(order::action);
993  virtual ~order__at_market(void);
994  virtual bool try_fill__long(const instrument_price_bar& price_bar,
995  double& transaction_price, bool& incurred_slippage)override;
996  virtual bool try_fill__short(const instrument_price_bar& price_bar,
997  double& transaction_price, bool& incurred_slippage)override;
998  virtual void calculate_estimated_transaction_price(const instrument_price_bar& price_bar)override;
999  };
1000 
1002 
1003 } // namespace tsa
1004 
1005 #endif
type
Order type.
Definition: TSAOrder.h:101
double max(const series< double > &series, size_t period)
Returns the highest value found in series over given .
Definition: TSAFunctionsScalar.cpp:180
Expires at the end of day/session.
Definition: TSAOrder.h:135
At the &#39;close&#39; of the next bar.
Definition: TSAOrder.h:120
internal use
Definition: TSAOrder.h:105
identifier_t instrument_id
The instrument ID.
Definition: TSAOrder.h:142
internal use
Definition: TSAOrder.h:97
Class order is the library&#39;s internal order representation. Users are not intended to interact with t...
Definition: TSAOrder.h:49
Namespace for the &#39;Trading System API&#39; library.
Definition: original1.TSA3Core.cpp:20
status
Order state.
Definition: TSAOrder.h:57
Definition: TSAOrder.h:125
Good Till Cancel (default).
Definition: TSAOrder.h:134
int64_t quantity_t
Definition: TSATypeDef.h:444
variant objects can represent values of different types.
Definition: TSAVariant.h:140
Represents a reference to an internally managed order object. Working with class order directly is no...
Definition: TSAOrder.h:422
Represents a duration - the difference between two date_time values.
Definition: TSATime.h:945
Parent class for many library classes.
Definition: TSATypeDef.h:462
Represents a transaction that occurred on an exchange or as part of a simulation when an order is fil...
Definition: TSATransaction.h:40
sref< bool > operator==(numeric_series_cref a, numeric_series_cref b)
comparison.
Definition: TSADSLBase.cpp:130
Information about an order.
Definition: TSAOrder.h:140
action
Order action.
Definition: TSAOrder.h:94
The order is a &#39;buy&#39; order.
Definition: TSAOrder.h:95
time_in_force
Time-in-force (TIF). By default all orders are placed as good-till-cancel (GTC) orders. See instrument::default_time_in_force(order::time_in_force).
Definition: TSAOrder.h:133
&#39;Limit&#39; order.
Definition: TSAOrder.h:104
identifier_t order_id
The order ID.
Definition: TSAOrder.h:141
The order is a &#39;sell&#39; order.
Definition: TSAOrder.h:96
Class representing a gregorian-date and time-of-day combination. The time component has microsecond r...
Definition: TSATime.h:428
int64_t identifier_t
type for ID&#39;s
Definition: TSATypeDef.h:117
&#39;Stop&#39; order (includes &#39;stop limit&#39; orders).
Definition: TSAOrder.h:103
The order has been created but has not been transmitted, and can still be modified.
Definition: TSAOrder.h:60
During the entire next &#39;bar&#39;.
Definition: TSAOrder.h:121
exec_interval
The order &#39;execution interval&#39; determines when an order will be executed. Note: By default market ord...
Definition: TSAOrder.h:118
Class instrument represents a tradeable security such as a stock, option, futures contract...
Definition: TSAInstrument.h:64
&#39;Market&#39; order.
Definition: TSAOrder.h:102
At the &#39;open&#39; of next bar.
Definition: TSAOrder.h:119