Code Snippets needed! - NinjaTrader Programming | futures io social day trading
futures io futures trading


Code Snippets needed!
Updated: Views / Replies:3,421 / 11
Created: by zeller4 Attachments:0

Welcome to futures io.

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

futures io is the largest futures trading community on the planet, with over 90,000 members. At futures io, our goal has always been and always will be to create a friendly, positive, forward-thinking community where members can openly share and discuss everything the world of trading has to offer. The community is one of the friendliest you will find on any subject, with members going out of their way to help others. Some of the primary differences between futures io and other trading sites revolve around the standards of our community. Those standards include a code of conduct for our members, as well as extremely high standards that govern which partners we do business with, and which products or services we recommend to our members.

At futures io, our focus is on quality education. No hype, gimmicks, or secret sauce. The truth is: trading is hard. To succeed, you need to surround yourself with the right support system, educational content, and trading mentors Ė all of which you can find on futures io, utilizing our social trading environment.

With futures io, you can find honest trading reviews on brokers, trading rooms, indicator packages, trading strategies, and much more. Our trading review process is highly moderated to ensure that only genuine users are allowed, so you donít need to worry about fake reviews.

We are fundamentally different than most other trading sites:
  • We are here to help. Just let us know what you need.
  • We work extremely hard to keep things positive in our community.
  • We do not tolerate rude behavior, trolling, or vendors advertising in posts.
  • We firmly believe in and encourage sharing. The holy grail is within you, we can help you find it.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.

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

Reply
 
Thread Tools Search this Thread
 

Code Snippets needed!

  #1 (permalink)
Elite Member
Florida
 
Futures Experience: Intermediate
Platform: NT7
Favorite Futures: CL, NQ, NG
 
Posts: 456 since Jun 2009
Thanks: 1,359 given, 383 received

Code Snippets needed!

Hello Mike and all,

Mike, I'm not sure how the download section for code snippets will work since most snippets I've seen are only a portion of a working code...

My programming experience came from finding indicators built-in to NT or on the NT support forum (that I could then rename with a _v01) and then proceed to try new things that I was learning at the time. I started using the wizard several months ago and am now able to customize some of the indicators and strategies that I like to use.

I'd appreciate others sharing code snippets that you find worthwhile. I'll share a couple things I like to use here and in the following posts:

PLOTTING LINES OF VARIOUS SIZE / COLOR
 
Code
 
Add(new Plot(Color.FromKnownColor(KnownColor.Green), PlotStyle.Line, "Plot1"));
 
or

Add(new Plot(Color.Orange, "HMA"));
This shows up with line width 1 and may be too narrow to see well plus I never understood "FromKnownColor" purpose.

or
 
Code
Add(new Plot(Color.Blue, "EMAup"));
Plots[0].Pen.Width = 3;
Plots[0].Pen.DashStyle = DashStyle.Dash;
3 lines of code required...

 
Code
Add(new Plot(new Pen(Color.Green,3), PlotStyle.Line, "ChannelHigh"));
 
- okay, now I've got the line width built-in to the plot command...

For newbies the process of learning how to get things to plot are quite a challenge - i.e. the difference required when using DashStyle.Dash and PlotStyle.Line. I can't begin to calculate how many hours I spent trying to get plots to work... Plotting in Strategies is another matter...

Kirk


Last edited by zeller4; June 27th, 2009 at 10:17 AM.
Reply With Quote
The following 2 users say Thank You to zeller4 for this post:
 
  #2 (permalink)
Elite Member
Florida
 
Futures Experience: Intermediate
Platform: NT7
Favorite Futures: CL, NQ, NG
 
Posts: 456 since Jun 2009
Thanks: 1,359 given, 383 received

CODE SNIPPETS needed continued

ALERT SOUNDS
 
Code
PlaySound(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\NinjaTrader 6.5\\sounds\\alarma_submarino.wav");
 
If your code is set up and all you hear is the incessant alarm going off, you may need to use "FirstTickOfBar":
 
Code
if (CurrentBar == 0 && FirstTickOfBar)
{
PlaySound(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\NinjaTrader 6.5\\sounds\\Train.wav");
 
}
You can find several resources here and on the NT support forum for alerts.

Try them on your basic script routines and you'll learn where / where not to use them.
Kirk


Last edited by zeller4; June 27th, 2009 at 10:19 AM.
Reply With Quote
The following 2 users say Thank You to zeller4 for this post:
 
  #3 (permalink)
Elite Member
Florida
 
Futures Experience: Intermediate
Platform: NT7
Favorite Futures: CL, NQ, NG
 
Posts: 456 since Jun 2009
Thanks: 1,359 given, 383 received

CODE SNIPPETS needed continued


PLOTTING GRAPHICS / DOTS, DIAMONDS, TRIANGLES, etc.
Do you like basic dots?
 
Code
DrawDot("Dot",false,0,0,Color.Blue);
or
 
Code
DrawDiamond("Down Diamond" + CurrentBar, 0, SMA(20)[0], Color.Magenta);//I like to name them what their purpose or color is
DrawTriangleDown("Down Triangle" + CurrentBar,0,LinReg(High,(7))[0],Color.Aqua);
 
Or enhanced "larger" dots or different shapes
In Variables
 
Code
private Font textFontBig = new Font("Arial", 16);
private Font textFontMedium = new Font("Arial", 14);
private Font textFontSmall = new Font("Arial", 10);
private Font textFontSymbol = new Font("Webdings", 20);//"a" is checkmark
private Font textFontSymbolWing = new Font("Wingdings", 12);//"l" is a circle
see continued section below...


Last edited by zeller4; June 27th, 2009 at 10:21 AM.
Reply With Quote
The following 3 users say Thank You to zeller4 for this post:
 
  #4 (permalink)
Elite Member
Florida
 
Futures Experience: Intermediate
Platform: NT7
Favorite Futures: CL, NQ, NG
 
Posts: 456 since Jun 2009
Thanks: 1,359 given, 383 received

...continued from previous post...

if you look at your character map for these different fonts (Webdings, Wingdings, etc.), you'll soon see some useful graphics that will help highlight your charts. You just have to figure out what keystroke is required to give you the desired symbol.

In OnBarUpdate for a larger Wingdings "l" (small-case L) solid circle,
 
Code
DrawText("DotBlue"+CurrentBar, false, "l", 0, Low[0]-6*TickSize, Color.Blue, textFontSymbolWing, 
StringAlignment.Center, Color.Empty, Color.Empty, 10);
 
This allows you to use the font size from variables to make as large as you need.

Kirk


Last edited by zeller4; June 27th, 2009 at 10:22 AM.
Reply With Quote
The following 2 users say Thank You to zeller4 for this post:
 
  #5 (permalink)
Elite Member
Calgary Canada
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: IB/Kinetic
Favorite Futures: 6E, CL
 
jackyd's Avatar
 
Posts: 47 since Jun 2009
Thanks: 44 given, 18 received

Strategy max profit/max loss code

Good idea zeller. Here's some code I came up with from various samples to give me some peace of mind if you were to run your strategies unattended. (in case I ever have a strategy that makes money!!!)

This is probably very simplistic and old hat for many, but hopefully it will help someone. It can be easily personalized with your own messages, input variables, etc.

 
Code
// At the start of a new session calculate the prior cum profit so it won't be included in the 
// calculation. Need this for historical testing.
if (Bars.FirstBarOfSession)
	{priorCumProfit = Performance.AllTrades.TradesPerformance.Currency.CumProfit;}

// *** Calculate the toal profit (cumulative profit minus prior profit plus the current position profit 
double myMaxProfit = (double) 1000;
double myMaxLoss = (double) -1000;
double cumProfit = (double) Performance.AllTrades.TradesPerformance.Currency.CumProfit;
double curPosition = (double) Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
double totCumProfit = (double) cumProfit - priorCumProfit + curPosition ;
			
// *** STOP the strategy! if a total profit or loss exceeds the max
if (totCumProfit <= myMaxLoss || totCumProfit >= myMaxProfit)
	{
	if (Position.MarketPosition == MarketPosition.Long) {ExitLong("DMA: Exit Long - max Profit/Loss exceeded", "");}
	if (Position.MarketPosition == MarketPosition.Short) {ExitShort("DMA: Exit Short - max Profit/Loss exceeded", "");}
	Print(Time[0] + ": EXIT STRATEGY - Max Profit/Loss exceeded: $" + myMaxProfit + "/$" + myMaxLoss + ", Current: $" + totCumProfit);
	return;
	}

Reply With Quote
The following 4 users say Thank You to jackyd for this post:
 
  #6 (permalink)
Elite Member
Florida
 
Futures Experience: Intermediate
Platform: NT7
Favorite Futures: CL, NQ, NG
 
Posts: 456 since Jun 2009
Thanks: 1,359 given, 383 received

Thanks jackyd,

only questions I have before trying,

where in OnBarUpdate do you place this, near bottom after trades are placed?
or is there some critical items to address before or after?

I understand about the variables to define...

kz

Reply With Quote
 
  #7 (permalink)
Elite Member
Calgary Canada
 
Futures Experience: Intermediate
Platform: Ninja
Broker/Data: IB/Kinetic
Favorite Futures: 6E, CL
 
jackyd's Avatar
 
Posts: 47 since Jun 2009
Thanks: 44 given, 18 received

You're welcome. I place it at the very top of OnBarUpdate as a standard first thing it always checks for. Also, keep in mind that it doesn't actually stop the strategy, but only prevents it from taking any more trades for that session, which is also necessary so that it doesn't interfere with backtesting.

Reply With Quote
 
  #8 (permalink)
Elite Member
Arcata, CA
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: index futures, oil
 
Saroj's Avatar
 
Posts: 482 since Jun 2009
Thanks: 223 given, 389 received

Collecting and providing access to code snippets

I'm still mulling over how to provide a "store" of code snippets in a way that makes them easy to find and easy to figure out how to integrate into a custom code file... within the futures.io (formerly BMT) forum construct... (or possibly "without" - in the mathematical sense - it may be best to create a wiki type environment which can be linked to from within futures.io (formerly BMT)).

An example is the one I did on how to code sound alerts.. but that takes a lot of time and I wouldn't want to require that from all contributors... however there is no question that we need categories to organize them... "fundamentals"; "data definitions and usage"; "plotting and drawing"; "referencing objects from other code files"; "performance tips and tricks"; "order handling", etc.

Any and all ideas are welcomed.

Reply With Quote
 
  #9 (permalink)
Elite Member
UK
 
Futures Experience: Intermediate
Platform: NT
Broker/Data: ib
Favorite Futures: Electric Guitar
 
Mindset's Avatar
 
Posts: 355 since Sep 2009
Thanks: 88 given, 285 received

Error correction on Forex

Programming some code today I discovered an error that is unusual and took me a while to solve.

Very occasionally on an IB feed I noticed that my entryprice vs current price in ticks was out by 1 ie 10t profit was showing as 9t in my code but NOT all the time.
Eventually I reasoned that it's due to some internal rounding that NT does. It is most noticeable on forex futures eg 6E.

The solution seems messy but it does work

price = Close[0] or whatever you want;


 
Code
val = RoundPrice(Math.Abs((price - entryprice)))/TickSize;
//solves rounding issue
Obviously for a short position you would invert the above code (entryprice - price)

Here is the function code for RoundPrice

 
Code
//RoundPrice(Round2TickSize)
public Double RoundPrice ( double price) 
{
return base.Bars.Instrument.MasterInstrument.Round2TickSize(price);
}

Reply With Quote
 
  #10 (permalink)
Elite Member
Near the BEuTiFULL Horse Shoe
 
Futures Experience: Beginner
Platform: NinjaTrader
Broker/Data: MBTrading Dukascopy ZenFire
Favorite Futures: $EURUSD when it is trending
 
Trader.Jon's Avatar
 
Posts: 500 since Jul 2009
Thanks: 401 given, 176 received

COBC not good for NT7 strategy


Not exactly a code snippet, yet ...

NT Support recommends that any indicator that is used within a strategy NOT have any reference to CalculateOnBarClose ...

I looked at a random sample of the default indicators and they do not have COBC in their code. I tried it myself for backtesting a strategy and it does make a small difference in outputs (depending on the complexity of the indicator)

YMMV !!

Jon

Reply With Quote
The following user says Thank You to Trader.Jon for this post:

Reply



futures io > > > > > Code Snippets needed!

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)

Linda Bradford Raschke: Reading The Tape

Elite only

Adam Grimes: TBA

Elite only

NinjaTrader: TBA

January

Ran Aroussi: TBA

Elite only
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
help needed red burns Traders Hideout 3 May 19th, 2011 10:38 PM
Turtle TS-code help needed from programmers abcplot EasyLanguage Programming 1 May 11th, 2011 03:29 PM
Just a little code assist needed for entry filter Trader.Jon NinjaTrader Programming 2 October 27th, 2010 02:25 PM
code snippets Saroj Feedback and Announcements 1 September 11th, 2009 02:10 PM


All times are GMT -4. The time now is 12:34 AM.

Copyright © 2017 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
Page generated 2017-12-11 in 0.16 seconds with 19 queries on phoenix via your IP 107.20.115.174