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

SetTrailStop with SendMail function

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

    SetTrailStop with SendMail function

    Hello NT7 gurus.
    I have developed a simple managed approach strategy that generate signals for LONGs and SHORTs and each time that the strategy operates it use the SendMail function to inform and explain the type of trade to execute.
    In a last improvement version to the strategy, I introduced a Trail Stop that gave me a lot of satisfaction in terms of P/L but I realize that the Close of the Positions is no more monitored as the closing orders are generated by Ninja in an kind of low level layer without any message.
    So I would need to send also in case of triggering the Stop Orders to send an email as I do for the Entering LONG/SHORT orders to open the positions.

    What can I do ? Is there another function/method to monitor and generate emails with stop orders ?

    Thanks a lot

    #2
    sample code...

    To be more clear, this is a sample of the code I am using( Sell to Close orders for LONG Positions):
    if (vix < VIXLOW)
    {
    SetTrailStop("L1", CalculationMode.Percent, PT, true);
    SetTrailStop("L1B", CalculationMode.Percent, PT, true);
    SendMail(indirizzo[0],indirizzo[1] , "LOUT", "VENDERE tutte le quote in possesso di " + strumento[0] + " sul prezzo base di" + Closes[0][0]+" CON STOP TRIAL DEL "+PT+" %" );
    SendMail(indirizzo[0],indirizzo[2] , "LOUT", "VENDERE tutte le quote in possesso di " + strumento[0] + " sul prezzo base di" + Closes[0][0]+" CON STOP TRIAL DEL "+PT+" %" );
    }
    else
    {
    SetTrailStop("L1", CalculationMode.Percent, PTX, true);
    SetTrailStop("L1B", CalculationMode.Percent, PTX, true);
    SendMail(indirizzo[0],indirizzo[1] , "LOUT", "VENDERE tutte le quote in possesso di " + strumento[0] + " sul prezzo base di" + Closes[0][0]+" CON STOP TRIAL DEL "+PTX+" %" );
    SendMail(indirizzo[0],indirizzo[2] , "LOUT", "VENDERE tutte le quote in possesso di " + strumento[0] + " sul prezzo base di" + Closes[0][0]+" CON STOP TRIAL DEL "+PTX+" %" );
    }

    Comment


      #3
      Hello avalassina,

      What are you looking to capture specifically here?

      Just when you have set the trail stops or when they are filled?
      Cal H.NinjaTrader Customer Service

      Comment


        #4
        Hi Cal.
        I need to track when they are filled.
        Once the market price trig the stop limit price, I need to send an email.

        Thanks

        Alessandro

        Comment


          #5
          Hello avalassina,

          Thank you for your response.

          You would use OnExecution() and check for the execution.Name and execution.Order.OrderState == OrderState.Filled. The name you would check for is "Stop loss" for Stop Loss and "Profit target" for Profit Target, and Trail Stop is "Trail stop".

          For information on OnExecution() please visit the following link: http://www.ninjatrader.com/support/h...iexecution.htm

          Comment


            #6
            Hi Patrick.
            Following your hint and using the manual, I have wrote this sample part, inserted in my strategy after the OnBarUpdate section:
            protected override void OnExecution(IExecution execution)
            {

            // Remember to check the underlying IOrder object for null before trying to access its properties
            if (execution.Order != null && execution.Order.OrderState == OrderState.Filled
            && execution.Name == "Trail stop" && execution.MarketPosition == MarketPosition.Long )
            {
            SendMail(indirizzo[0],indirizzo[1] , "CLOSING POSITION LONG", "LONG Position Closing of " + strumento[0] + " al prezzo corrente di " );
            Print(execution.ToString());
            }
            if (execution.Order != null && execution.Order.OrderState == OrderState.Filled
            && execution.Name == "Trail stop" && execution.MarketPosition == MarketPosition.Short )
            {
            SendMail(indirizzo[0],indirizzo[1] , "CLOSING POSITION SHORT", "SHORT Position Closing of " + strumento[0] + " al prezzo corrente di " );
            Print(execution.ToString());
            }
            }

            I cannot test it as my strategy is at low frequency and I suppose I need to test it only live, being not possible in backtesting mode.

            I am just wondering if the "Name" test is correct...
            Does it means that NT for every SetStopTrail executed order use that "Trail stop" name to tag those orders filled ? Is it a kind of standard named convention ?

            Thanks a lot.

            Comment


              #7
              Yes, that is correct. It would be the standard for the SetStopLoss(), SetProfitTarget() and SetTrailStop().

              Comment


                #8
                Originally posted by NinjaTrader_PatrickH View Post
                Yes, that is correct. It would be the standard for the SetStopLoss(), SetProfitTarget() and SetTrailStop().
                Hi Patrick.
                I am still here: as mine is a low frequency strategy, I needed to put in production the change and wait for the right movement to test it.
                Unfortunately it seems that something is not working properly.

                After having inserted ad the end of my code the following part:

                protected override void OnExecution(IExecution execution)
                {

                // Remember to check the underlying IOrder object for null before trying to access its properties
                if (execution.Order != null && execution.Order.OrderState == OrderState.Filled
                && execution.Name == "Trail stop" && execution.MarketPosition == MarketPosition.Long )
                {
                SendMail(indirizzo[0],indirizzo[1] , "CLOSING POSITION LONG", "VENDERE tutte le quote in possesso di " + strumento[0] + " al prezzo corrente di " + execution.Price );
                Print(execution.ToString());
                }
                if (execution.Order != null && execution.Order.OrderState == OrderState.Filled
                && execution.Name == "Trail stop" && execution.MarketPosition == MarketPosition.Short )
                {
                SendMail(indirizzo[0],indirizzo[1] , "CLOSING POSITION SHORT", "RICOMPRARE tutte le quote in possesso di " + strumento[0] + " al prezzo corrente di " + execution.Price);
                Print(execution.ToString());
                }
                }


                Where, in the protected override void OnBarUpdate() section I had previously set the indirizzo.[0] and [1] variable with the usual email address I use.

                The code worked as I can read from the Output Window this new row :
                Execution='7f2a55774d944e87beafe32a63f0eff2' Instrument='SPY' Account='Sim101' Name='Trail stop' Exchange=Default Price=202,97 Quantity=207 Market position=Short Commission=0 Order='3889e01a559a4615b57c68066efedaf8' Time='27/01/2015 15.45.41'


                But, no email was sent by Ninja !
                Any idea ?

                I am already using in other part of the srategy the Sendmail function, and it has been always worked properly, as for example in this part:

                if (Positions[0].MarketPosition == MarketPosition.Short
                // && Positions[0].GetProfitLoss (Close[0], PerformanceUnit.Currency)<0
                )
                {
                SendMail(indirizzo[0],indirizzo[1] , "SOUTB", "Chiusura posizione SHORT -> RICOMPRARE tutte le quote in possesso di " + strumento[0] + " al prezzo di apertura del " + Time[0].ToString() );
                SendMail(indirizzo[0],indirizzo[2] , "SOUTB", "Chiusura posizione SHORT -> RICOMPRARE tutte le quote in possesso di " + strumento[0] + " al prezzo di apertura del " + Time[0].ToString() );
                ExitShort(0, Positions[0].Quantity,"SOUTB", "");
                Svar1=0; Svar2=0; Svar3=0; Svar4=0; Svar5=0; Svar6=0; Svar7=0; Svar8=0; Svar9=0; Svar10=0;
                }


                Thanks a lot!

                Comment


                  #9
                  Hello avalassina,

                  Thank you for your response.

                  Do you see any messages in the Log tab of the NinjaTrader Control Center when this e-mail should be sent?

                  If you add another Print() before the print of the execution.ToString() such as Print(Time[0] + "Trail Stop ."); do you see it print in the Output window?

                  Comment


                    #10
                    Originally posted by NinjaTrader_PatrickH View Post
                    Hello avalassina,

                    Thank you for your response.

                    Do you see any messages in the Log tab of the NinjaTrader Control Center when this e-mail should be sent?

                    If you add another Print() before the print of the execution.ToString() such as Print(Time[0] + "Trail Stop ."); do you see it print in the Output window?
                    Hi Patrick.
                    Yes! Got ir.
                    In fact there were these alert:
                    27/01/2015 15.45.42 Default Failed to send mail: You must setup your email account settings via Control Center->Tools->Options->Misc.

                    But I am already sending email leaving the "default" profile in the Options.
                    Why now it is necessary to set an email account email ?

                    Comment


                      #11
                      Hello avalassina,

                      Thank you for your response.

                      I would ask you to go to Tools > Options > Misc > Test. If you receive any errors please try filling out the needed information as detailed in the following link: http://www.ninjatrader.com/support/f...008#post262008

                      Comment


                        #12
                        Originally posted by NinjaTrader_PatrickH View Post
                        Hello avalassina,

                        Thank you for your response.

                        I would ask you to go to Tools > Options > Misc > Test. If you receive any errors please try filling out the needed information as detailed in the following link: http://www.ninjatrader.com/support/f...008#post262008
                        Hi Patrick.

                        I had tried this also before but when I test it, I get always the same error related to authentication from Gmail, regarding an error code 5.5.1.
                        I have followed the hints provided so:
                        user = myuser (without @gmail.com)
                        paswd = my Gmail password
                        SSL yes
                        Port = 587

                        Of course, I have set my Gmail account in order to disable the high security level access mode.
                        Any idea ?

                        Best regards,

                        Comment


                          #13
                          Do you have POP enabled in the account options?

                          Comment


                            #14
                            Hi Patrick.
                            No, I did not.
                            Now I have enabled it but the result doesn't change: always authentication error.
                            I get some message in the Gmail Accounts Account alert telling me that they are blocking for security reason, access from a place (Oregon..) so distant from my habitual access points.

                            Foreach alert I declared that it was me that I was trying to access but it seems that they still continue to block the login from an external app.

                            I suppose, being Gmail utilized by millions of users, that in the Ninja Trader users world somebody else could had the same problem as mine.

                            But, anyway, Patrick, I repeat that till now I had always used email without configuring my Gmail account but using the NT default.
                            Is it something changed in your policy regarding this feature ?

                            Best regards,

                            Comment


                              #15
                              Originally posted by avalassina View Post
                              Hi Patrick.
                              No, I did not.
                              Now I have enabled it but the result doesn't change: always authentication error.
                              I get some message in the Gmail Accounts Account alert telling me that they are blocking for security reason, access from a place (Oregon..) so distant from my habitual access points.

                              Foreach alert I declared that it was me that I was trying to access but it seems that they still continue to block the login from an external app.

                              I suppose, being Gmail utilized by millions of users, that in the Ninja Trader users world somebody else could had the same problem as mine.

                              But, anyway, Patrick, I repeat that till now I had always used email without configuring my Gmail account but using the NT default.
                              Is it something changed in your policy regarding this feature ?

                              Best regards,
                              Hi Patrick.
                              Good news!
                              I have searched again in the Google Account support site and I have discovered this site:


                              After having checked the "I agree" button, now the send email Test works fine and I have tried it with a couple of different mail domain.

                              So, I hope that this setting will be maintained by Gmail also further so to allow to NT to send my trade execution signals.

                              For the moment, thanks a lot for the quick reply.

                              Best regards.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              26 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, 02-22-2024, 01:11 AM
                              5 responses
                              32 views
                              0 likes
                              Last Post wzgy0920  
                              Started by wzgy0920, Yesterday, 09:53 PM
                              2 responses
                              49 views
                              0 likes
                              Last Post wzgy0920  
                              Started by Kensonprib, 04-28-2021, 10:11 AM
                              5 responses
                              192 views
                              0 likes
                              Last Post Hasadafa  
                              Started by GussJ, 03-04-2020, 03:11 PM
                              11 responses
                              3,235 views
                              0 likes
                              Last Post xiinteractive  
                              Working...
                              X