![]() |
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
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
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:
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? |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Jun 2009
Location: Denver, CO
Posts: 3,149
Thanks: 10
Thanked 89 times in 81 posts
|
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(...)
}
Austin
NinjaTrader Customer Service |
|
|
|
|
|
#3 | |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
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:
Would that do the same thing? Or will it allow additional entries on further incoming ticks on the same bar? |
|
|
|
|
|
|
#4 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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;
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
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?
|
|
|
|
|
|
#6 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
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;
////////////////////////////////////////////////////////////////////////////////
Last edited by MrTicks; 10-05-2010 at 06:59 AM.
Reason: Left out the changes I made
|
|
|
|
|
|
#7 | |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
Does COBC = True have to be set for the above sample to work or would it work with COBC = False? |
|
|
|
|
|
|
#8 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
That didn't work either, I just tried the following;
under variables; Code:
private bool tradeLong = false; private bool tradeShort = false; 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;
}
|
|
|
|
|
|
#9 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
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.
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#10 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
|
|
|
|
|
|
#11 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
|
|
|
|
|
|
#12 |
|
NinjaTrader Customer Service
Join Date: Sep 2009
Location: Denver, CO
Posts: 8,117
Thanks: 249
Thanked 418 times in 415 posts
|
Glad to hear it's working for you, MrTicks. Thanks for the update.
Ryan M
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#14 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
|
MrTicks,
This will not work when using ATM strategies in NinjaScript: Code:
Position.MarketPosition == MarketPosition.Flat
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#15 | |
|
Member
Join Date: Jan 2008
Location: Dublin, Ireland.
Posts: 71
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
|
|
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |