Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Sendmail()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    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?

    #2
    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:

    Code:
    string myText = "The close: " + Close[0] + " some other message like the High: " + High[0];
    You can also use string.Format which I see as a cleaner approach:

    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.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Thanks heaps for the prompt reply - that worked a treat.

      Comment


        #4
        So I got that working (many thanks)....but I realised since I trade forex that the spread is important so I really need bid/ask prices.

        Is there a way to get sendmail() to include both the Bid Low and the Ask High price in the email?

        Comment


          #5
          I just realised I could solve the problem by running the same email alert strategy twice and selecting bid/ask for the data series option, but I was hoping there might be a more elegant solution

          Comment


            #6
            Hello,

            Per your statement
            running the same email alert strategy twice and selecting bid/ask for the data series option
            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.

            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.
            JesseNinjaTrader Customer Service

            Comment


              #7
              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


                #8
                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:

                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);
                }
                I look forward to being of further assistance.
                JesseNinjaTrader Customer Service

                Comment


                  #9
                  In the example that you have given the email will simply return the current bid/ask prices, but what I am really after is the Low[0] (Bid) and the High[0] (Ask). Is there a way to do that in ninjascript?

                  Comment


                    #10
                    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:

                    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);
                    		}	
                    	}
                    }
                    I look forward to being of further assistance.
                    JesseNinjaTrader Customer Service

                    Comment


                      #11
                      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:

                      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);
                      		}	
                      	}
                      }
                      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?

                      Comment


                        #12
                        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.
                        JesseNinjaTrader Customer Service

                        Comment


                          #13
                          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

                          Code:
                          Add("", PeriodType.Minute, 5, MarketDataType.Ask);
                          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"

                          Comment


                            #14
                            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:

                            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);
                                    }
                            If there is a better way of doing it (or you can see a problem with the above) then please let me know.

                            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


                              #15
                              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

                              Code:
                              double myPrice = Close[0] + 2 * TickSize;
                              This would equate to the price plus 2 ticks of the primary instrument.


                              Can you confirm if this was what you had intended?

                              I look forward to being of further assistance.
                              JesseNinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by selu72, Today, 02:01 PM
                              1 response
                              3 views
                              0 likes
                              Last Post NinjaTrader_Zachary  
                              Started by WHICKED, Today, 02:02 PM
                              2 responses
                              8 views
                              0 likes
                              Last Post WHICKED
                              by WHICKED
                               
                              Started by f.saeidi, Today, 12:14 PM
                              8 responses
                              21 views
                              0 likes
                              Last Post f.saeidi  
                              Started by Mikey_, 03-23-2024, 05:59 PM
                              3 responses
                              50 views
                              0 likes
                              Last Post Sam2515
                              by Sam2515
                               
                              Started by Russ Moreland, Today, 12:54 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Erick  
                              Working...
                              X