Previous candle prices

Created at 11 Dec 2016, 17:24
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!
AV

avpco

Joined 11.12.2016

Previous candle prices
11 Dec 2016, 17:24


Hi guys.

How can I get access data of previous candle prices (Open, Close, Min, Max) ?

Thanks in advance.


@avpco
Replies

yisrgg
16 Dec 2016, 13:13

Hello. That's very easy:

int BarsAgo;

//Last closed bar open price
double OpenPrice = MarketSeries.Open.Last(BarsAgo);

//Last closed bar close price
double ClosePrice = MarketSeries.Close.Last(BarsAgo);

//Last closed bar high price
double HighPrice = MarketSeries.High.Last(BarsAgo);

//Las closed bar low price
double LowPrice = MarketSeries.Low.Last(BarsAgo);

If you would like to get those prices for a different symbol from the one on the chart:

int n;
MarketSeries YourSymbol = MarketData.GetSymbol(Symbol);

//Open price n bars ago
double OpenPrice = YourSymbol.Open.Last(n);

//Close price n bars ago
double ClosePrice = YourSymbol.Close.Last(n);

//High price n bars ago
double HighPrice = YourSymbol.High.Last(n);

//Low price n bars ago
double LowPrice = YourSymbol.Low.Last(n);

If n=0, then you will get the information for the actual bar. If n=1, you will get the information for the last closed bar. And so on.


@yisrgg

avpco
16 Dec 2016, 17:58

RE:

Hi, fcomanjoncabeza.

Thanks a lot!

fcomanjoncabeza said:

Hello. That's very easy:

int BarsAgo;

//Last closed bar open price
double OpenPrice = MarketSeries.Open.Last(BarsAgo);

//Last closed bar close price
double ClosePrice = MarketSeries.Close.Last(BarsAgo);

//Last closed bar high price
double HighPrice = MarketSeries.High.Last(BarsAgo);

//Las closed bar low price
double LowPrice = MarketSeries.Low.Last(BarsAgo);

If you would like to get those prices for a different symbol from the one on the chart:

int n;
MarketSeries YourSymbol = MarketData.GetSymbol(Symbol);

//Open price n bars ago
double OpenPrice = YourSymbol.Open.Last(n);

//Close price n bars ago
double ClosePrice = YourSymbol.Close.Last(n);

//High price n bars ago
double HighPrice = YourSymbol.High.Last(n);

//Low price n bars ago
double LowPrice = YourSymbol.Low.Last(n);

If n=0, then you will get the information for the actual bar. If n=1, you will get the information for the last closed bar. And so on.

 


@avpco

avpco
18 Dec 2016, 17:45

RE:

Hello fcomanjoncabeza.

I would like to make the Indicator, which on one chart would read the pricing information (High, Low, Close, Open) on all timeframes and all currency pairs. Please tell me how to do it. I am new to programming and would like to learn how to create trading robots and indicators. Thanks in advance.

fcomanjoncabeza said:

Hello. That's very easy:

int BarsAgo;

//Last closed bar open price
double OpenPrice = MarketSeries.Open.Last(BarsAgo);

//Last closed bar close price
double ClosePrice = MarketSeries.Close.Last(BarsAgo);

//Last closed bar high price
double HighPrice = MarketSeries.High.Last(BarsAgo);

//Las closed bar low price
double LowPrice = MarketSeries.Low.Last(BarsAgo);

If you would like to get those prices for a different symbol from the one on the chart:

int n;
MarketSeries YourSymbol = MarketData.GetSymbol(Symbol);

//Open price n bars ago
double OpenPrice = YourSymbol.Open.Last(n);

//Close price n bars ago
double ClosePrice = YourSymbol.Close.Last(n);

//High price n bars ago
double HighPrice = YourSymbol.High.Last(n);

//Low price n bars ago
double LowPrice = YourSymbol.Low.Last(n);

If n=0, then you will get the information for the actual bar. If n=1, you will get the information for the last closed bar. And so on.

 


@avpco

avpco
18 Dec 2016, 19:56

RE:

Hello fcomanjoncabeza.

I would like to make the Indicator, which on one chart would read the pricing information (High, Low, Close, Open) on all timeframes and all currency pairs. Please tell me how to do it. I am new to programming and would like to learn how to create trading robots and indicators. Thanks in advance.

fcomanjoncabeza said:

Hello. That's very easy:

int BarsAgo;

//Last closed bar open price
double OpenPrice = MarketSeries.Open.Last(BarsAgo);

//Last closed bar close price
double ClosePrice = MarketSeries.Close.Last(BarsAgo);

//Last closed bar high price
double HighPrice = MarketSeries.High.Last(BarsAgo);

//Las closed bar low price
double LowPrice = MarketSeries.Low.Last(BarsAgo);

If you would like to get those prices for a different symbol from the one on the chart:

int n;
MarketSeries YourSymbol = MarketData.GetSymbol(Symbol);

//Open price n bars ago
double OpenPrice = YourSymbol.Open.Last(n);

//Close price n bars ago
double ClosePrice = YourSymbol.Close.Last(n);

//High price n bars ago
double HighPrice = YourSymbol.High.Last(n);

//Low price n bars ago
double LowPrice = YourSymbol.Low.Last(n);

If n=0, then you will get the information for the actual bar. If n=1, you will get the information for the last closed bar. And so on.

 


@avpco

ctid2434759
03 Aug 2022, 02:16 ( Updated at: 03 Aug 2022, 08:42 )

RE: RE:How would I do this for the updated code base in 2022?

I want to get the last 1 minute bars high print from a differant symbol and place the order based off of that last bars high price.


This is what I currently have but unfortunately it gets the last bars highest price of the open instance and not the other symbols it's referencing when placing the orders.

       public Symbol[] MySymbols;

        protected override void OnStart()
        {

            MySymbols = Symbols.GetSymbols("EURUSD", "GBPUSD");
        
            foreach (var symbolName in MySymbols)
            {
                PlaceLimitOrder(TradeType.Buy, symbolName, 1000, Bars.HighPrices[Bars.Count -2]-6*symbolName.PipSize,"myLimitOrder", null, 18);
            }

        }

Updated. This works.

            var mySymbol = Symbols.GetSymbol("SYMBOLNAME");
            var mySymbolSeries = MarketData.GetBars(TimeFrame.Minute, "SYMBOLNAME");
            var mySymbolPrice = mySymbolSeries.LastBar.High;
            
            PlaceLimitOrder(TradeType.Buy, mySymbol, 10000, mySymbolPrice-7*mySymbol.PipSize,"myLimitOrder", null, 18);


@ctid2434759