Topics
Replies
bp2012
19 May 2014, 02:35
I think what you are looking for is example 2 here:
/api/reference/dataseries/last-895c
@bp2012
bp2012
19 May 2014, 02:26
mt4 uses index 0 to be the current bar, but ctrader uses a more traditional approach where index 0 is the first (oldest) bar in the series. Personally I prefer the ctrader perspective.
There is a bit of a learning curve for cTrader, but once you have a few indicators and robots under your belt I think that most people will find it superior to other platforms.
The information you are looking for can be found below. I've pasted in the relevant section and a link to the source. I'd recommend writing indicators before doing heavy lifting on a robot. (Just my 2 cents.)
Good Luck!
/api/guides/indicators
Calculate
The calculate method is the main method of the indicator. It is called for each historic bar starting from the beginning of the series up to the current bar and then on each incoming tick. In the case of multi-symbol / multi-timeframe implementation, it will be called on each tick of each symbol that is used in the indicator.
It is typically used for the calculation of an indicator based on a formula. In this case, it is the difference of the High and the Low prices of each bar. The index parameter, represents each bar, ranging from 0 up to the current (last) bar. So, at each bar the Calculate method will be called and the Result will be assigned with the difference between the High and the Low of that bar.
@bp2012
bp2012
19 May 2014, 02:15
There are a couple things you must do to fix the issue. Please see the code below. It shows how to declare and use a struct.
using System; using cAlgo.API; using cAlgo.API.Indicators; namespace cAlgo.Indicators { [Indicator(IsOverlay = false)] public class TestIndicator : Indicator { [Parameter(DefaultValue = 0.0)] public double Parameter { get; set; } [Output("Main")] public IndicatorDataSeries Result { get; set; } protected override void Initialize() { // Initialize and create nested indicators cena a = new cena(); a.poprzednia = Symbol.Bid; a.obecna = Symbol.Bid; a.schowek = Symbol.Bid; } public override void Calculate(int index) { // Calculate value at specified index // Result[index] = ... } } public struct cena { public double poprzednia; public double obecna; public double schowek; } }
@bp2012
bp2012
08 Dec 2013, 23:23
"You do not need selected chart timezone for this indicator. Just specify NY time zone for the indicator and all timestamps will be present in this timezone, so you can easily check that time is between 16:30 and 17:00 NY:"
Thank you for this! I had an incorrect perspective about how the timezone translation works in cTrader. In MT4 any timezone translation has to be done by hand. I see that cTrader handles all of the translation for us behind the scenes. I experimented by calling the drawline method that takes a date in the parameter list. Then I changed to different timezones in my sample indicator and in the cTrader chart. Everything worked flawlessly. This is fantastic! I'm impressed with work done for us behind the scenes in this area. Dealing with TimeZone issues (daylight savings time, etc) in MT4 makes me cringe.
"So you want to draw an arrow let's say 5 pixels above the bar. You are right, this is impossible now. But if we implement access to the chart scale (this is not easy for us right now), we will have to run your code every time the chart is scrolled or updated, which is too frequent. So, we recommend to use text for such drawings:"
That is almost exactly how I do it today, except that I add a vertical offset based on the chart timeframe. The offset is smaller for shorter timeframes and larger for longer timeframe. To be honest it is just a little extra overhead in my coding and not a very big deal. It's not exact, but it is close enough.
"The example is rather clear. To be honest, we prefer to provide proper solution here, not API workarounds. But we will also consider implementing your suggestions."
I'm with you. I'd rather that Spotware provide a solution rather than have to scale each of the consolidated indicators myself ;) Any API workaround would be messy and I'm sure that it would not perform as well as something the cAlgo/cTrader team could put together.
It's always challenging to develop features that appeal to a broad audience so I'd expect that indicator developers would like to be able to choose the scaling methodology when adding multiple indicators to a single window pane. For example, fixed scaling would take the highest and lowest displayed values out of all the indicators for scaling. This is useful when the indicators all have relatively the same magnitude and differences between the values are important. Independent scaling would be scaling each indicator separately (just as if you were to overlay indicator windows over each other). This is useful when indicators have different orders of magnitude and direction, slope, and other behaviors are most important than differences in indicator values. That's my 2 cents for what it's worth.
Keep up the great work. This product just keeps getting better and better. Now, if you could only get a US broker to see the light....
@bp2012
bp2012
03 Dec 2013, 23:37
No problem. For the TimeZone request, it would apply to any indicator that makes use of a specific time. For example one that looks for triggers that take place during certain times such as the last 30 minutes of New York trading. Knowing the selected timezone for the chart on which the indicator is displayed is necessary.
The Price scale is needed when placing an arrow above or below a price bar. Vertical positioning is determined by price. I could simply apply an offset using a multiple of Symbol.PipSize. The trouble comes in when changing TimeFrames. On a one hour chart 5 pips might have the arrow look like it is sitting on the bar. Whereas on a 1M chart 5 pips might have the arrow off of the screen. Now imagine trying to place the arrow around a BollingerBand indicator. Being able to fine tune exactly where the arrow is displayed would be ideal. It would allow the arrow to be placed in relatively the same position regardless of the chart timeframe.
Visible bars are needed as a work around to being able to display two indicators in the same window pane. Having a separate pane for multiple indicators can take up valuable space. For example, I might want to display a MACD histogram and an oscillator like RSI in the same pane. That takes up a lot less space than having a separate window for the rsi. The problem is that scaling of the rsi might make the MACD too small to see. In Metatrader 4 I would simply throw both indicators into the same window. In cTrader I can't do that. As a workaround I can create a custom indicator that contains both the macd and rsi. Which leads to the problem of vertical scaling. I need to scale one or both of the indicators so that they display properly. Having the number of visible bars would allow me to grab the highs and lows of both indicators and manually scale as needed. I'll readily admit that it will be ugly. But as a workaround it would work.
Hope that helps! Please let me know if I can provide additional clarification.
@bp2012
bp2012
02 Dec 2013, 16:27
It's definitely not a big priority. However, I have a moderately complex indicator that performs the historical heavy lifting in the initialize method. Performing the code on each index in the calculate method would cause a big delay on load. So I use isRealTime in the calculate method to exit the method if it is being call on historical data. In a nutshell, I can quickly run the code on the data needed in the initialize method and then process the new data as it comes through in the calculate method.
The calculate method will alert when specific triggers are met from that time forward. However, when just starting up it is good to alert the user if a trigger was met in the last x bars.
Thanks for the follow up!
@bp2012
bp2012
01 Dec 2013, 21:22
Is there a specific design reason for this behavior? Otherwise, I'd say that it is a defect. Whether or not it is a priority for you to fix is of course completely up to you. It's a low priority for me personally since messageboxes can be displayed in the initialize method.
@bp2012
bp2012
21 Sep 2013, 22:38
Thanks Yassine. I'm pretty sure that MTF data was available for indicators on weekends when the functionality first came out. I think something happened in one of the more recent updates.
I tried running the MultiTF_MA indicator provided as a reference and it fails too. Spotware, can you comment on the availability of MTF data on Saturdays and Sundays? It makes sense that Robots should not try to run on non-trading days, but surely indicators should still work. I do most of my coding when the markets are closed as I would prefer to spend my time trading when markets are open. If I have to choose between the two, I suspect that I won't be developing many mtf indicators in the near future.
-bp
@bp2012
bp2012
21 Sep 2013, 20:23
If you're looking at a small period moving average (like 3 periods), it's going to fluctuate significantly with the change in price. For example at 16:15 the moving average turns down, it might look like you've hit a peak, then in the last 10 minutes of the hour a huge spike jumps the price up. Then your moving average no longer looks like a peak. Your peak gets erased and it looks like an increasing line.
The situation you are experiencing is one of the reasons that backtesting using only the bar close can give you very different results than backtesting using tick data. Your indicators can change significantly between the open and close of each bar.
Hope that helps and best of luck in your indicator development.
-bp
@bp2012
bp2012
05 Sep 2013, 16:52
Hi gorin,
I'm still wrapping my head around the multi timeframe functionality as well, but thought I would post a quick comment. It looks like your code is only taking the index differences into consideration when it comes to displaying the results. I think that it also needs to be considered when calculating.
Here is my understanding. Assume that you have 7000 bars available on a 1M chart marketseries. That marketseries represents 7000 minutes. Let's say you pull a marketseries for the Minute5 timeframe. It doesn't automatically pull only the bars that match your current chart time span. It may pull 8000 bars based on the data available. 8000 bars on a 5M series represents a much larger time span than is in your Minute1 chart. Therefore, if you use the index supplied in the calculate method you aren't going to get the results you expect. Hopefully this code snippet will make it clearer. If you put this in cAlgo and set your instance timeframe to Minute, the log will show the time differences between the chart data and the other time frame data.
using System; using cAlgo.API; using cAlgo.API.Indicators; using cAlgo.API.Internals; namespace cAlgo.Indicators { [Indicator(IsOverlay = true)] public class TFDataTest : Indicator { [Parameter(DefaultValue = "Minute5")] public TimeFrame Period { get; set; } private MarketSeries ms; protected override void Initialize() { ms = MarketData.GetSeries(Period); Print("Chart Time Frame: " + TimeFrame + " Data Time Frame: " + ms.TimeFrame); Print("Chart Bars: " + MarketSeries.Close.Count + " Data Bars: " + ms.Close.Count); Print("Index 1000 Chart time: " + MarketSeries.OpenTime[1000].ToString("dd MMMM yyyy H:mm:ss") + " Index 1000 Data time: " + ms.OpenTime[1000].ToString("dd MMMM yyyy H:mm:ss")); } public override void Calculate(int index) { } } }
Good Luck!
- bp
@bp2012
bp2012
01 Sep 2013, 01:00
I am seeing Daily Bars for Non Trading days on the demo environment. For example, there are bars for EURUSD on Aug 17 and August 31. (Screenshots below.) Both bars have a volume of 2.
The same data comes through when pulling from the MarketSeries in cAlgo. It throws off indicators that are looking to pull differences between the bars.
For now I can code around the bars and simply exclude those that are outside trading hours, but I'd hate to have to do this for every indicator. If there is some settings that I can adjust to resolve the issue I would greatly appreciate the information.
Thanks in advance for your help!
- bp
@bp2012
bp2012
22 May 2014, 16:05
Thank you!
@bp2012