herculesone

Info
Username: | herculesone |
Name: | herculesone |
Member since: | 02 Nov 2020 |
About
Signature
Last Forum Posts
@Multi time frame data: 12 Sep 2021, 15:40
I was able to solve this as I finally understood how the index and time are related with these functions and I have chosen to do the following:
private void EMAAnalysis(int index)
{
Bars shortChart = MarketData.GetBars(ShortMATimeFrame);
Bars longChart = MarketData.GetBars(LongMATimeFrame);
int shortIndex = shortChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(index));
int longIndex = longChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(index));
MovingAverage shortEMA = Indicators.MovingAverage(shortChart.ClosePrices, ShortMALength, ShortMAType);
MovingAverage longEMA = Indicators.MovingAverage(longChart.ClosePrices, LongMALength, LongMAType);
double shortResult = 0;
double longResult = 0;
for (int i = index; i < 100; i++)
{
shortResult = shortEMA.Result.Last(i);
if (!double.IsNaN(shortResult) && shortResult != 0)
{
Print("Short EMA = ", shortResult);
Print("Short Index = ", i);
break;
}
}
for (int i = index; i < 100; i++)
{
longResult = longEMA.Result.Last(i);
if (!double.IsNaN(longResult) && longResult != 0)
{
Print("Long EMA = ", longResult);
Print("Long Index = ", i);
break;
}
}
if (shortResult > longResult)
{
_longtrend = true;
}
if (shortResult < longResult)
{
_longtrend = false;
}
}
This way we look back at the real index of each bar until we get a number that is valid. Due to the way tick data is collected it seems that the NaN check needs to be there all the time, if not the values are sometimes all over the place as we get a lot of NaN and actual values.
Solved. Thank you!
@Loading multiple timeframes on indicator and painting: 11 Sep 2021, 18:18
Thank you very much for your response, I indeed discovered this property and it helped get the time I need still the readings are way off sometimes, I made another post about this today with some example code here:
cTDN Forum - Multi time frame data (ctrader.com)
Thank you again for your help!
@Multi time frame data: 11 Sep 2021, 18:15
Hey there peeps.
I have been having a real issue using multi timeframe EMAs, and extracting their data. Although the indicators paint well there are a lot of NaN values, and some crazy results.
Here is a function that does not return correct values, and I need to know what I am doing wrong. Your help is appreaciated.
Below is the function I have:
private void EMAAnalysis()
{
Bars shortChart = MarketData.GetBars(ShortMATimeFrame);
Bars longChart = MarketData.GetBars(LongMATimeFrame);
int shortIndex = shortChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(0));
int longIndex = longChart.OpenTimes.GetIndexByExactTime(Bars.OpenTimes.Last(0));
MovingAverage shortEMA = Indicators.MovingAverage(shortChart.ClosePrices, ShortMALength, ShortMAType);
MovingAverage longEMA = Indicators.MovingAverage(longChart.ClosePrices, LongMALength, LongMAType);
Print("Short EMA = ", shortEMA.Result.Last(shortIndex));
Print("Long EMA = ", longEMA.Result.Last(longIndex));
if (shortEMA.Result.Last(shortIndex) > longEMA.Result.Last(longIndex))
{
_longtrend = true;
}
if (shortEMA.Result.Last(shortIndex) < longEMA.Result.Last(longIndex))
{
_longtrend = false;
}
}
@Loading multiple timeframes on indicator and painting: 13 Aug 2021, 02:14
Hello there!
I would like some help to make an indicator, which is fairly simple but does not show as I want it.
I would like to show the closing prices of different timeframes on the same chart. However, I am unable to make the chart match the output I want to show.
Here is the code:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class Closes_4hr_D_W_M_Q_Y : Indicator
{
[Output("4 Hour", LineColor = "Green", PlotType = PlotType.Points, Thickness = 2)]
public IndicatorDataSeries Hour4 { get; set; }
[Output("Daily", LineColor = "Black", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
public IndicatorDataSeries Daily { get; set; }
[Output("Weekly", LineColor = "Red", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
public IndicatorDataSeries Weekly { get; set; }
[Output("Monthly", LineColor = "Navy", PlotType = PlotType.Line, LineStyle = LineStyle.Solid, Thickness = 2)]
public IndicatorDataSeries Monthly { get; set; }
public Bars _daily, _4h, _weekly, _month;
protected override void Initialize()
{
_4h = MarketData.GetBars(TimeFrame.Hour4);
_daily = MarketData.GetBars(TimeFrame.Daily);
_weekly = MarketData.GetBars(TimeFrame.Weekly);
_month = MarketData.GetBars(TimeFrame.Monthly);
}
public override void Calculate(int index)
{
Hour4[index] = _4h.ClosePrices[index];
Daily[index] = _daily.ClosePrices[index];
Weekly[index] = _weekly.ClosePrices[index];
Monthly[index] = _month.ClosePrices[index];
}
}
}
I would expect that on a 1 hour chart, the Daily output would show as a straight line until the next day, when there would be another straight line etc. Instead, the indicators paint further back as if they are attached to another chart.
It is like the "index" applies to the chart I am calling and not the "index" of the applicable chart, which does make sense in a way but I want to work around it and cannot figure out how.
In essence when I call "index" of a 1 hour chart, the price on the daily chart of the same index should be the same until all the hours are complete.
Any help is appreciated, thank you!
@VOLUME CALCULATION ON XAUUSD NOT CALCULATING CORRECTLY AFTER UPDATE TO 4.1: 10 Aug 2021, 16:31
amusleh said:
Hi,
Not sure what's your exact problem, but you can try this method to get volume in percentage:
/// <summary> /// Returns the amount of volume based on your provided risk percentage and stop loss /// </summary> /// <param name="symbol">The symbol</param> /// <param name="riskPercentage">Risk percentage amount</param> /// <param name="accountBalance">The account balance</param> /// <param name="stopLossInPips">Stop loss amount in Pips</param> /// <returns>double</returns> public double GetVolume(Symbol symbol, double riskPercentage, double accountBalance, double stopLossInPips) { return symbol.NormalizeVolumeInUnits(riskPercentage / (Math.Abs(stopLossInPips) * symbol.PipValue / accountBalance * 100)); }
Thank you for your response! :)
I will give this a try.
The problem is that on cTrader you have 2 different values for trade volume.
Quantity (lots)
Volume (in units)
On XAUUSD specifically, the conversion from Quantity to Volume does not work correctly.
@VOLUME CALCULATION ON XAUUSD NOT CALCULATING CORRECTLY AFTER UPDATE TO 4.1: 10 Aug 2021, 15:55
Hello there,
After the latest update, one of the trading bots I have created does not return the correct value on one particular instrument which is the XAUUSD.
Here is function that calculates the volume that is the entered in the methods that place and execute orders:
Function Inputs:
percent of the volume I want since I split the orders in multiples
stop loss of the position to calculate the lots based on a fixed risk % of the account
Parameters used:
[Parameter("Risk (%)", Group = "Risk Management", DefaultValue = 1, MinValue = 0.1, MaxValue = 100, Step = 0.05)]
public double Risk { get; set; }
[Parameter("Fixed Volume", Group = "Risk Management", DefaultValue = true)]
public bool FixedVolume { get; set; }
[Parameter("Volume (in lots)", Group = "Risk Management", DefaultValue = 0.01, MinValue = 0.01, Step = 0.01)]
public double Volume { get; set; }
Function:
protected double CalculateRisk(double percent, double stoploss)
{
if (!FixedVolume && stoploss != 0)
{
double risk = Account.Balance * (Risk / 100);
double riskPerTrade = risk * (percent / 100);
double lots = Math.Round(riskPerTrade / (Symbol.PipValue * stoploss), 2);
Print("Quantity in lots = ", lots);
double volume = Symbol.QuantityToVolumeInUnits(lots);
Print("Volume = ", volume);
if (volume < Symbol.VolumeInUnitsMin)
{
volume = Symbol.VolumeInUnitsMin;
}
if (volume > Symbol.VolumeInUnitsMax)
{
volume = Symbol.VolumeInUnitsMax;
}
return Symbol.NormalizeVolumeInUnits(volume, RoundingMode.ToNearest);
}
else
{
double lts = Volume * (percent / 100);
Print("Quantity in lots = ", lts);
double vol = Symbol.QuantityToVolumeInUnits(lts);
Print("Volume = ", vol);
if (stoploss == 0 || vol < Symbol.VolumeInUnitsMin)
{
vol = Symbol.VolumeInUnitsMin;
}
if (vol > Symbol.VolumeInUnitsMax)
{
vol = Symbol.VolumeInUnitsMax;
}
return Symbol.NormalizeVolumeInUnits(vol, RoundingMode.ToNearest);
}
}
Usage:
double volume = CalculateRisk(50, stoploss);
var trade = ExecuteMarketOrder(TradeType.Sell, SymbolName, volume, label);
Every instrument or when Fixed Lots are used, calculates correctly except on the XAUUSD when the risk is used, where it calculates maximum lots.
Your help is appreciated.
Thank you!