Calculating in different timeframes

Created at 21 Dec 2022, 12:30
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!
MI

mindfulness

Joined 04.07.2021

Calculating in different timeframes
21 Dec 2022, 12:30


Hello,

i am already using Ichimiko in a script on different timeframes. Maybe easy because it´s already a indicator.

Now i want to calculate a value based on the following in different timeframes:

var kyushu_bullish = Bars.OpenPrices.Last(8) < Bars.ClosePrices.LastValue;

I have already tried the following:

var kyushu_d = MarketData.GetBars(TimeFrame.Daily);
var kyushu_h4 = MarketData.GetBars(TimeFrame.Hour4);
var kyushu_h1 = MarketData.GetBars(TimeFrame.Hour);
var kyushu_m30 = MarketData.GetBars(TimeFrame.Minute30);
var kyushu_m15 = MarketData.GetBars(TimeFrame.Minute15);


var kyushu_bullish = Bars.OpenPrices.Last(8) < Bars.ClosePrices.LastValue;
var kyushu_d_bullish = kyushu_d.kyushu_bullish;
var kyushu_h4_bullish = kyushu_h4.kyushu_bullish;
var kyushu_h1_bullish = kyushu_h1.kyushu_bullish;
var kyushu_m30_bullish = kyushu_m30.kyushu_bullish;
            
var kyushu_bearish = Bars.OpenPrices.Last(8) > Bars.ClosePrices.LastValue;
var kyushu_d_bearish = kyushu_d.kyushu_bearish;
var kyushu_h4_bearish = kyushu_h4.kyushu_bearish;
var kyushu_h1_bearish = kyushu_h1.kyushu_bearish;
var kyushu_m30_bearish = kyushu_m30.kyushu_bearish;

But this seems to be wrong. Can someone help me, how i can do this?

thanks in advanced

Alex


@mindfulness
Replies

PanagiotisChar
21 Dec 2022, 13:37

Hi Alex,

Please share the complete code and explain what do you think is wrong.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@PanagiotisChar

mindfulness
23 Dec 2022, 12:04 ( Updated at: 21 Dec 2023, 09:23 )

RE:

Hi,

sure here is the code i am using:

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

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.EEuropeStandardTime, AccessRights = AccessRights.None)]
    public class ClickAlgoSchoolSMA : Robot
    {
        #region User defined parameters


        [Parameter("Instance Name", DefaultValue = "001")]
        public string InstanceName { get; set; }

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

        #endregion

        #region Indicator declarations

        //private StochasticOscillator _stochastic;
        private ExponentialMovingAverage _ema9 { get; set; }
        private ExponentialMovingAverage _ema26 { get; set; }
        private ExponentialMovingAverage _ema51 { get; set; }

        private BollingerBands _bollingerBands;

        private IchimokuKinkoHyo _ichimokuKinkoHyo;

        [Parameter("Source")]
        public DataSeries Source { get; set; }

        [Parameter("Period", DefaultValue = 20)]
        public int Period { get; set; }
        
        [Parameter("Stop Loss", DefaultValue = 35)]
        public double StopLoss { get; set; }

        [Parameter("Risk %", DefaultValue = 1, MinValue = 0.01, Step = 0.01)]
        public double RiskPerTrade { get; set; }
        #endregion

        #region cTrader events


        protected override void OnStart()
        {
                }

        [Obsolete()]
        protected override void OnBar()
        {
            if (!CalculateOnBar)
            {
                return;
            }

            ManagePositions();
        }


        protected override void OnStop()
        {
            // unused
        }

        #endregion

        #region Position management

        [Obsolete()]
        private void ManagePositions()
        {

            #region Daten f�r timeframes
            
            var kyushu_d = MarketData.GetBars(TimeFrame.Daily);
            var kyushu_h4 = MarketData.GetBars(TimeFrame.Hour4);
            var kyushu_h1 = MarketData.GetBars(TimeFrame.Hour);
            var kyushu_m30 = MarketData.GetBars(TimeFrame.Minute30);
            var kyushu_m15 = MarketData.GetBars(TimeFrame.Minute15);
            var kyushu_m5 = MarketData.GetBars(TimeFrame.Minute5);
            #endregion

            
             #region Variablen Kyushu Ashi
            var kyushu_bullish = Bars.OpenPrices.Last(8) < Bars.ClosePrices.LastValue;
            var kyushu_d_bullish = kyushu_d.kyushu_bullish;
            var kyushu_h4_bullish = kyushu_h4.kyushu_bullish;
            var kyushu_h1_bullish = kyushu_h1.kyushu_bullish;
            var kyushu_m30_bullish = kyushu_m30.kyushu_bullish;
            
            var kyushu_bearish = Bars.OpenPrices.Last(8) > Bars.ClosePrices.LastValue;
            var kyushu_d_bearish = kyushu_d.kyushu_bearish;
            var kyushu_h4_bearish = kyushu_h4.kyushu_bearish;
            var kyushu_h1_bearish = kyushu_h1.kyushu_bearish;
            var kyushu_m30_bearish = kyushu_m30.kyushu_bearish;
            
            #endregion

                    

            #region Trading
                #region Entry
                
                var higher_trend = (kyushu_d_bullish || kyushu_d_bearish) || (kyushu_h4_bullish || kyushu_h4_bearish);
                var lower_trend = (kyushu_h1_bullish || kyushu_h1_bearish) || (kyushu_m30_bullish || kyushu_m30_bearish);
                
                var confirmation_bullish = (kyushu_d_bullish || kyushu_h4_bullish) && (kyushu_h1_bullish || kyushu_m30_bullish) && kyushu_bullish;
                var confirmation_bearish = (kyushu_d_bearish || kyushu_h4_bearish) && (kyushu_h1_bearish || kyushu_m30_bearish) && kyushu_bearish;
                
                
                #endregion
                
                #region exit
                var before_BE = LastResult.Position.Pips < 30;
                var after_BE = LastResult.Position.Pips > 30;
                
                var exit_bullish_before_BE = (kyushu_bearish || !higher_trend) && before_BE;
                var exit_bearish_before_BE = (kyushu_bullish || !lower_trend) && before_BE;
                
                var exit_bullish_after_BE = !higher_trend  && after_BE;
                var exit_bearish_after_BE = !lower_trend  && after_BE;
                
                var exit_bullish = exit_bullish_before_BE || exit_bullish_after_BE;
                var exit_bearish = exit_bearish_before_BE || exit_bearish_after_BE;
                
                #endregion

                #region Tradingzeiten
                var tradingStarts = TimeSpan.ParseExact("07:00", "hh\\:mm", null);
                var tradingStops = TimeSpan.ParseExact("17:00", "hh\\:mm", null);
                #endregion

                #region Orderaufgabe
                if (Server.Time.TimeOfDay >= tradingStarts && Server.Time.TimeOfDay < tradingStops)
                {
                    if (confirmation_bullish)
                    {
                        if (!IsPositionOpenByType(TradeType.Buy))
                        {
                            OpenPosition(TradeType.Buy);
                        }
                    }

                    if (exit_bullish)
                    {
                        ClosePosition(TradeType.Buy);
                    }

                    if (confirmation_bearish)
                    {
                        if (!IsPositionOpenByType(TradeType.Sell))
                        {
                            OpenPosition(TradeType.Sell);
                        }
                    }

                    if (exit_bearish)
                    {
                        ClosePosition(TradeType.Sell);
                    }
                }
                
        }



        [Obsolete()]
            private void OpenPosition(TradeType type)
            {


            ExecuteMarketOrder(type, this.Symbol, GetVolume(StopLoss), InstanceName, StopLoss, null);
        }


            private void ClosePosition(TradeType type)
            {
                var p = Positions.Find(InstanceName, this.Symbol, type);

                if (p != null)
                {
                    ClosePosition(p);
                }
            }

            protected override void OnTick()
            {
              //  foreach (var position in Positions.FindAll(InstanceName, Symbol))
              //  {
              //      if (position.StopLoss != position.EntryPrice)
              //      {
              //          if (position.Pips >= Trigger)
              //          {
              //              ModifyPosition(position, position.EntryPrice, position.TakeProfit);
              //          }
              //      }
              //  }
            }
        #endregion


        #region Position Information

        
        [Obsolete()]
            private bool IsPositionOpenByType(TradeType type)
            {
                var p = Positions.FindAll(InstanceName, Symbol, type);

                if (p.Count() >= 1)
                {
                    return true;
                }

                return false;
            }


        private double GetVolume(double? stopLossPips = null)
        {
            double costPerPip = (double)((int)(Symbol.PipValue * 10000000)) / 100;

            // Change this to Account.Balance if you want to
            double baseNumber = Account.Equity;

            double sizeInLots = Math.Round((baseNumber * RiskPerTrade / 100) / (stopLossPips.Value * costPerPip), 2);

            var result = Symbol.QuantityToVolumeInUnits(sizeInLots);

            if (result > Symbol.VolumeInUnitsMax)
            {
                result = Symbol.VolumeInUnitsMax;
            }
            else if (result < Symbol.VolumeInUnitsMin)
            {
                result = Symbol.VolumeInUnitsMin;
            }
            else if (result % Symbol.VolumeInUnitsStep != 0)
            {
                result = result - (result % Symbol.VolumeInUnitsStep);
            }

            return result;
        }
        #endregion

        #endregion


    }
}

#endregion

 

And this are the error messages:

Hope this helps.

 

PanagiotisChar said:

Hi Alex,

Please share the complete code and explain what do you think is wrong.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 

 


@mindfulness

PanagiotisChar
23 Dec 2022, 13:00

Hi Alex,

I think the messages in the log are more than clear. I am not sure what you are trying to do with the code or what you think its doing. But lines of code like this make no sense

            var kyushu_d_bullish = kyushu_d.kyushu_bullish;
            var kyushu_h4_bullish = kyushu_h4.kyushu_bullish;
            var kyushu_h1_bullish = kyushu_h1.kyushu_bullish;
            var kyushu_m30_bullish = kyushu_m30.kyushu_bullish;

Did you copy this from somewhere?

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

mindfulness
23 Dec 2022, 14:21

RE:

No, i haven´t copied them from somewhere. I only try to undersdtand how to do it. I am no a programmer, so i need to look to a similiar solution to get things working. That´s why i asked here. The error message didn´t point me to my error or things i have to change. And yes i try to learn by coping frames and testing them in my scripts. Isn´t this the way lot´s of people around here started?
I tried to get the data from the timeframe and combine them with the code of kyushu ashi. Sorry, if this is not that clear to me.

PanagiotisChar said:

Hi Alex,

I think the messages in the log are more than clear. I am not sure what you are trying to do with the code or what you think its doing. But lines of code like this make no sense

            var kyushu_d_bullish = kyushu_d.kyushu_bullish;
            var kyushu_h4_bullish = kyushu_h4.kyushu_bullish;
            var kyushu_h1_bullish = kyushu_h1.kyushu_bullish;
            var kyushu_m30_bullish = kyushu_m30.kyushu_bullish;

Did you copy this from somewhere?

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@mindfulness

Waxy
23 Dec 2022, 22:15

RE: RE:

What he means is that in

var kyushu_d_bullish = kyushu_d.kyushu_bullish;

Since kyushu_d is a Bars object, it doesn't have any "kyushu_bullish" property

kyushu_d has properties like kyushu_d.OpenPrices.Last(n) and etc you can use instead

 


@Waxy

PanagiotisChar
26 Dec 2022, 20:58

What I mean is that the code does not make sense and the error message is clear regarding what the problem is. The property does not exist. So my guess was that the code was copied from somewhere and its out of context. 

Instead of asking us to fix a non sensical code which we don't understand what is supposed to do, maybe it's better to tell us what are you trying to achieve and ask for a small example.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us


@PanagiotisChar

mindfulness
27 Dec 2022, 13:42

RE:

Ok, understand. I was not aware of it, that it doesn‘t make sense and I thought I was asking the right way. Sorry for the misunderstanding.

What I want to have is the following. I want to look if the  Kyushu formula in the script is true in different timeframes to get a entry decision.

For example: It has to be true in daily or H4 AND in H1 or M30.

Thats why I tried to place the variable behind the timeframe variable to combine them. Maybe without any sense as you said. Didn‘t found any example to place a formula like this in timeframes. Only already existing indicators, which can be referenced like this. Like Ichimoku for example.

thanks for any help so far.

Alex


PanagiotisChar said:

What I mean is that the code does not make sense and the error message is clear regarding what the problem is. The property does not exist. So my guess was that the code was copied from somewhere and its out of context. 

Instead of asking us to fix a non sensical code which we don't understand what is supposed to do, maybe it's better to tell us what are you trying to achieve and ask for a small example.

Aieden Technologies

Need help? Join us on Telegram

Need premium support? Trade with us

 


@mindfulness