NexusFi: Find Your Edge


Home Menu

 





Enter on CalOnBarClose=True, but Exit Immediately on X Ticks


Discussion in Traders Hideout

Updated
      Top Posters
    1. looks_one spinnybobo with 8 posts (1 thanks)
    2. looks_two albertjay with 5 posts (1 thanks)
    3. looks_3 xplorer with 5 posts (0 thanks)
    4. looks_4 maryfromcolorado with 2 posts (0 thanks)
    1. trending_up 9,665 views
    2. thumb_up 3 thanks given
    3. group 4 followers
    1. forum 22 posts
    2. attach_file 0 attachments




 
Search this Thread

Enter on CalOnBarClose=True, but Exit Immediately on X Ticks

  #1 (permalink)
 albertjay 
Los Angeles CA
 
Experience: Advanced
Platform: NT
Trading: ES
Posts: 12 since Aug 2015
Thanks Given: 3
Thanks Received: 5

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

Started this thread Reply With Quote

Can you help answer these questions
from other members on NexusFi?
Better Renko Gaps
The Elite Circle
NexusFi Journal Challenge - April 2024
Feedback and Announcements
New Micros: Ultra 10-Year & Ultra T-Bond -- Live Now
Treasury Notes and Bonds
Are there any eval firms that allow you to sink to your …
Traders Hideout
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Get funded firms 2023/2024 - Any recommendations or word …
61 thanks
Funded Trader platforms
38 thanks
NexusFi site changelog and issues/problem reporting
27 thanks
GFIs1 1 DAX trade per day journal
19 thanks
The Program
18 thanks
  #3 (permalink)
 
xplorer's Avatar
 xplorer 
London UK
Site Moderator
 
Experience: Beginner
Platform: CQG
Broker: S5
Trading: Futures
Posts: 5,944 since Sep 2015
Thanks Given: 15,447
Thanks Received: 15,291



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

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.

HTH

Reply With Quote
  #4 (permalink)
 albertjay 
Los Angeles CA
 
Experience: Advanced
Platform: NT
Trading: ES
Posts: 12 since Aug 2015
Thanks Given: 3
Thanks Received: 5

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.

Started this thread Reply With Quote
  #5 (permalink)
 
ratfink's Avatar
 ratfink 
Birmingham UK
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader
Broker: TST/Rithmic
Trading: YM/Gold
Posts: 3,633 since Dec 2012
Thanks Given: 17,423
Thanks Received: 8,425


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

@spinnybobo is doing something similar, if you're also ok with just live.



Cheers

Travel Well
Visit my NexusFi Trade Journal Reply With Quote
Thanked by:
  #6 (permalink)
 albertjay 
Los Angeles CA
 
Experience: Advanced
Platform: NT
Trading: ES
Posts: 12 since Aug 2015
Thanks Given: 3
Thanks Received: 5

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.

Started this thread Reply With Quote
Thanked by:
  #7 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Trading: ES, CL, EUR/USD, TF
Posts: 173 since Aug 2009
Thanks Given: 105
Thanks Received: 61


ratfink View Post
@spinnybobo is doing something similar, if you're also ok with just live.



Cheers

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

 
Code
protected override void Initialize()
{
     CalculateOnBarClose = true;
     Add(PeriodType.Tick, 1);  
     // adding 1 tick data to the chart which will make it load slowly so only add no more than 5 days
}
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.

 
Code
private IOrder entryOrder  = null;
private IOrder target1Order = null;
private IOrder stopOrder = null;

protected override void Initialize()
{
     Add(PeriodType.Tick, 1);  // BarsInProgress == 0 for base chart and 1 for ticks
     Unmanaged = true; // I always use unmanaged strategies because I prefer to cancel my stops and targets myself explicitly
}
protected override void OnBarUpdate()
{
     if (CurrentBars[0] > 3)
    {
        if (BarsInProgress == 0)
        {
            // base chart logic.  So if your using 15 min chart this happens every 15 minutes
        }
        if (BarsInProgress == 1)
       {
           // tick chart logic.  This chart you can't see but is synced with base chart and added in background
           
       }

    }
}
protected override void OnOrderUpdate(IOrder order)
{
    // handle setting stops and targets once the order is "order.Filled"
}

Follow me on Twitter Reply With Quote
  #8 (permalink)
 albertjay 
Los Angeles CA
 
Experience: Advanced
Platform: NT
Trading: ES
Posts: 12 since Aug 2015
Thanks Given: 3
Thanks Received: 5

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.


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

 
Code
protected override void Initialize()
{
     CalculateOnBarClose = true;
     Add(PeriodType.Tick, 1);  
     // adding 1 tick data to the chart which will make it load slowly so only add no more than 5 days
}
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.

 
Code
private IOrder entryOrder  = null;
private IOrder target1Order = null;
private IOrder stopOrder = null;

protected override void Initialize()
{
     Add(PeriodType.Tick, 1);  // BarsInProgress == 0 for base chart and 1 for ticks
     Unmanaged = true; // I always use unmanaged strategies because I prefer to cancel my stops and targets myself explicitly
}
protected override void OnBarUpdate()
{
     if (CurrentBars[0] > 3)
    {
        if (BarsInProgress == 0)
        {
            // base chart logic.  So if your using 15 min chart this happens every 15 minutes
        }
        if (BarsInProgress == 1)
       {
           // tick chart logic.  This chart you can't see but is synced with base chart and added in background
           
       }

    }
}
protected override void OnOrderUpdate(IOrder order)
{
    // handle setting stops and targets once the order is "order.Filled"
}


Started this thread Reply With Quote
  #9 (permalink)
 
spinnybobo's Avatar
 spinnybobo 
Crete, IL/USA
 
Experience: Intermediate
Platform: NinjaTrader, Mt4
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 View Post
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()

 
Code
// 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

Follow me on Twitter Reply With Quote
  #10 (permalink)
 
kburks's Avatar
 kburks 
Boynton Beach
 
Experience: Intermediate
Platform: NinjaTrader
Trading: ES,TF,CL
Posts: 203 since Mar 2012
Thanks Given: 327
Thanks Received: 146



xplorer View Post
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.

HTH

What platform do you use??

Sent from my SM-G920V using NexusFi mobile app

Reply With Quote




Last Updated on October 18, 2016


© 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