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

Created at 30 Nov 2024, 17:42
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!
KO

kouekam18alain

Joined 30.11.2024

I can't use the Bars.ClosePrices.Last(1) line of code in the onTick method from the plugins
30 Nov 2024, 17:42


namespace cAlgo.Plugins
{
    [Plugin(AccessRights = AccessRights.None)]
    public class plugingBuy : Plugin
    {
        
        
        private Button _buttonBuy;
        private Window _window;
        
                
        
        protected override void OnStart()
        {
           _buttonBuy = new Button
           {
                BackgroundColor = Color.SeaGreen,
                Height = 25,
                Text = "BuyOrder"
            
           };

            _buttonBuy.Click += _buttonBuy_Click;
            
            


            _window = new Window
            {
                Height = 150,
                Width = 150,
                Padding = new Thickness(5, 10, 10, 5)

            };



            _window.Child = _buttonBuy;
            _window.Show();


        }
        
        
        protected override void OnTick()  // I have an error on onTick
        {
            // Handle price updates here
            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);
            
        }
        
        
        
        

        private void _buttonBuy_Click(ButtonClickEventArgs obj)
        { 
           
            var result = ExecuteMarketOrder(TradeType.Buy, "BTCUSD", 0.1, "order 1", 100, 100);
        }



        protected override void OnStop()
        {
            // Handle Plugin stop here
        }
    }        
}

@kouekam18alain
Replies

firemyst
01 Dec 2024, 14:24

What's the error you receive?


@firemyst

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

PanagiotisCharalampous
02 Dec 2024, 07:43

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


@PanagiotisCharalampous

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

PanagiotisCharalampous
09 Dec 2024, 07:28

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

kouekam18alain said: 

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

Can you share the exact error you receive?


@PanagiotisCharalampous