Issue where looking at Result.Position after creating an order is null?

Created at 12 Apr 2022, 03:10
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!
MA

Issue where looking at Result.Position after creating an order is null?
12 Apr 2022, 03:10


I'm doing something where I have a List<Position>, and when an order executes I'm adding it to this list:

However, result.Position is Null in this case, I figure it may be something to do with asynchronous code, or delays. But not sure I understand exactly as I am not using ExecuteMarketOrderAysnc.

 

When I tried to use ExecuteMarketOrderAysnc, with await, I got an error and looking up other threads here it seems that calgo does not support async/await type logic.

Can someone shed some light on why the Result.Position is null, causing the algo to:
 

 

When it tries to access the element in the list expecting a Position but finding null instead?


@manoj.clsd.kumar@gmail.com
Replies

amusleh
12 Apr 2022, 10:16

Hi,

After you call execute market order method you should check result.IsSuccessful property before trying to access the Position property of result, example:

            var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin);

            if (result.IsSuccessful)
            {
                Print("Order was placed successfully, now you can use result.Position");

                var position = result.Position;
            }
            else
            {
                Print("Order was not placed because of: {0}", result.Error);
                Print("In this case result.Position is null");
            }

The above code is the right way for using result of an ExecuteMarketOrder non async call.

In case you just want to store a position on a collection when it opens you can use Positions.Opened event, example:

using System;
using System.Collections.Generic;
using cAlgo.API;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private readonly List<Position> _positions = new List<Position>();

        [Parameter(DefaultValue = "MyBot")]
        public string Label { get; set; }

        protected override void OnStart()
        {
            Positions.Opened += Positions_Opened;
        }

        // This will be called whenever a new position is opened
        // on trading account
        private void Positions_Opened(PositionOpenedEventArgs obj)
        {
            if (obj.Position.Label.Equals(Label, StringComparison.Ordinal))
            {
                _positions.Add(obj.Position);
            }
        }
    }
}

For ExecuteMarketOrderAsync you should use the callback delegate:

using System.Collections.Generic;
using cAlgo.API;

namespace NewcBot
{
    [Robot(AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        private readonly List<Position> _positions = new List<Position>();

        [Parameter(DefaultValue = "MyBot")]
        public string Label { get; set; }

        protected override void OnStart()
        {
            ExecuteMarketOrderAsync(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, ExecuteMarketOrderCallback);
        }

        private void ExecuteMarketOrderCallback(TradeResult result)
        {
            if (result.IsSuccessful)
            {
                var position = result.Position;

                _positions.Add(position);

                Print("Position opened at {0}", position.EntryPrice);
            }
            else
            {
                Print("Error: {0}", result.Error);
            }
        }
    }
}

 


@amusleh

manoj.clsd.kumar@gmail.com
12 Apr 2022, 11:55

RE:

Thank you.


@manoj.clsd.kumar@gmail.com