AutoHotKey scripts for editing/debugging NT scripts in Visual Studio - futures io
futures io futures trading



AutoHotKey scripts for editing/debugging NT scripts in Visual Studio


Discussion in NinjaTrader

Updated
    1. trending_up 2,468 views
    2. thumb_up 5 thanks given
    3. group 2 followers
    1. forum 5 posts
    2. attach_file 0 attachments




Welcome to futures io: the largest futures trading community on the planet, with well over 125,000 members
  • Genuine reviews from real traders, not fake reviews from stealth vendors
  • Quality education from leading professional traders
  • We are a friendly, helpful, and positive community
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts
  • We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community.  It's free and simple.

-- Big Mike, Site Administrator

(If you already have an account, login at the top of the page)

 
Search this Thread
 

AutoHotKey scripts for editing/debugging NT scripts in Visual Studio

(login for full post details)
  #1 (permalink)
Raleigh NC/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: stocks
 
Posts: 32 since Oct 2014
Thanks: 183 given, 15 received

If you debug ninjascripts in Visual Studio, you know what a pain the repetitive process of launching everything, recompiling the script and attaching VS to the NT process is.

I used AutoHotKey to automate some of this.

First script:
1. launches NT
2. connect to data provider
3. open Ninjascript in NT
4. launch VS
5. open Ninjascript in VS
6. compile script in NT
7. reload all scripts in VS
8. attach VS to NT process (I use the Attach to Anything extension in VS and created a little toolbar; would be nice to access this with a shortcut, but not sure how or if possible)
9. close NT editor

Script is pretty extensively commented, so hopefully it's easy to follow along. I'm pretty new to AHK, and used a lot of pauses and WinWait commands to prevent the script from outrunning itself. It works pretty well now, and I could probably go back and eliminate some of the pauses and WinWait's to make it a touch quicker. If you make any useful mods to this or have any suggestions, post 'em up.

 
Code
#NoEnv
SetWorkingDir %A_ScriptDir%
CoordMode, Mouse, Window
SendMode Input
#SingleInstance Force
SetTitleMatchMode 2
DetectHiddenWindows On
#WinActivateForce
SetControlDelay 1
SetWinDelay 0
SetKeyDelay -1
SetMouseDelay -1
SetBatchLines -1


F3::
Macro1:
Run, "C:\Program Files (x86)\NinjaTrader 7\bin64\NinjaTrader.exe", "C:\Program Files (x86)\NinjaTrader 7\bin64"  ; script to: 1. launch NT;  2. connect to data provider;  3. open Ninjascript in NT;  4. launch VS;  5. open Ninjascript  in VS; 6. compile script in NT;  7. reload all scripts in VS;  8. attach VS to NT process;  9. close NT editor
WinWait, Control Center -
Sleep, 333
WinActivate, Control Center -
Sleep, 333
WinWaitActive, Control Center -
Sleep, 333
Send, {LAlt Down}{f}{LAlt Up}{c}  ; connect to data provider
Send, {k}  ; 'k' is for Kinetick
Sleep, 20
Send, {LAlt Down}{o}{LAlt Up}{e}  ; open existing Ninjascript
Send, {s}  ; 's' is for Strategy 'i' is for Indicator
WinWaitActive, Edit Strategy
Sleep, 333
WinWaitClose, Edit Strategy  ; wait until script manually selected
Sleep, 333
Run, "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe", "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\"  ; launch VS
WinWait, Microsoft Visual Studio
Sleep, 333
WinActivate, Microsoft Visual Studio
Sleep, 333
WinWaitActive, Microsoft Visual Studio
Sleep, 333
Send, {LControl Down}{o}  ; CTRL + o = open file
Send, {LControl Up}
WinWait, Open File
Sleep, 333
WinActivate, Open File
Sleep, 333
WinWaitClose, Open File  ; wait until file manually selected
Sleep, 333
Sleep, 300
WinActivate, Strategy -  ; switch back to Ninjascript editor
Sleep, 333
WinWaitActive, Strategy -  ; would be nice to ensure Ninjascript editor has debugging mode enabled.
Sleep, 333
Send, {F5}  ; compile script in Ninjascript editor
Sleep, 300
Send, {Enter}  ; ok to Ninjascript editor warning
Sleep, 300
WinActivate, Microsoft Visual Studio  ; switch back to VS
Sleep, 333
WinWaitActive, ahk_class #32770
Sleep, 333
Send, {LShift Down}{Y}  ; Shift + y to reload all
Send, {LShift Up}
Sleep, 300
WinActivate, Microsoft Visual Studio
Sleep, 333
WinWaitActive, Microsoft Visual Studio
Sleep, 333
Sleep, 300
Click, 1188, 77 Left, Down  ; accessing Attach to Anything extension's toolbar
Sleep, 300
Click, 1188, 77 Left, Up
Sleep, 500
Send, {Down}{Down}  ; Ninjatrader is the second process in my Attach to Anything toolbar's list
Send, {Enter}
Sleep, 300
WinClose, Strategy -  ; close Ninjascript editor. Don't want it open while editing the same script in VS.
Sleep, 333
Return
If anyone has other useful AHK scripts they use to automate steps when working with VS and NT, or suggestions, feel free to add to this thread.

Started this thread Reply With Quote
The following 2 users say Thank You to swinger for this post:

Can you help answer these questions
from other members on futures io?
Webinar: Finding your own way in trading w/Adam Grimes
The Elite Circle
If ES tracks SPX, why do support/resistance, Level 2, vo …
Emini and Emicro Index
Automating strategies on multiple charts with same symbol
TradeStation
OFA (Order Flow Analytics) Licence
NinjaTrader
SimplerOptions
Trading Reviews and Vendors
 
 
(login for full post details)
  #3 (permalink)
Raleigh NC/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: stocks
 
Posts: 32 since Oct 2014
Thanks: 183 given, 15 received


This next AHK script is intended to be used when making changes to your ninjascript while debugging. After saving changes in VS, run this script to stop debugging, open the script in NT editor, compile, reload in VS, attach VS to NT process and close NT editor.

 
Code
F4::
Macro2:
WinActivate, Microsoft Visual Studio  ; script intended to be used after saving changes made to Ninjascript in VS while debugging.  script: 1. stops debugging;  2. manually open script in NT editor;  3. compile script in NT editor;  4. reload all scripts in VS;  5. attach VS to NT process;  6. close NT editor
Sleep, 333
WinWaitActive, Microsoft Visual Studio
Sleep, 333
Send, {LShift Down}{F5}  ; Shift + F5 = stop debugging
Send, {LShift Up}
Sleep, 300
WinActivate, Control Center -
Sleep, 333
WinWaitActive, Control Center -
Sleep, 333
Send, {LAlt Down}{o}{LAlt Up}{e}{s}
WinWaitActive, Edit Strategy
Sleep, 333
WinWaitClose, Edit Strategy
Sleep, 333
Send, {F5}  ; compile script in Ninjascript editor
Sleep, 300
Send, {Enter}  ; ok to Ninjascript editor warning
Sleep, 300
WinActivate, Microsoft Visual Studio
Sleep, 333
WinWaitActive, ahk_class #32770
Sleep, 333
Send, {LShift Down}{Y}  ; Shift + y to reload all
Send, {LShift Up}
WinActivate, Microsoft Visual Studio
Sleep, 333
WinWaitActive, Microsoft Visual Studio
Sleep, 333
Click, 1188, 77 Left, Down  ; accessing Attach to Anything extension's toolbar
Click, 1188, 77 Left, Up
Sleep, 500
Send, {Down}{Down}  ; Ninjatrader is the second process in my Attach to Anything toolbar's list
Send, {Enter}
Sleep, 300
WinClose, Strategy -  ; close Ninjascript editor. Don't want it open while editing the same script in VS.
Sleep, 333
Return

Started this thread Reply With Quote
The following user says Thank You to swinger for this post:
 
(login for full post details)
  #4 (permalink)
Raleigh NC/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: stocks
 
Posts: 32 since Oct 2014
Thanks: 183 given, 15 received

A little more detail on Attach to Anything ("A2A") as some might find this useful, regardless of whether they are accessing it in a script or manually - either way, it's a lot quicker than Debug > Attach to Process > scroll down to find NT > Attach.

You can grab this extension by going to VS Extension Manager. Search Online Gallery for "Attach to Anything". In A2A's options (can access from either toolbar or Tools > Options > Debugging > A2A), select the the Ninjatrader process and Add. Now in the A2A toolbar, you will have Ninjatrader.exe.



It would be nice to access this by shortcut (in my AHK script, I access it by mouse pointer), but I'm not sure how, or if it's even possible. If anyone knows how to do this, please let me know, thanks.

Started this thread Reply With Quote
The following user says Thank You to swinger for this post:
 
(login for full post details)
  #5 (permalink)
Indianapolis/IN
 
Experience: Intermediate
Platform: SC, NT, ToS, my own
Broker: Stage5, InteractiveBrokers
Trading: ES
 
Posts: 67 since Jan 2013
Thanks: 1,029 given, 109 received

FWIW, here is a snippet that works on my machine. Both VS2013 and NT7 are assumed to be open. You have installed the ReAttach extension, and you have used it to attach to the NT process. If you hover over the red play button, you'll see that Ctrl+R,Ctrl+1 re-attaches (that is the assumption in the script). Your NT debugging is set to Debug Mode.
Once you have run the script, F4 (from any window, not just VS or NT) will ReAttach to NT, open an indicator, compile it and close the window. Now you can Reload NinjaScript from a chart to hit breakpoints in VS.

 
Code
; This is for NT7
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

SetTitleMatchMode 2 ; partial match

F4::

; This does the attach of an open Visual Studio (2013) to NT7 using the ReAttach extension
WinActivate, Microsoft Visual Studio  
Sleep, 333
WinWaitActive, Microsoft Visual Studio
Sleep, 333
Send, ^r
Sleep, 10
Send,^1  ; look at the ReAttach shortcuts to ensure you're getting the correct NT process (most recently used)

; This does the compile of an indicator (and everything else) in NT
WinActivate, Control Center - ; case specific
Sleep, 333
WinWaitActive, Control Center -
Sleep, 333
Send, {LAlt Down}{o}{LAlt Up}{e}{i}{Down}{Down}{Down}{Down}{Down}!o ; move to some indicator high in the list, then Okay
Sleep, 50
WinActivate, Indicator -
Sleep, 333
WinWaitActive, Indicator -
Sleep, 333
Send, {F5}  ; compile script in Ninjascript editor
Sleep, 300
Send, {Enter}  ; ok to Ninjascript editor warning
Sleep, 1000
Send, {LAlt Down}{F4}{LAlt Up} ; close the indicator edit window
return ; now we are in debug mode. Reload NinjaScript from a chart, or whatever, to debug

Reply With Quote
 
(login for full post details)
  #6 (permalink)
Indianapolis/IN
 
Experience: Intermediate
Platform: SC, NT, ToS, my own
Broker: Stage5, InteractiveBrokers
Trading: ES
 
Posts: 67 since Jan 2013
Thanks: 1,029 given, 109 received

This is an old thread, and it has helped me.
But this month Brett at NinjaTrader put out a sample project that makes all of this very simple, IMHO.
The link is at: What version of the compiler is NT using? - NinjaTrader Support Forum

In a nutshell:

Quote:
Originally Posted by NinjaTrader_Brett View Post
C# 6.0 came out near the tail end of our development and is actually a little tricky to integrate support for that since the compiler is no longer part of .net framework. You have to use the new roslyn compiler now separately. Furthermore various third party tools we use didn't support it yet so we made the decision to hold off on support for that.

There is a workaround which is developing in Visual Studio to compile a .dll and import it into NinjaTrader. I made this template visual studio file which allows you to do just that. As long as NT is installed in default location you can develop in this visual studio project. Press F5 and it will copy over a DLL to NT to autoload and start NinjaTrader in debug mode. The example uses an AddOn but you could do indicators/strategies/other as well.

-Brett
Brett,

My additional notes:

The NinjaTraderAddon project properties Build Steps has two xcopy commands under Build Steps to copy its dll and pdb files to the proper location, and the NinjaTraderStartupProject project properties Debug has "Start external program" to launch NT8.

Reply With Quote
The following user says Thank You to dalebru for this post:


futures io Trading Community Platforms and Indicators NinjaTrader > AutoHotKey scripts for editing/debugging NT scripts in Visual Studio


Last Updated on August 10, 2016


Upcoming Webinars and Events
 

NinjaTrader Indicator Challenge!

Ongoing
 

Battlestations! Show us your trading desk - $1,500 in prizes!

March
 

Importance of Finding Your Own Way w/Adam Grimes

Elite only
 

Journal Challenge w/Jigsaw

April
     



Copyright © 2021 by futures io, s.a., Av Ricardo J. Alfaro, Century Tower, Panama, +507 833-9432, info@futures.io
All information is for educational use only and is not investment advice.
There is a substantial risk of loss in trading commodity futures, stocks, options and foreign exchange products. Past performance is not indicative of future results.
no new posts