snowbird
Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
hi, need help with ninjascript of equivolume chart
Collapse
X
-
here is code
//
// Copyright (C) 2006, NinjaTrader LLC <www.ninjatrader.com>.
//
#region Using declarations
using System;
using System.ComponentModel;
using System.Drawing;
using System.Xml.Serialization;
#endregion
// This namespace holds all chart styles. Do not change it.
namespace NinjaTrader.Gui.Chart
{
/// <summary>
/// </summary>
public class CandleVolStyle : ChartStyles
{
private static bool registered = Chart.ChartStyle.Register(new CandleVolStyle());
private SolidBrush downBrush = new SolidBrush(Color.Red);
private SolidBrush upBrush = new SolidBrush(Color.LightGreen);
/// <summary>
/// </summary>
public CandleVolStyle()
: base(ChartStyleType.CandleStick)
{
this.DownColor = Color.Red;
this.UpColor = Color.LightGreen;
}
/// <summary>
/// </summary>
/// <returns></returns>
public override object Clone()
{
CandleVolStyle ret = new CandleVolStyle();
ret.BarWidth = BarWidth;
ret.DownColor = DownColor;
ret.Pen = Gui.Globals.Clone(Pen);
ret.Pen2 = Gui.Globals.Clone(Pen2);
ret.UpColor = UpColor;
return ret;
}
/// <summary>
/// </summary>
/// <returns></returns>
public override string DisplayName
{
get { return "CandlestickVol"; }
}
/// <summary>
/// </summary>
public override void Dispose()
{
base.Dispose();
downBrush.Dispose();
upBrush.Dispose();
}
/// <summary>
/// </summary>
/// <param name="barWidth"></param>
/// <returns></returns>
public override int GetBarPaintWidth(int barWidth)
{
// middle line + 2 * half of the body width + 2 * border line
return (int)(1 + 2 * (barWidth) + 2 * Pen.Width);
}
/// <summary>
/// </summary>
/// <param name="propertyDescriptor"></param>
/// <param name="chartStyle"></param>
/// <param name="attributes"></param>
/// <returns></returns>
public override PropertyDescriptorCollection GetProperties(PropertyDescriptor propertyDescriptor, ChartStyle chartStyle, Attribute[] attributes)
{
PropertyDescriptorCollection properties = base.GetProperties(propertyDescriptor, chartStyle, attributes);
// here is how you change the display name of the property on the properties grid
Gui.Design.DisplayNameAttribute.SetDisplayName(pro perties, "BarWidthUI", "\r\r\rBar width");
Gui.Design.DisplayNameAttribute.SetDisplayName(pro perties, "DownColor", "\r\r\rColor for down bars");
Gui.Design.DisplayNameAttribute.SetDisplayName(pro perties, "Pen", "\r\r\rCandle outline");
Gui.Design.DisplayNameAttribute.SetDisplayName(pro perties, "Pen2", "\r\r\rWick");
Gui.Design.DisplayNameAttribute.SetDisplayName(pro perties, "UpColor", "\r\r\rColor for up bars");
return properties;
}
/// <summary>
/// </summary>
public override bool IsTransparent
{
get { return UpColor == Color.Transparent && DownColor == Color.Transparent && Pen.Color == Color.Transparent; }
}
/// <summary>
/// </summary>
/// <param name="chartControl"></param>
/// <param name="graphics"></param>
/// <param name="bars"></param>
/// <param name="panelIdx"></param>
/// <param name="fromIdx"></param>
/// <param name="toIdx"></param>
/// <param name="bounds"></param>
/// <param name="max"></param>
/// <param name="min"></param>
public override void PaintBars(ChartControl chartControl, Graphics graphics, Data.Bars bars, int panelIdx, int fromIdx, int toIdx, Rectangle bounds, double max, double min)
{
if (downBrush.Color != DownColor)
downBrush.Color = DownColor;
if (upBrush.Color != UpColor)
upBrush.Color = UpColor;
Color barColor;
int barWidthValue = bars.BarsData.ChartStyle.BarWidthUI;
int barWidth;
SolidBrush brush = upBrush;
int close;
double closeValue;
int high;
int low;
Color oldPenColor = Pen.Color;
Color pen2Color = Pen2.Color;
int open;
double openValue;
int x;
//get avg vol
double avgVol = 0;
double maxVol = 0;
double minVol = 0;
for (int idx = fromIdx; idx <= toIdx; idx++)
{
if (maxVol < bars.GetVolume(idx))
{
maxVol = bars.GetVolume(idx);
}
if (minVol > bars.GetVolume(idx))
{
minVol = bars.GetVolume(idx);
}
avgVol = avgVol + bars.GetVolume(idx);
}
avgVol = avgVol / (double)toIdx;
//*********************
//scale bar width linearly with the volume
//y = k * x + m
//k=(y1-y2)/(x1-x2)
//m=y-k*x
//y=barwidth
//x=volume
//k=deltaBarwidth/deltaVol
double maxBarWidthDbl = (double)barWidthValue;
double minBarWidthDbl = 4.0;
double deltaBarWidth = maxBarWidthDbl - minBarWidthDbl;
double deltaVol = maxVol - minVol;
double k = deltaBarWidth / deltaVol;
//find m, plug in (minVol, minBarWidthDbl)
double m = minBarWidthDbl - k * minVol;
//done, now we can plug in any vol to get the bar width, y(x)=k*x+m
double dblBarwidth;
for (int idx = fromIdx; idx <= toIdx; idx++)
{
//calc dynamic bar width
dblBarwidth = k * bars.GetVolume(idx) + m;
barWidth = (int)(Math.Round(dblBarwidth, 0));
barColor = chartControl.GetBarColor(bars, idx);
// barWidth = GetBarPaintWidth(barWidthValue);
closeValue = bars.GetClose(idx);
close = chartControl.GetYByValue(bars, closeValue);
high = chartControl.GetYByValue(bars, bars.GetHigh(idx));
low = chartControl.GetYByValue(bars, bars.GetLow(idx));
openValue = bars.GetOpen(idx);
open = chartControl.GetYByValue(bars, openValue);
x = chartControl.GetXByBarIdx(bars, idx);
Color candleOutlineColorScript = chartControl.GetCandleOutlineColor(bars, idx);
bool isCandleOutlineColorScriptSet = (chartControl.GetCandleOutlineColor(bars, idx) != Color.Empty);
Pen.Color = oldPenColor;
if (barColor != Color.Empty)
Pen.Color = (candleOutlineColorScript != Color.Empty ? candleOutlineColorScript : barColor);
else if (candleOutlineColorScript != Color.Empty)
Pen.Color = candleOutlineColorScript;
if (open == close)
graphics.DrawLine(Pen, x - barWidth / 2, close, x + barWidth / 2, close);
else
{
brush = closeValue >= openValue ? upBrush : downBrush;
Color oldColor = brush.Color;
if (barColor != Color.Empty)
brush.Color = barColor;
graphics.FillRectangle(brush, x - barWidth / 2 + 1, Math.Min(close, open) + 1,
barWidth - 1, Math.Max(open, close) - Math.Min(close, open) - 1);
if (barColor != Color.Empty)
brush.Color = oldColor;
graphics.DrawRectangle(Pen, x - (barWidth / 2) + (Pen.Width / 2), Math.Min(close, open), barWidth - Pen.Width, Math.Max(open, close) - Math.Min(close, open));
}
Pen2.Color = isCandleOutlineColorScriptSet ? candleOutlineColorScript : pen2Color;
if (high < Math.Min(open, close))
graphics.DrawLine(Pen2, x, high, x, Math.Min(open, close));
if (low > Math.Max(open, close))
graphics.DrawLine(Pen2, x, low, x, Math.Max(open, close));
}
Pen.Color = oldPenColor;
Pen2.Color = pen2Color;
}
}
}
Comment
-
Hello neoikon,
The posted NinjaScript above will not work in NT7 as it is right now as it has compiling errors that need to be resolved.
This script was not developed by NinjaTrader, and I am not aware of any updated version of the script as well. I will leave this forum open for any members of the community or author has more information.JCNinjaTrader Customer Service
Comment
-
Originally posted by NinjaTrader_JC View PostThe posted NinjaScript above will not work in NT7 as it is right now as it has compiling errors that need to be resolved.
This script was not developed by NinjaTrader, and I am not aware of any updated version of the script as well. I will leave this forum open for any members of the community or author has more information.
So when it says "Copyright NinjaTrader" on the top, it doesn't necessarily mean it was developed by NT?
Thanks in advance for any help!
DanielAttached Files
Comment
-
Hello neoikon,
It could have been created using the @ChartStyles.cs file in the Type folder which could explain why it has the Copyright NinjaTrader at the top of the code.
That is correct on where you put the file, and I can confirm with that ".cs" file that you attached can compile. It is a chart style so it would be under the Chart style just below the Type. I have imported it in and it appears that it overrides the normal CandleStick Chart style as well so be wary of that when you are import in that file.
Let me know if I can be of further assistance.JCNinjaTrader Customer Service
Comment
Latest Posts
Collapse
Topics | Statistics | Last Post | ||
---|---|---|---|---|
Started by Aviram Y, Today, 05:29 AM
|
0 responses
1 view
0 likes
|
Last Post
by Aviram Y
Today, 05:29 AM
|
||
Started by quantismo, 04-17-2024, 05:13 PM
|
3 responses
25 views
0 likes
|
Last Post Today, 05:23 AM | ||
Started by ScottWalsh, 04-16-2024, 04:29 PM
|
7 responses
34 views
0 likes
|
Last Post Today, 05:15 AM | ||
Started by cls71, Today, 04:45 AM
|
0 responses
6 views
0 likes
|
Last Post
by cls71
Today, 04:45 AM
|
||
Started by mjairg, 07-20-2023, 11:57 PM
|
3 responses
216 views
1 like
|
Last Post
by PaulMohn
Today, 04:22 AM
|
Comment