Index and next bar

Created at 19 Apr 2023, 23:31
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!
KH

khlopez

Joined 13.04.2023

Index and next bar
19 Apr 2023, 23:31


Hello,

Is there any way to find the index of a bar and then act on the following one?

For example, I would like to get something like this:

 protected override void OnBar()
        {
            if (Positions.Count > 0)
                return;
             
                    
            double lastLowestLow = Bars.LowPrices.Minimum(5);
            int lastLowestLowIndex = -1;
            int nextbar = lastLowestLowIndex + 1;

            if Bars.ClosePrices[nextbarindex] > Bars.OpenPrices[nextbarindex])

            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "", 15, 25);
            }
         }

What I'm trying to do is calculate where the lowest low in the last five candles is, and then, if the next bar bullish, open a buy order.

I hope you can help me out, because I don't know how many hours I've spend already trying to find this information.

 

Thanks a lot in advance.


@khlopez
Replies

firemyst
20 Apr 2023, 05:29

First, you'll probably want to get the current index:

int lastIndex = Bars.OpenTimes.GetIndexByTime(Bars.OpenTimes.LastValue)

Then you can loop over the last 5 indexes:

int low = Int32.MaxValue;

for (int x=lastIndex - 5; x <= lastIndex; x++)

{

  if (Bars.LowPrices[x] < low)

    low = Bars.LowPrices[x];

}

 

The above is all pseudo code so may not work exactly as is since I'm not in front of anything other than a tablet typing this.

If you want to find the index of another bar, assuming you're on a chart that's based on time (eg, not Renko or Range bars), then in the call to Bars.OpenTimes.GetIndexByTime, provide the time of the bar that you want.

Hope that helps?


@firemyst

Shares4UsDevelopment
13 Apr 2024, 13:36 ( Updated at: 14 Apr 2024, 08:02 )

try this

try:

 

 

     protected override void OnBar()
        {
            if (Positions.Count > 0)
                return;

            var OpenPrice = Bars[Bars.Count - 1].Open;
            var FinishedBar = Bars[Bars.Count - 2];

            if (FinishedBar.Open < FinishedBar.Close) // Bull
            {
                var Lowest = Bars.LowPrices.TakeLast(5 + 2).Skip(2).Min(p => p);
                if(Lowest< OpenPrice)
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Symbol.VolumeInUnitsMin, "",(OpenPrice - Lowest)/Symbol.PipSize, 25);
            }
        }



@Shares4UsDevelopment