30 min time frame

Created at 16 Jun 2016, 23:37
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!
KA

kaliszczak1991

Joined 16.06.2016

30 min time frame
16 Jun 2016, 23:37


Hello,

How to set this robot to open/close positions only at "close of candle". Because now its open position each tick if parameters are positive. I want to set this robot like: check parameters each "candle close" and if its possitive, then open/ close opened position.

Could you help me with it?

Rsi range robot:

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 RSIRangeRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Stop Loss (pips)", DefaultValue = 10, MinValue = 1)]
        public int StopLoss { get; set; }

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

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnTick()
        {
            if (rsi.Result.LastValue < 30)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue > 70)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", Symbol, tradeType);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "SampleRSI");
        }
    }
}


@kaliszczak1991
Replies

whis.gg
16 Jun 2016, 23:45

Hi, change OnTick() to OnBar() which is called on each incoming bar.


@whis.gg

kaliszczak1991
16 Jun 2016, 23:54

Thanks TMC :)

 

That was sooo easy


@kaliszczak1991

kaliszczak1991
17 Jun 2016, 00:34

Can you help me with one another think?

Could you tell me what is wrong? Because robot only open Sell positions, why its not open Buy position?

 

 

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 SampleRSIRobot : Robot
    {
        [Parameter("Source")]
        public DataSeries Source { get; set; }

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

        [Parameter("Stop Loss (pips)", DefaultValue = 10, MinValue = 1)]
        public int StopLoss { get; set; }

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

        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
        }

        protected override void OnBar()
        {
            if (rsi.Result.LastValue > 70)
            {
                Open(TradeType.Buy);
            }
            else if (rsi.Result.LastValue < 30)
            {
                Open(TradeType.Sell);
            }
            else if (rsi.Result.LastValue > 30)
            {
                Close(TradeType.Sell);
            }
            else if (rsi.Result.LastValue < 70)
            {
                Close(TradeType.Buy);
            }
        }

        private void Close(TradeType tradeType)
        {
            foreach (var position in Positions.FindAll("SampleRSI", Symbol, tradeType))
                ClosePosition(position);
        }

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("SampleRSI", Symbol, tradeType);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, Volume, "SampleRSI");
        }
    }
}


@kaliszczak1991

whis.gg
17 Jun 2016, 00:48

RE:

kaliszczak1991 said:

if (rsi.Result.LastValue > 70)
{
    Open(TradeType.Buy);
}
else if (rsi.Result.LastValue < 30)
{
    Open(TradeType.Sell);
}
else if (rsi.Result.LastValue > 30)
{
    Close(TradeType.Sell);
}
else if (rsi.Result.LastValue < 70)
{
    Close(TradeType.Buy);
}

 

Because you are using else if everywhere it never passes to the condition else if (rsi.Result.Last < 70) so there is still opened your first long position.

var position = Positions.Find("SampleRSI", Symbol, tradeType);

if (position == null)    // position contains long position which will never be closed
    ExecuteMarketOrder(tradeType, Symbol, Volume, "SampleRSI");

Because of that bot can't open another long.

 

Change else if just to if and it will be fine.


@whis.gg

kaliszczak1991
18 Jun 2016, 05:58

Great, that was that :) Thank you very much :)

But I have last question.
Is there an option to set that robot to works only in some hours, lets say during London session?

I download all the robots avaible, and I couldn't find this option. 

Could you help me with it?

I will be gratefull :)


@kaliszczak1991

whis.gg
18 Jun 2016, 17:23

RE:

kaliszczak1991 said:

Is there an option to set that robot to works only in some hours, lets say during London session?

Have a look at this thread: /forum/calgo-reference-samples/543


@whis.gg

1007601
02 Sep 2016, 16:01

RE: RE:

tmc. said:

kaliszczak1991 said:

Is there an option to set that robot to works only in some hours, lets say during London session?

Have a look at this thread: /forum/calgo-reference-samples/543

tmc, I'd like to "thumbs up" all of your posts -- thanks from everyone here!


@1007601