Understanding of the state of bot.Positions if there is trade occur in the loop

Created at 07 Jun 2021, 18:44
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!
JO

journeyforlife93

Joined 07.06.2021

Understanding of the state of bot.Positions if there is trade occur in the loop
07 Jun 2021, 18:44


Hi, I will like to understand more when we execute bot.Positions.

Example:

                foreach (var pos in this.bot.Positions.Where(x => x.SymbolName == this.symbol.Name))
                {
                   TradeResult res = this.bot.ExecuteMarketOrder(this.currDirection, this.symbol.Name, inputLot, this.symbol.VolumeInUnitsToQuantity(inputLot).ToString());

// INSIDE HERE
                }

What will happen to the Positions state if we perform close trade/execute trade/modify trade during the looping? 

Will the looping immediately include the newly opened trade into the Positions? And will the Positions count will increase?

OR

The Positions state still remains as previous(before closing the trade until I exited the loop? 

 

Thanks


@journeyforlife93
Replies

amusleh
08 Jun 2021, 12:04

Hi,

The positions collections will be updated immediately after sending a position execution request, try this sample code:

using cAlgo.API;
using cAlgo.API.Internals;
using System.Text;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class RSIRangeRobot : Robot
    {
        protected override void OnStart()
        {
            ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);
        }

        protected override void OnTick()
        {
            foreach (var position in Positions)
            {
                var stringBuilder = new StringBuilder();

                stringBuilder.AppendFormat("Before Execution: {0}", Positions.Count);

                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);

                stringBuilder.AppendFormat(" | After Execution: {0}", Positions.Count);

                Print(stringBuilder);
            }
        }
    }
}

 


@amusleh