How can I get the latest position information?

Created at 20 Jul 2023, 11:11
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!
yomm0401's avatar

yomm0401

Joined 11.04.2020

How can I get the latest position information?
20 Jul 2023, 11:11


Information is not updated when trying to retrieve information immediately after closing a position.

I have a position at the same time as the cBot start with the code below, and immediately after modifying the position, why does the information on Quantity not match the modified Quantity when I get the information on Quantity?

Please let me know if anyone can figure this out.

Best regards.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {

        private string _symbol;
        private double quantity;

        protected override void OnStart()
        {
            _symbol = Chart.SymbolName;

            ExecuteMarketOrder(TradeType.Sell, _symbol, 4000);

            var positionsell = Positions.FindAll(null, _symbol, TradeType.Sell);
            foreach (Position p in positionsell)
            {
                foreach (var Position in positionsell)
                {
                    if (Position.Id == p.Id)
                    {
                        ModifyPositionAsync(Position, 1000);
                    }
                }
            }

            var positionsell2 = Positions.FindAll(null, _symbol, TradeType.Sell);
            foreach (Position q in positionsell2)
            {
                quantity += q.Quantity;
            }
            Chart.DrawStaticText("test", quantity.ToString(), VerticalAlignment.Bottom, HorizontalAlignment.Center, Color.White);
        }

        protected override void OnTick()
        {
        }

        protected override void OnStop()
        {
        }
    }
}

 


@yomm0401
Replies

PanagiotisChar
20 Jul 2023, 15:56

Hi there, 

Because you are using an Async method. Don't use an Async method if you need positions to be updated before you proceed.

Aieden Technologies

Need help? Join us on Telegram

 


@PanagiotisChar

yomm0401
24 Jul 2023, 06:47

RE: How can I get the latest position information?

PanagiotisChar said: 

Hi there, 

Because you are using an Async method. Don't use an Async method if you need positions to be updated before you proceed.

Aieden Technologies

Need help? Join us on Telegram

 

Appreciate the replies.
You have solved all my problems.

Thank you.


@yomm0401