Average Price for positions

Created at 07 Sep 2018, 06:44
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!
swingfish's avatar

swingfish

Joined 25.06.2016

Average Price for positions
07 Sep 2018, 06:44


the Position window in "aggregated view" shows Average pips, price and P/L 

is there a way to get this information via the Indicator API ? 

 


@swingfish
Replies

PanagiotisCharalampous
07 Sep 2018, 09:50

Hi swingfish,

You can easily calculate this information. For example

Positions.Average(x => x.Pips);

Note that you need to add a reference to System.Linq to your indicator

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

Best Regards,

Panagiotis


@PanagiotisCharalampous

swingfish
07 Sep 2018, 10:20

thanks for the reply, for some reason cTrader 3.0 and 3.01 do not have the Positions.Average call 

i use this now to calculate the average price (but as i said before, its not very accurate)

the goal is to plot a average line on the chart based on entries/volume .. like a breakeven-line so to speak

 

            // breakeven line
            ie = 0;
            foreach (var position in Positions)
            {
                if (position.SymbolCode == Symbol.Code)
                {
                    ie++;
                    ev = position.Volume;
                    tv += ev;
                    if (position.TradeType == TradeType.Sell)
                        ev = ev * -1;
                    e = e + position.EntryPrice * ev;
                    v = v + ev;

                    if (position.EntryPrice >= epmx)
                    {
                        epmx = position.EntryPrice;
                    }

                    if (position.EntryPrice <= epmn)
                    {
                        epmn = position.EntryPrice;
                    }
                }
            }

            p = e / v;
            if (ie > 0)
            {
                ChartObjects.DrawLine("brline", index - 5, p, index + 10, p, Colors.LimeGreen);
            }
            else
            {
                ChartObjects.RemoveObject("brline");
            }

 


@swingfish

PanagiotisCharalampous
07 Sep 2018, 10:26

Hi swingfish,

"thanks for the reply, for some reason cTrader 3.0 and 3.01 do not have the Positions.Average call ". 

This is not part of cTrader but of .Net. As i said you need to use System.Linq library.

Best Regards,

Panagiotis


@PanagiotisCharalampous