Recall calcuate() on regular basis

Created at 06 Feb 2018, 10:15
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!
itmfar's avatar

itmfar

Joined 08.11.2017

Recall calcuate() on regular basis
06 Feb 2018, 10:15


I need to recall my main method wich is calculate() on regular basis .Although I know that this method is called by each index, due to some interrupts and miss calcuation ,I demand to call it every minute or every candle. How it would be possible to refresh old calculations 


@itmfar
Replies

PanagiotisCharalampous
06 Feb 2018, 11:36

Hi itmfar,

Calculate() is called on each tick. Therefore pretty often on major pairs. You could have timer but I don't think this would be a good idea. It would be better to tell us what the problem is so that we could propose a more proper solution.

Best Regards,

Panagiotis


@PanagiotisCharalampous

itmfar
06 Feb 2018, 20:28

RE:

Panagiotis Charalampous said:

Hi itmfar,

Calculate() is called on each tick. Therefore pretty often on major pairs. You could have timer but I don't think this would be a good idea. It would be better to tell us what the problem is so that we could propose a more proper solution.

Best Regards,

Panagiotis

My code for example in 1 minute timeframe, after 1 hour generates some signals but when you refresh the chart some of them will be lost and some of them will be added due to some interruptions. So I need to refresh the chart on each candle to address this problem. Or we can cancalculate once exactly when a candle is closing

using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
using System.Collections.Generic;

namespace cAlgo
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class testRefresh : Indicator
    {

        [Parameter("Source")]
        public DataSeries Source { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }
        private StochasticOscillator _stochastic;
        private List<bool> ssdRising = new List<bool>();

        protected override void Initialize()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        public override void Calculate(int index)
        {
            ssdRising.Add(_stochastic.PercentD.IsRising());
            FindSSCrossovers(index);
            DrawLinesSSD(index);
        }

        private void FindSSCrossovers(int index)
        {

            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {

                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {

                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (ssdRising[index - 1] != ssdRising[index] && _stochastic.PercentD[index] > 60 && ssdRising[index] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);

            }
            else if (ssdRising[index - 1] != ssdRising[index] && _stochastic.PercentD[index] < 40 && ssdRising[index] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);

            }
        }
    }
}
 

 


@itmfar

PanagiotisCharalampous
07 Feb 2018, 11:24

Hi itmfar,

Thanks for posting the indicator. I used your indicator on a minute chart for some time but did not experience any problem. It would be helpful to be able to reproduce the problem so that I can see it and see how I can help you.

Best Regards,

Panagiotis


@PanagiotisCharalampous

Jiri
07 Feb 2018, 14:48

Hi itmfar,

The Calculate() method is called on each bar when processing historical data and on each incoming tick when processing real-time data. You should be adding a new element to the list only on a new bar. Try this:

var lastIndex = ssdRising.Count - 1;

if (index > lastIndex)
{
    ssdRising.Add(_stochastic.PercentD.IsRising());
}
else
{
    ssdRising[lastIndex] = _stochastic.PercentD.IsRising();
}

Also, you should remove chart objects if the conditions are not met. Otherwise, it might remain there from previous tick before a bar is closed and show false results.


@Jiri

itmfar
08 Feb 2018, 16:29

RE:

Panagiotis Charalampous said:

Hi itmfar,

Thanks for posting the indicator. I used your indicator on a minute chart for some time but did not experience any problem. It would be helpful to be able to reproduce the problem so that I can see it and see how I can help you.

Best Regards,

Panagiotis

Hi Panagotis

I still have my problem. how we can refresh or restart the indicator on regular basis? 

 


@itmfar

itmfar
08 Feb 2018, 16:33

RE:

tmc. said:

Hi itmfar,

The Calculate() method is called on each bar when processing historical data and on each incoming tick when processing real-time data. You should be adding a new element to the list only on a new bar. Try this:

var lastIndex = ssdRising.Count - 1;

if (index > lastIndex)
{
    ssdRising.Add(_stochastic.PercentD.IsRising());
}
else
{
    ssdRising[lastIndex] = _stochastic.PercentD.IsRising();
}

Also, you should remove chart objects if the conditions are not met. Otherwise, it might remain there from previous tick before a bar is closed and show false results.

Hi tmc
​Thanks for solution but it still does not work. if I remove all object how i can reproduce them for pervious calculations?


@itmfar

PanagiotisCharalampous
08 Feb 2018, 16:45

Hi itmfar,

I don't know what to suggest since I cannot reproduce the problem and understand what is going wrong. Could you share a short video demonstrating the problem? Also, I don't understand how you will solve the problem by calling Calculate on a more frequent basis. Calculate is called on every tick therefore frequently enough. Do you mean redrawing the entire indicator instead? Even if this is what you are looking for, it is not a proper solution. It would be easier to understand how the issue is caused and apply a proper solution instead of forcing a brute force redrawing of the indicator.

Best Regards

Panagiotis


@PanagiotisCharalampous

itmfar
08 Feb 2018, 17:29 ( Updated at: 21 Dec 2023, 09:20 )

RE:

Panagiotis Charalampous said:

Hi itmfar,

I don't know what to suggest since I cannot reproduce the problem and understand what is going wrong. Could you share a short video demonstrating the problem? Also, I don't understand how you will solve the problem by calling Calculate on a more frequent basis. Calculate is called on every tick therefore frequently enough. Do you mean redrawing the entire indicator instead? Even if this is what you are looking for, it is not a proper solution. It would be easier to understand how the issue is caused and apply a proper solution instead of forcing a brute force redrawing of the indicator.

Best Regards

Panagiotis

I captured two print-screens one before refresh the other after 

it is obvious that after refresh only D-sing will be shown


@itmfar

itmfar
08 Feb 2018, 17:31

RE:

Panagiotis Charalampous said:

Hi itmfar,

I don't know what to suggest since I cannot reproduce the problem and understand what is going wrong. Could you share a short video demonstrating the problem? Also, I don't understand how you will solve the problem by calling Calculate on a more frequent basis. Calculate is called on every tick therefore frequently enough. Do you mean redrawing the entire indicator instead? Even if this is what you are looking for, it is not a proper solution. It would be easier to understand how the issue is caused and apply a proper solution instead of forcing a brute force redrawing of the indicator.

Best Regards

Panagiotis

with every refresh some D will be added and S will be gone


@itmfar

Jiri
08 Feb 2018, 21:58

Works on my end.

using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

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

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }

        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();

        protected override void Initialize()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        public override void Calculate(int index)
        {
            var lastIndex = _ssdRising.Count - 1;
            if (lastIndex < index)
            {
                _ssdRising.Add(_stochastic.PercentD.IsRising());
            }
            else
            {
                _ssdRising[lastIndex] = _stochastic.PercentD.IsRising();
            }

            FindSSCrossovers(index);
            DrawLinesSSD(index);
        }

        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[index - 1] != _ssdRising[index] && _stochastic.PercentD[index] > 60 && _ssdRising[index] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[index - 1] != _ssdRising[index] && _stochastic.PercentD[index] < 40 && _ssdRising[index] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
    }
}

 


@Jiri

itmfar
09 Feb 2018, 10:37

RE:

tmc. said:

Works on my end.

using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

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

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }

        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();

        protected override void Initialize()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        public override void Calculate(int index)
        {
            var lastIndex = _ssdRising.Count - 1;
            if (lastIndex < index)
            {
                _ssdRising.Add(_stochastic.PercentD.IsRising());
            }
            else
            {
                _ssdRising[lastIndex] = _stochastic.PercentD.IsRising();
            }

            FindSSCrossovers(index);
            DrawLinesSSD(index);
        }

        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[index - 1] != _ssdRising[index] && _stochastic.PercentD[index] > 60 && _ssdRising[index] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[index - 1] != _ssdRising[index] && _stochastic.PercentD[index] < 40 && _ssdRising[index] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
    }
}

you are genius . thank you so much. it really saves my time :)

 


@itmfar

itmfar
09 Feb 2018, 14:01

RE:

tmc. said:

Works on my end.

using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;

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

        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }

        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();

        protected override void Initialize()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        public override void Calculate(int index)
        {
            var lastIndex = _ssdRising.Count - 1;
            if (lastIndex < index)
            {
                _ssdRising.Add(_stochastic.PercentD.IsRising());
            }
            else
            {
                _ssdRising[lastIndex] = _stochastic.PercentD.IsRising();
            }

            FindSSCrossovers(index);
            DrawLinesSSD(index);
        }

        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[index - 1] != _ssdRising[index] && _stochastic.PercentD[index] > 60 && _ssdRising[index] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[index - 1] != _ssdRising[index] && _stochastic.PercentD[index] < 40 && _ssdRising[index] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
    }
}

is there any way to convert or use indicator as a bot? for example when D-sign pops up a boolean whould be set true, and a result of that bool, we exacute ExecteMarketOrder(TradeType.Sell, Symbol, 20, "lalbelmax", 120, 200);

 


@itmfar

PanagiotisCharalampous
09 Feb 2018, 14:34

Hi itmfar,

See below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }
        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        protected override void OnStart()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        protected override void OnBar()
        {

            _ssdRising.Add(_stochastic.PercentD.IsRising());
            if (_ssdRising.Count > 1)
            {
                FindSSCrossovers(Source.Count - 1);
                DrawLinesSSD(Source.Count - 1);
            }
        }
        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] > 60 && _ssdRising[_ssdRising.Count - 1] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] < 40 && _ssdRising[_ssdRising.Count - 1] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this is working for you,

Best Regards,

Panagiotis


@PanagiotisCharalampous

Jiri
09 Feb 2018, 15:35

RE: RE:

itmfar said:

you are genius . thank you so much. it really saves my time :)

You're welcome.


@Jiri

itmfar
11 Feb 2018, 12:05

RE:

Panagiotis Charalampous said:

Hi itmfar,

See below

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }
        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        protected override void OnStart()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        protected override void OnBar()
        {

            _ssdRising.Add(_stochastic.PercentD.IsRising());
            if (_ssdRising.Count > 1)
            {
                FindSSCrossovers(Source.Count - 1);
                DrawLinesSSD(Source.Count - 1);
            }
        }
        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] > 60 && _ssdRising[_ssdRising.Count - 1] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] < 40 && _ssdRising[_ssdRising.Count - 1] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this is working for you,

Best Regards,

Panagiotis

Thank you Panagiotis, it is really helpful and it works for D-sign . but when buys or sells are excecuted in S conditions it repeats numerous times.


@itmfar

PanagiotisCharalampous
12 Feb 2018, 11:49

Hi itmfar,

I was with the impression that you wanted to place orders only on the D sign, Here it is for S sign as well

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }
        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        protected override void OnStart()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        protected override void OnBar()
        {

            _ssdRising.Add(_stochastic.PercentD.IsRising());
            if (_ssdRising.Count > 1)
            {
                FindSSCrossovers(Source.Count - 1);
                DrawLinesSSD(Source.Count - 1);
            }
        }
        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] > 60 && _ssdRising[_ssdRising.Count - 1] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] < 40 && _ssdRising[_ssdRising.Count - 1] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this works.

Best Regards,

Panagiotis


@PanagiotisCharalampous

itmfar
16 Feb 2018, 14:33

RE:

Panagiotis Charalampous said:

Hi itmfar,

I was with the impression that you wanted to place orders only on the D sign, Here it is for S sign as well

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;
namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 0.0)]
        public double Parameter { get; set; }
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType maType { get; set; }
        private StochasticOscillator _stochastic;
        private readonly List<bool> _ssdRising = new List<bool>();
        [Parameter("Source")]
        public DataSeries Source { get; set; }
        protected override void OnStart()
        {
            _stochastic = Indicators.StochasticOscillator(9, 3, 9, maType);
        }

        protected override void OnBar()
        {

            _ssdRising.Add(_stochastic.PercentD.IsRising());
            if (_ssdRising.Count > 1)
            {
                FindSSCrossovers(Source.Count - 1);
                DrawLinesSSD(Source.Count - 1);
            }
        }
        private void FindSSCrossovers(int index)
        {
            if (_stochastic.PercentD.HasCrossedAbove(_stochastic.PercentK, 0))
            {
                ChartObjects.DrawText("MAXss1" + index, "S⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.Yellow);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("MAXss1" + index);
            }

            if (_stochastic.PercentK.HasCrossedAbove(_stochastic.PercentD, 0))
            {
                ChartObjects.DrawText("Minss1" + index, "S⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.Yellow);
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("Minss1" + index);
            }
        }

        private void DrawLinesSSD(int index)
        {
            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] > 60 && _ssdRising[_ssdRising.Count - 1] == false)
            {
                ChartObjects.DrawText("maxrssd" + index, "D⮟", index, Source[index], VerticalAlignment.Top, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Sell, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("maxrssd" + index);
            }

            if (_ssdRising[_ssdRising.Count - 2] != _ssdRising[_ssdRising.Count - 1] && _stochastic.PercentD[index] < 40 && _ssdRising[_ssdRising.Count - 1] == true)
            {
                ChartObjects.DrawText("minrssd" + index, "D⮝", index, Source[index], VerticalAlignment.Bottom, HorizontalAlignment.Center, Colors.White);
                ExecuteMarketOrder(TradeType.Buy, Symbol, 1000, "lalbelmax", 120, 200);
            }
            else
            {
                ChartObjects.RemoveObject("minrssd" + index);
            }
        }
        protected override void OnStop()
        {
            // Put your deinitialization logic here
        }
    }
}

Let me know if this works.

Best Regards,

Panagiotis
 

thanks panagiotis

 


@itmfar