00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00023 #pragma once
00024
00025 #include "RTOrder.h"
00026 #include "RTExchangeType.h"
00027
00028 namespace RT
00029 {
00030 enum PositionType
00031 {
00032 enShort,
00033 enLong
00034 };
00035
00036 static char* PositionTypeStr[] =
00037 {
00038 "Short",
00039 "Long"
00040 };
00041
00042
00049 class RTPosition
00050 {
00051 public:
00052
00053 RTPosition():
00054 symbol_("")
00055 ,qty_(0)
00056 ,type_(enShort)
00057 ,average_price_(0)
00058 ,realized_pnl_(0)
00059 ,old_average_price (0)
00060 ,old_qty (0)
00061 {
00062 }
00063 RTPosition(RTOrder *order):
00064 symbol_(order->sec_id())
00065 ,qty_(order->original_qty())
00066 ,average_price_(0)
00067 ,realized_pnl_(0)
00068 {
00069 if(order->side() == OS_Buy)
00070 type_ = enLong;
00071 else
00072 type_ = enShort;
00073 }
00074 RTPosition(const RTExecutionReport * report):
00075 symbol_("")
00076 ,qty_(0)
00077 ,type_(enShort)
00078 ,average_price_(0)
00079 ,realized_pnl_(0)
00080 {
00081 symbol_ =report->symbol ;
00082 qty_ = 0;
00083 type_ = enShort;
00084
00085 update(report->side , 0, 0);
00086 }
00087 RTPosition(const std::string& symbol, int qty,int type):
00088 symbol_(symbol)
00089 ,type_(enShort)
00090 ,average_price_(0)
00091 ,qty_(qty)
00092 ,realized_pnl_(0)
00093 {
00094 if(type > 1 || type < 0)
00095 type_ =(PositionType) 0;
00096 else
00097 type_ =(PositionType) type;
00098 }
00099
00101 void update (OrderSide side, int qty, double last_px)
00102 {
00103 bool old_long_or_short = (qty_ > 0) ? true : false;
00104 double realized_pnl_incremental = 0;
00105
00106 old_qty = qty_;
00107 old_average_price = average_price_;
00108
00109 if( side == OS_Buy)
00110 update(qty);
00111 else
00112 update(-qty);
00113
00114 bool new_long_or_short = (qty_ > 0) ? true : false;
00115
00116 if (qty_ == 0)
00117 {
00118 realized_pnl_incremental = old_qty * (last_px - old_average_price);
00119 average_price_ = 0;
00120 }
00121 else
00122 if (old_long_or_short != new_long_or_short)
00123 {
00124 if( old_qty > 0)
00125 realized_pnl_incremental = (qty - old_qty) * (last_px - old_average_price);
00126 average_price_ = last_px;
00127 }
00128 else if ( (old_long_or_short == true && side == OS_Buy)
00129 || (old_long_or_short == false && side == OS_Sell))
00130 {
00131 if( (side == OS_Buy && type_== enLong) ||
00132 (side == OS_Sell && type_== enShort))
00133 {
00134 average_price_ = (double)( average_price_*abs(old_qty)+ last_px*abs(qty) )/(double)abs(qty_);
00135 }
00136 else
00137 {
00138 average_price_ = (double)( average_price_*abs(old_qty)- last_px*abs(qty) )/(double)abs(qty_);
00139 }
00140 }
00141 else if (old_long_or_short == new_long_or_short)
00142 {
00143 if (old_average_price!=0 &&last_px !=0 )
00144 {
00145 if (side == OS_Buy)
00146 realized_pnl_incremental = qty * (old_average_price - last_px);
00147 else
00148 realized_pnl_incremental = qty * (last_px -old_average_price);
00149 }
00150 else {
00151
00152 }
00153 }
00154
00155 realized_pnl_ +=realized_pnl_incremental;
00156 }
00158 void update (int qty)
00159 {
00160 qty_ += qty;
00161
00162 if(qty_>0)
00163 type_= enLong;
00164 else
00165 type_= enShort;
00166 }
00167
00169 void update (const RTExecutionReport * report, int denominator = 1)
00170 {
00171 if(denominator ==0)
00172 denominator = 1;
00173
00174 switch(report->ord_status)
00175 {
00176 case os_partially_filled :
00177 case os_filled :
00178 update(report->side, report->last_shares, (double)report->last_px / (double)denominator);
00179 break;
00180 default :
00181
00182 break;
00183 }
00184 }
00185 ~RTPosition(){}
00186
00188 const std::string& symbol() const{return symbol_;}
00190 void symbol(const std::string& symbol){symbol_ = symbol;}
00191
00193 const int& qty() const{return qty_;}
00195 double average_price(){ return average_price_;}
00197 double realized_pnl(){return realized_pnl_;}
00198
00199
00201 const int type() {return (int)type_;}
00203 void to_xml(std::ostream& stream)
00204 {
00205 stream<<"<Position>";
00206 stream<<"<Symbol>"<<symbol_.c_str()<<"</Symbol>";
00207 stream<<"<PositionType>"<<PositionTypeStr[(int)type_]<<"</PositionType>";
00208 stream<<"<Quantity>"<<qty_<<"</Quantity>";
00209 stream<<"<Value></Value>";
00210 stream<<"<Profit_Loss></Profit_Loss>";
00211 stream<<"</Position>";
00212 }
00213
00214 private:
00215 void realized_pnl(const double realized_pnl) {realized_pnl_=realized_pnl;}
00216 void type (const int type)
00217 {
00218 if(type > 1 || type < 0)
00219 type_ =(PositionType) 0;
00220 else
00221 type_ =(PositionType) type;
00222 }
00223
00225 void qty(const int qty){qty_ = qty;}
00226
00227 std::string symbol_;
00228 int qty_;
00229 double average_price_;
00230 PositionType type_;
00231 double realized_pnl_;
00232
00233 double old_average_price ;
00234 int old_qty;
00235
00236 };
00238 typedef std::list<RTPosition> PositionList;
00239 }