Enter on CalOnBarClose=True, but Exit Immediately on X Ticks - Traders Hideout | futures.io
futures.io futures trading
 

Go Back   futures.io

> Futures Trading, News, Charts and Platforms > Traders Hideout


Enter on CalOnBarClose=True, but Exit Immediately on X Ticks
Started:September 13th, 2016 (05:30 PM) by albertjay Views / Replies:709 / 22
Last Reply:October 18th, 2016 (09:16 PM) Attachments:0

Welcome to futures.io.

Welcome, Guest!

This forum was established to help traders (especially futures traders) by openly sharing indicators, strategies, methods, trading journals and discussing the psychology of trading.

We are fundamentally different than most other trading forums:
  • We work extremely hard to keep things positive on our forums.
  • We do not tolerate rude behavior, trolling, or vendor advertising in posts.
  • We firmly believe in openness and encourage sharing. The holy grail is within you, it is not something tangible you can download.
  • We expect our members to participate and become a part of the community. Help yourself by helping others.


You'll need to register in order to view the content of the threads and start contributing to our community. It's free and simple, and we will never resell your private information.

-- Big Mike
     

Reply
 
Thread Tools Search this Thread

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

Old September 13th, 2016, 05:30 PM   #1 (permalink)
Elite Member
Los Angeles CA
 
Futures Experience: Advanced
Platform: NT
Favorite Futures: Stocks
 
Posts: 8 since Aug 2015
Thanks: 0 given, 2 received

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

Reply With Quote
     

Old September 13th, 2016, 05:30 PM   #2 (permalink)
Quick Summary
Quick Summary Post

Quick Summary is created and edited by users like you... Add FAQ's, Links and other Relevant Information by clicking the edit button in the lower right hand corner of this message.

     

Old September 13th, 2016, 05:38 PM   #3 (permalink)
Market Wizard
London UK
 
Futures Experience: Beginner
Platform: CQG
Favorite Futures: Futures
 
xplorer's Avatar
 
Posts: 1,466 since Sep 2015
Thanks: 2,407 given, 1,825 received
Forum Reputation: Legendary



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
     

Old September 13th, 2016, 05:58 PM   #4 (permalink)
Elite Member
Los Angeles CA
 
Futures Experience: Advanced
Platform: NT
Favorite Futures: Stocks
 
Posts: 8 since Aug 2015
Thanks: 0 given, 2 received

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.

Reply With Quote
     

Old September 13th, 2016, 05:59 PM   #5 (permalink)
Market Wizard
Birmingham UK
 
Futures Experience: Intermediate
Platform: NinjaTrader
Broker/Data: IG/eSignal
Favorite Futures: Dax
 
ratfink's Avatar
 
Posts: 2,907 since Dec 2012
Thanks: 9,207 given, 5,648 received
Forum Reputation: Legendary

Futures Edge on FIO

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.

https://futures.io/ninjatrader/40409-access-tick-data-within-barsinprogress-minute-loop.html#post595490

Cheers

Travel Well
Reply With Quote
     
The following user says Thank You to ratfink for this post:
     

Old September 13th, 2016, 06:09 PM   #6 (permalink)
Elite Member
Los Angeles CA
 
Futures Experience: Advanced
Platform: NT
Favorite Futures: Stocks
 
Posts: 8 since Aug 2015
Thanks: 0 given, 2 received

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.

Reply With Quote
     
The following user says Thank You to albertjay for this post:
     

Old September 13th, 2016, 08:01 PM   #7 (permalink)
Elite Member
Crete, IL/USA
 
Futures Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker/Data: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Favorite Futures: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 171 since Aug 2009
Thanks: 99 given, 47 received


ratfink 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"
}


Last edited by spinnybobo; September 13th, 2016 at 08:08 PM.
Reply With Quote
     

Old September 13th, 2016, 11:36 PM   #8 (permalink)
Elite Member
Los Angeles CA
 
Futures Experience: Advanced
Platform: NT
Favorite Futures: Stocks
 
Posts: 8 since Aug 2015
Thanks: 0 given, 2 received

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"
}


Reply With Quote
     

Old September 14th, 2016, 01:05 PM   #9 (permalink)
Elite Member
Crete, IL/USA
 
Futures Experience: Intermediate
Platform: NinjaTrader, Mt4
Broker/Data: Tradestation/Tradestation, NinjaTrader, FXCM and Tallinex
Favorite Futures: ES, CL, EUR/USD, TF
 
spinnybobo's Avatar
 
Posts: 171 since Aug 2009
Thanks: 99 given, 47 received


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

Reply With Quote
     

Old September 14th, 2016, 01:26 PM   #10 (permalink)
skrubtrader
Boynton Beach
 
Futures Experience: Intermediate
Platform: NinjaTrader
Favorite Futures: ES,TF,CL
 
kburks's Avatar
 
Posts: 182 since Mar 2012
Thanks: 298 given, 129 received



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 futures.io mobile app

Reply With Quote
     

Reply



futures.io > Futures Trading, News, Charts and Platforms > Traders Hideout > Enter on CalOnBarClose=True, but Exit Immediately on X Ticks

Thread Tools Search this Thread
Search this Thread:

Advanced Search



Upcoming Webinars and Events (4:30PM ET unless noted)
 

NinjaTrader 8: Features and Enhancements, Tips and Tricks

Dec 6
 

Al Brooks: Stop Losing when a Good Trade goes Bad, Correcting Mistakes

Elite only
 

Trading Technologies: Algo Design Lab hands-on

Dec 13
     

Similar Threads
Thread Thread Starter Forum Replies Last Post
Enter and Exit market using different chart resolution vhanded MultiCharts 0 May 1st, 2015 06:37 AM
Enter and exit positions within the same bar mandelmus EasyLanguage Programming 2 March 12th, 2015 10:41 AM
How to enter long buy on a bar immediately before it closes. bmtfken EasyLanguage Programming 19 July 27th, 2014 08:01 PM
Best Price Enter/Exit? NJAMC Elite Automated NinjaTrader Trading 3 July 8th, 2011 06:00 PM
Viper has a "order entry strategy" that you can use to enter and exit. rcr3908 NinjaTrader 2 April 30th, 2011 06:20 PM


All times are GMT -4. The time now is 12:35 AM.

Copyright © 2016 by futures.io. 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.
 
no new posts

Page generated 2016-12-02 in 0.14 seconds with 37 queries on phoenix via your IP 54.146.141.60