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 gregid with 6 posts (3 thanks)
    3. looks_3 sam028 with 6 posts (10 thanks)
    4. looks_4 Big Mike with 1 posts (2 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,218 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

  #21 (permalink)
 
Big Mike's Avatar
 Big Mike 
Manta, Ecuador
Site Administrator
Developer
Swing Trader
 
Experience: Advanced
Platform: Custom solution
Broker: IBKR
Trading: Stocks & Futures
Frequency: Every few days
Duration: Weeks
Posts: 50,463 since Jun 2009
Thanks Given: 33,237
Thanks Received: 101,661


dsraider View Post
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

You need to check for null/existance before using lastTrade, you can do this with something like Performance.AllTrades.Count > 0.

Mike

We're here to help: just ask the community or contact our Help Desk

Quick Links: Change your Username or Register as a Vendor
Searching for trading reviews? Review this list
Lifetime Elite Membership: Sign-up for only $149 USD
Exclusive money saving offers from our Site Sponsors: Browse Offers
Report problems with the site: Using the NexusFi changelog thread
Follow me on Twitter Visit my NexusFi Trade Journal Reply With Quote
Thanked by:

Can you help answer these questions
from other members on NexusFi?
How to apply profiles
Traders Hideout
REcommedations for programming help
Sierra Chart
What broker to use for trading palladium futures
Commodities
MC PL editor upgrade
MultiCharts
Cheap historycal L1 data for stocks
Stocks and ETFs
 
  #22 (permalink)
dsraider
New York, NY
 
Posts: 142 since Dec 2009
Thanks Given: 41
Thanks Received: 87

Hi Mike,

That's what makes it even stranger because I have the following in place:

 
Code
                            
protected override void OnBarUpdate()


// Check to make sure there is at least one trade in the collection 
if (Performance.RealtimeTrades.Count 0
{     
    
// Get the last completed real-time trade (at index 0) 
    
Trade lastTrade Performance.AllTrades[Performance.AllTrades.Count 1];
                                
                                
    if (
lastTrade != null
    Print(
"The last trade profit is " lastTrade.ProfitPoints); 

To add to it, Print works perfectly as my output window currently reads "The last trade's profit is 2.5". Eventually, I want to add an "if' statement so if lastTrade >= 0, it will add a "+" to the string but I can't get it working at all.

Thanks for your reply,
Dave

Reply With Quote
  #23 (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, have you tried this:

 
Code
                            
Print("The last trade profit is " + (lastTrade.ProfitPoints >= "+" lastTrade.ProfitPoints lastTrade.ProfitPoints)) 


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

Hi Greg,

Thanks for that but it gave me the following error message:

Type of conditional expression cannot be determined because there is no implicit conversion between 'string' and 'double'



Dave

Reply With Quote
  #25 (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

I can't check it now but try something along these lines:

 
Code
                            
Print("The last trade profit is " + (lastTrade.ProfitPoints >= "+" Convert.ToString(lastTrade.ProfitPoints) : Convert.ToString(lastTrade.ProfitPoints))) 


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

Greg,

You're a genius! It's worked like a charm in Print. Thank you! Can I do that in SendMail as well? I just don't get why changing

 
Code
                            
SendMail("""[email protected]"Instrument.MasterInstrument.Name " Cross2050 Stop Filled " order.AvgFillPrice ": " lastTrade""); 

to

 
Code
                            
SendMail("""[email protected]"Instrument.MasterInstrument.Name " Cross2050 Stop Filled " order.AvgFillPrice ": " lastTrade.ProfitPoints""); 

not only doesn't work but prevents my strat from trading.

Thanks again!

Dave

Reply With Quote
  #27 (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

Check if you haven't missed any curly brackets in your code:

 
Code
                            
if (Performance.RealtimeTrades.Count 0)  

{      
    
// Get the last completed real-time trade (at index 0)  
    
Trade lastTrade Performance.AllTrades[Performance.AllTrades.Count 1]; 
                                 
                                 
    if (
lastTrade != null
    { 
        Print(
"The last trade profit is " + (lastTrade.ProfitPoints >= "+" Convert.ToString(lastTrade.ProfitPoints) : Convert.ToString(lastTrade.ProfitPoints)))  
        
SendMail("""[email protected]"Instrument.MasterInstrument.Name " Cross2050 Stop Filled " order.AvgFillPrice ": " lastTrade.ProfitPoints"");  
    }

If all brackets are in place try converting to string... just guessing here

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

Hi Greg,

I'm worried that I might have an issue with what's below as I have both my target and stop emailing me when filled so I will try using if(lastTrade != null) ---> SendMail() in both parts when I get home.

Thanks, Greg. I really appreciate your help with this.

Dave

P.S. As of now, my brackets are okay. I've checked. And checked again. And again

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

Well, I tried throwing:

 
Code
                            
SendMail("""[email protected]"Instrument.MasterInstrument.Name " Cross2050 Stop Filled " order.AvgFillPrice ": " lastTrade.ProfitPoints""); 

into

 
Code
                            
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.ProfitPoints"");
                    
                if (
order.OrderState == OrderState.Cancelled || order.OrderState == OrderState.Filled || order.OrderState == OrderState.Rejected)    
                    
// Remove from collection
                    
{
                    
stopLossTokens.Remove(order.Token);
                        
entryOrder1 null;
                        
entryOrder2 null;
                    }
                    
                }
                
// Print out the current stop loss price
                
else
                    Print(
"The order name " order.Name " stop price is currently " order.StopPrice);
            } 
but came up with the same issue. It just brings my strat to a hault. No trades are taken and the output window freezes. WEIRD!

Dave

Reply With Quote
  #30 (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


Dave,

You didn't check if lastTrade contains any value before using it. You should check it every time you ask for its value.

Greg

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