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

journeyforlife93 since: 07 Jun 2021;

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

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

amusleh's avatar

amusleh since: 01 Mar 2021;

  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);
            }
        }
    }
}
Community Developer | Spotware | Telegram: @algodeveloper