Data export to file

Created at 11 Nov 2012, 19:38
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!
RK

rkokerti

Joined 28.06.2012

Data export to file
11 Nov 2012, 19:38


Hello Admin,

Based on your instruction, I wrote a code that export backtest result data to file. I try to add almost all of the columns that available in cAlgo backtest.  

Coud you please help me, how can I add more columns like:

- Close Price

- Gross profit

- Pips

I attached the part of the code below that I wrote:

    string _ID = _position.Id.ToString();
    string _ShortDate = Convert.ToString(Server.Time.Year+"-"+Server.Time.Month);
    string _Date = Convert.ToString(Server.Time.Year+"-"+Server.Time.Month+"-"+Server.Time.Day);
    string _DayName = Server.Time.DayOfWeek.ToString();
    string _EntryTime = _position.EntryTime.ToString();
    string _Volume = _position.Volume.ToString();
    string _Type = _position.TradeType.ToString();
    string _EntryPrice = _position.EntryPrice.ToString();
    string _TakeProfit = takeProfit.ToString();
    string _StopLoss = stopLoss.ToString();
    string _Equity = Account.Equity.ToString();
    string _Balance = Account.Balance.ToString();
    
    _fileWriter.WriteLine(_ID+"    "+_ShortDate+"    "+_Date+"    "+_DayName+"    "+_EntryTime+"    "+_Volume+"    "+_Type+"    "+_EntryPrice+"    "+_TakeProfit+"    "+_StopLoss+"    "+_Equity+"    "+_Balance);

This part of code is in "protected override void OnPositionOpened(Position openedPosition)" section.

Thanks for your help!


@rkokerti
Replies

rkokerti
12 Nov 2012, 11:55

Hello Admin,

I solved most of the problem. I've only one open question.

How can I add column: Event (CreatePosition, TakeProfitHit, etc...)

Thanks for your help!


@rkokerti

admin
12 Nov 2012, 12:10

Hello,

To get the close price of the last completed bar it would be:

var index = MarketSeries.Close.Count - 1; // the current bar being created

car close = MarketSeries.Close[index - 1]; // the last completed bar's close price

The Gross profit would be _position.GrossProfit

For pips you would need to calculate it according to this:

Sell: Entry price - Ask  -- pips = Math.Round((_position.EntryPrice - Symbol.Ask)/Symbol.PipSize, Symbol.Digits);

Buy: Entry price - Bid -- pips = Math.Round((_position.EntryPrice - Symbol.Bid)/Symbol.PipSize, Symbol.Digits);

note that OnPositionOpened the value of gross profit would be minimal maybe you want to add it to OnPositionClosed.

 

 

 


@admin

admin
12 Nov 2012, 12:25

RE:
rkokerti said:

Hello Admin,

I solved most of the problem. I've only one open question.

How can I add column: Event (CreatePosition, TakeProfitHit, etc...)

Thanks for your help!

Hello,
 
It is unclear what is the data that you need for this column. The event at which the position is created is OnPositionOpened and the event at which there is a Take Profit Hit is OnPositionClosed (You would need a calculation to verify if it is Take Profit or Stop Loss Hit)
 
 
 
 

@admin

joeatbayes
26 Dec 2014, 00:30

See: /algos/cbots/show/591 and /algos/cbots/show/588


@joeatbayes