Code Snippets

View as Markdown

Code Snippets

Code Snippets can provide you with useful code templates to speed up your coding process.

Understanding Code Snippet shortcuts

ShortcutMethod Signature
dapDraw.AndrewsPitchfork(this, "MyAndrewsPitchfork", false, 10, Close[10], 5, High[5], 0, Low[5], Brushes.Blue, DashStyleHelper.Solid, 1);
daDraw.Arc(this, "MyDrawArc", false, 10, Close[10], 0, Close[0], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
ddDraw.ArrowDown(this, "MyArrowDown", false, 0, High[0], Brushes.Red);
duDraw.ArrowUp(this, "MyArrowUp", false, 0, Low[0], Brushes.Red);
ddiDraw.Diamond(this, "MyDiamond", false, 0, High[0] + 2 * TickSize, Brushes.Blue);
dtDraw.Dot(this, "MyDot", false, 0, High[0] + 2 * TickSize, Brushes.Blue);
deDraw.Ellipse(this, "MyEllipse", 10, Low[10], 0, High[0], Brushes.Blue);
diDraw.ExtendedLine(this, "MyExtendedLine", 10, Close[10], 0, Close[0], Brushes.Blue);
dfcDraw.FibonacciCircle(this, "MyFibonacciCircle", true, 10, Close[10], 0, Close[0]);
dfeDraw.FibonacciExtensions(this, "MyFibonacciExtensions", true, 15, Close[15], 10, Close[10], 5, Close[5]);
dfrDraw.FibonacciRetracements(this, "MyFibonacciRetracements", false, 10, Close[10], 0, Close[0]);
dftDraw.FibonacciTimeExtensions(this, "MyFibonacciTimeExtensions", false, 10, Close[10], 0, Close[0]);
dgDraw.GannFan(this, "MyGannFan", true, 10, Close[10]);
dhDraw.HorizontalLine(this, "MyHorizontalLine", Close[0], Brushes.Blue);
dlDraw.Line(this, "MyLine", 10, Close[10], 0, Close[0], Brushes.Blue);
dyDraw.Ray(this, "MyRay", 10, Close[10], 0, Close[0], Brushes.Blue);
drDraw.Rectangle(this, "MyRectangle", 10, Low[10], 0, High[0], Brushes.Blue);
dreDraw.Region(this, CurrentBar, 0, Bollinger(2, 14).Upper, Bollinger(2, 14).Lower, Brushes.Green, Brushes.Blue, 50);
drxDraw.RegionHighlightX(this, "MyRegionHighlightX", 10, 0, Brushes.Blue);
dryDraw.RegionHighlightY(this, "MyRegionHighlightY", High[0], Low[0], Brushes.Blue, Brushes.Green, 20);
drrDraw.RiskReward(this, "MyRiskReward", false, 0, High[0], 10, Low[0], 2, true);
druDraw.Ruler(this, "tag1", true, 4, Low[4], 3, High[3], 1, Low[1]);
dsDraw.Square(this, "MySquare", false, 0, High[0] + 2 * TickSize, Brushes.Blue);
dxDraw.Text(this, "MyText", "Sample text ", 0, High[0] + 2 * TickSize, Brushes.Blue);
dxfDraw.TextFixed(this, "MyTextFixed", "Text to draw", TextPosition.TopRight);
dtcDraw.TrendChannel(this, "TrendChannel", true, 10, Low[10], 0, High[0], 10, High[10] + 5 * TickSize);
dtdDraw.TriangleDown(this, "MyTriangleDown", false, 0, High[0] + 2 * TickSize, Brushes.Red);
dtuDraw.TriangleUp(this, "MyTriangleUp", false, 0, Low[0] - 2 * TickSize, Brushes.Blue);
dvDraw.VerticalLine(this, "MyVerticalLine", 0, Brushes.Blue);

How to insert Code Snippets via the mouse or F2 key

Using the keyboard

Enter the text in the left column and press the “Tab” key within the NinjaScript Editor.

NameDescription
cbCurrentBar
oOpen[0]
hHigh[0]
lLow[0]
vVolume[0]
iInput[0]

Previous Bar Values

ValueDescription
c1Close[1]
o1Open[1]
h1High[1]
l1Low[1]
v1Volume[1]
i1Input[1]

Indicator Plotting

NameDescription
lineAddLine(new Stroke(Brushes.Blue, 1), 0, "Line");
plotAddPlot(new Stroke(Brushes.Blue, 1), PlotStyle.Line, "Plot");

Arithmetic

NameDescription
absMath.Abs(value)
minMath.Min(value1, value2)
maxMath.Max(value1, value2)

Event Handler Callback Methods

accountprotected override void OnAccountItemUpdate(Account account, AccountItem accountItem, double value) \{\}
tradeprotected override void OnAddTrade(Cbi.Trade trade) \{\}
barschangepublic override void OnBarsChanged() \{\}
minmaxpublic override void OnCalculateMinMax() \{ /*It is important to set MinValue and MaxValue to the min/max Y values your drawing tool uses if you want it to support auto scale*/ \}
calcperfprotected override void OnCalculatePerformanceValue(StrategyBase strategy) \{\}
connectionprotected override void OnConnectionStatusUpdate(ConnectionStatus orderStatus, ConnectionStatus priceStatus) \{\}
datapointprotected override void OnDataPoint(Bars bars, double open, double high, double low, double close, DateTime time, long volume, bool isBar, double bid, double ask) \{\}
executionprotected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time) \{\}
fundamentalprotected override void OnFundamentalData(FundamentalDataEventArgs fundamentalDataUpdate) \{\}
dataprotected override void OnMarketData(MarketDataEventArgs marketDataUpdate) \{\}
depthprotected override void OnMarketDepth(MarketDepthEventArgs marketDepthUpdate) \{\}
mergeperfprotected override void OnMergePerformanceMetric(PerformanceMetricBase merge) \{\}
mousedpublic override void OnMouseDown(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint) \{\}
mousempublic override void OnMouseMove(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint) \{\}
mouseupublic override void OnMouseUp(ChartControl chartControl, ChartPanel chartPanel, ChartScale chartScale, ChartAnchor dataPoint) \{\}
optimizeprotected override void OnOptimize() \{\}
ordertprotected override void OnOrderTrace(DateTime timestamp, string message) \{\}
orderuprotected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError) \{\}
positionprotected override void OnPositionUpdate(Position position, double averagePrice, int quantity, MarketPosition marketPosition) \{\}
renderprotected override void OnRender(ChartControl chartControl, ChartScale chartScale) \{\}
windowcprotected override void OnWindowCreated(Window window) \{\}
windowdprotected override void OnWindowDestroyed(Window window) \{\}

Control Statements

NameDescription
ifif (expression) \{\} else \{\}
forfor (int index = 0; index \< count; index++) \{\}
switchswitch (expression) \{ case value1: break; case value2: break; default: break; \}

Drawing

ShortcutMethod Signature
dapDraw.AndrewsPitchfork(this, "MyAndrewsPitchfork", false, 10, Close[10], 5, High[5], 0, Low[5], Brushes.Blue, DashStyleHelper.Solid, 1);
daDraw.Arc(this, "MyDrawArc", false, 10, Close[10], 0, Close[0], Brushes.LimeGreen, DashStyleHelper.Dot, 2);
ddDraw.ArrowDown(this, "MyArrowDown", false, 0, High[0], Brushes.Red);
duDraw.ArrowUp(this, "MyArrowUp", false, 0, Low[0], Brushes.Red);
ddiDraw.Diamond(this, "MyDiamond", false, 0, High[0] + 2 * TickSize, Brushes.Blue);
dtDraw.Dot(this, "MyDot", false, 0, High[0] + 2 * TickSize, Brushes.Blue);
deDraw.Ellipse(this, "MyEllipse", 10, Low[10], 0, High[0], Brushes.Blue);
diDraw.ExtendedLine(this, "MyExtendedLine", 10, Close[10], 0, Close[0], Brushes.Blue);
dfcDraw.FibonacciCircle(this, "MyFibonacciCircle", true, 10, Close[10], 0, Close[0]);
dfeDraw.FibonacciExtensions(this, "MyFibonacciExtensions", true, 15, Close[15], 10, Close[10], 5, Close[5]);
dfrDraw.FibonacciRetracements(this, "MyFibonacciRetracements", false, 10, Close[10], 0, Close[0]);
dftDraw.FibonacciTimeExtensions(this, "MyFibonacciTimeExtensions", false, 10, Close[10], 0, Close[0]);
dgDraw.GannFan(this, "MyGannFan", true, 10, Close[10]);
dhDraw.HorizontalLine(this, "MyHorizontalLine", Close[0], Brushes.Blue);
dlDraw.Line(this, "MyLine", 10, Close[10], 0, Close[0], Brushes.Blue);
dyDraw.Ray(this, "MyRay", 10, Close[10], 0, Close[0], Brushes.Blue);
drDraw.Rectangle(this, "MyRectangle", 10, Low[10], 0, High[0], Brushes.Blue);
dreDraw.Region(this, CurrentBar, 0, Bollinger(2, 14).Upper, Bollinger(2, 14).Lower, Brushes.Green, Brushes.Blue, 50);
drxDraw.RegionHighlightX(this, "MyRegionHighlightX", 10, 0, Brushes.Blue);
dryDraw.RegionHighlightY(this, "MyRegionHighlightY", High[0], Low[0], Brushes.Blue, Brushes.Green, 20);
drrDraw.RiskReward(this, "MyRiskReward", false, 0, High[0], 10, Low[0], 2, true);
druDraw.Ruler(this, "tag1", true, 4, Low[4], 3, High[3], 1, Low[1]);
dsDraw.Square(this, "MySquare", false, 0, High[0] + 2 * TickSize, Brushes.Blue);
dxDraw.Text(this, "MyText", "Sample text ", 0, High[0] + 2 * TickSize, Brushes.Blue);
dxfDraw.TextFixed(this, "MyTextFixed", "Text to draw", TextPosition.TopRight);
dtcDraw.TrendChannel(this, "TrendChannel", true, 10, Low[10], 0, High[0], 10, High[10] + 5 * TickSize);
dtdDraw.TriangleDown(this, "MyTriangleDown", false, 0, High[0] + 2 * TickSize, Brushes.Red);
dtuDraw.TriangleUp(this, "MyTriangleUp", false, 0, Low[0] - 2 * TickSize, Brushes.Blue);
dvDraw.VerticalLine(this, "MyVerticalLine", 0, Brushes.Blue);

How to insert Code Snippets via the mouse of F2 key

Via mouse or pressing the F2 key

  1. Right mouse click in the NinjaScript Editor and select the menu name “Insert Code Snippet”

    NS_Editor_10

  2. A menu will display all available code snippets.

    NS_Editor_11