IchimokuKinkoHyo

Created at 27 Oct 2016, 00: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!
VA

varunthakur.outlook

Joined 27.10.2016

IchimokuKinkoHyo
27 Oct 2016, 00:54


Hi

Since I cannot see the code behind in-built IchimokuKinkoHyo indicator in cAlgo, I would like to know how to access the values of Tenkan, Chinkou, etc for the last market series closed bar, and not their last values.

Thank you

Cheers


@varunthakur.outlook
Replies

... Deleted by UFO ...

... Deleted by UFO ...

varunthakur.outlook
01 Nov 2016, 10:58

Thank you

First of all thank you, lucian.

Below is my code, it does work; however, see if you can suggest any improvements.

 

//#reference: ..\Indicators\HeikenAshi.algo

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class HAichimoku : Robot
    {


        [Parameter("Buy", DefaultValue = true)]
        public bool Buy { get; set; }

        [Parameter("Sell", DefaultValue = true)]
        public bool Sell { get; set; }


        [Parameter("First Volume", DefaultValue = 1, MinValue = 1, Step = 1)]
        public int FirstVolume { get; set; }


        [Parameter("Max Spread", DefaultValue = 3)]
        public double MaxSpread { get; set; }

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


        [Parameter("SL", DefaultValue = 0, MinValue = 0)]
        public int SL { get; set; }

        [Parameter("MaxPipsSlippage", DefaultValue = 3, MinValue = 1)]
        public int MPS { get; set; }




        //[Parameter("MaxNegativeGrossProfit", DefaultValue = 100, MinValue = 100)]
        //public double MaxNegativeGrProfit { get; set; }



        private bool LossMoreThanUserWants;



        //private string Label = Convert.ToString(Symbol.Code);
        //private Position position; //to check position by position

        private double sp_d;



        private HeikenAshi HAJ;
        private int index;



        //--------------------------------------------------------------------------------------
        private IchimokuKinkoHyo _ichi;
        private bool ichiHAJSell = false;
        private bool ichiHAJBuy = false;



        //------------------------------------------------------------------------------------------


        //private int takeProfitInPips => Math.Round(TP / Symbol.PipSize, 1);

        protected override void OnStart()
        {

            HAJ = Indicators.GetIndicator<HeikenAshi>(1);

            _ichi = Indicators.IchimokuKinkoHyo(9, 26, 52);

        }


//---------------------------------------------------------------------------------------------------------------------------------------------------------



//---------------------------------------------------------------------------------------------------------------------------------------------------------------




        protected override void OnError(Error error)
        {
            if (error.Code == ErrorCode.NoMoney)
            {

                Print("openning stopped because: not enough money");
            }
        }

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



        protected override void OnBar()
        {

            index = MarketSeries.Close.Count - 2;

            double HAJCloseValue = HAJ.xClose[index];
            double HAJOpenValue = HAJ.xOpen[index];
            double HAJLowValue = HAJ.xLow[index];
            double HAJHighValue = HAJ.xHigh[index];

            double MktSeriesCloseValue = MarketSeries.Close.LastValue;


            sp_d = (Symbol.Ask - Symbol.Bid) / Symbol.PipSize;

            if ((HAJCloseValue < _ichi.SenkouSpanA[index]) && (HAJCloseValue < _ichi.SenkouSpanB[index]) && (_ichi.TenkanSen.LastValue < _ichi.KijunSen.LastValue) && (HAJCloseValue > (_ichi.ChikouSpan[index - 26])))
            {
                if ((HAJOpenValue == HAJHighValue))
                {
                    if (sp_d <= MaxSpread)
                    {
                        Open_24Sell();
                    }
                }
            }




            if ((HAJ.xClose[index] > _ichi.SenkouSpanA[index]) && (HAJ.xClose[index] > _ichi.SenkouSpanB[index]) && (_ichi.TenkanSen.LastValue > _ichi.KijunSen.LastValue) && (HAJ.xClose[index] < (_ichi.ChikouSpan[index - 26])))
            {
                if (HAJ.xOpen[index] == HAJ.xLow[index])
                {
                    if (sp_d <= MaxSpread)
                    {
                        Open_24Buy();
                    }
                }
            }



            ChartObjects.DrawText("SpreadTxt", "spread " + Convert.ToString(Math.Round(sp_d, 2)), StaticPosition.TopCenter, Colors.Tomato);


        }


        protected override void OnStop()
        {
            // ChartObjects.RemoveAllObjects();
        }



        private void Open_24Buy()
        {


            OrderSend(TradeType.Buy, FirstVolume);


        }


        private void Open_24Sell()
        {

            OrderSend(TradeType.Sell, FirstVolume);

        }


        private void OrderSend(TradeType TrdTp, long iVol)
        {

            if (iVol > 0)
            {
                TradeResult result = ExecuteMarketOrder(TrdTp, Symbol, iVol, Convert.ToString(Symbol.Code), SL, TP, MPS, Convert.ToString(Symbol.Code));


            }


        }


    }

}




 


@varunthakur.outlook

behkam21
06 Oct 2018, 19:46

RE:

lucian said:

Last value means last closed bar (if you use OnBar)

According to Time Frame setting, the last bar represent the last 1 Hour bar, or the last 1 minute bar, etc.

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 9)]
        public int tenkanSenPeriods { get; set; }
        [Parameter(DefaultValue = 25)]
        public int kijunSenPeriods { get; set; }
        [Parameter(DefaultValue = 32)]
        public int senkouSpanBPeriods { get; set; }
        private IchimokuKinkoHyo ichimokuKinkoHyo;

        protected override void OnStart()
        {
            ichimokuKinkoHyo = Indicators.IchimokuKinkoHyo(tenkanSenPeriods, kijunSenPeriods, senkouSpanBPeriods);
        }
        protected override void OnBar()
        {
            Print("KijunSen Value = {0}", ichimokuKinkoHyo.KijunSen.LastValue);
            Print("SenkouSpanA Value = {0}", ichimokuKinkoHyo.SenkouSpanA.LastValue);
            Print("SenkouSpanB Value = {0}", ichimokuKinkoHyo.SenkouSpanB.LastValue);
            Print("TenkanSen Value = {0}", ichimokuKinkoHyo.TenkanSen.LastValue);

        }

    }
}

If you don't feel confident with the above code try this:
The   count-2 represent the last closed bar.

 

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class NewcBot : Robot
    {
        [Parameter(DefaultValue = 9)]
        public int tenkanSenPeriods { get; set; }
        [Parameter(DefaultValue = 25)]
        public int kijunSenPeriods { get; set; }
        [Parameter(DefaultValue = 32)]
        public int senkouSpanBPeriods { get; set; }
        private IchimokuKinkoHyo ichimokuKinkoHyo;

        protected override void OnStart()
        {
            ichimokuKinkoHyo = Indicators.IchimokuKinkoHyo(tenkanSenPeriods, kijunSenPeriods, senkouSpanBPeriods);
        }
        protected override void OnBar()
        {
            int index = MarketSeries.Close.Count - 2;

            Print("KijunSen Value = {0}", ichimokuKinkoHyo.KijunSen[index]);
            Print("SenkouSpanA Value = {0}", ichimokuKinkoHyo.SenkouSpanA[index]);
            Print("SenkouSpanB Value = {0}", ichimokuKinkoHyo.SenkouSpanB[index]);
            Print("TenkanSen Value = {0}", ichimokuKinkoHyo.TenkanSen[index]);

        }

    }
}

 

 


@behkam21