how to get the distance between two MA in (pips) and current price distance from MA

18 Jun 2017, 14:34how to get the distance between two MA in (pips) and current price distance from MA#1
hiba7rainposts: 63since: 20 Jul 2014

Hi need support to have code that will help geting the distance between two moving avareges and the current price distance from one of the moveing avareges in number of pips 

Thanks in Advance 

19 Jun 2017, 12:31#2
hiba7rainposts: 63since: 20 Jul 2014

any support is much appriciated 

Thanks 

20 Jun 2017, 13:07#3
paymentposts: 15since: 14 Aug 2015

You can use this code to start with

https://ctdn.com/algos/cbots/show/8

 

 

20 Jun 2017, 13:18#4
paymentposts: 15since: 14 Aug 2015

HERE IS THE SAME CODE BELOW WITH THE BITS I'VE ADDED UNDERLINED:

 

---------------------------

 

 

usingSystem;
usingSystem.Linq;
usingcAlgo.API;
usingcAlgo.API.Indicators;
usingcAlgo.API.Internals;
usingcAlgo.Indicators;
 
namespacecAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    publicclassSampleTrendRobot : Robot
    {
        [Parameter("MA Type")]
        publicMovingAverageType MAType { get; set; }
 
        [Parameter()]
        publicDataSeries SourceSeries { get; set; }
 
        [Parameter("Slow Periods", DefaultValue = 10)]
        publicintSlowPeriods { get; set; }
 
        [Parameter("Fast Periods", DefaultValue = 5)]
        publicintFastPeriods { get; set; }
 
        [Parameter(DefaultValue = 10000, MinValue = 0)]
        publicintVolume { get; set; }
 
        privateMovingAverage slowMa;
        privateMovingAverage fastMa;
        privateconststringlabel = "Sample Trend Robot";
 
public double distance = 0;
public double distance1 = 0;
public double distance2 = 0;
 
        protectedoverridevoidOnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }
 
        protectedoverridevoidOnBar()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);
 
            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);
 
 
distance = Math.Abs((currentSlowMa - currentFastMa) / symbol.PipSize);
distance1 = Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / symbol.PipSize);
distance2 = Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / symbol.PipSize);
 
 
Print(distance + " D1=" + distance1 + " D2=" + distance2);
 
            if(previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)
            {
                if(shortPosition != null)
                    ClosePosition(shortPosition);
                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);
            }
            elseif(previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)
            {
                if(longPosition != null)
                    ClosePosition(longPosition);
                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);
            }
        }
    }
}
20 Jun 2017, 13:40RE:#5
hiba7rainposts: 63since: 20 Jul 2014

Thanks Payment that was helpfull much appriciated 

if to use onTick what will be changed

and how to get the disstance between the current price and one of the moving averages  

payment said:

HERE IS THE SAME CODE BELOW WITH THE BITS I'VE ADDED UNDERLINED:

 

---------------------------

 

 

using System;

using System.Linq;

using cAlgo.API;

using cAlgo.API.Indicators;

using cAlgo.API.Internals;

using cAlgo.Indicators;

 

namespace cAlgo.Robots

{

    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]

    public class SampleTrendRobot : Robot

    {

        [Parameter("MA Type")]

        public MovingAverageType MAType { get; set; }

 

        [Parameter()]

        public DataSeries SourceSeries { get; set; }

 

        [Parameter("Slow Periods", DefaultValue = 10)]

        public int SlowPeriods { get; set; }

 

        [Parameter("Fast Periods", DefaultValue = 5)]

        public int FastPeriods { get; set; }

 

        [Parameter(DefaultValue = 10000, MinValue = 0)]

        public int Volume { get; set; }

 

        private MovingAverage slowMa;

        private MovingAverage fastMa;

        private const string label = "Sample Trend Robot";

 

public double distance = 0;

public double distance1 = 0;

public double distance2 = 0;

 

        protected override void OnStart()

        {

            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);

            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);

        }

 

        protected override void OnBar()

        {

            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);

            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

 

            var currentSlowMa = slowMa.Result.Last(0);

            var currentFastMa = fastMa.Result.Last(0);

            var previousSlowMa = slowMa.Result.Last(1);

            var previousFastMa = fastMa.Result.Last(1);

 

 

distance = Math.Abs((currentSlowMa - currentFastMa) / symbol.PipSize);

distance1 = Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / symbol.PipSize);

distance2 = Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / symbol.PipSize);

 

 

Print(distance + " D1=" + distance1 + " D2=" + distance2);

 

            if (previousSlowMa > previousFastMa && currentSlowMa <= currentFastMa && longPosition == null)

            {

                if (shortPosition != null)

                    ClosePosition(shortPosition);

                ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, label);

            }

            else if (previousSlowMa < previousFastMa && currentSlowMa >= currentFastMa && shortPosition == null)

            {

                if (longPosition != null)

                    ClosePosition(longPosition);

                ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, label);

            }

        }

    }

}

 

20 Jun 2017, 14:18#6
paymentposts: 15since: 14 Aug 2015

TRY THIS ONE I TESTED IT

 

----------------------------------


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

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

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

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

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend Robot";

        public double distance = 0;
        public double distance1 = 0;
        public double distance2 = 0;


        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);


            distance = Math.Round(Math.Abs((currentSlowMa - currentFastMa) / Symbol.PipSize));
            distance1 = Math.Round(Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));
            distance2 = Math.Round(Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));


            Print("DISTANCE BETWEEN MA=" + distance + "\tFAST=" + distance1 + "\tSLOW=" + distance2);

            //Print(currentSlowMa);
            //Print(currentFastMa);

        }
    }
}

20 Jun 2017, 14:20#7
paymentposts: 15since: 14 Aug 2015

Actually change the FAST to SLOW and SLOW TO FAST  then it's correct

 

Print("DISTANCE BETWEEN MA=" + distance + "\tSLOW=" + distance1 + "\tFAST=" + distance2);

 

 

Look under LOG when you run it and it will give you the distances every tick

20 Jun 2017, 14:21RE:#8
hiba7rainposts: 63since: 20 Jul 2014

Thanks Payment 

ill test it 

payment said:

TRY THIS ONE I TESTED IT

 

----------------------------------


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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

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

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

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

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend Robot";

        public double distance = 0;
        public double distance1 = 0;
        public double distance2 = 0;


        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);


            distance = Math.Round(Math.Abs((currentSlowMa - currentFastMa) / Symbol.PipSize));
            distance1 = Math.Round(Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));
            distance2 = Math.Round(Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));


            Print("DISTANCE BETWEEN MA=" + distance + "\tFAST=" + distance1 + "\tSLOW=" + distance2);

            //Print(currentSlowMa);
            //Print(currentFastMa);

        }
    }
}

 

20 Jun 2017, 14:24RE:#9
hiba7rainposts: 63since: 20 Jul 2014

how would i get also the distance between current price ant the moving avarege say for example the slow MA

payment said:

Actually change the FAST to SLOW and SLOW TO FAST  then it's correct

 

Print("DISTANCE BETWEEN MA=" + distance + "\tSLOW=" + distance1 + "\tFAST=" + distance2);

 

 

Look under LOG when you run it and it will give you the distances every tick

 

20 Jun 2017, 14:34#10
paymentposts: 15since: 14 Aug 2015

When you run this it gives you 3 values every tick

1. Distance between MA

2. Distance to SLOW MA

3. Distance to FAST MA

 

Just look under Log when you run it

 

--------------------

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

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class TrendRobot : Robot
    {
        [Parameter("MA Type")]
        public MovingAverageType MAType { get; set; }

        [Parameter()]
        public DataSeries SourceSeries { get; set; }

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

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

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

        private MovingAverage slowMa;
        private MovingAverage fastMa;
        private const string label = "Sample Trend Robot";

        public double distance = 0;
        public double distance1 = 0;
        public double distance2 = 0;


        protected override void OnStart()
        {
            fastMa = Indicators.MovingAverage(SourceSeries, FastPeriods, MAType);
            slowMa = Indicators.MovingAverage(SourceSeries, SlowPeriods, MAType);
        }

        protected override void OnTick()
        {
            var longPosition = Positions.Find(label, Symbol, TradeType.Buy);
            var shortPosition = Positions.Find(label, Symbol, TradeType.Sell);

            var currentSlowMa = slowMa.Result.Last(0);
            var currentFastMa = fastMa.Result.Last(0);
            var previousSlowMa = slowMa.Result.Last(1);
            var previousFastMa = fastMa.Result.Last(1);


            distance = Math.Round(Math.Abs((currentSlowMa - currentFastMa) / Symbol.PipSize));
            distance1 = Math.Round(Math.Abs((currentSlowMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));
            distance2 = Math.Round(Math.Abs((currentFastMa - MarketSeries.Close.Last(0)) / Symbol.PipSize));


            Print("DISTANCE BETWEEN MA=" + distance + "\tSLOW=" + distance1 + "\tFAST=" + distance2);



            //Print(currentSlowMa);
            //Print(currentFastMa);

        }
    }
}