NexusFi: Find Your Edge


Home Menu

 





AutoHotKey scripts for editing/debugging NT scripts in Visual Studio


Discussion in NinjaTrader

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




 
Search this Thread

AutoHotKey scripts for editing/debugging NT scripts in Visual Studio

  #1 (permalink)
 swinger 
Raleigh NC/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: stocks
Posts: 33 since Oct 2014
Thanks Given: 217
Thanks Received: 16

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
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Pivot Indicator like the old SwingTemp by Big Mike
NinjaTrader
Trade idea based off three indicators.
Traders Hideout
MC PL editor upgrade
MultiCharts
Exit Strategy
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Just another trading journal: PA, Wyckoff & Trends
33 thanks
Tao te Trade: way of the WLD
24 thanks
My NQ Trading Journal
14 thanks
GFIs1 1 DAX trade per day journal
11 thanks
HumbleTraders next chapter
11 thanks
  #3 (permalink)
 swinger 
Raleigh NC/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: stocks
Posts: 33 since Oct 2014
Thanks Given: 217
Thanks Received: 16


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
Thanked by:
  #4 (permalink)
 swinger 
Raleigh NC/USA
 
Experience: Beginner
Platform: NinjaTrader
Trading: stocks
Posts: 33 since Oct 2014
Thanks Given: 217
Thanks Received: 16

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.

[IMG]http://i175.photobucket.com/albums/w128/pbucket_pics/forum%20images/Screenshot2015-01-28073202crop.png[/IMG]

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
Thanked by:
  #5 (permalink)
 dalebru 
Indianapolis/IN
 
Experience: Intermediate
Platform: SC, NT, ToS, my own
Broker: EdgeClear
Trading: ES
Posts: 67 since Jan 2013
Thanks Given: 1,035
Thanks Received: 110

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
  #6 (permalink)
 dalebru 
Indianapolis/IN
 
Experience: Intermediate
Platform: SC, NT, ToS, my own
Broker: EdgeClear
Trading: ES
Posts: 67 since Jan 2013
Thanks Given: 1,035
Thanks Received: 110

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? - [AUTOLINK]NinjaTrader[/AUTOLINK] 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
Thanked by:




Last Updated on August 10, 2016


© 2024 NexusFi™, s.a., All Rights Reserved.
Av Ricardo J. Alfaro, Century Tower, Panama City, Panama, Ph: +507 833-9432 (Panama and Intl), +1 888-312-3001 (USA and Canada)
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.
About Us - Contact Us - Site Rules, Acceptable Use, and Terms and Conditions - Privacy Policy - Downloads - Top
no new posts