Using Candle or Bar to exist

Created at 25 Jan 2017, 18:54
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!
EM

emmamail

Joined 21.03.2012

Using Candle or Bar to exist
25 Jan 2017, 18:54


Hello CTDN team,

How can i use   Bar(s) to exist or close a position ?

Let assume i want to close a position on three price bars .

Email : emmalleres@gmail.com

Thanks.

 

Emmanuel Armah.


@emmamail
Replies

... Deleted by UFO ...

emmamail
26 Jan 2017, 18:41

RE:

lucian said:

I am sorry, but I don't understand what do you want exactly by " three price bars"

You can use OnBar() by example:

 

 protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 1;
            if (MarketSeries.Close[index - 1] > MarketSeries.Close[index - 2])
            {
                foreach (var position in Positions)
                    ClosePosition(position);
            }
        }

 

Hi Lucian,

 

What i mean is to close my open position on three candles.

 

thanks.

 

emmanuel.


@emmamail

... Deleted by UFO ...

emmamail
31 Jan 2017, 18:38

thanks

Hi Lucian,

 

Thanks for your assistance .. i will add your code and revert to you.

 

Thanks once again.

 

Emmanuel


@emmamail

emmamail
01 Feb 2017, 12:15

hi

Hello Lucia,

 

I want assistance if  you can assist with adding  the three bar exit to the below RSI mean reverting strategy;

 

// -------------------------------------------------------------------------------------------------
//
//    This code is a cAlgo API sample.
//
//    This robot is intended to be used as a sample and does not guarantee any particular outcome or
//    profit of any kind. Use it at your own risk.
//
//    All changes to this file will be lost on next application start.
//    If you are going to modify this file please make a copy using the "Duplicate" command.
//
//    The "Sample RSI Range Robot" will create a buy order when the Relative Strength Index indicator crosses the  level 30, 
//    and a Sell order when the RSI indicator crosses the level 70. The order is closed be either a Stop Loss, defined in 
//    the "Stop Loss" parameter, or by the opposite RSI crossing signal (buy orders close when RSI crosses the 70 level 
//    and sell orders are closed when RSI crosses the 30 level). 
//
//    The robot can generate only one Buy or Sell order at any given time.
//
// -------------------------------------------------------------------------------------------------
 
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 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");
        }
    }
}

 


@emmamail