Description
This is just a simple multitimeframe VWAP with deviation bands
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class VWAP : Indicator
{
[Parameter("TimeFrame")]
public TimeFrame TF { get; set; }
[Parameter("Show Interruptions", DefaultValue = false)]
public bool ShowInterruptions { get; set; }
[Parameter("Show Standard Deviations", Group = "Standard Deviations", DefaultValue = false)]
public bool ShowSTDs { get; set; }
[Parameter("First Multiplier", Group = "Standard Deviations", DefaultValue = 1)]
public double STD1M { get; set; }
[Parameter("Second Multiplier", Group = "Standard Deviations", DefaultValue = 2)]
public double STD2M { get; set; }
[Parameter("Third Multiplier", Group = "Standard Deviations", DefaultValue = 3)]
public double STD3M { get; set; }
[Output("Main", Color = Colors.Orange, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries Result { get; set; }
[Output("Upper Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD1U { get; set; }
[Output("Lower Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD1D { get; set; }
[Output("Upper Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD2U { get; set; }
[Output("Lower Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD2D { get; set; }
[Output("Upper Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD3U { get; set; }
[Output("Lower Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD3D { get; set; }
[Parameter("Build Lines", Group = "Lines", DefaultValue = false)]
public bool BuilLines { get; set; }
public IndicatorDataSeries tpv;
private MarketSeries Series;
private int LastStartIndex = 0;
protected override void Initialize()
{
Series = MarketData.GetSeries(TF);
tpv = CreateDataSeries();
}
public override void Calculate(int index)
{
tpv[index] = MarketSeries.Close[index] * MarketSeries.TickVolume[index];
int startIndex = MarketSeries.OpenTime.GetIndexByTime(Series.OpenTime[Series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);
if (startIndex > LastStartIndex)
{
if (ShowInterruptions)
Chart.DrawIcon("Interruption " + startIndex, ChartIconType.Diamond, startIndex, Result[startIndex - 1], Color.Yellow);
}
double cptv = 0, cv = 0;
for (int i = index; i >= startIndex; --i)
{
cptv += tpv[i];
cv += MarketSeries.TickVolume[i];
}
Result[index] = cptv / cv;
//Standard deviation
if (ShowSTDs)
{
double squaredErrors = 0;
for (int i = index; i >= startIndex; --i)
{
squaredErrors += Math.Pow(MarketSeries.Close[i] - Result[index], 2);
}
squaredErrors /= (index - startIndex + 1);
squaredErrors = Math.Sqrt(squaredErrors);
STD1U[index] = squaredErrors * STD1M + Result[index];
STD1D[index] = Result[index] - squaredErrors * STD1M;
STD2U[index] = squaredErrors * STD2M + Result[index];
STD2D[index] = Result[index] - squaredErrors * STD2M;
STD3U[index] = squaredErrors * STD3M + Result[index];
STD3D[index] = Result[index] - squaredErrors * STD3M;
}
//Build lines
if (BuilLines)
Chart.DrawTrendLine("Fixed " + startIndex, startIndex, Result[startIndex - 1], index, Result[startIndex - 1], Color.Yellow);
}
}
}
cysecsbin.01
Joined on 10.11.2018 Blocked
- Distribution: Free
- Language: C#
- Trading platform: cTrader Automate
- File name: VWAP.algo
- Rating: 5
- Installs: 4961
- Modified: 13/10/2021 09:54
Comments
Thanks for the work put in and I will definitely give it a bash and see what you have created. Thank you for your contributions. :)
@moiz.forex You're gonna have to be a bit more specific. An image link would be ideal. But the short answer is that this is not tradingview and the goal isn't to be tradingview. However if there is an element to the indicator on another platform that you would l;like to point out as a suggestion then pls include the name and author of the indicator, a link to an image marked up with suggested areas of interest as well as textual description in as much detail as possible as to the exact functionality that you are suggesting incorporating. Simply saying "its not like tradingview one" is not very useful. Thanks.
Hi Bot guys, My question is why i dont see the upper and lower band in Vwap as i see that in trading view can someone tell me what i am doing wrong. this indicator shows everything and all bands but how can i use this indicator as what i see in trading view
Hi there, a generous person on the cTrader Telegram who goes by the name /DarthMaulAtWork/ showed me what to do. Thanks again!
I copied and pasted the code below. It only highlights the 1sd but if you add:
[Cloud("Upper Deviation 2", "Lower Deviation 2")]
and/or
[Cloud("Upper Deviation 3", "Lower Deviation 3")]
below [Cloud("Upper Deviation 1", "Lower Deviation 1")]
you can shade the 2nd and 3rd Std Deviation bands too.
Thank you for the coding of this indicator, it is amazing!!!
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;
using cAlgo.Indicators;
namespace cAlgo
{
[Cloud("Upper Deviation 1", "Lower Deviation 1")]
[Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class VWAPCloud : Indicator
{
[Parameter("TimeFrame")]
public TimeFrame TF { get; set; }
[Parameter("Show Interruptions", DefaultValue = false)]
public bool ShowInterruptions { get; set; }
[Parameter("Show Standard Deviations", Group = "Standard Deviations", DefaultValue = false)]
public bool ShowSTDs { get; set; }
[Parameter("First Multiplier", Group = "Standard Deviations", DefaultValue = 1)]
public double STD1M { get; set; }
[Parameter("Second Multiplier", Group = "Standard Deviations", DefaultValue = 2)]
public double STD2M { get; set; }
[Parameter("Third Multiplier", Group = "Standard Deviations", DefaultValue = 3)]
public double STD3M { get; set; }
[Output("Main", Color = Colors.Orange, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries Result { get; set; }
[Output("Upper Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD1U { get; set; }
[Output("Lower Deviation 1", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD1D { get; set; }
[Output("Upper Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD2U { get; set; }
[Output("Lower Deviation 2", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD2D { get; set; }
[Output("Upper Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD3U { get; set; }
[Output("Lower Deviation 3", Color = Colors.RoyalBlue, Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD3D { get; set; }
[Parameter("Build Lines", Group = "Lines", DefaultValue = false)]
public bool BuilLines { get; set; }
public IndicatorDataSeries tpv;
private MarketSeries Series;
private int LastStartIndex = 0;
protected override void Initialize()
{
Series = MarketData.GetSeries(TF);
tpv = CreateDataSeries();
}
public override void Calculate(int index)
{
tpv[index] = MarketSeries.Close[index] * MarketSeries.TickVolume[index];
int startIndex = MarketSeries.OpenTime.GetIndexByTime(Series.OpenTime[Series.OpenTime.GetIndexByTime(MarketSeries.OpenTime[index])]);
if (startIndex > LastStartIndex)
{
if (ShowInterruptions)
Chart.DrawIcon("Interruption " + startIndex, ChartIconType.Diamond, startIndex, Result[startIndex - 1], Color.Yellow);
}
double cptv = 0, cv = 0;
for (int i = index; i >= startIndex; --i)
{
cptv += tpv[i];
cv += MarketSeries.TickVolume[i];
}
Result[index] = cptv / cv;
//Standard deviation
if (ShowSTDs)
{
double squaredErrors = 0;
for (int i = index; i >= startIndex; --i)
{
squaredErrors += Math.Pow(MarketSeries.Close[i] - Result[index], 2);
}
squaredErrors /= (index - startIndex + 1);
squaredErrors = Math.Sqrt(squaredErrors);
STD1U[index] = squaredErrors * STD1M + Result[index];
STD1D[index] = Result[index] - squaredErrors * STD1M;
STD2U[index] = squaredErrors * STD2M + Result[index];
STD2D[index] = Result[index] - squaredErrors * STD2M;
STD3U[index] = squaredErrors * STD3M + Result[index];
STD3D[index] = Result[index] - squaredErrors * STD3M;
}
//Build lines
if (BuilLines)
Chart.DrawTrendLine("Fixed " + startIndex, startIndex, Result[startIndex - 1], index, Result[startIndex - 1], Color.Yellow);
}
}
}
Hi there,
Thank you for this awesome indicator!
Ctrader now enables you to draw semi-transparent clouds between two indicator lines. I was wondering if you could draw a cloud between the Upper and Lower 1SD VWAP Bands if possible.
Happy to pay for this to be done.
Please let me know.
Love your work.
Thank you
JS
i did use chatgpt to update this one
using System;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
namespace cAlgo
{
[Cloud("Upper Deviation 1", "Lower Deviation 1")]
[Indicator(IsOverlay = true, AutoRescale = false, TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class VWAPCloud : Indicator
{
[Parameter("TimeFrame", DefaultValue = "Daily")]
public TimeFrame TF { get; set; }
[Parameter("Show Interruptions", DefaultValue = false)]
public bool ShowInterruptions { get; set; }
[Parameter("Show Standard Deviations", Group = "Standard Deviations", DefaultValue = false)]
public bool ShowSTDs { get; set; }
[Parameter("First Multiplier", Group = "Standard Deviations", DefaultValue = 1)]
public double STD1M { get; set; }
[Parameter("Second Multiplier", Group = "Standard Deviations", DefaultValue = 2)]
public double STD2M { get; set; }
[Parameter("Third Multiplier", Group = "Standard Deviations", DefaultValue = 3)]
public double STD3M { get; set; }
[Output("Main", LineColor = "Orange", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries Result { get; set; }
[Output("Upper Deviation 1", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD1U { get; set; }
[Output("Lower Deviation 1", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD1D { get; set; }
[Output("Upper Deviation 2", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD2U { get; set; }
[Output("Lower Deviation 2", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD2D { get; set; }
[Output("Upper Deviation 3", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD3U { get; set; }
[Output("Lower Deviation 3", LineColor = "RoyalBlue", Thickness = 2, PlotType = PlotType.DiscontinuousLine)]
public IndicatorDataSeries STD3D { get; set; }
[Parameter("Build Lines", Group = "Lines", DefaultValue = false)]
public bool BuildLines { get; set; }
private IndicatorDataSeries tpv;
private int lastStartIndex = 0;
protected override void Initialize()
{
// Create the typical price volume series
tpv = CreateDataSeries();
}
public override void Calculate(int index)
{
// Calculate TPV (typical price volume)
tpv[index] = Bars.ClosePrices[index] * Bars.TickVolumes[index];
// Determine the starting index of the new session based on timeframes
int startIndex = GetStartIndex(index);
// Handle interruptions between different periods
if (startIndex > lastStartIndex && ShowInterruptions)
{
Chart.DrawIcon("Interruption " + startIndex, ChartIconType.Diamond, startIndex, Result[startIndex - 1], Color.Yellow);
}
// Calculate cumulative price-volume and volume within the session
double cumulativeTPV = 0, cumulativeVolume = 0;
for (int i = index; i >= startIndex; i--)
{
cumulativeTPV += tpv[i];
cumulativeVolume += Bars.TickVolumes[i];
}
// VWAP calculation (typical price weighted by volume)
Result[index] = cumulativeTPV / cumulativeVolume;
// Standard deviation calculations (if enabled)
if (ShowSTDs)
{
double squaredErrors = 0;
for (int i = index; i >= startIndex; i--)
{
squaredErrors += Math.Pow(Bars.ClosePrices[i] - Result[index], 2);
}
squaredErrors /= (index - startIndex + 1);
squaredErrors = Math.Sqrt(squaredErrors);
// Setting upper and lower bands for each deviation level
STD1U[index] = squaredErrors * STD1M + Result[index];
STD1D[index] = Result[index] - squaredErrors * STD1M;
STD2U[index] = squaredErrors * STD2M + Result[index];
STD2D[index] = Result[index] - squaredErrors * STD2M;
STD3U[index] = squaredErrors * STD3M + Result[index];
STD3D[index] = Result[index] - squaredErrors * STD3M;
}
// Drawing fixed lines if the option is enabled
if (BuildLines)
{
Chart.DrawTrendLine("Fixed " + startIndex, startIndex, Result[startIndex - 1], index, Result[startIndex - 1], Color.Yellow);
}
}
private int GetStartIndex(int index)
{
// Retrieve the start index based on the chosen time frame
DateTime currentOpenTime = Bars.OpenTimes[index];
for (int i = index; i >= 0; i--)
{
if (Bars.OpenTimes[i].Day != currentOpenTime.Day)
{
return i + 1;
}
}
return 0; // Default to zero if no change found
}
}
}