Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Ninjatrader 8 – Button Trading OpenSource Code

6 min read

Here is the open-source code for building a simple buy and sell button-based API call to the Algomojo Platform. Now one can use Ninjatrader 8 to build customized trading requirements using Algomojo API.

Requirements

Installing the Algomojo Bridge

Copy the C# Bridge and Paste it in the Ninjatrader 8 Folder as mentioned below

MyDocuments\NinjaTrader 8\bin\Custom\AMNT64BRIDGE.dll

Add References to AMNT64BRIDGE.dll from NinjaEditor

Goto Control Center -> New -> Ninjscript Editor

Installing the Ninjascript Addon

Step 1: Download the Ninjascript Algomojo Button Addon

After you have downloaded 3rd Party Add-On, they can be imported from the NinjaTrader Control Center.

Step 2: From the Control Center window select the menu Tools > ImportNinjaScript Add-On… to open the “Import” dialog window as shown below

Step 3: Select the file Algomojo Buttons.zip you want to import

Step 4: Press the “Import” button

Add Dll References manually

Ninjascript Code

#region Using declarations
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml.Serialization;
using NinjaTrader.Cbi;
using NinjaTrader.Gui;
using NinjaTrader.Gui.Chart;
using NinjaTrader.Gui.SuperDom;
using NinjaTrader.Gui.Tools;
using NinjaTrader.Data;
using NinjaTrader.NinjaScript;
using NinjaTrader.Core.FloatingPoint;
using NinjaTrader.NinjaScript.DrawingTools;
using AMNT64BRIDGE;
#endregion

//This namespace holds Indicators in this folder and is required. Do not change it. 
namespace NinjaTrader.NinjaScript.Indicators
{
	public class Algomojo : Indicator
	{
		private string strg_name;
        private string Tsym;
        private string user_apikey;
		private string api_secret;
        private int qty;
        private string ordertype;
        //private string exch;
        private string strategies;
        private string cTag;
		private string s_prdt_ali;
        private double Price;
        private int discqty;
        private double TrigPrice;
        private bool start=false;
		private System.Windows.Controls.Button myBuyButton;
        private System.Windows.Controls.Button mySellButton;
        private System.Windows.Controls.Grid   myGrid;
		ExchangeEnumIndicator exch = ExchangeEnumIndicator.NSE;
	    ProdTypeEnumIndicator Product = ProdTypeEnumIndicator.MIS;
		BrokerCodeEnumIndicator Broker = BrokerCodeEnumIndicator.ab;
		
			private void OnMyButtonClick(object sender, RoutedEventArgs rea)
			{
				  System.Windows.Controls.Button button = sender as System.Windows.Controls.Button;
				  
				  if(button.Name.Equals("MyBuyButton"))
				  {
//					  Print("Buy Button Pressed");
//					  string message = "Simple MessageBox";  
//						string title = "Title";  
						
					  Type algo = Type.GetTypeFromProgID("AMAMIBRIDGE.main");
					  if(algo == null) throw new InvalidOperationException(
    					 "Invalid prog-id: " + "AMAMIBRIDGE.main");
			          dynamic algomojo = Activator.CreateInstance(algo);
					  
					  //Main am = new Main();
					  string api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Symbol+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"B"+"\",\"Ret\":\""+"DAY"+"\",\"prctyp\":\""+"MKT"+"\",\"qty\":\""+Quantity.ToString()+"\",\"discqty\":\""+discqty.ToString()+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+Price.ToString()+"\",\"TrigPrice\":\""+TrigPrice.ToString()+"\",\"Pcode\":\""+ProdType.ToString()+"\",\"AMO\":\""+"NO"+"\"}";
					  Print(api_data);
					  //MessageBox.Show(api_data, "API Data");  
					  string result = algomojo.AMDispatcher(Apikey,Apisecret,"PlaceOrder",api_data,Broker.ToString(),version);  
					  Print(result);
					  //MessageBox.Show(result, "Order Response");  
					  
				  }
				
				   if(button.Name.Equals("MySellButton"))
				  {
					     start=true;
					   Type algo = Type.GetTypeFromProgID("AMAMIBRIDGE.main");
					  if(algo == null) throw new InvalidOperationException(
    					 "Invalid prog-id: " + "AMAMIBRIDGE.main");
			          dynamic algomojo = Activator.CreateInstance(algo);
					    
					    string api_data ="{\"strg_name\":\""+strg_name+"\",\"s_prdt_ali\":\""+s_prdt_ali+"\",\"Tsym\":\""+Symbol+"\",\"exch\":\""+exch+"\",\"Ttranstype\":\""+"S"+"\",\"Ret\":\""+"DAY"+"\",\"prctyp\":\""+"MKT"+"\",\"qty\":\""+Quantity.ToString()+"\",\"discqty\":\""+discqty.ToString()+"\",\"MktPro\":\""+"NA"+"\",\"Price\":\""+Price.ToString()+"\",\"TrigPrice\":\""+TrigPrice.ToString()+"\",\"Pcode\":\""+ProdType.ToString()+"\",\"AMO\":\""+"NO"+"\"}";
					   Print(api_data);  
					  string result = algomojo.AMDispatcher(Apikey,Apisecret,"PlaceOrder",api_data,Broker.ToString(),version);  
					  Print(result);
				  }
			
			}
		
		protected override void OnStateChange()
		{
			if (State == State.SetDefaults)
			{
				Description									= @"Place your orders from Ninjatrader Button Trading.";
				Name										= "Algomojo";
				Calculate									= Calculate.OnBarClose;
				IsOverlay									= true;
				IsOverlay									= true;
				DisplayInDataBox							= true;
				DrawOnPricePanel							= true;
				DrawHorizontalGridLines						= true;
				DrawVerticalGridLines						= true;
				PaintPriceMarkers							= true;
				ScaleJustification							= NinjaTrader.Gui.Chart.ScaleJustification.Right;

				ordertype = "MKT";
                strategies = "NT8";
                strg_name ="Ninjatrader 8";		
				s_prdt_ali="BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML";
			    Apikey					="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
				Apisecret               ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
				Quantity = 1;
				version = "1.0";
				Symbol					="INFY-EQ";
				DiscQty                = 0;
		        TriggerPrice           = 0;
				Price           = 0;
				 
			}
			else if (State == State.Configure)
			{
			}
			 else if (State == State.DataLoaded)
            {

            }
			else if(State==State.Historical)
			{
				ChartControl.Dispatcher.InvokeAsync((() =>
   			 {
        // Grid already exists
        if (UserControlCollection.Contains(myGrid))
          return;
        // Add a control grid which will host our custom buttons
        myGrid = new System.Windows.Controls.Grid
      	  {
          Name = "MyCustomGrid",
          // Align the control to the top right corner of the chart
          HorizontalAlignment = HorizontalAlignment.Left,
          VerticalAlignment = VerticalAlignment.Top,
        };
 
        // Define the two columns in the grid, one for each button
        System.Windows.Controls.ColumnDefinition column1 = new System.Windows.Controls.ColumnDefinition();
        System.Windows.Controls.ColumnDefinition column2 = new System.Windows.Controls.ColumnDefinition();
 
        // Add the columns to the Grid
        myGrid.ColumnDefinitions.Add(column1);
        myGrid.ColumnDefinitions.Add(column2);
 
        // Define the custom Buy Button control object
        myBuyButton = new System.Windows.Controls.Button
        {
          Name = "MySellButton",
          Content = "SELL",
          Foreground = Brushes.White,
          Background = Brushes.Red
        };
		 mySellButton = new System.Windows.Controls.Button
        {
          Name = "MyBuyButton",
          Content = "BUY",
          Foreground = Brushes.White,
          Background = Brushes.Green
        };
        myBuyButton.Click += OnMyButtonClick;
	    mySellButton.Click += OnMyButtonClick;
        System.Windows.Controls.Grid.SetColumn(myBuyButton, 0);
		System.Windows.Controls.Grid.SetColumn(myBuyButton, 1);
        myGrid.Children.Add(myBuyButton);
	    myGrid.Children.Add(mySellButton);
        UserControlCollection.Add(myGrid);
 
    }));
  }
 
  else if (State == State.Terminated)
  {
    if (ChartControl == null)
        return;
    ChartControl.Dispatcher.InvokeAsync((() =>
    {
        if (myGrid != null)
        {
          if (myBuyButton != null)
          {
              myGrid.Children.Remove(myBuyButton);
              myBuyButton.Click -= OnMyButtonClick;
              myBuyButton = null;
          }
		   if (mySellButton != null)
          {
              myGrid.Children.Remove(mySellButton);
              mySellButton.Click -= OnMyButtonClick;
              mySellButton = null;
          }
        }
    }));
			}
        
		}

		protected override void OnBarUpdate()
		{
			//Add your custom indicator logic here.
			
		}
		
		public override string DisplayName
		{
			get { return Name; }
		}
		
        public enum ExchangeEnumIndicator
		 {
			 NSE,NFO,BSE,CDS,MCX
		 }
		 public enum ProdTypeEnumIndicator
		 {
			MIS,NRML,CNC,CO
		 }
		 public enum BrokerCodeEnumIndicator
		 {
			 ab,tj,zb
		 }
		#region Properties
		 
		[Description("Property defined in Indicator")]
		 [Display(Name="Exchange", Order=5, GroupName="Parameters")]
		 public  NinjaTrader.NinjaScript.Indicators.Algomojo.ExchangeEnumIndicator
		 Exchange
		 {
		 get { return exch; }
		 set { exch = value; }
		 }
		 
	    [Description("Property defined in Indicator")]
		 [Display(Name="ProdType", Order=5, GroupName="Parameters")]
		 public  NinjaTrader.NinjaScript.Indicators.Algomojo.ProdTypeEnumIndicator
		 ProdType
		 {
		 get { return Product; }
		 set { Product = value; }
		 }
		 
		[NinjaScriptProperty]
		[Display(Name="Apikey", Description="Enter your Apikey here", Order=1, GroupName="Parameters")]
		public string Apikey
		{ get; set; }

		[NinjaScriptProperty]
		[Display(Name="Apisecret", Description="Enter your Apisecret here", Order=2, GroupName="Parameters")]
		public string Apisecret
		{ get; set; }
		
		[NinjaScriptProperty]
		[Display(Name="Broker", Description="Enter your Broker Shortcode", Order=3, GroupName="Parameters")]
		public  NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator
		BrokerCode
		 {
		 get { return Broker; }
		 set { Broker = value; }
		 }
		
		
		[NinjaScriptProperty]
		[Display(Name="Version", Description="Enter Algomojo API Version", Order=4, GroupName="Parameters")]
		public string version
		{ get; set; }

		[NinjaScriptProperty]
		[Display(Name="Symbol", Description="Enter tradeing Symbol", Order=5, GroupName="Parameters")]
		public string Symbol
		{ get; set; }

		[NinjaScriptProperty]
		[Range(1, int.MaxValue)]
		[Display(Name="Quantity", Order=6, GroupName="Parameters")]
		public int Quantity
		{ get; set; }

		[NinjaScriptProperty]
		[Range(0, int.MaxValue)]
		[Display(Name="DiscQty", Order=7,GroupName="Parameters")]
		public int DiscQty
		{ get; set; }

		[NinjaScriptProperty]
		[Range(0, double.MaxValue)]
		[Display(Name="TriggerPrice", Order=8, GroupName="Parameters")]
		public double TriggerPrice
		{ get; set; }
		#endregion

	}
}

#region NinjaScript generated code. Neither change nor remove.

namespace NinjaTrader.NinjaScript.Indicators
{
	public partial class Indicator : NinjaTrader.Gui.NinjaScript.IndicatorRenderBase
	{
		private Algomojo[] cacheAlgomojo;
		public Algomojo Algomojo(string apikey, string apisecret, NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator brokerCode, string version, string symbol, int quantity, int discQty, double triggerPrice)
		{
			return Algomojo(Input, apikey, apisecret, brokerCode, version, symbol, quantity, discQty, triggerPrice);
		}

		public Algomojo Algomojo(ISeries<double> input, string apikey, string apisecret, NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator brokerCode, string version, string symbol, int quantity, int discQty, double triggerPrice)
		{
			if (cacheAlgomojo != null)
				for (int idx = 0; idx < cacheAlgomojo.Length; idx++)
					if (cacheAlgomojo[idx] != null && cacheAlgomojo[idx].Apikey == apikey && cacheAlgomojo[idx].Apisecret == apisecret && cacheAlgomojo[idx].BrokerCode == brokerCode && cacheAlgomojo[idx].version == version && cacheAlgomojo[idx].Symbol == symbol && cacheAlgomojo[idx].Quantity == quantity && cacheAlgomojo[idx].DiscQty == discQty && cacheAlgomojo[idx].TriggerPrice == triggerPrice && cacheAlgomojo[idx].EqualsInput(input))
						return cacheAlgomojo[idx];
			return CacheIndicator<Algomojo>(new Algomojo(){ Apikey = apikey, Apisecret = apisecret, BrokerCode = brokerCode, version = version, Symbol = symbol, Quantity = quantity, DiscQty = discQty, TriggerPrice = triggerPrice }, input, ref cacheAlgomojo);
		}
	}
}

namespace NinjaTrader.NinjaScript.MarketAnalyzerColumns
{
	public partial class MarketAnalyzerColumn : MarketAnalyzerColumnBase
	{
		public Indicators.Algomojo Algomojo(string apikey, string apisecret, NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator brokerCode, string version, string symbol, int quantity, int discQty, double triggerPrice)
		{
			return indicator.Algomojo(Input, apikey, apisecret, brokerCode, version, symbol, quantity, discQty, triggerPrice);
		}

		public Indicators.Algomojo Algomojo(ISeries<double> input , string apikey, string apisecret, NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator brokerCode, string version, string symbol, int quantity, int discQty, double triggerPrice)
		{
			return indicator.Algomojo(input, apikey, apisecret, brokerCode, version, symbol, quantity, discQty, triggerPrice);
		}
	}
}

namespace NinjaTrader.NinjaScript.Strategies
{
	public partial class Strategy : NinjaTrader.Gui.NinjaScript.StrategyRenderBase
	{
		public Indicators.Algomojo Algomojo(string apikey, string apisecret, NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator brokerCode, string version, string symbol, int quantity, int discQty, double triggerPrice)
		{
			return indicator.Algomojo(Input, apikey, apisecret, brokerCode, version, symbol, quantity, discQty, triggerPrice);
		}

		public Indicators.Algomojo Algomojo(ISeries<double> input , string apikey, string apisecret, NinjaTrader.NinjaScript.Indicators.Algomojo.BrokerCodeEnumIndicator brokerCode, string version, string symbol, int quantity, int discQty, double triggerPrice)
		{
			return indicator.Algomojo(input, apikey, apisecret, brokerCode, version, symbol, quantity, discQty, triggerPrice);
		}
	}
}

#endregion

Indicator Settings

Enter the API Key, API SecretKey, Broker and other order parameters according to the requirement.

Ninjascript Output Window

Goto Ninjascript Control Center -> New -> Ninjascript Output to view the API request/API response parameters.

Rajandran R Founder of Marketcalls and Co-Founder Algomojo. Full-Time Derivative Trader. Expert in Designing Trading Systems (Amibroker, Ninjatrader, Metatrader, Python, Pinescript). Trading the markets since 2006. Mentoring Traders on Trading System Designing, Market Profile, Orderflow and Trade Automation.

Clarification on Business Standard Article and False Allegations made…

This article clarifies the false allegations made on Marketcalls and Algomojo.
Rajandran R
1 min read

How to Fetch Trading Account Balance using Amibroker

Amibroker recently launched their Amibroker 6.4 version which comes with new Javascript engines with JSON native object support which is very much required to...
Rajandran R
2 min read

Ninjatrader 8 – Button Trading OpenSource Code

Here is the open-source code for building a simple buy and sell button-based API call to the Algomojo Platform. Now one can use Ninjatrader...
Rajandran R
6 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More