Replies

jmenotts
12 Feb 2024, 08:48

RE: RE: Auto Lot Sizing Based on Account Balance/Equity

Hi and thankyou for the reply, How would i make the robot trade higher lots automatically per 1k account balance and add a spread filter please?


@jmenotts

jmenotts
11 Feb 2024, 15:48

Auto Lot Sizing Based on Account Balance/Equity

Would just like to thank you for all your help I have a cBot that works well tbh, On another note though how would I go about increasing lot size per 1k account balance/equity from lets say a percentage or every 1k? I'm unsure I've looked all over the net.

Thanks In Advance.

 


@jmenotts

jmenotts
08 Feb 2024, 13:56

RE: RE: RE: RE: RE: RE: Cant get Trailing StopLoss Working

your a diamond :) and i suppose your right buddy thanks once again…..


@jmenotts

jmenotts
08 Feb 2024, 12:58

RE: RE: RE: RE: Cant get Trailing StopLoss Working

Hi bud i tried to add that below tsl but im getting problems with it not sure if im being stupid here lol could you insert it into the code for me and paste it here as im getting no where sorry to be a pain and thank you for your help buddy


@jmenotts

jmenotts
08 Feb 2024, 11:49

Hi your a diamond that works ive just got to figure out how to make it only place 1 trade at a time lol thankyou so much again youve saved me an head ache.


@jmenotts

jmenotts
06 Feb 2024, 14:53

RE: RE: Cant get Trailing StopLoss Working

Thankyou for your reply buddy but ….. im still unable to get it running heres my code it must be something so simple im sure lol.

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 ADXCrossingBot : Robot
    {
        private bool isTradeOpen = false;

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

        [Parameter("ADX Periods", Group = "ADX", DefaultValue = 20, MinValue = 6, Step = 1, MaxValue = 1000)]
        public int AdxPeriods { get; set; }

        [Parameter("MA", Group = "ADX", DefaultValue = 21, MinValue = 6, Step = 1, MaxValue = 1000)]
        public int TrendMME { get; set; }

        [Parameter("Take Profit", Group = "Risk", DefaultValue = 150, MinValue = 0, Step = 1, MaxValue = 1000)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", Group = "Risk", DefaultValue = 150, MinValue = 0, Step = 1, MaxValue = 1000)]
        public int StopLoss { get; set; }

        [Parameter("Trade Label", DefaultValue = "ADX Crossing")]
        public string TradeLabel { get; set; }

        [Parameter("Use Trailing Stop", DefaultValue = false, Group = "Trailing Stop Loss")]
        public bool UseTSL { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20, Group = "Trailing Stop Loss")]
        public int TSLTrigger { get; set; }

        [Parameter("Distance (pips)", DefaultValue = 10, Group = "Trailing Stop Loss")]
        public int TSLDistance { get; set; }

        private DirectionalMovementSystem adx;
        private double volumeInUnits;
        private ExponentialMovingAverage mme;
        private RelativeStrengthIndex rsi;
        

        protected override void OnStart()
        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            adx = Indicators.DirectionalMovementSystem(AdxPeriods);
            mme = Indicators.ExponentialMovingAverage(Bars.ClosePrices, TrendMME);
            rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);

            Positions.Closed += OnPositionClosed;
        }

        protected override void OnTick()
        {
            // Check if a trade is already open
            if (isTradeOpen)
                return;

            if (adx.DIMinus.Last(3) < adx.DIPlus.Last(3) && adx.DIMinus.Last(1) > adx.DIPlus.Last(1) && adx.DIMinus.LastValue > adx.DIPlus.LastValue)
            {
                if (Symbol.Bid < mme.Result.Last(2))
                    if (rsi.Result.LastValue <= 70)
                    {
                        ExecuteMarketOrder(TradeType.Sell,  Symbol.Name, volumeInUnits, TradeLabel, StopLoss, TakeProfit);
                        isTradeOpen = true;
                    }
            }
            else if (adx.DIPlus.Last(3) < adx.DIMinus.Last(3) && adx.DIPlus.Last(1) > adx.DIMinus.Last(1) && adx.DIPlus.LastValue > adx.DIMinus.LastValue)
            {
                if (Symbol.Bid > mme.Result.Last(2))
                    if (rsi.Result.LastValue >= 30)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, TradeLabel, StopLoss, TakeProfit);
                        isTradeOpen = true;
                    }
            }

{
            // If Trailing Stop Loss is set to true, then we execute the following code block
            if (UseTSL)
            {
                // we iterate through all the instance's positions
                foreach (var position in Positions.Where(x => x.Label == TradeLabel))
                {
                    // If position's pips is above the trailing stop loss pips and the position has not trailing stop loss set
                    if (position.Pips > TSLTrigger && !position.HasTrailingStop)
                    {
                        // We check the position's trade type and excute the relevant code block
                        if (position.TradeType == TradeType.Buy)
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var stopLoss = Symbol.Bid - (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(stopLoss);

                            // We set the trailing stop loss to true
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                        else
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var sl = Symbol.Ask + (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(sl);
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                    }
                }
            }
        }}
        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            // Reset the flag when a position is closed
            isTradeOpen = false;
        }
    }
}


@jmenotts

jmenotts
06 Feb 2024, 14:51

RE: RE: Cant get Trailing StopLoss Working

Thankyou for your prompt response i have done this and still not getting anywhere this is driving me crazy im an absolute newby when it comes to this i have pasted the code again to show what i have done, Thankyou in advance…..

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 ADXCrossingBot : Robot
    {
        private bool isTradeOpen = false;

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

        [Parameter("ADX Periods", Group = "ADX", DefaultValue = 20, MinValue = 6, Step = 1, MaxValue = 1000)]
        public int AdxPeriods { get; set; }

        [Parameter("MA", Group = "ADX", DefaultValue = 21, MinValue = 6, Step = 1, MaxValue = 1000)]
        public int TrendMME { get; set; }

        [Parameter("Take Profit", Group = "Risk", DefaultValue = 150, MinValue = 0, Step = 1, MaxValue = 1000)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", Group = "Risk", DefaultValue = 150, MinValue = 0, Step = 1, MaxValue = 1000)]
        public int StopLoss { get; set; }

        [Parameter("Trade Label", DefaultValue = "ADX Crossing")]
        public string TradeLabel { get; set; }

        [Parameter("Use Trailing Stop", DefaultValue = false, Group = "Trailing Stop Loss")]
        public bool UseTSL { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20, Group = "Trailing Stop Loss")]
        public int TSLTrigger { get; set; }

        [Parameter("Distance (pips)", DefaultValue = 10, Group = "Trailing Stop Loss")]
        public int TSLDistance { get; set; }

        private DirectionalMovementSystem adx;
        private double volumeInUnits;
        private ExponentialMovingAverage mme;
        private RelativeStrengthIndex rsi;
        

        protected override void OnStart()
        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            adx = Indicators.DirectionalMovementSystem(AdxPeriods);
            mme = Indicators.ExponentialMovingAverage(Bars.ClosePrices, TrendMME);
            rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);

            Positions.Closed += OnPositionClosed;
        }

        protected override void OnTick()
        {
            // Check if a trade is already open
            if (isTradeOpen)
                return;

            if (adx.DIMinus.Last(3) < adx.DIPlus.Last(3) && adx.DIMinus.Last(1) > adx.DIPlus.Last(1) && adx.DIMinus.LastValue > adx.DIPlus.LastValue)
            {
                if (Symbol.Bid < mme.Result.Last(2))
                    if (rsi.Result.LastValue <= 70)
                    {
                        ExecuteMarketOrder(TradeType.Sell,  Symbol.Name, volumeInUnits, TradeLabel, StopLoss, TakeProfit);
                        isTradeOpen = true;
                    }
            }
            else if (adx.DIPlus.Last(3) < adx.DIMinus.Last(3) && adx.DIPlus.Last(1) > adx.DIMinus.Last(1) && adx.DIPlus.LastValue > adx.DIMinus.LastValue)
            {
                if (Symbol.Bid > mme.Result.Last(2))
                    if (rsi.Result.LastValue >= 30)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, TradeLabel, StopLoss, TakeProfit);
                        isTradeOpen = true;
                    }
            }

{
            // If Trailing Stop Loss is set to true, then we execute the following code block
            if (UseTSL)
            {
                // we iterate through all the instance's positions
                foreach (var position in Positions.Where(x => x.Label == TradeLabel))
                {
                    // If position's pips is above the trailing stop loss pips and the position has not trailing stop loss set
                    if (position.Pips > TSLTrigger && !position.HasTrailingStop)
                    {
                        // We check the position's trade type and excute the relevant code block
                        if (position.TradeType == TradeType.Buy)
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var stopLoss = Symbol.Bid - (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(stopLoss);

                            // We set the trailing stop loss to true
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                        else
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var sl = Symbol.Ask + (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(sl);
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                    }
                }
            }
        }}
        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            // Reset the flag when a position is closed
            isTradeOpen = false;
        }
    }
}

 


@jmenotts

jmenotts
06 Feb 2024, 13:57

Hi im still unable to get it working even with the tried and tested code am i being silly? lol  i pasted the exact code im using, im altering parameters and he results are no different when running back test im lost now any further help will be much appreciated thank you once again.

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 ADXCrossingBot : Robot
    {
        private bool isTradeOpen = false;

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

        [Parameter("ADX Periods", Group = "ADX", DefaultValue = 20, MinValue = 6, Step = 1, MaxValue = 1000)]
        public int AdxPeriods { get; set; }

        [Parameter("MA", Group = "ADX", DefaultValue = 21, MinValue = 6, Step = 1, MaxValue = 1000)]
        public int TrendMME { get; set; }

        [Parameter("Take Profit", Group = "Risk", DefaultValue = 150, MinValue = 0, Step = 1, MaxValue = 1000)]
        public int TakeProfit { get; set; }

        [Parameter("Stop Loss", Group = "Risk", DefaultValue = 150, MinValue = 0, Step = 1, MaxValue = 1000)]
        public int StopLoss { get; set; }

        [Parameter("Trade Label", DefaultValue = "ADX Crossing")]
        public string TradeLabel { get; set; }

        [Parameter("Use Trailing Stop", DefaultValue = false, Group = "Trailing Stop Loss")]
        public bool UseTSL { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20, Group = "Trailing Stop Loss")]
        public int TSLTrigger { get; set; }

        [Parameter("Distance (pips)", DefaultValue = 10, Group = "Trailing Stop Loss")]
        public int TSLDistance { get; set; }

        private DirectionalMovementSystem adx;
        private double volumeInUnits;
        private ExponentialMovingAverage mme;
        private RelativeStrengthIndex rsi;

        protected override void OnStart()
        {
            volumeInUnits = Symbol.QuantityToVolumeInUnits(Quantity);
            adx = Indicators.DirectionalMovementSystem(AdxPeriods);
            mme = Indicators.ExponentialMovingAverage(Bars.ClosePrices, TrendMME);
            rsi = Indicators.RelativeStrengthIndex(Bars.ClosePrices, 14);

            Positions.Closed += OnPositionClosed;
        }

        protected override void OnTick()
        {
            // Check if a trade is already open
            if (isTradeOpen)
                return;

            if (adx.DIMinus.Last(3) < adx.DIPlus.Last(3) && adx.DIMinus.Last(1) > adx.DIPlus.Last(1) && adx.DIMinus.LastValue > adx.DIPlus.LastValue)
            {
                if (Symbol.Bid < mme.Result.Last(2))
                    if (rsi.Result.LastValue <= 70)
                    {
                        ExecuteMarketOrder(TradeType.Sell, Symbol.Name, volumeInUnits, "ADX Sell", StopLoss, TakeProfit);
                        isTradeOpen = true;
                    }
            }
            else if (adx.DIPlus.Last(3) < adx.DIMinus.Last(3) && adx.DIPlus.Last(1) > adx.DIMinus.Last(1) && adx.DIPlus.LastValue > adx.DIMinus.LastValue)
            {
                if (Symbol.Bid > mme.Result.Last(2))
                    if (rsi.Result.LastValue >= 30)
                    {
                        ExecuteMarketOrder(TradeType.Buy, Symbol.Name, volumeInUnits, "ADX Buy", StopLoss, TakeProfit);
                        isTradeOpen = true;
                    }
            }

{
            // If Trailing Stop Loss is set to true, then we execute the following code block
            if (UseTSL)
            {
                // we iterate through all the instance's positions
                foreach (var position in Positions.Where(x => x.Label == TradeLabel))
                {
                    // If position's pips is above the trailing stop loss pips and the position has not trailing stop loss set
                    if (position.Pips > TSLTrigger && !position.HasTrailingStop)
                    {
                        // We check the position's trade type and excute the relevant code block
                        if (position.TradeType == TradeType.Buy)
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var stopLoss = Symbol.Bid - (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(stopLoss);

                            // We set the trailing stop loss to true
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                        else
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var sl = Symbol.Ask + (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(sl);
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                    }
                }
            }
        }}
        private void OnPositionClosed(PositionClosedEventArgs args)
        {
            // Reset the flag when a position is closed
            isTradeOpen = false;
        }
    }
}


@jmenotts

jmenotts
06 Feb 2024, 13:55

RE: RE: Cant get Trailing StopLoss Working

Hjmenotts said: 

PanagiotisCharalampous said: 

Hi there, 

I am really lazy figuring out why your code does not work therefore I am quoting some code that has been tested and runs well


        [Parameter("Use Trailing Stop", DefaultValue = false, Group = "Trailing Stop Loss")]
        public bool UseTSL { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20, Group = "Trailing Stop Loss")]
        public int TSLTrigger { get; set; }

        [Parameter("Distance (pips)", DefaultValue = 10, Group = "Trailing Stop Loss")]
        public int TSLDistance { get; set; }
 protected override void OnTick()
        {
            // If Trailing Stop Loss is set to true, then we execute the following code block
            if (UseTSL)
            {
                // we iterate through all the instance's positions
                foreach (var position in Positions.Where(x => x.Label == Instance))
                {
                    // If position's pips is above the trailing stop loss pips and the position has not trailing stop loss set
                    if (position.Pips > TSLTrigger && !position.HasTrailingStop)
                    {
                        // We check the position's trade type and excute the relevant code block
                        if (position.TradeType == TradeType.Buy)
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var stopLoss = Symbol.Bid - (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(stopLoss);

                            // We set the trailing stop loss to true
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                        else
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var sl = Symbol.Ask + (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(sl);
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                    }
                }
            }
        }

Thankyou I really appreciate the help will try it when I get back from working away and update here the results.

 


@jmenotts

jmenotts
06 Feb 2024, 04:50

RE: Cant get Trailing StopLoss Working

PanagiotisCharalampous said: 

Hi there, 

I am really lazy figuring out why your code does not work therefore I am quoting some code that has been tested and runs well


        [Parameter("Use Trailing Stop", DefaultValue = false, Group = "Trailing Stop Loss")]
        public bool UseTSL { get; set; }

        [Parameter("Trigger (pips)", DefaultValue = 20, Group = "Trailing Stop Loss")]
        public int TSLTrigger { get; set; }

        [Parameter("Distance (pips)", DefaultValue = 10, Group = "Trailing Stop Loss")]
        public int TSLDistance { get; set; }
 protected override void OnTick()
        {
            // If Trailing Stop Loss is set to true, then we execute the following code block
            if (UseTSL)
            {
                // we iterate through all the instance's positions
                foreach (var position in Positions.Where(x => x.Label == Instance))
                {
                    // If position's pips is above the trailing stop loss pips and the position has not trailing stop loss set
                    if (position.Pips > TSLTrigger && !position.HasTrailingStop)
                    {
                        // We check the position's trade type and excute the relevant code block
                        if (position.TradeType == TradeType.Buy)
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var stopLoss = Symbol.Bid - (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(stopLoss);

                            // We set the trailing stop loss to true
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                        else
                        {
                            // We calculate the stop loss based on the TSL Distance To Add parameter
                            var sl = Symbol.Ask + (TSLDistance * Symbol.PipSize);

                            // We modify the stop loss price
                            position.ModifyStopLossPrice(sl);
                            position.ModifyTrailingStop(true);

                            Print("Trailing Stop Loss Triggered");
                        }
                    }
                }
            }
        }

Thankyou I really appreciate the help will try it when I get back from working away and update here the results.


@jmenotts

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

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

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