Get historical trades trough layer

Created at 20 Jan 2020, 20:54
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!
DelFonseca's avatar

DelFonseca

Joined 25.06.2017

Get historical trades trough layer
20 Jan 2020, 20:54


Good night,

I am trying to get the net profit from trades through the label, but it is not working. 
The value 'HistoricalProfitInCash ' continues to increase, even though I tested with a label I never used
Can someone help me please? Thank you so much and Follow the code:

I try:
protected override void OnStart()
{
   HistoricalProfitInCash = 0;
   HistoricalTradeByLabel();
}

private void HistoricalTradeByLabel()
{
   foreach (HistoricalTrade trade in History)
   {
      if (trade.Label.Contains(Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
      /*I TRY THIS TOO:
      if (trade.Label == (Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate)
      */
      {
         HistoricalProfitInCash += trade.NetProfit;
      }
    }
}

protected override void OnTick()
{
   HistoricalTradeByLabel();
}
        

 


@DelFonseca
Replies

DelFonseca
20 Jan 2020, 22:48

I said layer but the correct is label

I try this too, but the problem is the same.

private void HistoricalTradeByLabel()
{
   foreach (HistoricalTrade trade in History.FindAll(Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
   {
      HistoricalProfitInCash += trade.NetProfit;
   }
}

 


@DelFonseca

PanagiotisCharalampous
21 Jan 2020, 08:49

Hi DelTrader,

Can you please post the complete cBot code so that we can have a look?

Best Regards,

Panagiotis 

Join us on Telegram

 


@PanagiotisCharalampous

DelFonseca
21 Jan 2020, 20:12

RE:

PanagiotisCharalampous said:

Hi DelTrader,

Can you please post the complete cBot code so that we can have a look?

Best Regards,

Panagiotis 

Join us on Telegram

 

 

Thank you very much for your reply!

The robot has almost 8 thousand lines with arrays and connections to databases, I created a basic one to facilitate you and the situation is the same.

Start cBot a stop. Close manually opened trades to be part of the history, after that starts cBot again and confirms that the 'HistoricalProfitInCash' in the 'Chart.DrawStaticText' will significantly change tick by tick and meaningless.

Please check. I am grateful to you!!

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 Teste : Robot
    {
        [Parameter("1st Symbol", Group = "First Symbol", DefaultValue = "EURUSD")]
        public string FirstSymbol { get; set; }
        [Parameter("Lote: Buy Symbol", Group = "First Symbol", DefaultValue = 0.01, MinValue = 0.01)]
        public double QuantityToFirst { get; set; }

        [Parameter("2nd Symbol", Group = "Second Symbol", DefaultValue = "USDJPY")]
        public string SecondSymbol { get; set; }
        [Parameter("Lote: Sell Symbol", Group = "Second Symbol", DefaultValue = 0.01, MinValue = 0.01)]
        public double QuantityToSecond { get; set; }

        [Parameter("Date (ddMMyyyy)", Group = "Settings", DefaultValue = "21012020")]
        public string TodaysDate { get; set; }

        private double VolumeInUnitsFirst
        {
            get { return Symbol.QuantityToVolumeInUnits(QuantityToFirst); }
        }
        private double VolumeInUnitsSecond
        {
            get { return Symbol.QuantityToVolumeInUnits(QuantityToSecond); }
        }

        private Symbol FirstSymbolGetSymbol, SecondSymbolGetSymbol;
        private const string Label = "Teste -  ";
        private int _FirstSymbolCount, _SecondSymbolCount;
        private double HistoricalProfitInCash;

        protected override void OnStart()
        {
            HistoricalProfitInCash = 0;
            FirstSymbolGetSymbol = Symbols.GetSymbol(FirstSymbol);
            SecondSymbolGetSymbol = Symbols.GetSymbol(SecondSymbol);
            HistoricalTradeByLabel();
        }

        protected override void OnTick()
        {
            HistoricalTradeByLabel();

            _FirstSymbolCount = Positions.Where(p => p.Label == Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate).Where(p => p.TradeType == TradeType.Buy).Count(p => p.SymbolName == FirstSymbol);
            _SecondSymbolCount = Positions.Where(z => z.Label == Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate).Where(z => z.TradeType == TradeType.Buy).Count(z => z.SymbolName == SecondSymbol);

            if (_FirstSymbolCount == 0)
                ExecuteMarketOrder(TradeType.Buy, FirstSymbolGetSymbol.Name, VolumeInUnitsFirst, Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate, 0, 10);
            if (_SecondSymbolCount == 0)
                ExecuteMarketOrder(TradeType.Buy, SecondSymbolGetSymbol.Name, VolumeInUnitsSecond, Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate, 0, 10);
        }

        private void HistoricalTradeByLabel()
        {
            //foreach (HistoricalTrade trade in History.FindAll(Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
            foreach (HistoricalTrade trade in History)
            {
                //if (trade.Label.Contains(Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
                if (trade.Label == (Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
                {
                    HistoricalProfitInCash += trade.NetProfit;
                }
            }
            Chart.DrawStaticText("LabelOutputForTeste", NewLine(10) + "HistoricalProfitInCash: " + HistoricalProfitInCash + " €", VerticalAlignment.Top, HorizontalAlignment.Right, Color.Yellow);
        }

        private string NewLine(int n)
        {
            return new string('\n', n);
        }
    }
}

 


@DelFonseca

PanagiotisCharalampous
22 Jan 2020, 10:02

Hi DelTrader,

This happens because you increase the value HistoricalProfitInCash on every tick. This should be a local variable and not a global one. See below

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 Teste : Robot
    {
        [Parameter("1st Symbol", Group = "First Symbol", DefaultValue = "EURUSD")]
        public string FirstSymbol { get; set; }
        [Parameter("Lote: Buy Symbol", Group = "First Symbol", DefaultValue = 0.01, MinValue = 0.01)]
        public double QuantityToFirst { get; set; }

        [Parameter("2nd Symbol", Group = "Second Symbol", DefaultValue = "USDJPY")]
        public string SecondSymbol { get; set; }
        [Parameter("Lote: Sell Symbol", Group = "Second Symbol", DefaultValue = 0.01, MinValue = 0.01)]
        public double QuantityToSecond { get; set; }

        [Parameter("Date (ddMMyyyy)", Group = "Settings", DefaultValue = "21012020")]
        public string TodaysDate { get; set; }

        private double VolumeInUnitsFirst
        {
            get { return Symbol.QuantityToVolumeInUnits(QuantityToFirst); }
        }
        private double VolumeInUnitsSecond
        {
            get { return Symbol.QuantityToVolumeInUnits(QuantityToSecond); }
        }

        private Symbol FirstSymbolGetSymbol, SecondSymbolGetSymbol;
        private const string Label = "Teste -  ";
        private int _FirstSymbolCount, _SecondSymbolCount;

        protected override void OnStart()
        {
            FirstSymbolGetSymbol = Symbols.GetSymbol(FirstSymbol);
            SecondSymbolGetSymbol = Symbols.GetSymbol(SecondSymbol);
            HistoricalTradeByLabel();
        }

        protected override void OnTick()
        {
            HistoricalTradeByLabel();

            _FirstSymbolCount = Positions.Where(p => p.Label == Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate).Where(p => p.TradeType == TradeType.Buy).Count(p => p.SymbolName == FirstSymbol);
            _SecondSymbolCount = Positions.Where(z => z.Label == Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate).Where(z => z.TradeType == TradeType.Buy).Count(z => z.SymbolName == SecondSymbol);

            if (_FirstSymbolCount == 0)
                ExecuteMarketOrder(TradeType.Buy, FirstSymbolGetSymbol.Name, VolumeInUnitsFirst, Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate, 0, 10);
            if (_SecondSymbolCount == 0)
                ExecuteMarketOrder(TradeType.Buy, SecondSymbolGetSymbol.Name, VolumeInUnitsSecond, Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate, 0, 10);
        }

        private void HistoricalTradeByLabel()
        {
            var HistoricalProfitInCash = 0.0;
            //foreach (HistoricalTrade trade in History.FindAll(Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
            foreach (HistoricalTrade trade in History)
            {
                //if (trade.Label.Contains(Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
                if (trade.Label == (Label + "1stSymbol " + FirstSymbol + " 2ndSymbol " + SecondSymbol + " - " + TodaysDate))
                {
                    HistoricalProfitInCash += trade.NetProfit;
                }
            }
            Chart.DrawStaticText("LabelOutputForTeste", NewLine(10) + "HistoricalProfitInCash: " + HistoricalProfitInCash + " €", VerticalAlignment.Top, HorizontalAlignment.Right, Color.Yellow);
        }

        private string NewLine(int n)
        {
            return new string('\n', n);
        }
    }
}

Best Regards,

Panagiotis 

Join us on Telegram


@PanagiotisCharalampous

DelFonseca
22 Jan 2020, 20:30

RE:

PanagiotisCharalampous said:

(...)

Thank you so much, you rock!!.


@DelFonseca