Topics
Replies
markmath01
28 Dec 2024, 12:49
( Updated at: 28 Dec 2024, 12:50 )
RE: RE: RE: MACD query - is the calculation correct in cTrader?
PanagiotisCharalampous said:
markmath01 said:
PanagiotisCharalampous said:
Hi there,
Please share the code you are using to print these values.
Best regards,
Panagiotis
Hi - see code below. Thanks.
protected override void OnStart()
{
_macd = Indicators.MacdCrossOver(SlowMAPeriod, FastMAPeriod, SignalLinePeriod);
Print("MACD-Analysis started with MACD parameters: Fast MA = {0}, Slow MA = {1}, Signal Line = {2}",
FastMAPeriod, SlowMAPeriod, SignalLinePeriod);
}protected override void OnBar()
{
double macdValue = _macd.MACD.LastValue;
//Print("MACD = {0:F7}, Time = {1}, {2}", macdValue, Bars.OpenTimes.LastValue, Bars.OpenTimes.Last(1));
//Print("MACD = {0:F7}", macdValue);
Print("Last MACD = {0:F7}, MACD = {1}, Last(0) = {2}, LastVal = {3}", lastMACD, macdValue, Bars.ClosePrices.Last(0), Bars.ClosePrices.LastValue);
// Check for zero-line crossing
if (lastMACD.HasValue)
{
if ((lastMACD < 0 && macdValue > 0) || (lastMACD > 0 && macdValue < 0))
{
// A zero-line crossing occurred
ProcessWave(macdValue < 0);
}
}lastMACD = macdValue; // Update the last MACD value
//Print("Last MACD = " + lastMACD) ;
}
Hi there,
You are printing the _macd.MACD.LastValue which prints the value of the current at the opening of the bar and then you compare it with the MA values of closed bars. The LastValue can change by the time the bar is closed. You should print _macd.MACD.Last(1) instead.
Best regards,
Panagiotis
I have checked the same piece of code in the “Calculate” event when drawing the Indicator and used it in various "On" events of a cBot and they do not perform equivalently, at least not in my calculations. Hence, the same piece of code will fire at different times and produce different results.
I have tried to perform the same calculation in OnTick, OnBar and OnBarClosed and NONE of them yield the same values that are correctly being produced in the “Calculate” event for an Indicator.
Can you guide please?
@markmath01
markmath01
03 Dec 2024, 09:09
RE: MACD query - is the calculation correct in cTrader?
PanagiotisCharalampous said:
Hi there,
Please share the code you are using to print these values.
Best regards,
Panagiotis
Hi - see code below. Thanks.
protected override void OnStart()
{
_macd = Indicators.MacdCrossOver(SlowMAPeriod, FastMAPeriod, SignalLinePeriod);
Print("MACD-Analysis started with MACD parameters: Fast MA = {0}, Slow MA = {1}, Signal Line = {2}",
FastMAPeriod, SlowMAPeriod, SignalLinePeriod);
}
protected override void OnBar()
{
double macdValue = _macd.MACD.LastValue;
//Print("MACD = {0:F7}, Time = {1}, {2}", macdValue, Bars.OpenTimes.LastValue, Bars.OpenTimes.Last(1));
//Print("MACD = {0:F7}", macdValue);
Print("Last MACD = {0:F7}, MACD = {1}, Last(0) = {2}, LastVal = {3}", lastMACD, macdValue, Bars.ClosePrices.Last(0), Bars.ClosePrices.LastValue);
// Check for zero-line crossing
if (lastMACD.HasValue)
{
if ((lastMACD < 0 && macdValue > 0) || (lastMACD > 0 && macdValue < 0))
{
// A zero-line crossing occurred
ProcessWave(macdValue < 0);
}
}
lastMACD = macdValue; // Update the last MACD value
//Print("Last MACD = " + lastMACD) ;
}
@markmath01
markmath01
02 Dec 2024, 18:38
Hi All,
I just wanted to add that there is definitely something strange / errorneous happening - and it doesn't appear to be with just the MACD.
I have just decided to output the previous and current MACD values, alongside the Bars.ClosePrices.Last(0) and Bars.ClosePrices.LastValue properties and they are definitely NOT Closing Prices.
They are OpenPrices.
See highlight below
Can someone shed some light as to what could be happening here as I can't see why the MACD is incorrect or why this is returning Open Prices and not Closing Prices?
Thanks
Mark
@markmath01
markmath01
09 Nov 2024, 18:01
( Updated at: 10 Nov 2024, 15:35 )
RE: Backtest: How to enter a trade at the Open of the *next* bar
firemyst said:
If you're programming a bot, use the OnBar() method.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using cAlgo.API;using cAlgo.API.Collections;using cAlgo.API.Indicators;using cAlgo.API.Internals;namespace cAlgo{ [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)] public class ThirdBarMarketOrderBot : Robot { private int barCount; protected override void OnStart() { barCount = 0; } protected override void OnBar() { barCount++; Print ("Bar count {0}", barCount); } }}
You can find more about it by googling “calgo onbar”
Thanks !
@markmath01
markmath01
02 Jan 2025, 09:01
RE: RE: RE: RE: RE: MACD query - is the calculation correct in cTrader?
PanagiotisCharalampous said:
Hi Panagiotis,
Firstly, the code has been changed.
Secondly, and most importantly, if you read my response carefully, you would have seen that the SAME code is now being used for both an Indicator and a cBOT. The SAME code.
Therefore, even if the code was wrong (which I do not believe it is any longer given your previous response), I would expect the same WRONG results to be produced for both the Indicator and the cBOT if the Calculate and various “On” events performed equivalently.
Like anyone else, I can easily create errors in my code but having been a developer for circa 40 years, I'm quite sure that the logic I have just referred to still holds true.
Many thanks,
Mark
@markmath01