Passing indicator values to bot

Created at 19 Jul 2017, 15:56
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!
HT

HTtrader

Joined 19.07.2017

Passing indicator values to bot
19 Jul 2017, 15:56


Hi there,

I am fairly new to ctrader and calgo. I have read the guide on calgo and bots section however running into a bit of a stumbling block now. I have basic coding experience and can comprehend c# well enough.

How exactly is one to code an entry level given by an indicayor? Say for example the pivot indicator gives defined levels, how do we pass those levels to a bot for automated trading? And say if we wanted to add a couple pips to account for spread would that just be a + or - command in the same order code? 

Also on the same topic, is there a way to cancel pending orders on red news given to us by a news indicator?

I have other questions I would like to ask, but that should get me started with what I want to get out of a bot.

 


@HTtrader
Replies

Spotware
19 Jul 2017, 16:26

Dear hungtonydang,

Thanks for posting your question in our forum. You can find how to reference a custom indicator in a cBot as well as a sample on how to get values from the indicator here

Let us know if you have any further questions.

Best Regards,


@Spotware

HTtrader
24 Jul 2017, 10:15

Thanks for the link, I have read that page already, I think I understand the logic. Is there a syntax library anywhere with definitions? As that would be really helpful with what I have in mind.
@HTtrader

Spotware
24 Jul 2017, 12:51

Dear hungtonydang,

There is no cAlgo specific syntax. The language used in C# and you can find a C# programming guide here. Documentation for cAlgo.API and its classes and interfaces can be found here. Let us know if you need any additional information.

Best Regards,

cTrader Team

 


@Spotware

HTtrader
09 Aug 2017, 00:41

After further interaction with the software and code, am I to understand that the indicator is to be called upon at start up of the calgo then the levels have to be calculated then we can say place a limit order at defined level? Can the last 2 be placed under the same function or do they have to be separate? It would also be good if you had a reference code in that forum that people could use. For example to place a limit or stop trade at the level determined by pivot point indicator or even simpler level of a Bollinger band.
@HTtrader

Spotware
09 Aug 2017, 09:53

Dear hungtonydang,

Currently there is no function that places an order based on the output of an indicator. You need to program this yourself.

Thanks for your suggestion for the example. We will be adding more examples in the Guide in the near future.

Best Regards,

cTrader Team


@Spotware

HTtrader
24 Aug 2017, 00:17

I am trying to use an indicator value as a Stop loss value however on backtesting the value of the SL looks like it is simply the spread. I have declared the variable is there something else that I am missing?

        protected override void OnBar()
        {
            var SL = BB.Main.LastValue;
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);
                    if (Positions.Count == 1)
                    {
                      Stop();                   
                    }

                    else if (Symbol.Ask > BollingBands.Top)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol, volumeInUnits, label, SL, TakeProfitInPips);
                    }

Any help would be appreciated.


@HTtrader

Spotware
24 Aug 2017, 11:46

Dear hungtonydang,

It would be easier for somebody to help you if you provide some additional information. If possible please provide us with the following

1) The complete code for the cBot. 

2) What values do you receive.

3) What would you expect to receive.

From what we can deduce from your code sample, you are using the value of the main line of a Bollinger Band. If this is the case, then it is not uncommon this value to be near the market price.

Best Regards,

cTrader Team


@Spotware

HTtrader
24 Aug 2017, 14:57

Hi Ctrader team,

I found my answer to that problem it has to do with absolute price compared with what the statement accepts. I have found a work around and will try that later.

But it brings me back to my original query of passing indicator values to a cbot.

If we can define absolute values as variables are they not able to be used as a limit or stop order entry price? As we can't use absolute values for stop loss and target profit is this the same? If not please point me in the direction of a sample as I am close to completing my cbot.

Thsnks,

Tony


@HTtrader

HTtrader
28 Aug 2017, 16:06

Hi ctraders,

Having other problems with this now, worked out the absolute price and pips problem but now my problem is accessing the indicator values. I have coded it properly and the build succeed but the crashes on startup. Upon looking at the code more closely I think it has to do with the indicator.

Is the right statement to use

var <indicator> = <indicator>.Result.LastValue;

Or does something inside the indicator need to be declared or calculated for it to have a value. 


@HTtrader

Spotware
28 Aug 2017, 16:14

Dear Tony,

Can you please post a cBot or at least a working section of the code which we can use to reproduce the problem, so that we can advise accordingly?

Best Regards,

cTrader Team


@Spotware

HTtrader
29 Aug 2017, 14:08

ok here is the code I have so far, build succeeds but not behaving as it should

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.GMTStandardTime, AccessRights = AccessRights.None)]
    public class HunterBBstop : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }
// declaration of symbol
        [Parameter("Quantity (Lots)", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }
// declaration of how much to trade
        [Parameter("Stop Loss (pips)", DefaultValue = 20, MinValue = 1)]
        public int StopLossInPips { get; set; }
// declaration of loss amount
        [Parameter("Take Profit (pips)", DefaultValue = 20, MinValue = 1)]
        public int TakeProfitInPips { get; set; }
// declaration of profit amount
        [Parameter(DefaultValue = 1.5)]
        public double StDeviation { get; set; }
// declaration of indicator value
        [Parameter(DefaultValue = 15)]
        public int Period { get; set; }
// declaration of indicator value
        [Parameter("MA Type", DefaultValue = MovingAverageType.Simple)]
        public MovingAverageType MAType { get; set; }
// declaration of indicator value
        [Output("Top", PlotType = PlotType.Points, Color = Colors.Red, Thickness = 4)]
        public IndicatorDataSeries Top { get; set; }
// declaration of indicator value
        [Output("Bottom", PlotType = PlotType.Points, Color = Colors.Green, Thickness = 4)]
        public IndicatorDataSeries Bottom { get; set; }
// declaration of indicator value
        BBandStopLine BBandStopLine;
        string label = "HunterBBstop";
        public Position position;
// declaration of trade parameters
        protected override void OnStart()
        {
            BBandStopLine = Indicators.GetIndicator<BBandStopLine>(Source, StDeviation, Period, MAType);
        }
// referencing custom indicator
        protected override void OnBar()
        {
            var Top = BBandStopLine.Top.Last(0);
            //get last value of custom indicator
            var Bottom = BBandStopLine.Bottom.Last(0);
            //get last value of custom indicator
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);
            //int Expiration = 1;
            var datetime = DateTime.Now.AddHours(1);
            //Order expiration of 1 hour only
            {
                //to allow no more than 3 active position at any given time
                if (Positions.Count == 3)
                {
                    // if price is below the top indicator then execute stop order with entry value as per indicator
                    if (Symbol.Ask > Top)
                    {
                        PlaceStopOrder(TradeType.Sell, Symbol, volumeInUnits, Top, label, StopLossInPips, TakeProfitInPips, datetime);
                        //order should have expiry of 1 hour to go with indicator
                    }
                    // if price is above the bottom indicator then execute stop order with entry as per indicator
                    else if (Symbol.Bid < Bottom)
                    {
                        PlaceStopOrder(TradeType.Buy, Symbol, volumeInUnits, Bottom, label, StopLossInPips, TakeProfitInPips, datetime);
                        //order should have expiry of 1 hour to go with indicator
                    }
                }
            }

        }
        protected override void OnStop()
        {
            Stop();
        }
    }
}

and here is the indicator that is associated with the file

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, AccessRights = AccessRights.None)]
    public class BBandStopLine : Indicator
    {
        [Parameter()]
        public DataSeries Source { get; set; }

        [Parameter(DefaultValue = 1.5)]
        public double StDeviation { get; set; }

        [Parameter(DefaultValue = 15)]
        public int Period { get; set; }

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

        [Output("Top", PlotType = PlotType.Points, Color = Colors.Red, Thickness = 4)]
        public IndicatorDataSeries Top { get; set; }

        [Output("Bottom", PlotType = PlotType.Points, Color = Colors.Green, Thickness = 4)]
        public IndicatorDataSeries Bottom { get; set; }

        private BollingerBands _bband;
        private int _flag;

        protected override void Initialize()
        {
            _bband = Indicators.BollingerBands(Source, Period, StDeviation, MAType);
        }

        public override void Calculate(int index)
        {
            Top[index] = _bband.Top[index];
            Bottom[index] = _bband.Bottom[index];


            if (MarketSeries.Close[index] > _bband.Top[index])
                _flag = 1;
            else if (MarketSeries.Close[index] < _bband.Bottom[index])
                _flag = -1;

            if (_flag == 1)
            {
                if (_bband.Bottom[index] < Bottom[index - 1])
                    Bottom[index] = Bottom[index - 1];
                Top[index] = double.NaN;
            }
            else if (_flag == -1)
            {
                if (_bband.Top[index] > Top[index - 1])
                    Top[index] = Top[index - 1];
                Bottom[index] = double.NaN;
            }
        }
    }
}

the indicator works fine and as it should, just trying to get the values as absolute price to be used in my code is proving the problem at the moment.


@HTtrader