Indicator in the robot

Created at 16 Jul 2013, 22:03
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!
IR

iRobot

Joined 17.02.2013

Indicator in the robot
16 Jul 2013, 22:03


Hi.

How to implement Price Channel indicator in the robot, so I could set up length of periods, use it as stop loss and see indicator overlayed on the chart?

Thanks


@iRobot
Replies

atrader
24 Jul 2013, 16:11

RE:
iRobot said:

Hi.

How to implement Price Channel indicator in the robot, so I could set up length of periods, use it as stop loss and see indicator overlayed on the chart?

Thanks

this is a robot that uses the price channels, but it is missing a lot of functionality since you have not described the trading logic.
You can use this as a starting point or you can share the rest of the trading logic, e.g. how are trading signals triggered, based on price channels as well? How many positions does the robot open simultaneously, etc.

This robot starts a trade based on an input parameter (Buy) and volume.

The stop loss is calculated based on the channels. There is Take profit as input parameter.

//#reference: ..\Indicators\PriceChannels.algo
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using cAlgo.API;
using cAlgo.API.Requests;
using cAlgo.Indicators;

namespace cAlgo.Robots
{
    [Robot]
    public class PriceChannelRobot:Robot
    {
        private PriceChannels _priceChannels;

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

        [Parameter(DefaultValue = "PriceChannelRobot")]
        public string LabelName { get; set; }

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

        [Parameter(DefaultValue = true)]
        public bool Buy { get; set; }
        
        [Parameter(DefaultValue = 10, MinValue = 1)]
        public int TakeProfit { get; set; }

        protected double StopLossPrice
        {
            get { return TradeCommand == TradeType.Buy ? LowValue : HighValue; }
        }
        protected double HighValue
        {
            get { return _priceChannels.HiChannel.LastValue; }
        }

        protected double LowValue
        {
            get { return _priceChannels.LowChannel.LastValue; }
        }
        
        protected override void OnStart()
        {
            _priceChannels = Indicators.GetIndicator<PriceChannels>(Period);
            
        }
        
        protected TradeType TradeCommand
        {
            get { return Buy ? TradeType.Buy : TradeType.Sell; }
        }


        protected override void OnBar()
        {
            // Your Trade condition
            bool tradeCondition = Account.Positions.Count(pos => pos.Label == LabelName) == 0;
            
            if (tradeCondition)
                ExecuteOrder(Volume, TradeCommand);
        }

        private void ExecuteOrder(int volume, TradeType tradeType)
        {
            var request = new MarketOrderRequest(tradeType, volume)
            {
                Label = LabelName,
                StopLossPips = (int?) (Math.Abs(StopLossPrice - Symbol.Bid)/Symbol.PipSize),
                TakeProfitPips = TakeProfit
            };

            Trade.Send(request);

        }
    }
}



 

 


@atrader

iRobot
25 Jul 2013, 08:38

Great! Thanks for the help. For starting that will be ok.


@iRobot

iRobot
25 Jul 2013, 13:44

Although there is one thing you could me me a bit more.

I want to use price channels for stop loss in some cases. Let's say long position is opened and I want to fix lower band of price channel as a stop loss. If position is profitable, I want to change from "fixed" value of stop loss to a trailing stop.

 

Thanks.


@iRobot

atrader
25 Jul 2013, 14:55

RE:
iRobot said:

Although there is one thing you could me me a bit more.

I want to use price channels for stop loss in some cases. Let's say long position is opened and I want to fix lower band of price channel as a stop loss. If position is profitable, I want to change from "fixed" value of stop loss to a trailing stop.

 

Thanks.

Hi, you can use the code from the cAlgo Sample Trailing.


@atrader

iRobot
26 Jul 2013, 08:42

But how to "fix" the stop loss value when position is opened?


@iRobot

cAlgo_Fanatic
26 Jul 2013, 11:55

When the position is opened you can set the stop loss in pips. 

var request = new MarketOrderRequest(tradeType, volume)
                    {
                        Label = LabelName,
                        StopLossPips = StopLoss // This is a fixed value when the position is opened.
                        TakeProfitPips = TakeProfit
                    };

 

1. Create a method for trailing stop

2. Add a condition that checks if the position is profitable. You may check this in the OnBar or the OnTick events.

If the position is profitable call the method that implements trailing stop


@cAlgo_Fanatic

iRobot
31 Jul 2013, 23:13

Guys, sorry for stupid questions... 

However, could you give suggestions on:

1) in a given robot, where to put other conditions for closing a profitable position (take profit, but not on specified pip size, but rather on conditions ment by other indicators);

2) how to make just one position opened at the time (now it opens new postions despite the number of curent positions).

 

Thank you very much.


@iRobot

iRobot
04 Aug 2013, 12:08

Did it another way.


@iRobot

iRobot
05 Aug 2013, 21:42

Although I have another question in this issue: how can I index price channel values? Let's say I need to have its values for for 3 periods in the past = should I use indexing or "foreach"?


@iRobot

cAlgo_Fanatic
06 Aug 2013, 11:33

RE:
iRobot said:

Guys, sorry for stupid questions... 

However, could you give suggestions on:

1) in a given robot, where to put other conditions for closing a profitable position (take profit, but not on specified pip size, but rather on conditions ment by other indicators);

2) how to make just one position opened at the time (now it opens new postions despite the number of curent positions).

 

Thank you very much.

1) The conditions may be in the OnTick event or in another method that is being called in the OnTick. See for instance the Sample Trend Robot in cAlgo.

2) Use a global field that is initialized in the OnPositionOpened event. There are examples in the samples included in cAlgo, e.g. Sample Trend Robot, Sample SAR Trailing Stop (the global field is "position").


@cAlgo_Fanatic

cAlgo_Fanatic
06 Aug 2013, 11:40

RE:
iRobot said:

Although I have another question in this issue: how can I index price channel values? Let's say I need to have its values for for 3 periods in the past = should I use indexing or "foreach"?

You can use either one. The foreach statement traverses the whole list so you would need to add a break statement to stop once it reaches the last element you want to check.

The for loop uses indexing and you can specify how many elements it will traverse. 

See also this c# loops tutorial.


@cAlgo_Fanatic