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 > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 10-02-2010, 11:39 AM   #1
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default Prevent multiple entries

Is it possible to prevent multiple orders on the same bar? I have a ninjascript that generates the same order repeatedly from time to time, not under every signal condition is met though.

I have sorted out the issue on other strats by adding FirstTickofBar but on a LineBreak Chart it doesn't seem to help.

Here is my entry signal and ATM call code;
PHP Code:
// Long entry signal if cross above daily PP
                        
if (Position.MarketPosition == MarketPosition.Flat
                            
&& orderIdL.Length == 0  
                            
&& atmStrategyIdL.Length == 0
                            
&& FirstTickOfBar
                            
//&& Close[0] >= Open[0]
                            //&& Close[1] > GetCurrentAsk()
                            
&& BarsSinceEntry() > 1
                            
&& CrossAbove(ClosePivots(PivotRange.DailyHLCCalculationMode.CalcFromIntradayData00020).PP1)
                            || 
CrossAbove(ClosePivots(PivotRange.DailyHLCCalculationMode.CalcFromIntradayData00020).R11)
                      
                            
                            
//&& Close[1] + prevBarsPlusTicks*TickSize > GetCurrentAsk()
                            //&& Close[1]  < GetCurrentBid()
                            
)
                            
                               
                        {
                                
atmStrategyIdL GetAtmStrategyUniqueId();
                                
orderIdL GetAtmStrategyUniqueId();
                                
orderBarL CurrentBar;
                                
AtmStrategyCreate(Cbi.OrderAction.BuyOrderType.Market0High[1], TimeInForce.DayorderIdL"TF_2_Ren_DPP_Long"atmStrategyIdL);    
                
                        } 
My questions are:
1. Is it possible to implement && BarsSinceEntry() > 1 in an ATM strat somehow? If so, where do you place it?
2. Is there a master instrument reference I could check so if a contract in that class is open then no further signals will be taken? I have Position.MarketPosition == MarketPosition.Flat in the entry signal section to prevent multiple orders but as the order is passed to the ATM function the strat is under the impression that there are no open orders. Do I need to start using IOorders or arrays?
MrTicks is offline  
Reply With Quote
Old 10-02-2010, 12:51 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

MrTicks, you can track the entries by storing the bar of the entry, and then ignore the second entry if it is on the same bar.

Code:
if (entryConditions == true && entryBar != CurrentBar)
{
   entryBar = CurrentBar
   EnterLong(...)
}
Unfortunately there are no master instrument references available. A strategy only knows about positions opened by itself.
NinjaTrader_Austin is offline  
Reply With Quote
Old 10-04-2010, 06:59 AM   #3
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Austin View Post
MrTicks, you can track the entries by storing the bar of the entry, and then ignore the second entry if it is on the same bar.

Code:
if (entryConditions == true && entryBar != CurrentBar)
{
   entryBar = CurrentBar
   EnterLong(...)
}
Unfortunately there are no master instrument references available. A strategy only knows about positions opened by itself.
Hi Austin,

That wouldn't compile for me and gave the following error,
The name 'entryBar' does not exist in the current context

so I'm trying && BarsSinceEntry() > 1

which looks like this when coded,

PHP Code:
// Long entry signal with ATM attached.
                        
if (orderIdL.Length == 0  
                                
&& atmStrategyIdL.Length == 0
                                
&& Position.MarketPosition == MarketPosition.Flat
                                
&& Close [1] <= Open [1]
                                && 
Open[1] - PrevBarsMinusTicks*TickSize GetCurrentBid() 
                                && 
BarsSinceEntry() > 1
                                
//&& entryBar != CurrentBar
                            
)
                        
                        {
                                
//entryBar = CurrentBar;
                                
atmStrategyIdL GetAtmStrategyUniqueId();
                                
orderIdL GetAtmStrategyUniqueId();
                                
orderBarL CurrentBar;
                                
AtmStrategyCreate(Cbi.OrderAction.SellOrderType.Stop0Close[1], TimeInForce.DayorderIdL"FDAX_1_LB_short"atmStrategyIdL);    
                
                        } 

Would that do the same thing? Or will it allow additional entries on further incoming ticks on the same bar?
MrTicks is offline  
Reply With Quote
Old 10-04-2010, 09:31 AM   #4
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

Hello MrTicks,

BarsSinceEntry() wouldn't apply to ATM generated orders. You would have to manually track this value. The compile error you receive should be fixed by adding a variable declaration, in variables region of code.

private int entryBar;
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-05-2010, 04:48 AM   #5
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_RyanM View Post
Hello MrTicks,

BarsSinceEntry() wouldn't apply to ATM generated orders. You would have to manually track this value. The compile error you receive should be fixed by adding a variable declaration, in variables region of code.

private int entryBar;
Thanks, it compiled OK but I still get multiple entries on a line break chart. Would you know if there is another method I could test?
MrTicks is offline  
Reply With Quote
Old 10-05-2010, 06:53 AM   #6
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Just saw this post on the forum and am testing it now, http://www.ninjatrader.com/support/f...t=32875&page=2

This is what I'm currently testing;
private bool OkToTrade = true;
&& OkToTrade == true

AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);
OkToTrade = false;


Here is my latest code;

Code:
    public class TF_Pivot_ATM_cancel : Strategy
    {
        #region Variables
                private string  atmStrategyIdL          = string.Empty;
                private string  atmStrategyIdS          = string.Empty;
               
                private int prevBarsPlusTicks = 1; // Default setting for PrevBarsPlusTicks
                private int prevBarsMinusTicks = 1; // Default setting for PrevBarsPlusTicks
               
                private string  orderIdL                                = string.Empty;
                private string  orderIdS                                = string.Empty;
               
                private int orderBarL                           = 0;
                private int orderBarS                           = 0;
                private int entryBarL                           = 0;
                private int entryBarS                           = 0;
                private int cancelBars                          = 1;
                private int entryBar; 
                private bool OkToTrade                     = true;
                #endregion

        
        protected override void Initialize()
        {
            EntriesPerDirection = 1;
            EntryHandling = EntryHandling.AllEntries; 
            CalculateOnBarClose = false;
            TraceOrders = true;
            TimeInForce = Cbi.TimeInForce.Day;
        }
               
       
        protected override void OnBarUpdate()
        {
                        // Make sure this strategy does not execute against historical data
                        if (Historical)
                                return;

                        // 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
                        
                        // Long entry signal if cross above daily PP
                        if (Position.MarketPosition == MarketPosition.Flat
                            && orderIdL.Length == 0  
                            && atmStrategyIdL.Length == 0
                            && FirstTickOfBar
                            //&& Close[0] >= Open[0]
                            //&& Close[1] > GetCurrentAsk()
                            && entryBar != CurrentBar
                            && OkToTrade == true
                            //&& BarsSinceEntry() > 1
                            && CrossAbove(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).PP, 1)
                            || CrossAbove(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1, 1)
                            //&& Close[1] + prevBarsPlusTicks*TickSize > GetCurrentAsk()
                            //&& Close[1]  < GetCurrentBid()
                            )
                            
                               
                        {
                                atmStrategyIdL = GetAtmStrategyUniqueId();
                                orderIdL = GetAtmStrategyUniqueId();
                                orderBarL = CurrentBar;
                                entryBar = CurrentBar;

                                AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);    
                                OkToTrade = false; 
                        }
                        
                        
            
                        
                        // Short entry signal cross below PP
                        if (Position.MarketPosition == MarketPosition.Flat
                            && orderIdS.Length == 0  
                            && atmStrategyIdS.Length == 0  
                            && FirstTickOfBar
                            && entryBar != CurrentBar
                            && OkToTrade == true
                            //&& BarsSinceEntry() > 1
                            //&& Close[1] <= Open[1]
                            && CrossBelow(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).PP, 1)
                            || CrossBelow(Close, Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).R1, 1)
                            //&& Close[0] > High[1]
                            //&& Close[1]  > GetCurrentAsk()  
                             )
                                
                            // && Low   [1] - PrevBarsMinusTicks * TickSize < GetCurrentAsk())
                            
                               
                        {
                                atmStrategyIdS = GetAtmStrategyUniqueId();
                                orderIdS = GetAtmStrategyUniqueId();
                                orderBarS = CurrentBar;
                                entryBar = CurrentBar;
                                AtmStrategyCreate(Cbi.OrderAction.Sell, OrderType.Market, 0, Low[1], TimeInForce.Day, orderIdS, "TF_2_Ren_DPP_Short", atmStrategyIdS);
                                 OkToTrade = false; 
                        }
                        
                        
                       

                        // Check for a pending entry order
                        if (orderIdL.Length > 0)
                        {
                            string[] statusL = GetAtmStrategyEntryOrderStatus(orderIdL);
                             
                            // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                            if (statusL.GetLength(0) > 0)
                                
                                //Cancel long order after n bars
                                if ((CurrentBar - orderBarL) >= cancelBars)
                                    
                                
                                {
                                        AtmStrategyCancelEntryOrder(orderIdL);
                                       
                                        orderIdL = string.Empty;
                                }
                                
                                {
                                        // Print out some information about the order to the output window
                                        Print("The entry order average fill price is:\t" + statusL[0]);
                                      

                                        // If the order state is terminal, reset the order id value
                                        if (statusL[2] == "Filled" || statusL[2] == "Cancelled" || statusL[2] == "Rejected")
                                                orderIdL = string.Empty;
                                }
                        } // If the strategy has terminated reset the strategy id
                        else if (atmStrategyIdL.Length > 0 &&
                                GetAtmStrategyMarketPosition(atmStrategyIdL) == Cbi.MarketPosition.Flat)
                                atmStrategyIdL = string.Empty;
////////////////////////////////////////////////////////////////////////////////
                        // Check for a pending entry order
                        if (orderIdS.Length > 0)
                            
                        {
                            string[] statusS = GetAtmStrategyEntryOrderStatus(orderIdS);
                            
                            // If the status call can't find the order specified, the return array length will be zero otherwise it will hold elements
                                if (statusS.GetLength(0) > 0)
                                    
                                //Cancel short order after n bars
                                if ((CurrentBar - orderBarS) >= cancelBars)
                                
                                {
                                        AtmStrategyCancelEntryOrder(orderIdS);
                                       
                                        orderIdS = string.Empty;
                                }
                                
                                {
                                        // Print out some information about the order to the output window
                                        Print("The entry order average fill price is:\t" + statusS[0]);
                                       

                                        // If the order state is terminal, reset the order id value
                                        if (statusS[2] == "Filled" || statusS[2] == "Cancelled" || statusS[2] == "Rejected")
                                                orderIdS = string.Empty;
                                }
                        } // If the strategy has terminated reset the strategy id
                        else if (atmStrategyIdS.Length > 0 &&
                                GetAtmStrategyMarketPosition(atmStrategyIdS) == Cbi.MarketPosition.Flat)
                                atmStrategyIdS = string.Empty;
////////////////////////////////////////////////////////////////////////////////
Will let you know if it works.
Last edited by MrTicks; 10-05-2010 at 06:59 AM. Reason: Left out the changes I made
MrTicks is offline  
Reply With Quote
Old 10-05-2010, 12:27 PM   #7
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Austin View Post
MrTicks, you can track the entries by storing the bar of the entry, and then ignore the second entry if it is on the same bar.

Code:
if (entryConditions == true && entryBar != CurrentBar)
{
   entryBar = CurrentBar
   EnterLong(...)
}
Unfortunately there are no master instrument references available. A strategy only knows about positions opened by itself.
Hi Austin,

Does COBC = True have to be set for the above sample to work or would it work with COBC = False?
MrTicks is offline  
Reply With Quote
Old 10-05-2010, 02:13 PM   #8
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

That didn't work either, I just tried the following;

under variables;
Code:
private bool tradeLong                            = false;
private bool tradeShort                            = false;
under protected override void OnBarUpdate()
Code:
&& tradeLong == false
Code:
 {
    atmStrategyIdL = GetAtmStrategyUniqueId();
    orderIdL = GetAtmStrategyUniqueId();
    orderBarL = CurrentBar;
    entryBar = CurrentBar;

    AtmStrategyCreate(Cbi.OrderAction.Buy, OrderType.Market, 0, High[1], TimeInForce.Day, orderIdL, "TF_2_Ren_DPP_Long", atmStrategyIdL);    
    //OkToTrade = false; 
    tradeLong = true;
  }
It still repeats multiple entries though. Is there any chance you could run the attached strat and see why it happens? I was testing this on a Renko chart set at 2 bricks.
Attached Files
File Type: zip MultipleEntries.zip (16.1 KB, 6 views)
MrTicks is offline  
Reply With Quote
Old 10-05-2010, 02:21 PM   #9
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

Hello Mr Ticks,

This suggestion works best with COBC = true. If set to false, the condition can be evaluated multiple times on the same bar and could return true multiple times.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-05-2010, 02:56 PM   #10
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_RyanM View Post
Hello Mr Ticks,

This suggestion works best with COBC = true. If set to false, the condition can be evaluated multiple times on the same bar and could return true multiple times.
Thanks, will test with COBC = true.
MrTicks is offline  
Reply With Quote
Old 10-05-2010, 03:26 PM   #11
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_RyanM View Post
Hello Mr Ticks,

This suggestion works best with COBC = true. If set to false, the condition can be evaluated multiple times on the same bar and could return true multiple times.
Works a treat with COBC = true.

Thank you very much!!!!!!!!!!
MrTicks is offline  
Reply With Quote
Old 10-05-2010, 03:29 PM   #12
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

Glad to hear it's working for you, MrTicks. Thanks for the update.
NinjaTrader_RyanM is offline  
Reply With Quote
Old 10-06-2010, 08:19 AM   #13
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

I spoke to soon! It all looked OK today until it was doing repeat orders every now and again, albeit on a much smaller scale. Is there any chance you could run that strat I attached in a previous post and see if you can tell why? I have COBC = true on a 2 brick renko chart.
MrTicks is offline  
Reply With Quote
Old 10-06-2010, 08:35 AM   #14
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

MrTicks,

This will not work when using ATM strategies in NinjaScript:
Code:
Position.MarketPosition == MarketPosition.Flat
That is for NinjaScript positions and not ATM positions. To check ATM's position you need to use this instead: http://www.ninjatrader-support.com/H...yPosition.html
NinjaTrader_Josh is offline  
Reply With Quote
Old 10-06-2010, 08:43 AM   #15
MrTicks
Member
 
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
MrTicks,

This will not work when using ATM strategies in NinjaScript:
Code:
Position.MarketPosition == MarketPosition.Flat
That is for NinjaScript positions and not ATM positions. To check ATM's position you need to use this instead: http://www.ninjatrader-support.com/H...yPosition.html
Thanks for the prompt response Josh! Will edit that code.
MrTicks is offline  
Reply With Quote
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
prevent multiple alerts for same bar rakeshSharma Indicator Development 2 04-23-2009 07:04 PM
Multiple entries starrynight Strategy Development 3 01-22-2009 07:25 AM
Chart Trader: Condition based entries, time-axis entries, multiple OCO pairs etc. Elliott Wave Suggestions And Feedback 1 11-25-2008 03:23 AM
multiple entries jrtrader General Programming 1 11-04-2008 12:19 PM
Multiple entries SystemTrading Strategy Development 3 08-29-2008 08:18 AM


All times are GMT -6. The time now is 05:32 PM.