SendMail() for strategies - NinjaTrader Programming | futures io social day trading
futures io futures trading


SendMail() for strategies
Updated: Views / Replies:6,559 / 34
Created: by dsraider Attachments:4

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 dont 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
 4  
 
Thread Tools Search this Thread
 

SendMail() for strategies

  #11 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received

Sam,

Thanks for taking a look at this. I'm losing my mind.

Dave

EDIT: Added the zip in case that's easier. Ideally, I'd like to see the following but don't know if it's possible. Would love to only get one email for each upon fill:

entry: "ES Long 1150.50"
exit: "ES Long Exit 1154.50: +4"
stop: "ES Stop Filled 1147.50: -3"
target: "ES Target Filled 1170.50: +20"

Attached Files
Register to download File Type: cs Cross2050.cs (9.3 KB, 23 views)
Register to download File Type: zip Cross2050.zip (4.3 KB, 13 views)

Last edited by dsraider; May 12th, 2010 at 08:00 AM.
Reply With Quote
The following 2 users say Thank You to dsraider for this post:
 
  #12 (permalink)
Administrator: Retired Backtester
 Vendor: speedytradingservers.com 
Rennes France
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: IB/Kinetick
Favorite Futures: Futures
 
sam028's Avatar
 
Posts: 3,366 since Jun 2009
Thanks: 3,572 given, 3,982 received

I've just read it, can play with Ninja now, but some quick remarks:
- I don't see any boolean flag, to avoid multiple emails, and
- maybe using entryOrder1.AvgFillPrice instead of Position.AvgPrice

 
Code
                            
if ((entryOrder1.OrderState == OrderState.Filled) && (! _enter_email)) {
  
SendMail("""username@yahoo.com""ES Long " entryOrder1.AvgFillPrice"");
  
_enter_email true;} 

Success requires no deodorant! (Sun Tzu)
Reply With Quote
The following 2 users say Thank You to sam028 for this post:
 
  #13 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received


Hey Sam,

Good news: entryOrder1.AvgFillPrice worked perfectly. Thanks for that.

Bad news: Neither...

 
Code
Variables
private bool 	_enter_email	 	= false;

if (entryOrder1 != null && entryOrder1.Token == order.Token)
    {
    if (entryOrder1.OrderState == OrderState.Filled && (! _enter_email))
    SendMail("", "username@yahoo.com", "ES Long " + entryOrder1.AvgFillPrice, "");
    _enter_email = true;
    }
nor...

 
Code
Variables
private bool 	_enter_mail	 	= false;

if (entryOrder1 != null && entryOrder1.Token == order.Token)
    {
    if (entryOrder1.OrderState == OrderState.Filled && (! _enter_mail))
    SendMail("", "username@yahoo.com", "ES Long " + entryOrder1.AvgFillPrice, "");
    _enter_mail = true;
    }
produced any emails at all. It wouldn't let me code the bool under Initialize() so I tried it under Variables. Did I miss a step?

Thanks again for your time.
Dave

Reply With Quote
 
  #14 (permalink)
Administrator: Retired Backtester
 Vendor: speedytradingservers.com 
Rennes France
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: IB/Kinetick
Favorite Futures: Futures
 
sam028's Avatar
 
Posts: 3,366 since Jun 2009
Thanks: 3,572 given, 3,982 received

I've made a quick example, take a look at it.

MACrossEmail.cs

In this example, it will only send one mail, the boolean variable have to be re-initialized to false, when needed.
The first steps is, imho, to write, on a piece of paper, what you want to do, because the logic might be very clear first, as you'll have to play with multiple booleans values here.
And do some Print() first, instead of sirect sendmail, it'll be easier to debug.

Success requires no deodorant! (Sun Tzu)
Reply With Quote
The following 3 users say Thank You to sam028 for this post:
 
  #15 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received

Wow. You really went beyond the call of duty on that one. It worked like a charm. Onto targets and my trailing stop.

A million thank you's, sir.

Your humble servant,
Dave

Reply With Quote
 
  #16 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received

Okay,

I've added stops and targets. Everything emails and emails ONLY once. Thanks again for that. The only problem I'm now having is turning:

ES Stop Filled 1170

into

ES Stop Filled 11.70: +2.5

Here's where I'm at:

 
Code
Variables
private double 	lastProfit 				= 0;

protected override void OnBarUpdate()
if (Performance.RealtimeTrades.Count > 0) 
{ 
	// Get the last completed real-time trade (at index 0) 
	Trade lastTrade = Performance.RealtimeTrades[0]; 

        // Calculate the PnL for the last completed real-time trade 
	double lastProfit = lastTrade.ProfitPoints; 

	// Pring the PnL to the Output window 
	Print("The last trade's profit is " + lastProfit); 
} 

protected override void OnOrderUpdate(IOrder order)
if (stopLossTokens.Contains(order.Token))
{
	// Check order for terminal state
	if (order.OrderState == OrderState.Filled)
        {
	        // Print out information about the order
		Print(order.ToString());
		SendMail("", "username@yahoo.com", Instrument.MasterInstrument.Name + " Stop Filled " + order.AvgFillPrice + ": " + lastProfit, "");
		Print("Sendmail: " + Instrument.MasterInstrument.Name + " Stop Filled " +    order.Action + " " + order.AvgFillPrice + ": " + lastProfit);
		stopSent = true;
					
		// Remove from collection
		stopLossTokens.Remove(order.Token);
	}
}
It still emails but I get ES Stop Filled 1170: 0. Does anything jump out at you?

Thanks.

Reply With Quote
 
  #17 (permalink)
Elite Member
Wrocław, Poland
 
Futures Experience: Intermediate
Platform: NinjaTrader, Racket
Favorite Futures: Ockham's razor
 
gregid's Avatar
 
Posts: 651 since Aug 2009
Thanks: 321 given, 605 received

dsraider

in your code change this line:

double lastProfit = lastTrade.ProfitPoints;

to:

lastProfit = lastTrade.ProfitPoints;

Reply With Quote
The following user says Thank You to gregid for this post:
 
  #18 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received

gregid,

Apologies for not thanking you sooner for your last post but I'm afraid I just can't get this thing to work properly, even when trying to follow your instructions verbatim. I'm having the following issues:

1. Sometimes it enters upon a cross. Sometimes it doesn't.
2. Sometimes it emails on entry and exit. Sometimes it doesn't.
3. Filling of Target or Stop Loss results in no email.
4. Filling of Exit results in "ES Long Exit Filled 1170: 0" (instead of actual profit/loss).
5. In the output window, lastProfit only works with the first trade and doesn't change thereafter.

My guess is that some things are resetting after the first trade and some thing aren't but I'm currently at a loss. If you have a few minutes (and don't yet want to kill me) maybe you can see what the problem is?

Thanks,
Dave

Attached Files
Register to download File Type: zip Cross2050.zip (5.2 KB, 15 views)

Last edited by dsraider; May 23rd, 2010 at 08:25 AM.
Reply With Quote
The following user says Thank You to dsraider for this post:
 
  #19 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received

To anyone following along, I now have my strat emailing when the stop or target is filled. If interested, here's the code:

 
Code
                            
//Variables
private ArrayList stopLossTokens         = new ArrayList();
private 
ArrayList profitTargetTokens     = new ArrayList();

protected 
override void Initialize()
SetProfitTarget("Cross2050"CalculationMode.Ticks80);

protected 
override void OnBarUpdate() //using this for stop loss as I have a breakeven and trail in place
SetStopLoss("Cross2050"CalculationMode.Ticks12false);

protected 
override void OnOrderUpdate(IOrder order)
if (
order.OrderState == OrderState.PendingSubmit)
            {
                
// Add the "Stop loss" orders to the Stop Loss collection
                
if (order.Name == "Stop loss")
                    
stopLossTokens.Add(order.Token);
                
                
// Add the "Profit target" orders to the Profit Target collection
                
else if (order.Name == "Profit target")
                    
profitTargetTokens.Add(order.Token);
            }
            

            
// Process stop loss orders
            
if (stopLossTokens.Contains(order.Token))
            {
                
// Check order for terminal state
                
if (order.OrderState == OrderState.Filled)
                {
                    
// Print out information about the order
                    
Print(order.ToString());
                    
SendMail("""username@yahoo.com"Instrument.MasterInstrument.Name " Cross2050 Stop Filled " order.AvgFillPrice ": " lastTrade"");
                    
                if (
order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected)    
                    
// Remove from collection
                    
{
                    
stopLossTokens.Remove(order.Token);
                    }
                    
                }
                
// Print out the current stop loss price
                
else
                    Print(
"The order name " order.Name " stop price is currently " order.StopPrice);
            }
            
            
            
// Process profit target orders
            
if (profitTargetTokens.Contains(order.Token))
            {
                
// Check order for terminal state
                
if (order.OrderState == OrderState.Filled)
                {
                    
// Print out information about the order
                    
Print(order.ToString());
                    
SendMail("""username@yahoo.com"Instrument.MasterInstrument.Name " Cross2050 Target Filled " order.AvgFillPrice ": " lastTrade"");
                
                    if (
order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected)
                    {    
                    
// Remove from collection
                    
profitTargetTokens.Remove(order.Token);
                    }
                }
                
                
// Print out the current stop loss price
                
else
                    Print(
"The order name " order.Name " limit price is currently " order.LimitPrice);
            } 
The only thing left to do is to turn "ES Cross2050 Target Filled: 0" into "ES Cross2050 Target Filled: +20". I'm pretty close but not quite sure how to declare PnL as a variable. If anyone has an example and would like to post, great. Otherwise, I'll post the rest when I figure it out.

Dave


Last edited by sam028; May 31st, 2010 at 08:43 AM. Reason: add PHP tag
Reply With Quote
The following user says Thank You to dsraider for this post:
 
  #20 (permalink)
Trading for Fun
New York, NY
 
Futures Experience: Advanced
Platform: NinjaTrader
Broker/Data: Amp Futures/Zen-Fire
Favorite Futures: ES
 
Posts: 142 since Dec 2009
Thanks: 41 given, 83 received


Okay, as far as defining a variable is concerned, this works:

 
Code
                            
Variables
private Trade     lastTrade
The rest is weird. I changed lastTrade to lastTrade.ProfitPoints in SendMail() and it's brought my strat to a hault. Anyone know why?

Thanks,
Dave

Reply With Quote

Reply



futures io > > > > > SendMail() for strategies

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
Implementing a delay after first SendMail execution, possible? raindrop NinjaTrader Programming 13 August 17th, 2011 03:55 PM
Stop Loss/Target & SendMail() dsraider NinjaTrader Programming 4 May 30th, 2010 07:15 AM
Communication between strategies zwentz NinjaTrader Programming 4 May 2nd, 2010 11:15 PM
Question about sendmail in strategies fiki NinjaTrader Programming 3 June 24th, 2009 02:25 AM


All times are GMT -4. The time now is 10:53 PM.

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.12 seconds with 20 queries on phoenix via your IP 54.145.16.43