ninjascript i have
using System;
using NinjaTrader.Cbi;
namespace NinjaTrader.NinjaScript.Strategies
{
public class MySupertrendStrategy : Strategy
{
private double AtrMult = 0.7;
private int nATR = 4;
private AverageType AvgType = AverageType.Hull;
private bool PaintBars = true;
private bool showsignals = true;
private double ATR(double[] high, double[] close, double[] low)
{
return SMA(Typical, nATR)[0];
}
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = "Supertrend Strategy";
Name = "MySupertrendStrategy";
Calculate = Calculate.OnEachTick;
IsOverlay = true;
}
else if (State == State.Configure)
{
AddPlot(Brushes.White, "SupertrendUp");
AddPlot(Brushes.Cyan, "SupertrendDown");
}
}
protected override void OnBarUpdate()
{
double ATRValue = ATR(Highs, Closes, Lows);
double UPValue = Highs[0] + (AtrMult * ATRValue);
double DNValue = Highs[0] + (-AtrMult * ATRValue);
double STValue = Close[0] < STValue[1] ? UPValue : DNValue;
PlotBrushes[0][0] = Brushes.Transparent;
PlotBrushes[1][0] = Brushes.Transparent;
if (Close[1] > Close[2])
PlotBrushes[0][0] = Brushes.Green;
else if (Close[1] < Close[2])
PlotBrushes[1][0] = Brushes.Red;
if (STValue.CrossBelow(Close[0], 1))
{
PlotBrushes[0][0] = Brushes.White;
if (PaintBars && Close[0] < STValue)
BarBrush = Brushes.Red;
}
else if (STValue.CrossAbove(Close[0], 1))
{
PlotBrushes[1][0] = Brushes.Cyan;
if (PaintBars && Close[0] > STValue)
BarBrush = Brushes.Green;
}
if (Close[1] < Open[1] && Close[0] > Open[0] && PlotBrushes[0][0] == Brushes.Green)
Draw.ArrowUp(this, "MyUpArrow" + CurrentBar, true, 0, Lows[0] - TickSize, Brushes.Green);
else if (Close[1] > Open[1] && Close[0] < Open[0] && PlotBrushes[1][0] == Brushes.Red)
Draw.ArrowDown(this, "MyDownArrow" + CurrentBar, true, 0, Highs[0] + TickSize, Brushes.Red);
}
}
}
thinkorswim script
input AtrMult = .7;
input nATR = 4;
input AvgType = AverageType.HULL;
input PaintBars = yes;
input showsignals = yes;
#def
def ATR = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP = HL2 + (AtrMult * ATR);
def DN = HL2 + (-AtrMult * ATR);
def ST = if close < ST[1] then UP else DN;
def Vinculum = ST;
def plotlong = if ST crosses below close then 1 else 0;
def plotshort = if ST crosses above close then 1 else 0;
plots
plot x = plotlong;
Alert (plotlong, Sound.Ring);
x.SetPaintingStrategy(paintingStrategy = PaintingStrategy.BOOLEAN_ARROW_UP);
AssignPriceColor(if x then Color.WHITE else Color.CYAN);
plot y = plotshort;
Alert (Vinculum, Sound.Ring);
y.SetPaintingStrategy(paintingStrategy = PaintingStrategy.BOOLEAN_ARROW_DOWN);
AssignPriceColor(if x then Color.CYAN else Color.WHITE);
x.AssignValueColor(if close < ST then Color.RED else Color.CYAN);
AssignPriceColor(if PaintBars and close < ST
then Color.RED
else if PaintBars and close > ST
then Color.GREEN
else Color.CURRENT);
x.SetHiding(!showsignals);
y.SetHiding(!showsignals);
def ATR1 = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP1 = HL2 + (AtrMult * ATR1);
def DN1 = HL2 + (-AtrMult * ATR1);
def ST1 = if close < ST[1] then UP1 else DN1;
def trendChangeUp = ST1 crosses above ST[1];
def closeHigherAfterTrendChange = close > open and close[1] > open[1];
def trendIsUp = ST1 > ST[1];
plot BuySignal = trendChangeUp and closeHigherAfterTrendChange and trendIsUp;
BuySignal.SetPaintingStrategy(PaintingStrategy.BOO LEAN_ARROW_UP);
BuySignal.SetDefaultColor(Color.GREEN);
BuySignal.SetLineWeight(2);
##### down #####
def ATR2 = MovingAverage(AvgType, TrueRange(high, close, low), nATR);
def UP2 = HL2 + (AtrMult * ATR2);
def DN2 = HL2 + (-AtrMult * ATR2);
def ST2 = if close > ST[1] then dn2 else up2;
def trendChangedn = ST2 crosses below ST[1];
def closelowerAfterTrendChange = close < open and close[1] < open[1];
def trendIsdn = ST2 < ST[1];
plot BuySignal1 = trendChangedn and closelowerAfterTrendChange and trendIsdn;
BuySignal1.SetPaintingStrategy(PaintingStrategy.BO OLEAN_ARROW_down);
BuySignal1.SetDefaultColor(Color.red);
BuySignal1.SetLineWeight(2);

Comment