Announcement
Collapse
No announcement yet.
Partner 728x90
Collapse
NinjaTrader
Sendmail()
Collapse
X
-
Sendmail()
Is there a way to have the sendmail() method send information about a trade setup? I would like to have an alert emailed to me when my method triggers a setup (which I can do no problems) but I would like to include the High and Low information for the current bar. Is that possible at all?Tags: None
-
Hello,
Yes this would be possible. The SendMail method accepts a String, you can form that string anyway you would like.
Here are two examples of creating strings from variables:
You can also use string.Format which I see as a cleaner approach:Code:string myText = "The close: " + Close[0] + " some other message like the High: " + High[0];
Code:string myText = string.Format("My message, the close was: {0} while the High was: {1}", Close[0], High[0]);
Converts the value of objects to strings based on the formats specified and inserts them into another string. If you are new to the String.Format method, see Get started with the String.Format method for a quick overview.
Finally you could use this in the Body of the SendMail method or:
Code:string myText = string.Format("My message, the close was: {0} while the High was: {1}", Close[0], High[0]); SendMail("myFromAddress", "MyToAddress", "Some Subject", myText);
I look forward to being of further assistance.
-
Hello,
Per your statement
There is nothing wrong with this if they are not placing trades, but you could likely combine the logic into a single script as well to avoid multiple instances of the same script being run in unison.running the same email alert strategy twice and selecting bid/ask for the data series option
One example of a multi series strategy comes with the platform and is called SampleMultiTimeFrame
If you are only trying to access the last ask and bid prices you could also see these methods
I look forward to being of further assistance.
Comment
-
Hi,
Thanks for the prompt reply. What I am aiming for is:
If (//buy conditions)
{ sendmail }
If (//sell conditions)
{ sendmail }
Could I use getcurrentbid() and/or getcurrentask() in that context? (I have set COBC = true) Basically what I want to do is have my strategy send me an email as an alert with the relevant details so I can then check the market, decide if I want to trade and if so place the trade based on the information in the email.
Comment
-
Hello,
Yes in this case if you just need the prices, the methods would likely be the easiest approach.
Here is a simple example of a usage:
I look forward to being of further assistance.Code:if(Close[0] > Close[1]) { string myEmailText = string.Format("Close price: {0} Ask price: {1} Bid price: {2}", Close[0], GetCurrentAsk(), GetCurrentBid()); SendMail("to","from", "subject", myEmailText); }
Comment
-
Hello,
Thank you for the reply.
To clarify, you are asking for the Low and High of the ask and bid series, is this correct?
If so, you could use the Add() syntax to Add an Ask and Bid series and then use the Lows and Highs objects to get those series values:
I look forward to being of further assistance.Code:protected override void Initialize() { CalculateOnBarClose = true; Add("ES 12-16", PeriodType.Tick,1, MarketDataType.Ask); Add("ES 12-16", PeriodType.Tick,1, MarketDataType.Bid); } protected override void OnBarUpdate() { if(BarsInProgress == 0) { if(Close[0] > Close[1]) { double lowOfAskSeries = Lows[1][0]; double highOfAskSeries = Highs[1][0]; double lowOfBidSeries = Lows[2][0]; double highOfBidSeries = Highs[2][0]; string myEmailText = string.Format("Close price: {0} Ask low price: {1} Ask high price: {2} Bid low price: {3} Bid high price: {4}", Close[0], lowOfAskSeries, highOfAskSeries, lowOfBidSeries, highOfBidSeries); SendMail("to","from", "subject", myEmailText); } } }
Comment
-
Hi,
I am actually just after the High and the Low of the setup bar (which also be the most recent bar just closed as my script runs with COBC = true), so High[0] and Low[0].
Further to your reply I was thinking I would only need to add one series (say Ask prices) and select Bid as the data series when I load the strategy (that way I get the bid prices by default).
So taking the sample code from your previous post and modifying to accommodate my suggestion above:
If I then select Bid as the data series option the code above should return the Low (Bid price) of the setup bar and the High (Ask price) of the setup bar?Code:protected override void Initialize() { CalculateOnBarClose = true; Add("ES 12-16", PeriodType.Tick,1, MarketDataType.Ask); } protected override void OnBarUpdate() { if(BarsInProgress == 0) { if(Close[0] > Close[1]) { double highOfAskSeries = High[0]; string myEmailText = string.Format("Ask high price: {0} Bid low price: {1}, highOfAskSeries, Low[0]); SendMail("to","from", "subject", myEmailText); } } }
Comment
-
Hello,
Thank you for the reply.
In what you have provided, that would allow you to use only the ask and bid series if you select the Bid on the chart. Are you not intending to need Last data and only use Ask and Bid data? If so that would work for that purpose with the change of the High[0] as that references the Primary series. Instead you would need to use Highs[1][0] to access the Secondary series data for Ask.
If you are intending on using Last data as well, the sample I had provided would still be correct as that both ask and bid and would assume the chart is run on Last data. In that case you could access the high and low of all 3 series.
Please let me know if I may be of further assistance.
Comment
-
Hi,
In answer to your question no I don't intend to use Last price data...but I suppose adding both the Bid and Ask would avoid any unfortunate errors in future if for some reason I accidentally started a new strategy and forgot to change the price type to Bid so I'll go with your original suggestion to be on the safe side.
I would like to make the code a little more generic if possible - so that the Add() picks up the instrument name from whatever instrument I select when I start a new strategy. How would I go about doing that? I tried
but it didn't work; I got an error message saying "...A foreign key value cannot be inserted because a corresponding primary key value does not exist"Code:Add("", PeriodType.Minute, 5, MarketDataType.Ask);
Comment
-
So thinking about this I realized I could apply the initial thread response solution to the current post i.e. create a string type variable and then pass that into the add like so:
If there is a better way of doing it (or you can see a problem with the above) then please let me know.Code:protected override void Initialize() { CalculateOnBarClose = true; string market = Instrument.FullName; Add(market, PeriodType.Minute, 5, MarketDataType.Ask); Add(market,PeriodType.Minute, 5, MarketDataType.Bid); }
Sorry to be a pain but I do have another question on this topic: now that I can get the Ask High, Bid Low and FOREX pair sent to me in the email I would also like to calculate the position size.
I can figure out all the intermediate steps in ninja script as they are just basic arithmetic:
Account Balance * Risk % = Position Size (PS)
Initial Risk (IR) = Entry - Stop
Trade Size = PS / (IR * pip cost)
the question I have relates to getting the pip cost. I could use an IF/THEN combination for every single forex pair that I trade....but that would be a lot of IF/THEN statements in the one script. I could also just have one copy of the strategy per pair traded and hard code the pip cost for each one, but ideally I'd like to solve the problem with code if possible. Is there some equivalent of the VLOOKUP function in excel where I could create all the pairs with their pip cost and then check the signal pair against that list to get the right pip cost to insert in the position sizing calculation?
Comment
-
Hello,
Thank you for the reply.
It seems you had found the Instruments name, this would work for accessing the name dynamically.
Regarding the pip cost, are you referring to the Tick size of the instrument?
If so, you can access that by using TickSize
This would equate to the price plus 2 ticks of the primary instrument.Code:double myPrice = Close[0] + 2 * TickSize;
Can you confirm if this was what you had intended?
I look forward to being of further assistance.
Comment
Latest Posts
Collapse
| Topics | Statistics | Last Post | ||
|---|---|---|---|---|
|
Started by Geovanny Suaza, 02-11-2026, 06:32 PM
|
0 responses
601 views
0 likes
|
Last Post
|
||
|
Started by Geovanny Suaza, 02-11-2026, 05:51 PM
|
0 responses
347 views
1 like
|
Last Post
|
||
|
Started by Mindset, 02-09-2026, 11:44 AM
|
0 responses
103 views
0 likes
|
Last Post
by Mindset
02-09-2026, 11:44 AM
|
||
|
Started by Geovanny Suaza, 02-02-2026, 12:30 PM
|
0 responses
559 views
1 like
|
Last Post
|
||
|
Started by RFrosty, 01-28-2026, 06:49 PM
|
0 responses
558 views
1 like
|
Last Post
by RFrosty
01-28-2026, 06:49 PM
|

Comment