Welcome to NexusFi: the best trading community on the planet, with over 150,000 members Sign Up Now for Free
Genuine reviews from real traders, not fake reviews from stealth vendors
Quality education from leading professional traders
We are a friendly, helpful, and positive community
We do not tolerate rude behavior, trolling, or vendors advertising in posts
We are here to help, just let us know what you need
You'll need to register in order to view the content of the threads and start contributing to our community. It's free for basic access, or support us by becoming an Elite Member -- see if you qualify for a discount below.
-- Big Mike, Site Administrator
(If you already have an account, login at the top of the page)
Enter on CalOnBarClose=True, but Exit Immediately on X Ticks
I have a bunch of entry strategies that all rely on CalcOnBarClose=True. However, once in a while price goes in your direction really quickly and you want to exit to secure gains immediately, before the bar closes. For example, if price has 12 ticks gain on entry bar, then exit before close. Anyone have a good way of doing this without changing CalcOnBarClose to false?
Thanks in advance!
-Al
Can you help answer these questions from other members on NexusFi?
I don't have enought NT experience to give you the direct answer, but with the platform I use I rely on the function OTE (Open Trade Equity), and I set it to liquidate my trade if the profit is greater than a preset value.
Thanks, but that is not going to work for me. I don't want to set a hard profit target, b/c that will just sit on my broker's servers. I'd rather write a specific rule that if on the entry bar price moves in my direction by X ticks, then immediately exit before that bar closes and gives up the gains.
I knew I needed some type of an event similar to OnOrderUpdate and OnExecution, but since this is not a strategy I didn't know what other events I could use.
That seems promising... thanks. Although, as you alluded it's hard to go through hundreds of trades and determine which would of hit my onmarketdata rules in a backtest.
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61
Hey
so if I understand you correctly you have a strategy and you are in a position and want to exit BEFORE the bar closes.
So is your logic when it goes in your favor by 12 ticks? Are you using either stops or targets or both?
Tell me the logic of your strategy.
If you want to backtest it Historically you need to layer in 1 tick data like this
Otherwise you can use OnMarketData which is not historical just real time ticks
You would want to use IOrders
if you are using stops and targets, you can set Target1 to 12 ticks and it will execute intrabar lots of options depending on your logic.
Thanks - I have both a stop and a PT resting on my broker's servers. However, in one of my long "BTD" strategies, if the price climbs up at least 12 ticks on my entry bar only, I'd like to exit my position prior to my entry bar closing.
I'm aware I can set to false CalcOnBarClose, but I'd like to keep that as true. I could set multiple profit targets, but that comes at the cost of not letting certain winners run. The issue I'm trying to solve is the occurrence where the price goes strongly(ie at least 12 ticks) in your favor on your entry bar, and you want to lock in those gains before the inevitable drop.
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61
albertjay
Thanks - I have both a stop and a PT resting on my broker's servers. However, in one of my long "BTD" strategies, if the price climbs up at least 12 ticks on my entry bar only, I'd like to exit my position prior to my entry bar closing.
I'm aware I can set to false CalcOnBarClose, but I'd like to keep that as true. I could set multiple profit targets, but that comes at the cost of not letting certain winners run. The issue I'm trying to solve is the occurrence where the price goes strongly(ie at least 12 ticks) in your favor on your entry bar, and you want to lock in those gains before the inevitable drop.
try this code out
it does not work historically. Historically code would need Add(PeriodType.Tick, 1) added to Initialize()
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
[Description("used for testing things")]
public class sdPlayground : Strategy
{
private IOrder entryOrder = null;
private IOrder exitOrder = null;
private int currentBarOrderFilled = 0;
protected override void Initialize()
{
CalculateOnBarClose = true;
Unmanaged = true;
}
protected override void OnBarUpdate()
{
if (!Historical)
{
if (entryOrder == null && Flat)
{
entryOrder = SubmitOrder(0, OrderAction.SellShort, OrderType.Market, 1, 0, 0, "", "Entry");
Print("hitting this");
}
if (entryOrder != null)
Print(entryOrder.ToString());
if (Long)
Print("we are long: Positin.Quantity: " + Position.Quantity + " AvgPrice " + Position.AvgPrice);
}
}
protected override void OnMarketData(MarketDataEventArgs e)
{
if (e.MarketDataType == MarketDataType.Last)
{
double multiplier = 1;
OrderAction orderAction = OrderAction.Sell;
if (Short)
{
multiplier = -1;
orderAction = OrderAction.BuyToCover;
}
double threshold = Instrument.MasterInstrument.Round2TickSize(multiplier*(e.Price - Position.AvgPrice))/TickSize;
Print("threshold: " + threshold + " Position.AvgPrice: " + Position.AvgPrice + " e.Price: " + e.Price);
if (Long || Short)
Print("filled on bar : " + currentBarOrderFilled + " CurrentBar: " + CurrentBar);
if ((Long || Short)&& currentBarOrderFilled == CurrentBar)
{
if (threshold >= 2)
exitOrder = SubmitOrder(0, orderAction, OrderType.Market, 1, 0, 0, "", "Exit 12 Ticks");
}
}
}
protected override void OnOrderUpdate(IOrder order)
{
if (entryOrder != null && entryOrder == order)
{
Print(entryOrder.ToString());
if (order.OrderState == OrderState.Filled)
{
currentBarOrderFilled = CurrentBar;
Print("Filled on bar number: " + currentBarOrderFilled);
entryOrder = null;
}
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
entryOrder = null;
}
if (exitOrder != null && exitOrder == order)
{
if (order.OrderState == OrderState.Filled)
exitOrder = null;
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
exitOrder = null;
}
}
private bool Long { get { return (Position.MarketPosition == MarketPosition.Long);}}
private bool Short { get { return (Position.MarketPosition == MarketPosition.Short);}}
private bool Flat { get { return (Position.MarketPosition == MarketPosition.Flat);}}
#region Properties
#endregion
}
}
what I put in OnBarUpdate() was just to get an order
you would want to deal with OnMarketData and if you use IOrders, then OnOrderUpdate to set the CurrentBar to the bar you got filled on. I also included "helper" methods for Long, Short as well as Flat
I don't have enought NT experience to give you the direct answer, but with the platform I use I rely on the function OTE (Open Trade Equity), and I set it to liquidate my trade if the profit is greater than a preset value.