MarketSeries close/open/high/low same value OnBar

Created at 11 Jun 2014, 16:01
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!
MO

moemar

Joined 11.06.2014

MarketSeries close/open/high/low same value OnBar
11 Jun 2014, 16:01


using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MGMScalp01 : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Periods", DefaultValue = 14)]
        public int Periods { get; set; }

        [Parameter("Volume", DefaultValue = 10000, MinValue = 1000)]
        public int Volume { get; set; }

        [Parameter("Take profit %", DefaultValue = 50)]
        public int ProfitPercent { get; set; }

        private RelativeStrengthIndex rsi;
        private double open, high, low, close;
        private int index;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnBar()
        {
            if (Trade.IsExecuting)
                return;

            index = MarketSeries.Close.Count - 1;

            close = MarketSeries.Close[index];
            high = MarketSeries.High[index];
            low = MarketSeries.Low[index];
            open = MarketSeries.Open[index];

            if (rsi.Result.LastValue > 70)
            {
                Print("RSI>70: {0}", rsi.Result.LastValue);

                if (VerifyShootingStar())
                    Open(TradeType.Sell);
            }
        }

        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }

        private bool VerifyShootingStar()
        {
            double lowGap = (close < open) ? (close - low) : (open - low);
            double highGap = (close > open) ? (high - close) : (high - open);
            double body = (close > open) ? (close - open) : (open - close);
            double length = lowGap + body + highGap;

            Print("LG: {0}, HG: {1}, BD: {2}, LG: {3}, OHLC: {4},{5},{6},{7}", lowGap, highGap, body, length, open, high, low, close);

            return false;
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("MGMScalp01", Symbol, tradeType);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "MGMScalp01");
        }
    }
}

This code produce this output:


 

What am I doing wrong?


@moemar
Replies

Antonma
11 Jun 2014, 23:04

Hi,

use 

index = MarketSeries.Close.Count - 2;

for last closed bar. Count -1 is the current bar...

 

 

 


@Antonma

Spotware
12 Jun 2014, 09:19

Antonma is right, you need to take previous bar. Here is some clarifications:

OnBar event happens when new bar is opened. At that moment MarketSeries collection already contains tick from new bar. It means that last bar is not formed and in general cases open = high = low = close. If you want to access to last formed bar you need take previous one.

You can use following code to access to the previous bar:

   Print("Open: {0}, Close: {1}", MarketSeries.Open.Last(1), MarketSeries.Close.Last(1));


@Spotware

moemar
12 Jun 2014, 10:26

RE:

Thanks guys, it works :)


@moemar