NexusFi: Find Your Edge


Home Menu

 





SendMail() for strategies


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one dsraider with 20 posts (5 thanks)
    2. looks_two sam028 with 6 posts (10 thanks)
    3. looks_3 gregid with 6 posts (3 thanks)
    4. looks_4 Pristine with 1 posts (0 thanks)
      Best Posters
    1. looks_one Big Mike with 2 thanks per post
    2. looks_two sam028 with 1.7 thanks per post
    3. looks_3 gregid with 0.5 thanks per post
    4. looks_4 dsraider with 0.3 thanks per post
    1. trending_up 14,208 views
    2. thumb_up 20 thanks given
    3. group 5 followers
    1. forum 34 posts
    2. attach_file 4 attachments




 
Search this Thread

SendMail() for strategies

  #11 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

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
Elite Membership required to download: Cross2050.cs
Elite Membership required to download: Cross2050.zip
Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
Cheap historycal L1 data for stocks
Stocks and ETFs
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
MC PL editor upgrade
MultiCharts
What broker to use for trading palladium futures
Commodities
 
  #12 (permalink)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629

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("""[email protected]""ES Long " entryOrder1.AvgFillPrice"");
  
_enter_email true;} 

Success requires no deodorant! (Sun Tzu)
Follow me on Twitter Reply With Quote
Thanked by:
  #13 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87


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("", "[email protected]", "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("", "[email protected]", "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)
 
sam028's Avatar
 sam028 
Site Moderator
 
Posts: 3,765 since Jun 2009
Thanks Given: 3,825
Thanks Received: 4,629

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)
Follow me on Twitter Reply With Quote
Thanked by:
  #15 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

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)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

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("", "[email protected]", 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)
 
gregid's Avatar
 gregid 
Wrocław, Poland
 
Experience: Intermediate
Platform: NinjaTrader, Racket
Trading: Ockham's razor
Posts: 650 since Aug 2009
Thanks Given: 320
Thanks Received: 623

dsraider

in your code change this line:

double lastProfit = lastTrade.ProfitPoints;

to:

lastProfit = lastTrade.ProfitPoints;

Reply With Quote
Thanked by:
  #18 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

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
Elite Membership required to download: Cross2050.zip
Reply With Quote
Thanked by:
  #19 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

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("""[email protected]"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("""[email protected]"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

Reply With Quote
Thanked by:
  #20 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87


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




Last Updated on January 17, 2015


© 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