Untitled

mail@pastecode.io avatar
unknown
csharp
2 years ago
5.7 kB
2
Indexable
5759 line
        private int GetRandomNumber(int maxValue)
        {
            int randomNumber = 0;

            if (maxValue == 1)
            {
                randomNumber = maxValue;
            }
            else if (maxValue > 0)
            {
                int minValue = (int)maxValue / 2; // half number to create range
                Random random = new Random();
                randomNumber = random.Next(minValue, maxValue + 1);
            }

            return randomNumber;
        }



&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&

5686 line

 private bool FlattenEverything(string signalName, bool continueTillZeroRemainingQuantity, Instrument limitToSingleInstrument)
        {
            bool positionFound = false;

            if (!HasActiveMarketOrders() && !RealOrderService.InFlightOrderCache.HasElements())
            {
                CloseAllAccountPendingOrders(signalName, limitToSingleInstrument);

                if (!IsAccountFlat())
                {
                    double unrealizedProfitLoss = 0;
                    OrderAction orderAction = OrderAction.Buy;
                    int positionsCount = RealPositionService.PositionsCount;

                    for (int index = 0; index < positionsCount; index++)
                    {
                        RealPosition position = null;
                        if (RealPositionService.TryGetByIndex(index, out position))
                        {
                            positionFound = true;

                            if (limitToSingleInstrument == null || position.Instrument == limitToSingleInstrument)
                            {
                                position.StoreState();

                                unrealizedProfitLoss = GetPositionProfit(position);

                                if (position.MarketPosition == MarketPosition.Long)
                                    orderAction = OrderAction.Sell;
                                else if (position.MarketPosition == MarketPosition.Short)
                                    orderAction = OrderAction.Buy;

                                if (position.Quantity > 0 && !position.HasStateChanged() && !position.IsFlat())
                                {
                                    if (DebugLogLevel > 0) RealLogger.PrintOutput(signalName + " closing " + position.MarketPosition.ToString() + " " + position.Instrument.FullName + " Quantity=" + position.Quantity + " Profit=" + Convert.ToString(unrealizedProfitLoss), PrintTo.OutputTab1);

                                    SubmitMarketOrderChunked(position.Instrument, orderAction, OrderEntry.Automated, position.Quantity, continueTillZeroRemainingQuantity);
                                }

                                if (!continueTillZeroRemainingQuantity) break;
                            }
                        }
                    }
                }
            }

            return positionFound;
        }






&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&



5801 line

private void SubmitMarketOrderChunked(Instrument instrument, OrderAction orderAction, OrderEntry orderEntry, int quantity, bool continueTillZeroRemainingQuantity = true)
        {
         
            int quantityRemaining = quantity;
            int chunkedQuantity = 0;
            int cycleCount = 1;

            lock (MarketOrderLock)
            {
                if (quantityRemaining > 0)
                {
                    lock (MarketOrderLock)
                    {
                        while (quantityRemaining > 0)
                        {
                            if (quantityRemaining > this.SingleOrderChunkMaxQuantity)
                            {
                                int randomQuantity = GetRandomNumber(this.SingleOrderChunkMaxQuantity);
                                chunkedQuantity = randomQuantity;
                            }
                            else if (quantityRemaining > this.SingleOrderChunkMinQuantity)
                            {
                                int randomQuantity = GetRandomNumber(this.SingleOrderChunkMinQuantity);
                                chunkedQuantity = randomQuantity;
                            }
                            else
                            {
                                chunkedQuantity = quantityRemaining;
                            }

                            quantityRemaining -= chunkedQuantity;

                            if (cycleCount > 1 && SingleOrderChunkDelayMilliseconds > 0) Thread.Sleep(SingleOrderChunkDelayMilliseconds);

                            string orderName = RealOrderService.BuildExitOrderName();

                            try
                            {
                                Order exitOrder = account.CreateOrder(instrument, orderAction, OrderType.Market, orderEntry, TimeInForce.Day, chunkedQuantity, 0, 0, "", orderName, Core.Globals.MaxDate, null);

                                account.Submit(new[] { exitOrder });
                            }
                            catch (Exception ex)
                            {
                                RealLogger.PrintOutput("Exception in SubmitMarketOrder:" + ex.Message + " " + ex.StackTrace);  //log and ignore exception
                            }

                            cycleCount++;

                            if (!continueTillZeroRemainingQuantity) break;
                        }
                    }
                }
            }
        }