Help on extracting data printed from console i.e. (PID) and use it in the same bot

Created at 23 Sep 2022, 16:49
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!
SA

samachua2011

Joined 18.04.2022

Help on extracting data printed from console i.e. (PID) and use it in the same bot
23 Sep 2022, 16:49


Greetings,

I have a bot which has the following condition set to print market position id when it executes a market order.

ExecuteMarketOrder(type, SymbolName, volumeInUnits, Label, SL, TP);
Print(LastResult, Position.Id);

However, we need to call this result on an advanced take profit function within the same bot on the prompt......"if (PositionId == DefaultPositionIdParameterValue)

        protected override void OnStart()
        {
            if (PositionId == DefaultPositionIdParameterValue)
                PrintErrorAndStop("You have to specify \"Position Id\" in cBot Parameters");

Is there a way to retrieve the position id automatically within the bot's functionality for each trade to allow the advanced take profit functionality to work?

Thanks in advance

Best Regards,

Samuel Machua.


@samachua2011
Replies

PanagiotisCharalampous
26 Sep 2022, 10:31

Hi Samuel,

It is not very clear to me what you are trying to do. The position id is available in every position and all accouint positions are accessible from anywhere in the cBot. What is the problem you are trying to solve here?

 Best Regards,

Panagiotis 

Join us on Telegram and Facebook


@PanagiotisCharalampous

samachua2011
27 Sep 2022, 09:55

RE:

PanagiotisCharalampous said:

Hi Samuel,

It is not very clear to me what you are trying to do. The position id is available in every position and all accouint positions are accessible from anywhere in the cBot. What is the problem you are trying to solve here?

 Best Regards,

Panagiotis 

Join us on Telegram and Facebook

Hi Panagiotis,

We are trying to set Take Profit levels in a Reward Manager cbot where if a certain position is taken for example short on EURJPY 1 lot:

  • Take Profit 1   10 pips   50%
  • Take Profit 2   15 pips   30%
  • Take Profit 3   20 pips   20%

In order to do this, we need to reference the position id. However, since we want the bot to take multiple trades on its own, one cannot keep adding the position id manually to the bot each time a trade is taken hence the need to automate this process. What we want to do is have a command that picks the position id "PID" each time the cbot automatically takes a short/long position so that the cbot auto calculates the take profit levels and manages the position.

How do we code the "DefaultPositionIdParameterValue" term referenced on the snippet of code below "Snippet 1" to pick the Position id "PID" from the Execute Market Order in the code below referenced "Snippet2":

Snippet 1

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class PriceActionBotGuide2Reward : Robot
    {
        private const string DefaultPositionIdParameterValue = "PID";

        [Parameter("Position Id", Group = "Position", DefaultValue = 
        DefaultPositionIdParameterValue)]
        public string PositionId { get; set; }

 

Snippet 2

ExecuteMarketOrder(type, SymbolName, volumeInUnits, Label, SL, TP);
Print(LastResult, Position.Id);

 


@samachua2011