PDA

View Full Version : Sending E-mail upon order execution


dgregor5
07-13-2007, 03:51 AM
Hi.
I have had some great support from you guys trying to get the email function working upon order execution for live simulated trading using the Ninjascript. To ensure e-mails are not sent continuously upon order execution I am using the 'MarketPosition.Flat/Long' function filter. However, one issue I am noting is that when specific entry/exit conditions are met, 4-5 e-mails are sent prior to the order being filled/flattened. How is it possible to restrict the number of e-mails sent to just one per entry exit ??

thx
David

NinjaTrader_Dierk
07-13-2007, 04:18 AM
You would need to custom code this in your strategy by setting/resetting a flag variable to indicate that as mail has been sent.

dgregor5
07-13-2007, 07:28 AM
thx - where do I look in the help guide for developing such flags?

NinjaTrader_Ray
07-13-2007, 07:38 AM
There is no documentation on this since this is not NinjaScript specific but programming concept.

For example:

bool sentMail = false;

if (sentMail == false)
{
SendMail();
sentMail = true;
}

dgregor5
07-13-2007, 07:50 AM
thx - I will try this out & get back to you

dgregor5
07-15-2007, 04:42 AM
tried this & it dsoes work - thx for your help
David

dgregor5
07-22-2007, 04:08 AM
Hi ----

Although this strategy woks in principle, I often receive a number of e-mails per single order execution - I am assuming this is because there is a lag between conditions being met & order being executed. How is it possible to ensure that only a single e-mail is sent per order execution.

I have attached the custom NT strategy for your reference.

Many thx for your help.
David

:confused:

NinjaTrader_Dierk
07-22-2007, 04:49 AM
Sorry due to bandwith reasons we are unable to provide support to the level of actually coding or reviewing strategies.

Quick hint:
Your condition
bool sentMail=false;
if(sentMail==false)
always (!) triggers -> the filter you put in place is not effective.

You might consider contacting a NinjaScript consultant for further C#/NinjaScript coding support: http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm

dgregor5
07-22-2007, 09:34 AM
Hi.
Now I am confused. I was only implementing the advice given by Ray earlier in the thread - which appears to contradict your hint below.

in the on bar update section, I am struggling to understand how to do anything just the once............ for example, the following just continues to print NT in the output window - I presume this is because everything is reset for every update.

int x = 0;
if (x == 0)


{
Print("NT");

}

All options on pages 353-356 in your help manual print repeatedly the same item, nothing happens just the once.

How for example in NT do you ensure the order is executed only once & not for every bar update - surely this is just the same thing that I am trying to repeat??

I would appreciate your advice - I am not convinced that this is a C# programming issue.

thx in advance
David

NinjaTrader_Ray
07-22-2007, 09:39 AM
The problem is that you are declaring variables whose scope is only relevant for the OnBarUpdate() method. This means that its lifetime is only valid for each call to the OnBarUpdate() method.

Put the sentMail variable outside of the OnBarUpdate() method.

Such as:

private bool sentMail = false;

Please google "Scope and lifetime of a variable" for additional resources in understanding this topic.

dgregor5
07-22-2007, 10:30 AM
thx - that moves things in the right direction when printing to the output window - I can get this to happen just once using the simulated datafeed.
When replacing the print function with sendmail function, the mail is not sent. I am hoping that this is due to the fact that I am using the simulated datafeed function rather than real-time data feed - pls could you confirm this......

thx
David

NinjaTrader_Ray
07-23-2007, 03:58 AM
This will work in any real-time streaming data. It will not work in Historical.

You will need to check your program logic and debug why it does not work in a simulated data feed environment.

dgregor5
07-27-2007, 11:22 AM
thx Ray, but still struggling........this to me should be simple.

I have a

private bool x=false;

& then the following on OnBarUpdate().................

// Condition set 1

if(x == false)

{Print("BUY BUY BUY");
x=true;
}

this prints once (as expected) to the Output Window.
However, if I replace the Print function with the SendMail function (with the appropriate info after), no e-mail is sent & nothing is recorded in the log. Pls could you try the same................

this i don't understand.

thx in advance
DG

NinjaTrader_Ray
07-27-2007, 01:51 PM
The likely issue is that your SMTP server is not working or configured correctly. You can test this in the Tools > Options area.

If it prints the message, then for sure the code block is executing. Also check your log tab for any further errors.

dgregor5
07-27-2007, 02:29 PM
Hi Ray.

1/ The message does print to the Output Window once as expected.
2/ e-mail / SMTP is OK with test message (checked via Tools -> Options.....)
3/ continuous e-mails sent with placing a ';' after: if (x==false);
4/ no ';' ...... no e-mail sent.

Have you tried this yourself ?

thx
David

NinjaTrader_Dierk
07-27-2007, 10:54 PM
3+4 is a clear indicator that there is a coding issue in your logic, since email sending works in general. I suggest working the Print() statement for now and finish implementation of your strategy and then you may add SendMail() statements right after the Print() statements (not replace, to see that mails come in sync with the Print() statements).

dgregor5
07-28-2007, 12:42 AM
Thx for the response Dierk......hope you guys dont think I am being a pain on this !

In your note, are you suggesting that I have to wait until you fix the coding issue, before I will be able to use this function at all? or that if I use the Print () statement prior to the SendMail () function that should work.....since trying this the morning it doesn't. See below:

if(x == false)
{ Print("BUY BUY BUY");
SendMail("dgregor56@yahoo.co.uk", "dgregor56@yahoo.co.uk", "BUY BUY BUY", "");
Print("BUY BUY BUY");
x=true;
}.


Also, can I assume that it will be 3-4 weeks before a fix will be in place?
many thx
David

NinjaTrader_Dierk
07-28-2007, 10:59 PM
>> are you suggesting that I have to wait until you fix the coding issue, before I will be able to use this function at all?
No, we are not aware of any issue. I fixed the typo in my post. You are on the right track with your code snippet below.

dgregor5
07-29-2007, 03:19 AM
thx.
Current issue is that using a simulated data feed (because it is the w/e), I am unable to send any emails whatsoever from a strategy.....even one that is as simple as: SendMail() with no other code.
I noted this problem last w/e, but it was suggested there was a bug in my code.

This is the possible reason why when testing the snippet below over the w/e, Print() works by itself, but Print(); SendMail() only Prints.

thx for your help.
DG

NinjaTrader_Dierk
07-29-2007, 04:35 AM
a) I suppose you have set up your SMTP server at Tools->Options->Misc and tested that sending mails works ok, no?
b) you double checked that the mail address you are using is valid, no?

I just tested simple strategy below on the sim feed (5 tick chart) and it works as expected -> mails are sent as sim (live) data comes in:
protected override void OnBarUpdate()
{
if (Historical)
return;

if (CrossAbove(SMA(5), SMA(20), 1))
{
Print("EnterLong");
SendMail(..., "EnterLong", "EnterLong");
EnterLong();
}
else if (CrossBelow(SMA(5), SMA(20), 1))
{
Print("EnterShort");
SendMail(..., "EnterShort", "EnterShort");
EnterShort();
}
}

dgregor5
07-29-2007, 07:11 AM
thx for your respons.

I have now resolved why no e-mail has been sent - it was because the 'Exclude weekends' was set to true !!!!

So now I can send copious e-mails simulataneously through the SIM feed.
I am still unable to do this just the once, however, using a filter.

Pls try the following --- it executes the Print() command, but not the SendMail() command. I have been at this now for over two weeks.....I seem to be getting nowhere!

Is it how I set up the private bool expression?
Even if I change it to an int, I get the same result.

private bool x = false;
#endregion

/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>


protected override void OnBarUpdate()
{
// Condition set 1
if(x == false)
{
Print("BUY");
SendMail("dgregor55@yahoo.co.uk", "dgregor55@yahoo.co.uk", "BUY", "");
Print("BUY");
}
x=true;

NinjaTrader_Dierk
07-29-2007, 07:18 AM
This then is a bug on your strategy code (since SendMail as per my sample works with your SMTP setup). Unfortunately, due to bandwidth reasons, we are unable to provide support down to the level of actual coding/revising strategies. You might consider contacting a NinjaScript consultant: http://www.ninjatrader.com/webnew/partners_onlinetrading_NinjaScript.htm

Thanks for your understanding.

dgregor5
07-29-2007, 07:32 AM
Your example sends numerous emails per order execution.
Hence the need for a filter.
Hence my example in the prior thread below.

I do not understand how if the following prints "BUY" twice but does not send an e-mail means that there is fault in my code........there is nothing in the log either which suggests that it doesn't even attempt to send an e-mai.

Print()
SendMail()
Print()

I will ask you again - pls try what I have done below.
If you get it to work by changing the code then fine I will contact a consultant.

PS I will not give up :)

thx
David

NinjaTrader_Ray
07-29-2007, 03:11 PM
In post #12 I stated that SendMail() does not work on historical data. I suspect this is the logic flaw in your code.

Your code executes on the first bar only which is likely historical, try adding the code below to resolve your issue.

if (Historical)
return;

dgregor5
07-30-2007, 12:55 PM
Ray / Dierk
Thx for your patience & on-going support in trying to resolve this issue.
In the end yesterday I resorted to contacting a NinjaScript consultant.
Best cash I have spent in a long time.
Code delivered in less that 18 hours at a very reasonable price --- with a very positive response.
Should have done this 4 weeks ago............still at least I have an improved understanding of how your code & NT strategies work.

thx
David :)

NinjaTrader_Dierk
07-30-2007, 01:19 PM
Thanks for your feedback. Glad this approach made sense for you.