Category Trend  Published on 03/08/2023

Mean Price Indicator

An update for this algorithm is currently pending moderation. Please revisit this page shortly to access the algorithm's latest version.
Description

The Mean Price indicator displays two levels: the average price of all bars of the current day and the average price of the previous day. 

The useful information for trading is to find price zones when the price is above the two levels for long trades and to find price zones when the price is below the two levels for short trades. 

This trade strategy is based on analyzing the trade zones using information produced by the indicator.




mfejza's avatar
mfejza

Joined on 25.01.2022

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: mMeanPrice.algo
  • Rating: 5
  • Installs: 584
  • Modified: 03/08/2023 15:03
Comments
Log in to add a comment.
mfejza's avatar
mfejza · 1 year ago
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;

namespace cAlgo
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None, TimeZone = TimeZones.UTC)]
    public class mMeanPrice : Indicator
    {
        [Output("Mean", LineColor = "Black", PlotType = PlotType.Points, LineStyle = LineStyle.Solid, Thickness = 1)]
        public IndicatorDataSeries outMean { get; set; }
        [Output("Positive Mean", LineColor = "Green", PlotType = PlotType.Points, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries outMeanPositive { get; set; }
        [Output("Negative Mean", LineColor = "Red", PlotType = PlotType.Points, LineStyle = LineStyle.Solid, Thickness = 2)]
        public IndicatorDataSeries outMeanNegative { get; set; }
        [Output("LastPrice", LineColor = "FF68D0F7", PlotType = PlotType.Points, LineStyle = LineStyle.Dots, Thickness = 4)]
        public IndicatorDataSeries outLastPrice { get; set; }
        
        private Bars _barD1;
        private IndicatorDataSeries _price, _cnt, _lastprice;
        

        protected override void Initialize()
        {
            _price = CreateDataSeries();
            _cnt = CreateDataSeries();
            _lastprice = CreateDataSeries();
        }

        public override void Calculate(int i)
        {
            _barD1 = MarketData.GetBars(TimeFrame.Daily);
            var iD1 = _barD1.OpenTimes.GetIndexByTime(Bars.OpenTimes[i]);
            DateTimeOffset tradingStartTime = _barD1[iD1].OpenTime;
            var indexStart = Bars.OpenTimes.GetIndexByExactTime(tradingStartTime.DateTime);
            
            _price[i] = i == indexStart ? Bars.ClosePrices[i] : _price[i-1] + Bars.ClosePrices[i];
            _cnt[i] = i == indexStart ? 1 : _cnt[i-1] + 1;
            _lastprice[i] = i>1 && i == indexStart ? _price[i-1] / _cnt[i-1] : i>1 ? _lastprice[i-1] : Bars.ClosePrices[i];
            
            outMean[i] = _price[i] / _cnt[i];
            outLastPrice[i] = _lastprice[i];
            outMeanPositive[i] = Bars.ClosePrices[i] > _price[i] / _cnt[i] ?  _price[i] / _cnt[i] : double.NaN;
            outMeanNegative[i] = Bars.ClosePrices[i] < _price[i] / _cnt[i] ?  _price[i] / _cnt[i] : double.NaN;
        }
    }
}