Esempio n. 1
0
        /// <summary>
        /// this must be called once per position tracker, for each position update.
        /// if you are using your own position tracker with this trailing stop(eg from offset tracker, or somewhere else)
        /// you MUST call TrailTrackers Adjust and NOT call your position tracker's adjust
        /// </summary>
        /// <param name="fill"></param>
        public void Adjust(Trade fill)
        {
            // get index for symbol
            int idx = symidx(fill.FullSymbol);

            // only do following if we're tracking trail for this symbol
            if (idx != NOSYM)
            {
                // get current position size
                int psize = _pt[fill.FullSymbol].UnsignedSize;
                // get trailing information
                OffsetInfo trail = _trail[idx];
                // get actual position size after change
                int asize = psize - fill.UnsignedSize;
                // if expected and actual match, mark pending as false
                if (esize[fill.FullSymbol] == asize)
                {
                    D(fill.FullSymbol + " trailing stop completely filled with: " + fill.ToString());
                    _pendingfill[idx] = false;
                }
                else
                    v(fill.FullSymbol + " trail partial fill: " + fill.ToString() + " e: " + esize[fill.FullSymbol] + " != a: " + asize);

            }
            else
                v(fill.FullSymbol + " fill: " + fill.ToString() + " ignored while trail disabled.");

            _pt.Adjust(fill);
            // if we're flat now, make sure ref price is reset
            if (_pt[fill.FullSymbol].isFlat)
            {
                _ref[idx] = 0;
                v(fill.FullSymbol + " flat, reset trail reference price.");
            }
        }
Esempio n. 2
0
        public void GotFill(Trade f)
        {
            if (f.Id == 0)
            {
                Debug(" fill id is empty: " + f.ToString());
                return;
            }

            if (!IsTracked(f.Id))
            {
                Debug(" fill id not tracked: " + f.Id);
                return;
            }

            // add to fills
            _fills[f.Id] += f.TradeSize;

            Debug(f.FullSymbol + " filled size: " + _fills[f.Id] + " ; on trade detail: " + f.ToString());
        }
Esempio n. 3
0
 // these are for calculating closed pl
 // they do not adjust positions themselves
 /// <summary>
 /// Gets the closed PL on a per-share basis, ignoring how many shares are held.
 /// </summary>
 /// <param name="existing">The existing position.</param>
 /// <param name="closing">The portion of the position that's being closed/changed.</param>
 /// <returns></returns>
 public static decimal ClosePT(Position existing, Trade adjust)
 {
     if (!existing.isValid || !adjust.IsValid)
     {
         throw new Exception("Invalid position provided. (existing:" + existing.ToString() + " adjustment:" + adjust.ToString());
     }
     if (existing.isFlat)
     {
         return(0);                 // nothing to close
     }
     if (existing.isLong == adjust.Side)
     {
         return(0);                                // if we're adding, nothing to close
     }
     return(existing.isLong ? adjust.TradePrice - existing.AvgPrice : existing.AvgPrice - adjust.TradePrice);
 }
Esempio n. 4
0
        public void GotFill(Trade f)
        {
            if (f.Id == 0)
            {
                Debug(" fill id is empty: " + f.ToString());
                return;
            }

            if (!IsTracked(f.Id))
            {
                Debug(" fill id not tracked: " + f.Id);
                return;
            }

            // add to fills
            _fills[f.Id] += f.TradeSize;
            
            Debug(f.FullSymbol + " filled size: " + _fills[f.Id] + " ; on trade detail: " + f.ToString());
        }
Esempio n. 5
0
 // these are for calculating closed pl
 // they do not adjust positions themselves
 /// <summary>
 /// Gets the closed PL on a per-share basis, ignoring how many shares are held.
 /// </summary>
 /// <param name="existing">The existing position.</param>
 /// <param name="closing">The portion of the position that's being closed/changed.</param>
 /// <returns></returns>
 public static decimal ClosePT(Position existing, Trade adjust)
 {
     if (!existing.isValid || !adjust.IsValid)
         throw new Exception("Invalid position provided. (existing:" + existing.ToString() + " adjustment:" + adjust.ToString());
     if (existing.isFlat) return 0; // nothing to close
     if (existing.isLong == adjust.Side) return 0; // if we're adding, nothing to close
     return existing.isLong ? adjust.TradePrice - existing.AvgPrice : existing.AvgPrice - adjust.TradePrice;
 }