Ichimoku Bot Logic Help

Created at 08 Mar 2021, 23:03
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!
JM

jmenotts

Joined 08.03.2021

Ichimoku Bot Logic Help
08 Mar 2021, 23:03


Hi guys i've pinched a bit of code from various sites and i'm trying to learn how to put a bot together using the ichimoku cloud strategy but cant quite get my logic correct wondering if anyone can help me?

this must take a buy position once price has crossed above the cloud and tenkan sen has crossed above kijun sen

it must take a sell position once price has crossed below the cloud and tenkan sen has crossed below kijun sen

it wouldnt really matter in which order these instances occur they just must all be met for a signal

could someone please show me how to code this logic i've been pulling my hair out for days now and getting no where i'm an absolute idiot when it comes to coding and im hoping one you coding gods could help me?

i've attached the logic that i've borrowed from a fellow user on here trying to learn how it all works but bloody struggling lol.

        protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.LastValue) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.LastValue - Symbol.Ask) / Symbol.PipSize;


            if (positionsBuy.Length == 0 && positionsSell.Length == 0)
            {
                if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(1) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(1))
                {
                    if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(1))
                    {
                        if (distanceFromUpKumo <= 30)
                        {
                            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                        }
                    }
                }

                if (MarketSeries.Open.Last(1) >= ichimoku.SenkouSpanA.Last(1) && MarketSeries.Open.Last(1) < ichimoku.SenkouSpanB.Last(1))
                {
                    if (MarketSeries.Close.Last(1) < ichimoku.SenkouSpanA.Last(1))
                    {
                        if (distanceFromDownKumo <= 30)
                        {
                            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
                        }
                    }
                }
            }
        }
    }
}

Thank you very much for taking the time to read this guys and hopefully i've not been too much of a pain :)


@jmenotts
Replies

jmenotts
09 Mar 2021, 00:50 ( Updated at: 09 Mar 2021, 01:09 )

RE: I think i may have figured it out?

jmenotts said:

Hi guys i've pinched a bit of code from various sites and i'm trying to learn how to put a bot together using the ichimoku cloud strategy but cant quite get my logic correct wondering if anyone can help me?

this must take a buy position once price has crossed above the cloud and tenkan sen has crossed above kijun sen

it must take a sell position once price has crossed below the cloud and tenkan sen has crossed below kijun sen

it wouldnt really matter in which order these instances occur they just must all be met for a signal

could someone please show me how to code this logic i've been pulling my hair out for days now and getting no where i'm an absolute idiot when it comes to coding and im hoping one you coding gods could help me?

i've attached the logic that i've borrowed from a fellow user on here trying to learn how it all works but bloody struggling lol.

        protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.LastValue) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.LastValue - Symbol.Ask) / Symbol.PipSize;


            if (positionsBuy.Length == 0 && positionsSell.Length == 0)
            {
                if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(1) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(1))
                {
                    if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(1))
                    {
                        if (distanceFromUpKumo <= 30)
                        {
                            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                        }
                    }
                }

                if (MarketSeries.Open.Last(1) >= ichimoku.SenkouSpanA.Last(1) && MarketSeries.Open.Last(1) < ichimoku.SenkouSpanB.Last(1))
                {
                    if (MarketSeries.Close.Last(1) < ichimoku.SenkouSpanA.Last(1))
                    {
                        if (distanceFromDownKumo <= 30)
                        {
                            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
                        }
                    }
                }
            }
        }
    }
}

Thank you very much for taking the time to read this guys and hopefully i've not been too much of a pain :)

        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.LastValue) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.LastValue - Symbol.Ask) / Symbol.PipSize;


            if (positionsBuy.Length == 0 && positionsSell.Length == 0)
            {
                if (MarketSeries.Open.Last(1) <= ichimoku.SenkouSpanA.Last(1) && MarketSeries.Open.Last(1) > ichimoku.SenkouSpanB.Last(1))
                {
                    if (MarketSeries.Close.Last(1) > ichimoku.SenkouSpanA.Last(1))
                    {
                       if (ichimoku.TenkanSen.Last(1) > (ichimoku.KijunSen.Last(1)))
                        {
                            ExecuteMarketOrder(TradeType.Buy, Symbol, Volume, "Buy", StopLossPips, TakeProfitPips);
                        }
                    }
                }

                if (MarketSeries.Open.Last(1) >= ichimoku.SenkouSpanA.Last(1) && MarketSeries.Open.Last(1) < ichimoku.SenkouSpanB.Last(1))
                {
                    if (MarketSeries.Close.Last(1) < ichimoku.SenkouSpanA.Last(1))
                    {
                        if (ichimoku.TenkanSen.Last(1) < (ichimoku.KijunSen.Last(1)))
                        {
                            ExecuteMarketOrder(TradeType.Sell, Symbol, Volume, "Sell", StopLossPips, TakeProfitPips);
                        }
                    }
                }
            }
        }
    }
}


@jmenotts

jmenotts
09 Mar 2021, 13:52

I figured the final code out i think?

Hey Guys 

 

Just a quick update i know I've been speaking to myself at the moment but I'm trying to report my progress in the hope someone will put me right if I'm wrong, I've been doing some backtests and the code works however I'm had it on the charts since last night and it doesn't seem to be taking any trades maybe I'm trying to get results faster than the charts are giving me.

 

I've attached the code for someone to have a look over to see if I'm heading in the right direction or do I need to edit it slightly for it to enter live trades?

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MidasIndex : Robot
    {
        [Parameter(DefaultValue = 9)]
        public int periodFast { get; set; }

        [Parameter(DefaultValue = 26)]
        public int periodMedium { get; set; }

        [Parameter(DefaultValue = 52)]
        public int periodSlow { get; set; }

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

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLossPips { get; set; }

        [Parameter("Take Profit", DefaultValue = 25)]
        public int TakeProfitPips { get; set; }

        private IchimokuKinkoHyo ichimoku;

        protected override void OnStart()
        {
            ichimoku = Indicators.IchimokuKinkoHyo(periodFast, periodMedium, periodSlow);
        }

        protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            var distanceFromUpKumo = (Symbol.Bid - ichimoku.SenkouSpanA.LastValue) / Symbol.PipSize;
            var distanceFromDownKumo = (ichimoku.SenkouSpanA.LastValue - Symbol.Ask) / Symbol.PipSize;


            if (positionsBuy.Length == 0 && positionsSell.Length == 0)
            {
                if (Bars.OpenPrices.Last(1) <= ichimoku.SenkouSpanA.Last(1) && Bars.OpenPrices.Last(1) > ichimoku.SenkouSpanB.Last(1))
                {
                    if (Bars.ClosePrices.Last(1) > ichimoku.SenkouSpanA.Last(1))
                    {
                        if (ichimoku.TenkanSen.Last(1) > (ichimoku.KijunSen.Last(1)))
                        {
                            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "Buy", StopLossPips, TakeProfitPips);
                        }
                    }
                }

                if (Bars.OpenPrices.Last(1) >= ichimoku.SenkouSpanA.Last(1) && Bars.OpenPrices.Last(1) < ichimoku.SenkouSpanB.Last(1))
                {
                    if (Bars.ClosePrices.Last(1) < ichimoku.SenkouSpanA.Last(1))
                    {
                        if (ichimoku.TenkanSen.Last(1) < (ichimoku.KijunSen.Last(1)))
                        {
                            ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "Sell", StopLossPips, TakeProfitPips);
                        }
                    }
                }
            }
        }
    }
}
 


@jmenotts

amusleh
10 Mar 2021, 09:23

Hello,

Try this:

using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class MidasIndex : Robot
    {
        [Parameter(DefaultValue = 9)]
        public int PeriodFast { get; set; }

        [Parameter(DefaultValue = 26)]
        public int PeriodMedium { get; set; }

        [Parameter(DefaultValue = 52)]
        public int PeriodSlow { get; set; }

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

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLossPips { get; set; }

        [Parameter("Take Profit", DefaultValue = 25)]
        public int TakeProfitPips { get; set; }

        private IchimokuKinkoHyo _ichimoku;

        protected override void OnStart()
        {
            _ichimoku = Indicators.IchimokuKinkoHyo(PeriodFast, PeriodMedium, PeriodSlow);
        }

        protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");

            if (positionsBuy.Length > 0 || positionsSell.Length > 0) return;

            if (Bars.OpenPrices.Last(1) <= _ichimoku.SenkouSpanA.Last(1)
                && Bars.OpenPrices.Last(1) > _ichimoku.SenkouSpanB.Last(1)
                && Bars.ClosePrices.Last(1) > _ichimoku.SenkouSpanA.Last(1)
                && _ichimoku.TenkanSen.Last(1) > _ichimoku.KijunSen.Last(1))
            {
                ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "Buy", StopLossPips, TakeProfitPips);
            }

            if (Bars.OpenPrices.Last(1) >= _ichimoku.SenkouSpanA.Last(1)
                && Bars.OpenPrices.Last(1) < _ichimoku.SenkouSpanB.Last(1)
                && Bars.ClosePrices.Last(1) < _ichimoku.SenkouSpanA.Last(1)
                && _ichimoku.TenkanSen.Last(1) < _ichimoku.KijunSen.Last(1))
            {
                ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "Sell", StopLossPips, TakeProfitPips);
            }
        }
    }
}

I backtested and it worked fine.


@amusleh

jmenotts
10 Mar 2021, 10:37 ( Updated at: 10 Mar 2021, 10:43 )

RE: Hi and thank you for the reply

Ive edited it already but im so thankful for your input maybe we can make this better, run this on H30 stop 1000 take 30 use the full history of ctrader the results are amazing but she takes some bad trades if we could filter this out we may be onto a little but simple bot that doesnt do too bad at all, only con concerning thing is the stops are massive 1000 pips, this is a trend bot i think maybe an RSI or some other indicator may filter out the bad trades. i hav been manual trading this strategy and it works perfect on forex not on crypto.

 

using System;
using cAlgo.API;
using cAlgo.API.Indicators;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class ICCMJR : Robot
    {
        [Parameter(DefaultValue = 9)]
        public int periodFast { get; set; }

        [Parameter(DefaultValue = 26)]
        public int periodMedium { get; set; }

        [Parameter(DefaultValue = 52)]
        public int periodSlow { get; set; }

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

        [Parameter("Stop Loss", DefaultValue = 20)]
        public int StopLossPips { get; set; }

        [Parameter("Take Profit", DefaultValue = 25)]
        public int TakeProfitPips { get; set; }

        private IchimokuKinkoHyo ichimoku;

        protected override void OnStart()
        {
            ichimoku = Indicators.IchimokuKinkoHyo(periodFast, periodMedium, periodSlow);
        }

        protected override void OnBar()
        {
            var positionsBuy = Positions.FindAll("Buy");
            var positionsSell = Positions.FindAll("Sell");


            if (positionsBuy.Length == 0 && positionsSell.Length == 0)
            {
                if (ichimoku.SenkouSpanA.HasCrossedAbove(ichimoku.SenkouSpanB.LastValue, 0))
                {
                    if (Bars.ClosePrices.Last(1) > ichimoku.SenkouSpanA.Last(1))
                    {
                        if (ichimoku.TenkanSen.HasCrossedAbove(ichimoku.KijunSen.LastValue, 0))
                        {
                            ExecuteMarketOrder(TradeType.Buy, SymbolName, Volume, "Buy", StopLossPips, TakeProfitPips);
                        }
                    }
                }

                if (ichimoku.SenkouSpanA.HasCrossedBelow(ichimoku.SenkouSpanB.LastValue, 0))
                {
                    if (Bars.ClosePrices.Last(1) < ichimoku.SenkouSpanA.Last(1))
                    {
                        if (ichimoku.TenkanSen.HasCrossedBelow(ichimoku.KijunSen.LastValue, 0))
                        {
                            ExecuteMarketOrder(TradeType.Sell, SymbolName, Volume, "Sell", StopLossPips, TakeProfitPips);
                        }
                    }
                }
            }
        }
    }
}

 


@jmenotts