Count PendingOrderTypes

Created at 15 May 2018, 12:07
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
TH

thoy1

Joined 15.02.2018

Count PendingOrderTypes
15 May 2018, 12:07


Hi there, 

I was wondering if there is anyway to count PendingOrders of just one direction. And the same again for counting Positions of just one direction.

Example:
I have 70 open positions, and 280 pending orders. I want my bot to get a total count of (Buy Positions + Buy Pending Orders)

Thanks and regards,

Thoy


@thoy1
Replies

PanagiotisCharalampous
15 May 2018, 12:15

Hi thoy1,

Try

PendingOrders.Count(x => x.TradeType == TradeType.Buy)

Best Regards,

Panagiotis


@PanagiotisCharalampous

thoy1
15 May 2018, 12:39

Awesome! Thanks!
            if (Positions.Count + PendingOrders.Count < noOfIncrements * 2)
            {
                if ((PendingOrders.Count(x => x.TradeType == TradeType.Buy) + Positions.Count(x => x.TradeType == TradeType.Buy)) < maximumTrades)
                {
                    if (Symbol.Ask <= rangeLow && Symbol.Ask <= tpRangeLow)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, buyVolume, "" + newOrder, stopLoss, buytpExtreme);
                    }
                    if (Symbol.Ask <= rangeLow && Symbol.Ask > tpRangeLow)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, buyVolume, "" + newOrder, stopLoss, takeProfit);
                    }
                    if (Symbol.Ask > rangeLow && Symbol.Ask < rangeHigh)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, buyVolume, "" + newOrder, stopLoss, takeProfit);
                    }
                }
                if ((PendingOrders.Count(x => x.TradeType == TradeType.Sell) + Positions.Count(x => x.TradeType == TradeType.Sell)) < maximumTrades)
                {
                    if (Symbol.Ask > rangeLow && Symbol.Ask < rangeHigh)
                    {
                        ExecuteMarketOrder(TradeType.Sell, Symbol, sellVolume, "" + newOrder, stopLoss, takeProfit);
                    }
                    if (Symbol.Ask > rangeHigh && Symbol.Ask < tpRangeHigh)
                    {
                        ExecuteMarketOrder(TradeType.Sell, Symbol, sellVolume, "" + newOrder, stopLoss, takeProfit);
                    }
                    if (Symbol.Ask >= rangeHigh && Symbol.Ask >= tpRangeHigh)
                    {
                        ExecuteMarketOrder(TradeType.Sell, Symbol, sellVolume, "" + newOrder, stopLoss, selltpExtreme);
                    }
                }
            }

Awesome, here is what the code ended up looking like :) Now to see if this solves the behaviour I saw in backtesting. 


@thoy1

thoy1
01 Oct 2019, 05:58

RE:

Panagiotis Charalampous said:

Hi thoy1,

Try

PendingOrders.Count(x => x.TradeType == TradeType.Buy)

Best Regards,

Panagiotis

Hi Panagiotis,

 

Edit:

Nevermind, I used the first two lines, followed by the remaining code to show its purpose. This will be usefull to anyone running bot's which have multiple positions on multiple instruments.

int buyCount = Positions.Where(x => x.SymbolName == Symbol.Name).Where(x => x.TradeType == TradeType.Buy).Count();
int sellCount = Positions.Where(x => x.SymbolName == Symbol.Name).Where(x => x.TradeType == TradeType.Sell).Count();

Chart.DrawStaticText("Account Stats", "Balance: " + Account.Balance + "        Equity: " + Account.Equity, VerticalAlignment.Top, HorizontalAlignment.Center, Color.SkyBlue);
Chart.DrawStaticText("Chart Text", "Number of Positions: " + (buyCount + sellCount), VerticalAlignment.Bottom, HorizontalAlignment.Center, Color.SkyBlue);

if (buyCount == sellCount)
{
    Chart.RemoveObject("Symbol Strength");
    Chart.DrawStaticText("Symbol Strength", "Symbol is in Equilibrium", VerticalAlignment.Center, HorizontalAlignment.Center, Color.Yellow);
}
else if (buyCount > sellCount)
{
    Chart.RemoveObject("Symbol Strength");
    Chart.DrawStaticText("Symbol Strength", "Position is: Buy $" + (buyCount - sellCount) + "k", VerticalAlignment.Center,     HorizontalAlignment.Center, Color.Yellow);
}
else
{
    Chart.RemoveObject("Symbol Strength");
    Chart.DrawStaticText("Symbol Strength", "Position is: Sell $" + (sellCount - buyCount) + "k", VerticalAlignment.Center, HorizontalAlignment.Center, Color.Yellow);
}

Thanks and regards,

Thoy


@thoy1

thoy1
01 Oct 2019, 12:29 ( Updated at: 21 Dec 2023, 09:21 )

Hi All,

Here is a sample of what the above script does.

If you are running hundreds of positions then this script summarises the account/instrument info, so you can turn off all the positions from view (and save a tonne of CPU). 


@thoy1