new bar on Tick

Created at 03 Sep 2018, 10:06
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!
alexander.n.fedorov's avatar

alexander.n.fedorov

Joined 02.01.2018

new bar on Tick
03 Sep 2018, 10:06


How do I find onTick()
that the last bar is over?

 


@alexander.n.fedorov
Replies

PanagiotisCharalampous
03 Sep 2018, 10:57

Hi Sasha,

You can try something like this

 private double _lastClose;
 protected override void OnTick()
 {
    if (MarketSeries.Close.Last(1) != _lastClose)
    {
       //Bar changed
       _lastClose = MarketSeries.Close.Last(1);
    }
 }

Best Regards,

Panagiotis


@PanagiotisCharalampous

alexander.n.fedorov
05 Sep 2018, 14:07

Dear Panagiotis,

I am sorry  but it is still unclear how to open a new position on a new candle if I am OnTick

 

Regards,

 

Alexander


@alexander.n.fedorov

PanagiotisCharalampous
05 Sep 2018, 14:19

Hi Sasha,

In the code sample above, whenever the if statement is true, then you have a new candle.

Best Regards,

Panagiotis


@PanagiotisCharalampous

... Deleted by UFO ...

PanagiotisCharalampous
05 Sep 2018, 15:53

Hi alidarvish64@yahoo.com,

Thanks for posting in our forum. Could you please create a new thread for this issue since it does not seem to be related with the original post?

Best Regards,

Panagiotis


@PanagiotisCharalampous

alexander.n.fedorov
05 Sep 2018, 18:00

Panagiotis, so I do the following:

"

     protected override void OnTick()
        {

            var positions = Positions.FindAll(instance);
            if (positions.Count() == 0)
            {
                 if ((Symbol.Ask > dcl.Top.LastValue && bbMa.Result.IsRising()) && rsi.Result.LastValue > 50)
                {
                    ExecuteMarketOrder(TradeType.Buy, Symbol, volume, instance);
                }
                 if ((Symbol.Bid < dcl.Bottom.LastValue && bbMa.Result.IsFalling()) && rsi.Result.LastValue < 50)
                {
                    ExecuteMarketOrder(TradeType.Sell, Symbol, volume, instance);
                }
            }

            if (positions.Count() > 0)
            {

                if (IsNewCandle() == true)
                {

                    foreach (var position in positions)
                    {
                        if (position.Pips > atr.Result.LastValue * positionATR)
                        {
                            breakEven(position);
                            ExecuteMarketOrder(position.TradeType, Symbol, position.VolumeInUnits, instance);
                        }
                    }

                }

 

.....

 private bool IsNewCandle()
        {

       if (MarketSeries.Close.Last(1) != _lastClose)
            {

                //Bar changed
                _lastClose = MarketSeries.Close.Last(1);
                return true;
            }
            else return false;
        }

 

Correct?

 


@alexander.n.fedorov

... Deleted by UFO ...

... Deleted by UFO ...

PanagiotisCharalampous
06 Sep 2018, 11:30

Hi Sasha,

Yes correct. 

alidarvish64@yahoo.com I have deleted your posts, please create a new thread for your issue


@PanagiotisCharalampous

alexander.n.fedorov
06 Sep 2018, 12:56

Hi, Panagiotis, 

Thanks for the reply

Unfortunately, it does not work

Regards, 

Sasha


@alexander.n.fedorov

alexander.n.fedorov
06 Sep 2018, 12:57

Hi, Panagiotis,

Thanks for reply ,

Unfortunately it does not work

 

Regards, 

Sahsa


@alexander.n.fedorov

PanagiotisCharalampous
06 Sep 2018, 13:58

Hi Sasha,

Can you explain why it doesn't work? What does it do and what did you expect it to do?

Best Regards,

Panagiotis


@PanagiotisCharalampous

applata86
21 Aug 2019, 09:16

RE:

Panagiotis Charalampous said:

Hi Sasha,

You can try something like this

 private double _lastClose;
 protected override void OnTick()
 {
    if (MarketSeries.Close.Last(1) != _lastClose)
    {
       //Bar changed
       _lastClose = MarketSeries.Close.Last(1);
    }
 }

Best Regards,

Panagiotis

 

The section "//Bar changed" inside the IF statement is only executed once every candle right? I'm looking at this option because the OnBar() issue is still not resolved -->   https://ctrader.com/forum/calgo-support/3349?page=1#7


@applata86

applata86
21 Aug 2019, 09:28

I would like to confirm that the "OnTick bar changed" code is working.

I'm a .NET developer with morethan 10 yrs experience now and I can tell you that there's a threading problem with your OnBar() implementation.

Please fix


@applata86

... Deleted by UFO ...