Ejemplo n.º 1
0
        //worst case. buy offer and sell bid
        private void CalcSpreadQuote()
        {
            Quote spreadQuote = new Quote();

            spreadQuote.bid = market.ask(tickers[0]) * multipliers[0] * ratio[0]
                              - market.bid(tickers[1]) * multipliers[1] * ratio[1];
            spreadQuote.ask = market.bid(tickers[0]) * multipliers[0] * ratio[0]
                              - market.ask(tickers[1]) * multipliers[1] * ratio[1];
            spreadQuote.symbol = "spread";

            spreads.Add(spreadQuote);


            if (spreads.Count > pricelength)
            {
                spreads.RemoveAt(0);
            }
        }
Ejemplo n.º 2
0
        private void OnUpdate(object sender, UpdateEventArgs e)
        {
            try
            {
                market.trade(e.update.Symbol, e.update.Bid, e.update.Ask);

                attemptScratchLeg();

                //--------------XXX  make this better---
                //--------------------------------------
                List <SpreadOrder> delorders = new List <SpreadOrder>();



                //check to see if openorders are still attractive
                foreach (SpreadOrder so in orders)
                {
                    double[] wkprices = so.workPrices();

                    if (so.buySell == "Buy")
                    {
                        if (wkprices[0] != market.bid(procSpread.productOne.Product) &&
                            wkprices[1] != market.ask(procSpread.productTwo.Product) &&
                            so.fillPrice() == 0
                            )
                        {
                            if (log.IsDebugEnabled)
                            {
                                log.Debug("buy off market");
                            }
                            string[] sok  = so.getSOKs();
                            Order[]  legs = so.getLegs();

                            int delone = procSpread.Delete(sok[0], procSpread.productOne.ttOrderSet);
                            int deltwo = procSpread.Delete(sok[1], procSpread.productTwo.ttOrderSet);

                            int filledOne = procSpread.productOne.Size - delone;
                            int filledTwo = procSpread.productTwo.Size - deltwo;


                            /*
                             * if we couldnt get the cancel off we need to close it out but ignore
                             * if both legs are complete fills
                             */
                            if (delone != 0 || deltwo != 0)
                            {
                                if (filledOne != 0)
                                {
                                    if (log.IsDebugEnabled)
                                    {
                                        log.Debug("cancel didnt go off completely. selling " + filledOne);
                                    }
                                    procSpread.SendOrder("Sell", market.bid(procSpread.productOne.Product), filledOne, procSpread.productOne);
                                }
                                else
                                {
                                    offOrders[sok[0]] = legs[0];
                                }
                                if (filledTwo != 0)
                                {
                                    if (log.IsDebugEnabled)
                                    {
                                        log.Debug("cancel didnt go off completely. buying " + filledTwo);
                                    }
                                    procSpread.SendOrder("Buy", market.ask(procSpread.productTwo.Product), filledTwo, procSpread.productTwo);
                                }
                                else
                                {
                                    offOrders[sok[1]] = legs[1];
                                }
                            }

                            delorders.Add(so);
                            break;
                        }
                        //XXX shift the spread if necassary.
                        //     else if (so.fillPrice() == 0)
                        //     {
                        //         checkSpreadLevel(so);
                        //     }
                    }
                    else if (so.buySell == "Sell")
                    {
                        if (wkprices[0] != market.ask(procSpread.productOne.Product) &&
                            wkprices[1] != market.bid(procSpread.productTwo.Product) &&
                            so.fillPrice() == 0
                            )
                        {
                            if (log.IsDebugEnabled)
                            {
                                log.Debug("sell off market");
                            }
                            string[] sok  = so.getSOKs();
                            Order[]  legs = so.getLegs();

                            int delone = procSpread.Delete(sok[0], procSpread.productOne.ttOrderSet);
                            int deltwo = procSpread.Delete(sok[1], procSpread.productTwo.ttOrderSet);

                            int filledOne = procSpread.productOne.Size - delone;
                            int filledTwo = procSpread.productTwo.Size - deltwo;


                            //if we couldnt get the cancel off we need to close it out
                            if (delone != 0 || deltwo != 0)
                            {
                                if (filledOne != 0)
                                {
                                    if (log.IsDebugEnabled)
                                    {
                                        log.Debug("cancel didnt go off completely. buying " + filledOne);
                                    }
                                    procSpread.SendOrder("Buy", market.ask(procSpread.productOne.Product), filledOne, procSpread.productOne);
                                }
                                else
                                {
                                    offOrders[sok[0]] = legs[0];
                                }
                                if (filledTwo != 0)
                                {
                                    if (log.IsDebugEnabled)
                                    {
                                        log.Debug("cancel didnt go off completely. selling " + filledTwo);
                                    }
                                    procSpread.SendOrder("Sell", market.bid(procSpread.productTwo.Product), filledTwo, procSpread.productTwo);
                                }
                                else
                                {
                                    offOrders[sok[1]] = legs[1];
                                }
                            }

                            delorders.Add(so);
                            break;
                        }

                        //XXX shift the spread if necassary.
                        //     else if (so.fillPrice() == 0)
                        //     {
                        //          checkSpreadLevel(so);
                        //      }
                    }
                }


                foreach (SpreadOrder dspo in delorders)
                {
                    orders.Remove(dspo);
                }
                //--------------------------------------
                //--------------------------------------


                signal.trader();
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled)
                {
                    log.Error(ex.StackTrace);
                }
            }
        }