Replies

thelightingfast9
07 Jun 2021, 19:00 ( Updated at: 07 Jun 2021, 19:02 )

Hello.

Thanks for your help.

I have another issue, where bot does not close positions as instructed. 

It opens position when RSI goes above set number, but it doesn't close the position when RSI falls below the set level. for an example, on closing BUY position it should not allow RSI fall below 55 without closing the position, and it does.. at some instances, RSI drops on the floor and bot does not close the positions, it's rather random closing.

I've tried looking over forum and internet but no help.

Could somebody help with this, and possibly let me know how to inser "hascrossedabove" and "hascrossedbelow" into this code, in order for RSI to be following it and working?

Thanks!

 

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

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

        [Parameter("Take_Profit", DefaultValue = 180, MinValue = 1)]
        public int TakeProfit { get; set; }

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

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

        private RelativeStrengthIndex rsi;

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


        protected override void OnTick()
        {
            if (rsi.Result.Last(1) <= 60)
            {
                Open(TradeType.Buy);
            }
            else if (rsi.Result.Last(0) >= 55)
            {
                Close(TradeType.Buy);
            }


            if (rsi.Result.Last(1) <= 40)
            {
                Open(TradeType.Sell);
            }
            else if (rsi.Result.Last(0) >= 45)
            {
                Close(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");
        }
    }
}


@thelightingfast9