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

Created at 18 Jun 2017, 14:34
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!
HI

hiba7rain

Joined 20.07.2014

how to get the distance between two MA in (pips) and current price distance from MA
18 Jun 2017, 14:34


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 


@hiba7rain
Replies

hiba7rain
19 Jun 2017, 12:31

any support is much appriciated 

Thanks 


@hiba7rain

payment
20 Jun 2017, 13:07

You can use this code to start with

/algos/cbots/show/8

 

 


@payment

payment
20 Jun 2017, 13:18

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);
            }
        }
    }
}

@payment

hiba7rain
20 Jun 2017, 13:40

RE:

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);

            }

        }

    }

}

 


@hiba7rain

payment
20 Jun 2017, 14:18

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);

        }
    }
}


@payment

payment
20 Jun 2017, 14:20

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


@payment

hiba7rain
20 Jun 2017, 14:21

RE:

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);

        }
    }
}

 


@hiba7rain

hiba7rain
20 Jun 2017, 14:24

RE:

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

 


@hiba7rain

payment
20 Jun 2017, 14:34

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);

        }
    }
}


@payment

hiba7rain
20 Jun 2017, 15:34

RE:

Thanks payment 

it was nice of you 

now i will see if i can make it as drawn object on the chart 

payment said:

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);

        }
    }
}

 


@hiba7rain

hiba7rain
08 Aug 2018, 10:01

it would be appreciated if someone can add the draw. Text code to this so that it presents the value of differences on the chart 


@hiba7rain