NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Saturday May 25th at 11AM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


Go Back   NinjaTrader Support Forum > Application Technical Support > Automated Trading

Automated Trading Support for automated trading systems using NinjaScript. Support for our ATI (Automated Trading Interface) used to link an external application such as TradeStation and eSignal to NinjaTrader.

Reply
 
Thread Tools Display Modes
Old 08-05-2011, 08:10 PM   #1
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default Profitable exits being ignored

I am using Enter and Exit orders to unwind positions that are profitable after 3 ticks or better. They are both being ignored. Can't figure out why.

protected override void OnBarUpdate()
{
// Condition set 1
if (Close[0] > Close[1])

{
EnterLongLimit(1, GetCurrentBid(), "Ofc Long");
}
// States that if position is profitable by 3 ticks at bid price, exit position at bid price
if (GetCurrentBid() >= Position.AvgPrice + 3 * Ticksize)
{
ExitLongLimit (GetCurrentBid(), "Profit Long");
}

// States that if bar following entry long closes against initial position, exit long and go flat.
if (Close[0] < Position.AvgPrice)
{
ExitLongLimit (GetCurrentBid(), "Ofc Long");
}

Thank You, Jamie Hendrix
JamieHendrix is offline  
Reply With Quote
Old 08-06-2011, 12:46 PM   #2
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Jamie, please set TraceOrders = true in the Initialize() portion of your strategy to see why these orders are being ignored. You can also add some Print() statements to send output to the debugging window (Tools -> Output Window) to verify these code blocks are actually executing:
Code:
if   (Close[0] > Close[1])
            
            {
Print("entering long limit at bar: " + Time[0].ToString());
                EnterLongLimit(1, GetCurrentBid(), "Ofc Long");
            }
                // States that if position is profitable by 3 ticks at bid price, exit position at bid price
                if (GetCurrentBid() >= Position.AvgPrice + 3 * Ticksize)
                {
Print("trying to exit long at bar: " + Time[0].ToString());
                    ExitLongLimit (GetCurrentBid(), "Profit Long");
                }
NinjaTrader_Austin is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Austin for this post:
Old 08-10-2011, 11:26 AM   #3
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

Austin, Thanks very much.... I feel like I am very close.... I have an ATM strategy that I am comfortable working with..... is there any way to call that strategy into use via the automated code? If not, where can I find out how to code the parameters from that strategy into my automated system.

Thanks for your help, Jamie Hendrix
JamieHendrix is offline  
Reply With Quote
Old 08-10-2011, 11:36 AM   #4
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

There is such a way. Please take a look at the sample provided by NinjaTrader at Tools -> Edit NinjaScript -> Strategy -> SampleAtmStrategy for additional information.
NinjaTrader_Austin is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Austin for this post:
Old 08-12-2011, 02:47 PM   #5
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

I loaded the ATM Strategy template and have tried many things... I even cut and paste most of the code right into my own..... it just does not want to cooperate.... I have an ATM template that I use in manual trading and I would like to incorporate it into my automated system..... all I am missing in my auto system is an auto breakeven which this would take care of.....

I added it to a basic entry signal to test it.... here is what I have.... What am I doing wrong?......

// Submits an entry limit order at the current low price to initiate an ATM Strategy if both order id and strategy id are in a reset state
// **** YOU MUST HAVE AN ATM STRATEGY TEMPLATE NAMED 'AtmStrategyTemplate' CREATED IN NINJATRADER (SUPERDOM FOR EXAMPLE) FOR THIS TO WORK ****
if ((Close[0] >= Close[1] + 3 * TickSize ) && BarsSinceExit() >=2 || BarsSinceExit() == -1)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Limit, GetCurrentBid(), 0 , TimeInForce.Day, orderId, "Ofc", atmStrategyId);
}

if ((Close[0] <= Close[1] - 3 * TickSize ) && BarsSinceExit() >=2 || BarsSinceExit() == -1)
{
atmStrategyId = GetAtmStrategyUniqueId();
orderId = GetAtmStrategyUniqueId();
AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Limit, GetCurrentAsk(), 0 , TimeInForce.Day, orderId, "Ofc", atmStrategyId);
}


// Check for a pending entry order
if (orderId.Length > 0)
{
string[] status = GetAtmStrategyEntryOrderStatus(orderId);

// If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
if (status.GetLength(0) > 0)
{
// Print out some information about the order to the output window
Print("The entry order average fill price is: " + status[0]);
Print("The entry order filled amount is: " + status[1]);
Print("The entry order order state is: " + status[2]);

// If the order state is terminal, reset the order id value
if (status[2] == "Filled" || status[2] == "Cancelled" || status[2] == "Rejected")
orderId = string.Empty;
}
} // If the strategy has terminated reset the strategy id
else if (atmStrategyId.Length > 0 && GetAtmStrategyMarketPosition(atmStrategyId) == Cbi.MarketPosition.Flat)
atmStrategyId = string.Empty;


if (atmStrategyId.Length > 0)
{
// You can change the stop price
//if (GetAtmStrategyMarketPosition(atmStrategyId) != MarketPosition.Flat)
AtmStrategyChangeStopTarget(0, Low[0] - 3 * TickSize, "STOP1", atmStrategyId);

// Print some information about the strategy to the output window
Print("The current ATM Strategy market position is: " + GetAtmStrategyMarketPosition(atmStrategyId));
Print("The current ATM Strategy position quantity is: " + GetAtmStrategyPositionQuantity(atmStrategyId));
Print("The current ATM Strategy average price is: " + GetAtmStrategyPositionAveragePrice(atmStrategyId)) ;
Print("The current ATM Strategy Unrealized PnL is: " + GetAtmStrategyUnrealizedProfitLoss(atmStrategyId)) ;
}
}


Thanks Austin, Jamie Hendrix
JamieHendrix is offline  
Reply With Quote
Old 08-12-2011, 02:52 PM   #6
NinjaTrader_Austin
NinjaTrader Customer Service
 
NinjaTrader_Austin's Avatar
 
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
Default

Hi Jamie, were you able to get the SampleAtmStrategy working by itself? That is a necessary first step before you start using your own strategies. Please keep in mind these strategies do not work on historical data (backtesting).
NinjaTrader_Austin is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Austin for this post:
Old 08-13-2011, 03:32 PM   #7
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

Austin. I loaded the Sample Atm Strategy by itself but nothing happened... no entries??? Not sure what to do next.

Jamie
JamieHendrix is offline  
Reply With Quote
Old 08-13-2011, 05:37 PM   #8
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

Austin, I got it figured out, just didn't realize about the historical thing.... just zoned out on that one I guess. Two other things I am trying to figure out..... Is there a way for me to turn on the strategy and have it just ignore the first bar of the day and just start on bar two... can't use "to time" cause I use Tick charts. Number two is that I would like it to stop trading after a certain cumulative profit has been reached for the day ( Like once it hits $100, stop trading or 2 points then stop)?

Thanks man, I am almost there. Jamie Hendrix
JamieHendrix is offline  
Reply With Quote
Old 08-14-2011, 03:04 PM   #9
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

OK Austin I have the first bar thing pretty much figured out... just need the cumulative profit trigger (still has me stumped..... I also want to create a price bracket of the first half hour of trading (630- 700) and have my system only recognize buy signals above this range and only recognize sell signals below the range....

For example. Low price of range (630 - 700) == 1280 High price of range (630- 700) == 1300
recognize long signal > 1300 and recognize Short signal < 1280.

THanks man. Jamie Hendrix
JamieHendrix is offline  
Reply With Quote
Old 08-14-2011, 05:44 PM   #10
NinjaTrader_RyanM
NinjaTrader Customer Service
 
NinjaTrader_RyanM's Avatar
 
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
Default

Hi JamieHendrix,

Quote:
Is there a way for me to turn on the strategy and have it just ignore the first bar of the day and just start on bar two.
Yes, you can work with BarsSinceSession or FirstBarOfSession to identify the first bar of a session.

Quote:
I also want to create a price bracket of the first half hour of trading (630- 700) and have my system only recognize buy signals above this range and only recognize sell signals below the range....
This sample can help with getting the highest and lowest values of a time range.
http://www.ninjatrader.com/support/f...4911#post44911
NinjaTrader_RyanM is offline  
Reply With Quote
Old 08-14-2011, 11:57 PM   #11
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by JamieHendrix View Post
Austin, I got it figured out, just didn't realize about the historical thing.... just zoned out on that one I guess. Two other things I am trying to figure out..... Is there a way for me to turn on the strategy and have it just ignore the first bar of the day and just start on bar two... can't use "to time" cause I use Tick charts. Number two is that I would like it to stop trading after a certain cumulative profit has been reached for the day ( Like once it hits $100, stop trading or 2 points then stop)?

Thanks man, I am almost there. Jamie Hendrix
Code:
if (Bars.FirstBarOfSession) return;
;

before the first line that processes strategy orders, should take care of that.
koganam is offline  
Reply With Quote
The following user says thank you to koganam for this post:
Old 08-17-2011, 12:20 PM   #12
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

Thanks for your reply man. Do you happen to know how to get indicators to start calculating starting from the first bar? Mine are continuing from the previous days close... totally throwing everything off for about an hour or so.
JamieHendrix is offline  
Reply With Quote
Old 08-18-2011, 06:48 AM   #13
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by JamieHendrix View Post
Thanks for your reply man. Do you happen to know how to get indicators to start calculating starting from the first bar? Mine are continuing from the previous days close... totally throwing everything off for about an hour or so.
Unfotunately, that will depend entirely on how the code is written, but basically, you record the barNumber for the FirstBarOfSession, then create an escape condition to not calculate in the dead zone from that bar to the number of bars that need to exist before calculations start.

Code:
 
private int DeadZoneStartBar = 0;
private int DeadZoneEndBar = 7; // or whatever you need
private barsRequiredToStartCalculations = 7; // or whatever you need
Code:
 
if (Bars.FirstBarOfSession)
{
   DeadZoneStartBar = CurrentBar;
   DeadZoneEndBar = CurrentBar + barsRequiredToStartCalculations;
}
 
if (CurrentBar >= DeadZoneStartBar && CurrentBar < DeadZoneEndBar) return;
 
// do the rest of stuff here.
koganam is offline  
Reply With Quote
The following user says thank you to koganam for this post:
Old 08-18-2011, 10:28 AM   #14
JamieHendrix
Member
 
Join Date: Dec 2009
Posts: 47
Thanks: 12
Thanked 0 times in 0 posts
Default

Thanks for your help... it worked as far as waiting to execute N bars before it trades but it has no effect on when the EMA is calculated.... the chart is taking the last 20 bars of yesterdays close and using that data to calculate in todays open.... this totally throws off at least the first hour of the day.... I can tell it to wait until the second hour to start, but I would rather not.

Again, thanks so much for your help.

Jamie Hendrix
JamieHendrix is offline  
Reply With Quote
Old 08-18-2011, 10:53 AM   #15
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,201
Thanks: 24
Thanked 1,227 times in 998 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by JamieHendrix View Post
Thanks for your help... it worked as far as waiting to execute N bars before it trades but it has no effect on when the EMA is calculated.... the chart is taking the last 20 bars of yesterdays close and using that data to calculate in todays open.... this totally throws off at least the first hour of the day.... I can tell it to wait until the second hour to start, but I would rather not.

Again, thanks so much for your help.

Jamie Hendrix
If you want the EMA to do the same, then you have 3 choices that I can think of:
  1. Write your own EMA code inside the strategy, and use this escape
  2. Create an instance of an EMA, and use this escape
  3. Write a custom EMA script that has the same restriction, and use a public parameter to pass the correct periods into this custom EMA.
Option 1 is self-contained, the other 2 are not.
koganam is offline  
Reply With Quote
The following user says thank you to koganam for this post:
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NinjaTrader’s 3rd Party Partner Program adds Profitable-Daytrading.com NinjaTrader_Ray News and Announcements 0 12-13-2010 02:31 PM
try to develop some loe risk profitable stratgey tah123 Strategy Development 1 09-21-2010 07:21 AM
Slippage and commission applied to Percent Profitable in Strategy Analyzer results? adamus Version 7 Beta General Questions & Bug Reports 3 08-09-2010 09:08 AM
Profitable aty close maninjapan General Programming 1 10-15-2009 01:26 PM
sell at the first profitable open - is it possible? pasmao General Programming 1 10-14-2008 08:35 AM


All times are GMT -6. The time now is 04:40 PM.