NexusFi: Find Your Edge


Home Menu

 





Code for range break out strategy


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one NJAMC with 6 posts (3 thanks)
    2. looks_two fctrader with 5 posts (7 thanks)
    3. looks_3 ticker with 3 posts (1 thanks)
    4. looks_4 baruchs with 2 posts (2 thanks)
      Best Posters
    1. looks_one traderwerks with 2 thanks per post
    2. looks_two fctrader with 1.4 thanks per post
    3. looks_3 baruchs with 1 thanks per post
    4. looks_4 NJAMC with 0.5 thanks per post
    1. trending_up 15,660 views
    2. thumb_up 16 thanks given
    3. group 11 followers
    1. forum 22 posts
    2. attach_file 0 attachments




 
Search this Thread

Code for range break out strategy

  #11 (permalink)
 fctrader 
Fort Collins, Colorado
 
Experience: Intermediate
Platform: NT7
Trading: ES
Posts: 57 since Feb 2011
Thanks Given: 48
Thanks Received: 45

@NJMAC
Thanks for that. I was about to ask you why you felt the need to do it the way you were suggesting when your post came up! I like the futures.io (formerly BMT) feature of adding the newest while I'm reading....

I didn't perceive the original poster to be asking about server-side orders, but of course, a OCO option is better when the order is placed. Are you suggesting placing the code
 
Code
SubmitOrder(0,OrderAction.Buy,OrderType.Stop, 1, 0, Close[0] + 10 * TickSize, "EntryStopOCO", "BuyStopEntry" );
into the strategy in place of the standard order
 
Code
EnterLong(DefaultQuantity, "");
and in lieu of
 
Code
SetProfitTarget("", CalculationMode.Ticks, 4);
            SetStopLoss("", CalculationMode.Ticks, 4, true);
?

I have not played much with server side orders, so I am interested if this can be slapped into any strategy to enter a OCO.

I should say... I haven't run an NT Strategy live connected to the Broker, and have only used them to backtest and then alert me to place a trade live. From what I think you are saying, the method I used would not place a live stop when the order is sent, but would keep the stop on my machine and enter it only on triggering. Is that correct?

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to fctrader for this post:

Can you help answer these questions
from other members on NexusFi?
NexusFi Journal Challenge - May 2024
Feedback and Announcements
Request for MACD with option to use different MAs for fa …
NinjaTrader
ZombieSqueeze
Platforms and Indicators
 
Best Threads (Most Thanked)
in the last 7 days on NexusFi
Retail Trading As An Industry
63 thanks
NexusFi site changelog and issues/problem reporting
46 thanks
Battlestations: Show us your trading desks!
34 thanks
GFIs1 1 DAX trade per day journal
32 thanks
What percentage per day is possible? [Poll]
24 thanks

  #12 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,394


fctrader View Post
@NJMAC
Thanks for that. I was about to ask you why you felt the need to do it the way you were suggesting when your post came up! I like the futures.io (formerly BMT) feature of adding the newest while I'm reading....

I didn't perceive the original poster to be asking about server-side orders, but of course, a OCO option is better when the order is placed. Are you suggesting placing the code
 
Code
SubmitOrder(0,OrderAction.Buy,OrderType.Stop, 1, 0, Close[0] + 10 * TickSize, "EntryStopOCO", "BuyStopEntry" );
into the strategy in place of the standard order
 
Code
EnterLong(DefaultQuantity, "");
and in lieu of
 
Code
SetProfitTarget("", CalculationMode.Ticks, 4);
            SetStopLoss("", CalculationMode.Ticks, 4, true);
?

I have not played much with server side orders, so I am interested if this can be slapped into any strategy to enter a OCO.

I guess in a generic coding format, you would do the following for each Bar/Tick depending upon your desires in your OnBarUpdate():
{
//Calculate BuyPrice & SellPrice for current bar/tick
BuyPrice = your formula for top side Stop
SellPrice = your formula for bottom side Stop

// SetPT & SL using NT functions (Do this before placing the orders belo, if fixed values you can set in your Init() fuction)
...


// Place orders on market (depending upon your Broker, NT might manage locally like your code above)
SubmitOrder(0,OrderAction.Buy,OrderType.Stop, 1, 0, BuyPrice, "EntryStopOCO", "BuyStopEntry" );
SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, SellPrice, "EntryStopOCO", "SellStopEntry");

}

Hope that helps. With each Bar or Tick you would update your OCO to reflect what you would consider a breakout (becareful on the Tick as you generate LOTS of order to your broker and market, an statistically you will move your order out before you have a chance to trigger... Morale, be sure to set COBC=True using this approach or know what you are doing).

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to NJAMC for this post:
  #13 (permalink)
 fctrader 
Fort Collins, Colorado
 
Experience: Intermediate
Platform: NT7
Trading: ES
Posts: 57 since Feb 2011
Thanks Given: 48
Thanks Received: 45


So, to make sure I'm understanding in regards to the original poster; if he wants to enter a server side order at 9:30 based on his 9:00-9:30 range, he could enter your code into an if statement as follows, (assuming he has specified the stops, as you mentioned)

 
Code
if (ToTime(Time[0]) > ToTime(9, 30, 0) && TradePlaced <1)
			{
// Place orders on market (depending upon your Broker, NT might manage locally like your code above)
                                SubmitOrder(0,OrderAction.Buy,OrderType.Stop, 1, 0, CurrentOpenRangeHigh, "EntryStopOCO", "BuyStopEntry" );
                                SubmitOrder(0, OrderAction.Sell, OrderType.Stop, 1, 0, CurrentOpenRangeLow, "EntryStopOCO", "SellStopEntry");
                                TradePlaced++;
			}
And that would put the orders into the system to trigger when the condition is met, along with a stop and profit target? Are these GTC or Day orders? Can we control for that in the original order? Or, did I miss the object of doing it in the first place?

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to fctrader for this post:
  #14 (permalink)
 baruchs 
Israel
 
Experience: Intermediate
Platform: NinjaTrader
Broker: pfg
Trading: eminis
Posts: 323 since Jun 2009

The difference is not that in managed approach (demonstrated) are not server side orders, but there are NO orders until after a fact.
To do it in managed approach:
get rangehigh, rangelow and rangemiddle.
Run on 1 min TF and if above middle (if previous was below middle cancel sell order) fire longstop.
The opposite for below middle.
This will guarantee 99.99999999% of execution. I'm willing to miss 0.000000001% of trades. (which will happen if in 1 min the price jumps from below the middle of 30min range to above the high or vs.

Baruch

Reply With Quote
  #15 (permalink)
 traderwerks   is a Vendor
 
Posts: 692 since Jun 2009
Thanks Given: 436
Thanks Received: 465

I agree with @baruchs.

I would stay away from unmanaged orders. They are not worth it for something like this. I have done the same thing with managed orders, so I would say it is more risk than it is worth.

Math. A gateway drug to reality.
Reply With Quote
The following 2 users say Thank You to traderwerks for this post:
  #16 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,394


traderwerks View Post
I agree with @ baruchs.

I would stay away from unmanaged orders. They are not worth it for something like this. I have done the same thing with managed orders, so I would say it is more risk than it is worth.

I can tell you that Unmanaged orders are complex on the programming side. Best to avoid if you can.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote
  #17 (permalink)
 fctrader 
Fort Collins, Colorado
 
Experience: Intermediate
Platform: NT7
Trading: ES
Posts: 57 since Feb 2011
Thanks Given: 48
Thanks Received: 45

I really appreciate all of the explanation of the Managed vs Unmanaged Approach in NT7. I had not thought much about that, and it has inspired me to go into a little more depth in it. So, thanks to all for those comments.

Not to minimize any of that, but to return to the Thread topic, the original poster wanted a simple Timed Range breakout strategy. NJAMC said


NJAMC View Post
Hi,

This is the type of flexibility I was adding to NT using the LOM. This can only be done with unmanaged transactions which require a lot of overhead in NT7. The LOM project on futures.io (formerly BMT) is designed to encapsulate that complexity and give very complex transactions with minimal programming effort.

I will bring your attention to 2 functions:
publicvoid SetAutoSLPTTicks(int m_StopLoss, int m_ProfitTarget,int m_PositionNumber)
publicbool GoMarketBracket(int m_SharesTraded,double low,double high,int m_PositionNumber)


Use the first function in LOM to set your automatic SL & PT in Ticks. The 2nd function would be in your main OnBarUpdate () or OnMarketData() functions to set the High & Low brackets you wish to enter. Once the order is filled, LOM will set the SL & PT correctly depending if the Short or Long was entered based upon your bracket.

Which several people agreed with.

Now that everyone has come full circle to saying that we (less experienced programmers) should avoid using Unmanaged Transactions, which is repeated in NT7 help,


Quoting 
NT7 Help
The Unmanaged approach is reserved for VERY EXPERIENCED programmers. In place of the convenience layer that the Managed approach offered, the Unmanaged approach instead offers ultimate flexibility in terms of order submission and management. This section will discuss some of the basics of working with Unmanaged order methods.

is my code for the breakout strategy valid? It seems to me that it enters the trade (the Order for the Trade) when my PC sees the event (breakout) occur. If I use the SetStopLoss and SetProfitTarget as used in the code, the orders are placed on the server-side concurrently with the entry, as enumerated in NT Help here,


Quoting 
NT7 Help
Closing a Position using a Stop Loss, Trailing Stop and/or Profit Target

You can predefine a stop loss, trailing stop and/or profit target in a strategy by calling the SetStopLoss(), SetTrailStop() or SetProfitTarget() methods from inside the Initialize() method. When these methods are called, they submit live working orders in real-time operation as executions are reported as a result of calling an entry method. Stop and target orders are submitted to the market as soon as fills come in from an entry order. Trailing stop orders are modified in real-time and NOT on the close of a bar. A stop and target order are also tied via OCO (one cancels the other) so if one is filled, the other is automatically cancelled.

So, it seems to me that what the OP is asking for can be done without Unmanaged Transactions.

Mind you, I'm not saying my code is pretty or efficient, just that it works for his purpose. Is that correct?

Visit my NexusFi Trade Journal Reply With Quote
The following user says Thank You to fctrader for this post:
  #18 (permalink)
 
ticker's Avatar
 ticker 
Houston TX
 
Experience: Advanced
Platform: NinjaTrader
Trading: TF
Posts: 49 since Oct 2010
Thanks Given: 63
Thanks Received: 9

Wow guys you are impressive with your in depth knowledge if ninja/coding. I had an emergency come up at home and couldnt keep up with this thread till now.

Thanks @NJAMC and @fctrader for kicking off the responses and everyone for chipping in.

So, I need to improve my code reading skills to understand which of these code bits really answers the question...

Ill read thru a bit and see which one I can figure out.

- Ticker

Started this thread Reply With Quote
  #19 (permalink)
 
ticker's Avatar
 ticker 
Houston TX
 
Experience: Advanced
Platform: NinjaTrader
Trading: TF
Posts: 49 since Oct 2010
Thanks Given: 63
Thanks Received: 9

Would anyone know how to backtest this strategy? Ive been looking back manually for many many long hours, and thinking there must be a simpler way. I see overall 85% success rate so far, that on the first break of the range it goes for at least one point.

I assume this has to be coded vs being able to use the strategy wizard/backtest?

(I wish Ninja would be more intuitive, but you seem to have to be a coding genius to do anything with this tool)

Started this thread Reply With Quote
  #20 (permalink)
 
NJAMC's Avatar
 NJAMC 
Atkinson, NH USA
Market Wizard
 
Experience: Intermediate
Platform: NinjaTrader 8/TensorFlow
Broker: NinjaTrader Brokerage
Trading: Futures, CL, ES, ZB
Posts: 1,970 since Dec 2010
Thanks Given: 3,037
Thanks Received: 2,394



ticker View Post
Would anyone know how to backtest this strategy? Ive been looking back manually for many many long hours, and thinking there must be a simpler way. I see overall 85% success rate so far, that on the first break of the range it goes for at least one point.

I assume this has to be coded vs being able to use the strategy wizard/backtest?

(I wish Ninja would be more intuitive, but you seem to have to be a coding genius to do anything with this tool)

If you are not using Replay Testing or Forward live testing, it is unlikely you are getting good results. Just be careful using the results.

Nil per os
-NJAMC [Generic Programmer]

LOM WIKI: NT-Local-Order-Manager-LOM-Guide
Artificial Bee Colony Optimization
Visit my NexusFi Trade Journal Reply With Quote





Last Updated on April 20, 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