Calculating daily (specified date) NetProfit
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
}
}
}
Replies
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 ...
... Deleted by UFO ...