Using candle body range as source for SimpleMovingAverage
Using candle body range as source for SimpleMovingAverage
05 Jan 2024, 09:43
Hello, I am new to cTrader having used TradingView for a number of years. I am having difficulty converting the simple pinescript code shown below over to ctrader.
I have so far:
var bar = Bars[0];
var body = Math.Abs(bar.Close - bar.Open);
but if I try to do:
var body_sma = Indicators.SimpleMovingAverage(body, 100) * 3.0;
I get the error: cannot convert from ’double' to ‘cAlgo.API.DataSeries’
How would I go about replicating the sma value?
indicator('Test', overlay=true)
body = math.abs(close - open)
body_sma = ta.sma(body, 100) * 3.0
buy = false
if close < open and body > body_sma
buy := true
plotshape(buy, style=shape.arrowup, color=color.new(color.green, 0), location=location.belowbar)
Replies
Cwebhook
05 Jan 2024, 14:31
( Updated at: 06 Jan 2024, 08:16 )
RE: Using candle body range as source for SimpleMovingAverage
ctrader.guru said:
I'm not sure what you want to do, but start with this approach, if you explain better what you want to achieve I can help you better
using cAlgo.API;using cAlgo.API.Indicators;using System;namespace cAlgo{ [Indicator(IsOverlay = true, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)] public class test : Indicator { protected override void Initialize() { SimpleMovingAverage sma = Indicators.SimpleMovingAverage(Bars.ClosePrices, 100); double body = Math.Abs(Bars.ClosePrices.Last(0) - Bars.OpenPrices.Last(0)); double body_sma = sma.Result.Last(0) * 3.0; } public override void Calculate(int index) { } } }
Thanks for the reply.
The body var returns a pip value of the current candle, in tradingview that pip value is passed directly to the sma function which then returns an average pip value, for example if I log the output to the data window these are those 2 values, which are changing in realtime with tick data. I'm trying to achieve the same in ctrader but no idea how to.
@Cwebhook
ctrader.guru
06 Jan 2024, 08:46
I think I understand, you don't need to manipulate the SMA, you can recreate it, this is a simple example:
using System;
using cAlgo.API;
using cAlgo.API.Internals;
namespace cAlgo
{
[Indicator(IsOverlay = false, AccessRights = AccessRights.None)]
[Levels(0.0005)]
public class BodySMA : Indicator
{
[Parameter("Period", DefaultValue = 10, MinValue = 2)]
public int Period { get; set; }
[Output("Average", LineColor = "DodgerBlue")]
public IndicatorDataSeries Average { get; set; }
protected override void Initialize()
{
// TODO
}
public override void Calculate(int index)
{
if (index < Period) return;
double sum = 0;
for (int i = index - Period + 1; i <= index; i++)
{
sum += Math.Abs( Bars.ClosePrices[i] - Bars.OpenPrices[i]);
}
Average[index] = Math.Round( sum / Period, Symbol.Digits);
}
}
}
@ctrader.guru
Cwebhook
10 Jan 2024, 12:56
RE: Using candle body range as source for SimpleMovingAverage
ctrader.guru said:
I think I understand, you don't need to manipulate the SMA, you can recreate it, this is a simple example:
using System;using cAlgo.API;using cAlgo.API.Internals;namespace cAlgo{ [Indicator(IsOverlay = false, AccessRights = AccessRights.None)] [Levels(0.0005)] public class BodySMA : Indicator { [Parameter("Period", DefaultValue = 10, MinValue = 2)] public int Period { get; set; } [Output("Average", LineColor = "DodgerBlue")] public IndicatorDataSeries Average { get; set; } protected override void Initialize() { // TODO } public override void Calculate(int index) { if (index < Period) return; double sum = 0; for (int i = index - Period + 1; i <= index; i++) { sum += Math.Abs( Bars.ClosePrices[i] - Bars.OpenPrices[i]); } Average[index] = Math.Round( sum / Period, Symbol.Digits); } }}
Thank you! that is what I was after.
@Cwebhook
ctrader.guru
05 Jan 2024, 12:14
I'm not sure what you want to do, but start with this approach, if you explain better what you want to achieve I can help you better
@ctrader.guru