Calculating daily (specified date) NetProfit

Created at 27 Sep 2016, 10:32
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!
HA

harry

Joined 18.08.2016

Calculating daily (specified date) NetProfit
27 Sep 2016, 10:32


Hi All, I managed to get historical data of netprofit using code below.

However, Anyone knows how to filter the data into specied date (eg. today only)? Thanks in advance

 

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class Profit : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }


        double totalprofit;
        protected override void OnStart()
        {
            // Put your initialization logic here
            totalprofit = 0;
        }

        protected override void OnBar()
        {
            //This will take each of historical data with label "MyLabel"
            foreach (var pos in History.FindAll("MyLabel"))
            {
                Print("NetProfit is {0}", pos.NetProfit);
                totalprofit = totalprofit + pos.NetProfit;
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}


@harry
Replies

... Deleted by UFO ...

harry
27 Sep 2016, 15:04

RE:

 

thanks! will try it

lucian said:

 foreach (HistoricalTrade trade in History)
            {
                if (( trade.Label="MyLabel" && trade.SymbolCode == Symbol.Code) && (trade.ClosingTime.Date.Day == Server.Time.Date.Day) && (trade.ClosingTime.Date.Month == Server.Time.Date.Month))
                {
                    totalprofit += trade.NetProfit;
                }
            }

if necessary check : 

trade.ClosingTime.Date.Year == Server.Time.Date.Year)

 


@harry

harry
01 Oct 2016, 12:05

RE: RE:

My rusty brain can't think on how to not compounding the totalprofit from previous totalprofit calculation, so only 1 of each id gets calculated once inside the totalprofit.

Everytime Totalprofit is called, it will recalculate the Totalprofit from the current History data and add to the previously calculated totalprofit.

So I hold the previously calculated totalprofit into a variable to hold it, so clearly it becomes 0;

How to do so it calculates 1 of each ids and not compounding to the previous totalprofit?

Thanks

 

foreach (HistoricalTrade trade in History)
            {
                if (( trade.Label="MyLabel" && trade.SymbolCode == Symbol.Code) && (trade.ClosingTime.Date.Day == Server.Time.Date.Day) && (trade.ClosingTime.Date.Month == Server.Time.Date.Month))
                {
                    totalprofit += trade.NetProfit;
                    previousprofit = totalprofit;
                    currentprofit = totalprofit - previousprofit;

                }
            }

 

harry said:

 

thanks! will try it

lucian said:

 foreach (HistoricalTrade trade in History)
            {
                if (( trade.Label="MyLabel" && trade.SymbolCode == Symbol.Code) && (trade.ClosingTime.Date.Day == Server.Time.Date.Day) && (trade.ClosingTime.Date.Month == Server.Time.Date.Month))
                {
                    totalprofit += trade.NetProfit;
                }
            }

if necessary check : 

trade.ClosingTime.Date.Year == Server.Time.Date.Year)

 

 


@harry

... Deleted by UFO ...

harry
01 Oct 2016, 12:40

Hohoho, yeah, need to set the totalprofit to 0 first, took me like 2 hours to figure out, I know it is possible but hadn't thought to resetting the totalprofit to 0 first....

Thanks lucian!


@harry