Example #1
0
        private void OnSTIOrderUpdateXML(ref string strOrder)
        {
            if (testAlgo.status == "Stopped")
            {
                return;
            }

            XmlSerializer xs = new XmlSerializer(typeof(SterlingLib.structSTIOrderUpdate));

            SterlingLib.structSTIOrderUpdate structOrder = (SterlingLib.structSTIOrderUpdate)xs.Deserialize(new StringReader(strOrder));

            if (structOrder.bstrSymbol.ToUpper() == testAlgo.symbol.ToUpper() && structOrder.nOrderStatus == 5)
            {                                                                     //Limit Order has been filled
                if (Convert.ToDecimal(structOrder.fLmtPrice) < testAlgo.midPrice) //Buy order has been filled
                {
                    testAlgo.buyFills++;

                    //Calculate priceMovePL

                    if (testAlgo.currentPosition > 0)
                    {
                        testAlgo.priceMovePL -= testAlgo.currentPosition * testAlgo.incrementPrice;
                    }
                    else if (testAlgo.currentPosition < 0)
                    {
                        testAlgo.priceMovePL -= (testAlgo.currentPosition + testAlgo.incrementSize) * testAlgo.incrementPrice;
                    }



                    //Shift orders
                    //Normal behaviour, if lists are full
                    if (testAlgo.ordersAbove.Count == 5 && testAlgo.ordersBelow.Count == 5)
                    {
                        testAlgo.midPrice = Convert.ToDecimal(structOrder.fLmtPrice);

                        //UPDATE METRICS
                        if (testAlgo.currentPosition < 0) // If short, Add to incrementPL
                        {
                            testAlgo.incrementPL += testAlgo.incrementPrice * testAlgo.incrementSize;
                        }

                        //Update TotalPL

                        testAlgo.totalPL = testAlgo.incrementPL + testAlgo.priceMovePL;

                        //Update position

                        testAlgo.currentPosition += structOrder.nQuantity;

                        //Hard Stop Check

                        if (-(testAlgo.totalPL) >= testAlgo.hardStop)
                        {
                            testAlgo.stopAndCross();
                            updateAlgoStatusDB("Stopped");
                            return;
                        }


                        //Move filled order to completed list, remove from active list
                        SterlingLib.ISTIOrder filledOrder = testAlgo.ordersAbove.Find(i => i.ClOrderID == structOrder.bstrClOrderId);
                        testAlgo.ordersBelow.RemoveAt(0);
                        testAlgo.filledOrders.Add(filledOrder);

                        //Insert new order below 5 orders away

                        SterlingLib.STIOrder stiOrder = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = testAlgo.symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "B";
                        stiOrder.Quantity    = testAlgo.incrementSize;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(testAlgo.midPrice - (testAlgo.incrementPrice * 5));
                        stiOrder.Destination = "BATS";
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                        int orderStatus = stiOrder.SubmitOrder();
                        if (orderStatus != 0)
                        {
                            MessageBox.Show("Order Error: " + orderStatus.ToString());
                        }
                        else
                        {
                            //Add to appropriate list
                            testAlgo.ordersBelow.Add(stiOrder);
                        }

                        //Cancel the "6th" order from opposite side order list

                        SterlingLib.ISTIOrder cancelOrder = testAlgo.ordersAbove[testAlgo.ordersAbove.Count - 1];
                        orderMaint.CancelOrder(Globals.account, 0, cancelOrder.ClOrderID, Guid.NewGuid().ToString());
                        testAlgo.ordersAbove.RemoveAt(testAlgo.ordersAbove.Count - 1);

                        //Place new order in opposite side order list

                        int orderQuantity = testAlgo.incrementSize;

                        //Autobalance check

                        //If over long

                        if (testAlgo.currentPosition >= testAlgo.autoBalance)
                        {
                            orderQuantity = testAlgo.incrementSize * 2;
                        }

                        stiOrder             = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = testAlgo.symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "S";
                        stiOrder.Quantity    = orderQuantity;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(testAlgo.midPrice + (testAlgo.incrementPrice));
                        stiOrder.Destination = "BATS";
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                        orderStatus = stiOrder.SubmitOrder();
                        if (orderStatus != 0)
                        {
                            MessageBox.Show("Order Error: " + orderStatus.ToString());
                        }
                        else
                        {
                            //Add to appropriate list
                            testAlgo.ordersAbove.Insert(0, stiOrder);
                        }

                        //If over autobalance, check orders above and adjust sizes
                        if (testAlgo.currentPosition >= testAlgo.autoBalance)
                        {
                            int ifFilledPosition = testAlgo.currentPosition - testAlgo.ordersAbove[0].Quantity;
                            for (int i = 1; i < testAlgo.bracketedOrders; i++)
                            {
                                //Calculation
                                if (ifFilledPosition < testAlgo.autoBalance)
                                {
                                    if (testAlgo.ordersAbove[i].Quantity == testAlgo.incrementSize * 2)  // Order should be reverted to regular increment size
                                    {
                                        //First cancel order
                                        orderMaint.CancelOrder(Globals.account, 0, testAlgo.ordersAbove[i].ClOrderID, Guid.NewGuid().ToString());
                                        //Then resubmit with base increment size
                                        stiOrder             = new SterlingLib.STIOrder();
                                        stiOrder.Symbol      = testAlgo.symbol;
                                        stiOrder.Account     = Globals.account;
                                        stiOrder.Side        = "S";
                                        stiOrder.Quantity    = testAlgo.incrementSize;
                                        stiOrder.Tif         = "D"; //day order
                                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                                        stiOrder.LmtPrice    = testAlgo.ordersAbove[i].LmtPrice;
                                        stiOrder.Destination = "BATS";
                                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                                        //Remove cancelled order
                                        testAlgo.ordersAbove.RemoveAt(i);

                                        //Submit new order and add to list
                                        orderStatus = stiOrder.SubmitOrder();
                                        if (orderStatus != 0)
                                        {
                                            MessageBox.Show("Order Error: " + orderStatus.ToString());
                                        }
                                        else
                                        {
                                            //Add to appropriate list
                                            testAlgo.ordersAbove.Insert(i, stiOrder);
                                        }
                                        break;  //Should only be one order to adjust
                                    }
                                }
                                ifFilledPosition -= testAlgo.ordersAbove[i].Quantity;
                            }
                        }
                    }
                    foreach (SterlingLib.ISTIOrder order in testAlgo.ordersAbove)
                    {
                        Debug.WriteLine(order.LmtPrice);
                    }
                    foreach (SterlingLib.ISTIOrder order in testAlgo.ordersBelow)
                    {
                        Debug.WriteLine(order.LmtPrice);
                    }
                    Debug.WriteLine("Buy Fills: " + testAlgo.buyFills);
                    Debug.WriteLine("Sell Fills: " + testAlgo.sellFills);
                    Debug.WriteLine("Current Position: " + testAlgo.currentPosition);
                    Debug.WriteLine("Increment PL: " + testAlgo.incrementPL);
                    Debug.WriteLine("Price Move PL: " + testAlgo.priceMovePL);
                    Debug.WriteLine("Total PL: " + testAlgo.totalPL);
                    Debug.WriteLine("--------------");
                    Debug.WriteLine("--------------");
                }
                else if (Convert.ToDecimal(structOrder.fLmtPrice) > testAlgo.midPrice)
                {
                    testAlgo.sellFills++;

                    //Calculate priceMovePL

                    if (testAlgo.currentPosition < 0)
                    {
                        testAlgo.priceMovePL += testAlgo.currentPosition * testAlgo.incrementPrice;
                    }
                    else if (testAlgo.currentPosition > 0)
                    {
                        testAlgo.priceMovePL += (testAlgo.currentPosition - testAlgo.incrementSize) * testAlgo.incrementPrice;
                    }


                    //Shift orders
                    //Normal behaviour, if lists are full
                    if (testAlgo.ordersAbove.Count == 5 && testAlgo.ordersBelow.Count == 5)
                    {
                        testAlgo.midPrice = Convert.ToDecimal(structOrder.fLmtPrice);

                        //UPDATE METRICS
                        if (testAlgo.currentPosition > 0) // If long, Add to incrementPL
                        {
                            testAlgo.incrementPL += testAlgo.incrementPrice * testAlgo.incrementSize;
                        }
                        testAlgo.currentPosition -= structOrder.nQuantity; //Update position

                        //Update TotalPL

                        testAlgo.totalPL = testAlgo.incrementPL + testAlgo.priceMovePL;


                        //Hard Stop Check

                        if (-(testAlgo.totalPL) >= testAlgo.hardStop)
                        {
                            testAlgo.stopAndCross();
                            updateAlgoStatusDB("Stopped");
                            return;
                        }

                        //Move filled order to completed list, remove from active list
                        SterlingLib.ISTIOrder filledOrder = testAlgo.ordersAbove.Find(i => i.ClOrderID == structOrder.bstrClOrderId);
                        testAlgo.ordersAbove.RemoveAt(0);
                        testAlgo.filledOrders.Add(filledOrder);

                        //Insert new order above 5 orders away

                        SterlingLib.STIOrder stiOrder = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = testAlgo.symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "S";
                        stiOrder.Quantity    = testAlgo.incrementSize;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(testAlgo.midPrice + (testAlgo.incrementPrice * 5));
                        stiOrder.Destination = "BATS";
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                        int orderStatus = stiOrder.SubmitOrder();
                        if (orderStatus != 0)
                        {
                            MessageBox.Show("Order Error: " + orderStatus.ToString());
                        }
                        else
                        {
                            //Add to appropriate list
                            testAlgo.ordersAbove.Add(stiOrder);
                        }

                        //Cancel the "6th" order from opposite side order list

                        SterlingLib.ISTIOrder cancelOrder = testAlgo.ordersBelow[testAlgo.ordersBelow.Count - 1];
                        orderMaint.CancelOrder(Globals.account, 0, cancelOrder.ClOrderID, Guid.NewGuid().ToString());
                        testAlgo.ordersBelow.RemoveAt(testAlgo.ordersBelow.Count - 1);

                        //Place new order in opposite side order list

                        //Autobalance check

                        int orderQuantity = testAlgo.incrementSize;

                        //If over short

                        if (testAlgo.currentPosition <= -(testAlgo.autoBalance))
                        {
                            orderQuantity = testAlgo.incrementSize * 2;
                        }


                        stiOrder             = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = testAlgo.symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "B";
                        stiOrder.Quantity    = orderQuantity;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(testAlgo.midPrice - (testAlgo.incrementPrice));
                        stiOrder.Destination = "BATS";
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                        orderStatus = stiOrder.SubmitOrder();
                        if (orderStatus != 0)
                        {
                            MessageBox.Show("Order Error: " + orderStatus.ToString());
                        }
                        else
                        {
                            //Add to appropriate list
                            testAlgo.ordersBelow.Insert(0, stiOrder);
                        }

                        //If over autobalance, check orders above and adjust sizes
                        if (testAlgo.currentPosition <= -(testAlgo.autoBalance))
                        {
                            int ifFilledPosition = testAlgo.currentPosition + testAlgo.ordersBelow[0].Quantity;

                            for (int i = 1; i < testAlgo.bracketedOrders; i++)
                            {
                                //Calculation
                                if (ifFilledPosition > -(testAlgo.autoBalance))
                                {
                                    if (testAlgo.ordersBelow[i].Quantity == testAlgo.incrementSize * 2)  // Order should be reverted to regular increment size
                                    {
                                        //First cancel order
                                        orderMaint.CancelOrder(Globals.account, 0, testAlgo.ordersBelow[i].ClOrderID, Guid.NewGuid().ToString());
                                        //Then resubmit with base increment size
                                        stiOrder             = new SterlingLib.STIOrder();
                                        stiOrder.Symbol      = testAlgo.symbol;
                                        stiOrder.Account     = Globals.account;
                                        stiOrder.Side        = "B";
                                        stiOrder.Quantity    = testAlgo.incrementSize;
                                        stiOrder.Tif         = "D"; //day order
                                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                                        stiOrder.LmtPrice    = testAlgo.ordersBelow[i].LmtPrice;
                                        stiOrder.Destination = "BATS";
                                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                                        //Remove cancelled order
                                        testAlgo.ordersBelow.RemoveAt(i);

                                        //Submit new order and add to list
                                        orderStatus = stiOrder.SubmitOrder();
                                        if (orderStatus != 0)
                                        {
                                            MessageBox.Show("Order Error: " + orderStatus.ToString());
                                        }
                                        else
                                        {
                                            //Add to appropriate list
                                            testAlgo.ordersBelow.Insert(i, stiOrder);
                                        }
                                        break;  //Should only be one order to adjust
                                    }
                                }
                                ifFilledPosition += testAlgo.ordersBelow[i].Quantity;
                            }
                        }
                    }
                    Debug.WriteLine("Mid Price: " + testAlgo.midPrice);
                    Debug.WriteLine("--------------");
                    foreach (SterlingLib.ISTIOrder order in testAlgo.ordersAbove)
                    {
                        Debug.WriteLine(order.LmtPrice);
                    }
                    foreach (SterlingLib.ISTIOrder order in testAlgo.ordersBelow)
                    {
                        Debug.WriteLine(order.LmtPrice);
                    }
                    Debug.WriteLine("Buy Fills: " + testAlgo.buyFills);
                    Debug.WriteLine("Sell Fills: " + testAlgo.sellFills);
                    Debug.WriteLine("Current Position: " + testAlgo.currentPosition);
                    Debug.WriteLine("Increment PL: " + testAlgo.incrementPL);
                    Debug.WriteLine("Price Move PL: " + testAlgo.priceMovePL);
                    Debug.WriteLine("Total PL: " + testAlgo.totalPL);
                    Debug.WriteLine("--------------");
                    Debug.WriteLine("--------------");
                }
            }
        }
Example #2
0
        private void stiEvents_OnSTIOrderUpdateXML(ref string orderUpdateInfo)
        {
            XmlSerializer xs = new XmlSerializer(typeof(SterlingLib.structSTIOrderUpdate));

            SterlingLib.structSTIOrderUpdate structTrade = (SterlingLib.structSTIOrderUpdate)xs.Deserialize(new StringReader(orderUpdateInfo));
            //MessageBox.Show(structTrade.nOrderStatus.ToString());

            //Find associated order level, trigger new order



            SterlingAlgos.OrderLevel orderLevel = orderLevels.Where(i => i.sittingOrder.ClOrderID == structTrade.bstrClOrderId).FirstOrDefault();

            //MessageBox.Show(orderLevel.lastFilledOrder.fAvgExecPrice.ToString());

            Console.WriteLine("TU" + " : " + orderLevel.startPrice + " : " + orderLevel.sittingOrder.ClOrderID);

            if (orderLevel != null)
            {  //Check if orderLevel actually found
                if (orderLevel.isStop)
                {
                    if (structTrade.nOrderStatus == 5)
                    {
                        stop();
                    }
                }
                else if (structTrade.nOrderStatus == 5)                                                                                        //Check for complete fill
                {
                    bool isRestore = (direction == orderLevel.sittingOrder.Side) || (direction == "S" && orderLevel.sittingOrder.Side == "T"); //If true, size has been restored, so do TP order. If false, TP order is hit, calculate profit and do restore size order

                    if (direction == "B" && isRestore)                                                                                         // Restore Size has just occurred
                    {
                        orderLevel.totalFills += 1;
                        orderLevel.completedOrders.Add(orderLevel.sittingOrder);
                        orderLevel.lastFilledOrder = structTrade;

                        //Create take profit order
                        SterlingLib.STIOrder stiOrder = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "S";
                        stiOrder.Quantity    = incrementSize;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(orderLevel.startPrice + profitOffset);
                        stiOrder.Destination = Globals.desination;
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();
                        if (!isStopped)
                        {
                            //Submit order
                            int ord = stiOrder.SubmitOrder();
                            if (ord != 0)
                            {
                                MessageBox.Show("Order Error: " + ord.ToString());
                            }
                            else //No error, new sitting order is the new "buy" order (restore size order)
                            {
                                orderLevel.sittingOrder = stiOrder;
                                orderLevel.isRestore    = false;
                            }
                        }
                        else
                        {
                            orderLevel.sittingOrder = stiOrder;
                            orderLevel.isRestore    = false;
                        }
                    }
                    if (direction == "B" && !isRestore) //Take Profit has just occurred
                    {
                        //orderLevel.PL += Convert.ToDecimal(structTrade.fExecPrice - orderLevel.lastFilledOrder.fExecPrice);
                        orderLevel.totalFills += 1;
                        orderLevel.completedOrders.Add(orderLevel.sittingOrder);
                        orderLevel.lastFilledOrder = structTrade;



                        //Create restore size order
                        SterlingLib.STIOrder stiOrder = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "B";
                        stiOrder.Quantity    = incrementSize;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(orderLevel.startPrice);
                        stiOrder.Destination = Globals.desination;
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                        if (!isStopped)
                        {
                            //Submit order
                            if (restoreSize)
                            {
                                int ord = stiOrder.SubmitOrder();
                                if (ord != 0)
                                {
                                    MessageBox.Show("Order Error: " + ord.ToString());
                                }
                                else //No error, new sitting order is the new "buy" order (restore size order)
                                {
                                    orderLevel.sittingOrder = stiOrder;
                                }
                            }
                            else
                            {
                                orderLevel.sittingOrder = stiOrder; //Order will sit there but not execute is no restore size option
                                orderLevel.isRestore    = true;
                            }
                        }

                        else
                        {
                            orderLevel.sittingOrder = stiOrder;
                        }
                    }


                    if (direction == "S" && isRestore) // Restore Size has just occurred
                    {
                        orderLevel.totalFills += 1;
                        orderLevel.completedOrders.Add(orderLevel.sittingOrder);
                        orderLevel.lastFilledOrder = structTrade;

                        //Create take profit order
                        SterlingLib.STIOrder stiOrder = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "B";
                        stiOrder.Quantity    = incrementSize;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(orderLevel.startPrice - profitOffset);
                        stiOrder.Destination = Globals.desination;
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();

                        if (!isStopped)
                        {
                            //Submit order
                            int ord = stiOrder.SubmitOrder();
                            if (ord != 0)
                            {
                                MessageBox.Show("Order Error: " + ord.ToString());
                            }
                            else //No error, new sitting order is the new "buy" order (restore size order)
                            {
                                orderLevel.sittingOrder = stiOrder;
                                orderLevel.isRestore    = false;
                            }
                        }
                        else
                        {
                            orderLevel.sittingOrder = stiOrder;
                            orderLevel.isRestore    = false;
                        }
                    }
                    if (direction == "S" && !isRestore) //Take Profit has just occurred
                    {
                        //orderLevel.PL += Convert.ToDecimal(orderLevel.lastFilledOrder.fExecPrice - structTrade.fExecPrice);
                        orderLevel.totalFills += 1;
                        orderLevel.completedOrders.Add(orderLevel.sittingOrder);
                        orderLevel.lastFilledOrder = structTrade;

                        //Create restore size order
                        SterlingLib.STIOrder stiOrder = new SterlingLib.STIOrder();
                        stiOrder.Symbol      = symbol;
                        stiOrder.Account     = Globals.account;
                        stiOrder.Side        = "S";
                        stiOrder.Quantity    = incrementSize;
                        stiOrder.Tif         = "D"; //day order
                        stiOrder.PriceType   = SterlingLib.STIPriceTypes.ptSTILmt;
                        stiOrder.LmtPrice    = Convert.ToDouble(orderLevel.startPrice);
                        stiOrder.Destination = Globals.desination;
                        stiOrder.ClOrderID   = Guid.NewGuid().ToString();


                        if (!isStopped)
                        {
                            if (restoreSize)
                            {
                                //Submit order
                                int ord = stiOrder.SubmitOrder();
                                if (ord != 0)
                                {
                                    MessageBox.Show("Order Error: " + ord.ToString());
                                }
                                else //No error, new sitting order is the new "buy" order (restore size order)
                                {
                                    orderLevel.sittingOrder = stiOrder;
                                }
                            }
                            else
                            {
                                orderLevel.sittingOrder = stiOrder; //Order will sit there but not execute is no restore size option
                                orderLevel.isRestore    = true;
                            }
                        }
                        else
                        {
                            orderLevel.sittingOrder = stiOrder;
                        }
                    }
                }
            }
        }
Example #3
0
        /*private void stiEvents_OnSTIOrderUpdateXML(ref string orderUpdateInfo)
         * {
         *  XmlSerializer xs = new XmlSerializer(typeof(SterlingLib.structSTIOrderUpdate));
         *  SterlingLib.structSTIOrderUpdate structTrade = (SterlingLib.structSTIOrderUpdate)xs.Deserialize(new StringReader(orderUpdateInfo));
         *  //MessageBox.Show("Picked Up");
         *
         *
         *
         * }*/

        private void stiEvents_OnSTIOrderUpdateXML(ref string orderUpdateInfo)
        {
            XmlSerializer xs = new XmlSerializer(typeof(SterlingLib.structSTIOrderUpdate));

            SterlingLib.structSTIOrderUpdate structTrade = (SterlingLib.structSTIOrderUpdate)xs.Deserialize(new StringReader(orderUpdateInfo));
            //MessageBox.Show("Picked Up");


            if (autoProfitOn && Globals.profitTakeMethod == "3-Block")
            {
                if (structTrade.nOrderStatus == 13)
                {
                    if (structTrade.nPriceType == 7 || structTrade.nPriceType == 8)
                    {
                        Form rangePicker = new RangePicker()
                        {
                            TopMost = true, TopLevel = true, StartPosition = FormStartPosition.CenterScreen
                        };
                        int          rangeMultiple = 1;
                        DialogResult result        = rangePicker.ShowDialog();
                        rangePicker.Activate();
                        rangePicker.TopMost = true;
                        rangePicker.Focus();
                        rangePicker.BringToFront();


                        if (result == DialogResult.Yes)
                        {
                            rangeMultiple = 2;
                        }
                        if (result == DialogResult.No)
                        {
                            rangeMultiple = 3;
                        }
                        if (result == DialogResult.OK)
                        {
                            rangeMultiple = 4;
                        }
                        if (rangeMultiple == 1)
                        {
                            return;                     //Exit if no value chosen
                        }
                        string positionSize = stiPosition.GetPositionInfo(structTrade.bstrSymbol, "", Globals.account);
                        if (Math.Abs(Convert.ToInt32(positionSize)) == (structTrade.nQuantity))
                        {
                            SterlingLib.structSTIPositionUpdate positionInfo = stiPosition.GetPositionInfoStruct(structTrade.bstrSymbol, "", Globals.account);
                            decimal positionCost = Math.Abs(Convert.ToDecimal(positionInfo.fPositionCost));
                            decimal averagePrice = Math.Abs(positionCost / Convert.ToDecimal(positionSize));

                            decimal stopRange = Math.Abs(averagePrice - Convert.ToDecimal(structTrade.fStpPrice));
                            //MessageBox.Show(averagePrice.ToString() + " : " + structTrade.fStpPrice + " : " + stopRange.ToString());

                            decimal profitRange = stopRange * rangeMultiple;
                            //MessageBox.Show(profitRange.ToString());
                            //Now that we have the range, create a summary of the profit taker with the info...

                            stopPrice      = Convert.ToDecimal(structTrade.fStpPrice);
                            incrementPrice = Math.Abs(Math.Round((profitRange) / (Convert.ToInt32(positionSize) / 100), 2));
                            numIntervals   = Math.Abs(Convert.ToInt32(positionSize) / 100);
                            profitRange    = numIntervals * incrementPrice;
                            //MessageBox.Show(incrementPrice.ToString());

                            //Now we have increment price, max size, initial size, starting size, starting price (average price, rounded to 2 decimal places)

                            startingPrice = Math.Round(averagePrice, 2);
                            rangeSize     = Math.Abs(Math.Round(profitRange, 2));
                            startingSize  = Convert.ToInt32(positionSize);

                            if (Convert.ToInt32(positionSize) < 0)
                            {
                                direction = "S";
                                rangeEnd  = startingPrice - rangeSize;
                            }
                            else if (Convert.ToInt32(positionSize) > 0)
                            {
                                direction = "B";
                                rangeEnd  = startingPrice + rangeSize;
                            }
                            else
                            {
                                direction = "N";
                            }



                            var myForm = new Form2(startingPrice, true, Math.Abs(Convert.ToInt32(positionSize)), Math.Abs(Convert.ToInt32(positionSize)), Math.Abs(Convert.ToInt32(positionSize)),
                                                   rangeMultiple, Convert.ToDecimal(structTrade.fStpPrice), structTrade.bstrSymbol, direction);
                            this.Invoke((MethodInvoker) delegate()
                            {
                                myForm.Show();
                            });
                        }
                        else
                        {
                            MessageBox.Show("Stop order size must be equal to position size");
                        }
                    }
                }
            }
            else if (autoProfitOn && Globals.profitTakeMethod == "Increment")
            {
                if (structTrade.nOrderStatus == 13)
                {
                    if (structTrade.nPriceType == 7 || structTrade.nPriceType == 8)
                    {
                        Form rangePicker = new RangePicker()
                        {
                            TopMost = true, TopLevel = true, StartPosition = FormStartPosition.CenterScreen
                        };
                        int          rangeMultiple = 1;
                        DialogResult result        = rangePicker.ShowDialog();
                        rangePicker.Activate();
                        rangePicker.TopMost = true;
                        rangePicker.Focus();
                        rangePicker.BringToFront();


                        if (result == DialogResult.Yes)
                        {
                            rangeMultiple = 2;
                        }
                        if (result == DialogResult.No)
                        {
                            rangeMultiple = 3;
                        }
                        if (result == DialogResult.OK)
                        {
                            rangeMultiple = 4;
                        }
                        if (rangeMultiple == 1)
                        {
                            return;                     //Exit if no value chosen
                        }
                        string positionSize = stiPosition.GetPositionInfo(structTrade.bstrSymbol, "", Globals.account);
                        if (Math.Abs(Convert.ToInt32(positionSize)) == (structTrade.nQuantity))
                        {
                            SterlingLib.structSTIPositionUpdate positionInfo = stiPosition.GetPositionInfoStruct(structTrade.bstrSymbol, "", Globals.account);
                            decimal positionCost = Math.Abs(Convert.ToDecimal(positionInfo.fPositionCost));
                            decimal averagePrice = Math.Abs(positionCost / Convert.ToDecimal(positionSize));

                            decimal stopRange = Math.Abs(averagePrice - Convert.ToDecimal(structTrade.fStpPrice));
                            //MessageBox.Show(averagePrice.ToString() + " : " + structTrade.fStpPrice + " : " + stopRange.ToString());

                            decimal profitRange = stopRange * rangeMultiple;
                            //MessageBox.Show(profitRange.ToString());
                            //Now that we have the range, create a summary of the profit taker with the info...

                            stopPrice      = Convert.ToDecimal(structTrade.fStpPrice);
                            incrementPrice = Math.Abs(Math.Round((profitRange) / (Convert.ToInt32(positionSize) / 100), 2));
                            numIntervals   = Math.Abs(Convert.ToInt32(positionSize) / 100);
                            profitRange    = numIntervals * incrementPrice;
                            //MessageBox.Show(incrementPrice.ToString());

                            //Now we have increment price, max size, initial size, starting size, starting price (average price, rounded to 2 decimal places)

                            startingPrice = Math.Round(averagePrice, 2);
                            rangeSize     = Math.Abs(Math.Round(profitRange, 2));
                            startingSize  = Convert.ToInt32(positionSize);

                            if (Convert.ToInt32(positionSize) < 0)
                            {
                                direction = "S";
                                rangeEnd  = startingPrice - rangeSize;
                            }
                            else if (Convert.ToInt32(positionSize) > 0)
                            {
                                direction = "B";
                                rangeEnd  = startingPrice + rangeSize;
                            }
                            else
                            {
                                direction = "N";
                            }


                            decimal profitOffset = stopRange;

                            var myForm = new Form1(startingPrice, true, Math.Abs(Convert.ToInt32(positionSize)), Math.Abs(Convert.ToInt32(positionSize)), Math.Abs(Convert.ToInt32(positionSize)),
                                                   incrementPrice, profitOffset, 100, structTrade.bstrSymbol, direction);
                            this.Invoke((MethodInvoker) delegate()
                            {
                                myForm.Show();
                            });
                        }
                        else
                        {
                            MessageBox.Show("Stop order size must be equal to position size");
                        }
                    }
                }
            }
        }