Replies

komposter
06 Mar 2019, 10:43

RE:

Panagiotis Charalampous said:

Hi Andrii,

Can you provide the full cBot code please? e.g. what is signal?

Best Regards,

Panagiotis

Here is it:

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

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

        [Parameter("Lots", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
        public double Quantity { get; set; }

        [Parameter("SL (in renko)", DefaultValue = 2, MinValue = 1, Step = 0.1)]
        public double StopLoss { get; set; }

        [Parameter("TP (in renko)", DefaultValue = 4, MinValue = 1, Step = 0.1)]
        public double TakeProfit { get; set; }

        private RelativeStrengthIndex rsi;
        private int signal = 0;

        protected override void OnStart()
        {
            rsi = Indicators.RelativeStrengthIndex(Source, Periods);
            Print("The current symbol has pip size of: {0}, renko size = {1}", Symbol.PipSize, getRenkoSize());
        }

        protected override void OnTick()
        {
            if (signal > 0)
            {
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (signal < 0)
            {
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

        protected override void OnBar()
        {
            CalcSignal();

            double o = MarketSeries.Open.Last(0);
            double c = MarketSeries.Close.Last(0);
            if (c > o)
                Print("Bullish bar: {0} -> {1}, signal = {2}, rsi[0] = {3}, rsi[1] = {4}", o, c, signal, rsi.Result.LastValue, rsi.Result.Last(1));
            else if (c < o)
                Print("Bearish bar: {0} -> {1}, signal = {2}, rsi[0] = {3}, rsi[1] = {4}", o, c, signal, rsi.Result.LastValue, rsi.Result.Last(1));
            else
                Print("Doj bar: {0} -> {1}, signal = {2}, rsi[0] = {3}, rsi[1] = {4}", o, c, signal, rsi.Result.LastValue, rsi.Result.Last(1));
        }

        private void CalcSignal()
        {
            signal = 0;
            if (rsi.Result.HasCrossedAbove(40, 0))
            {
                signal = 1;
                Close(TradeType.Sell);
                Open(TradeType.Buy);
            }
            else if (rsi.Result.HasCrossedBelow(60, 0))
            {
                signal = -1;
                Close(TradeType.Buy);
                Open(TradeType.Sell);
            }
        }

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

        private void Open(TradeType tradeType)
        {
            var position = Positions.Find("Renko Test", Symbol, tradeType);
            var volumeInUnits = Symbol.QuantityToVolume(Quantity);
            var SL = Math.Round(getRenkoSize() * StopLoss, 1);
            var TP = Math.Round(getRenkoSize() * TakeProfit, 1);

            if (position == null)
                ExecuteMarketOrder(tradeType, Symbol, volumeInUnits, "Renko Test", SL, TP);
        }

        private double getRenkoSize()
        {
            return (Math.Round(Math.Abs(MarketSeries.Open.Last(1) - MarketSeries.Close.Last(1)) / Symbol.PipSize, 0));
        }
    }
}

 


@komposter