Replies

kouekam18alain
08 Dec 2024, 18:30 ( Updated at: 09 Dec 2024, 06:21 )

RE: I can't use the Bars.ClosePrices.Last(1) line of code in the onTick method from the plugins

PanagiotisCharalampous said: 

Hi there,

OnTick() is a method of a robot, not of a plugin. Same for Bars property. Here is a starting point for handling tick events and accessing bars in a plug in

using System;using cAlgo.API;using cAlgo.API.Collections;using cAlgo.API.Indicators;using cAlgo.API.Internals;namespace cAlgo.Plugins{    [Plugin(AccessRights = AccessRights.None)]    public class Test : Plugin    {        Bars _bars;         Symbol _symbol;        protected override void OnStart()        {            // To learn more about cTrader Automate visit our Help Center:            // https://help.ctrader.com/ctrader-automate           _bars = MarketData.GetBars(TimeFrame.Minute);           _symbol = Symbols.GetSymbol("EURUSD");            _symbol.Tick += _symbol_Tick;        }        private void _symbol_Tick(SymbolTickEventArgs obj)        {            Print(_bars.ClosePrices.Last(1));        }        protected override void OnStop()        {            // Handle Plugin stop here        }    }        }

Best regards,

Panagiotis

There is an error on this line :  Print(_bars.ClosePrices.Last(1));

_bars doesn't know the methods or ClosePrices.Last and therefore fails to call them.

_bars. doesn't call any method, hence the error


@kouekam18alain

kouekam18alain
02 Dec 2024, 00:35 ( Updated at: 02 Dec 2024, 06:51 )

RE: I can't use the Bars.ClosePrices.Last(1) line of code in the onTick method from the plugins

firemyst said: 

What's the error you receive?

Here's the error I get

I can't implement the onTick method and get the price of previous bars because the related lines of code are marked in red.

protected override void OnTick()  // I have an error on onTick
       {
           
           double v1 =  Bars.ClosePrices.Last(1);  // I have an error on Bars.ClosePrices
           double v2 = Bars.OpenPrices.Last(2); // I have an error on Bars.OpenPrices
           Print(v1);
           
       }

 


@kouekam18alain