NexusFi: Find Your Edge


Home Menu

 





stop loss & profit target order settings with the unamanaged approach?


Discussion in NinjaTrader

Updated
      Top Posters
    1. looks_one stoicbuddha with 2 posts (0 thanks)
    2. looks_two xplorer with 1 posts (0 thanks)
    3. looks_3 d.allen101 with 1 posts (0 thanks)
    4. looks_4 bltdavid with 1 posts (0 thanks)
    1. trending_up 3,733 views
    2. thumb_up 0 thanks given
    3. group 2 followers
    1. forum 5 posts
    2. attach_file 0 attachments




 
Search this Thread

stop loss & profit target order settings with the unamanaged approach?

  #1 (permalink)
stevien
Paris France
 
Posts: 2 since Nov 2015
Thanks Given: 1
Thanks Received: 0

Hello,

I am developing a new strategy with the unmanaged approach. When I enter a long/short position, I don't know how to exit this position, then set a stop loss order and a profit target order because everytime I run my script below, NinjaTrader doesn't respond.
Here is the script :

public class SStrategy : Strategy
{
#region Variables
// User defined variables (add any user defined variables below)
private int quantity = 1;
private int target = 30;

private IOrder longOrder = null;
private IOrder shortOrder = null;
private IOrder stopLossOrder = null;
private IOrder profitTargetOrder = null;
#endregion

/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
CalculateOnBarClose = true;

Unmanaged = true;

// TraceOrders = true;
}

/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Buying condition
if (Close[0] < Bollinger(2, 14).Lower[0])
{
longOrder = SubmitOrder(0,OrderAction.Buy,OrderType.Market,quantity,0,0,"","ELong");
// SubmitOrder(0,OrderAction.Sell,OrderType.StopLimit,quantity,0,0,"","XLong");
}

// Shorting condition
else if (Close[0] > Bollinger(2, 14).Upper[0])
{
shortOrder = SubmitOrder(0,OrderAction.SellShort,OrderType.Market,quantity,0,0,"","EShort");
// SubmitOrder(0,OrderAction.Buy,OrderType.StopLimit,quantity,0,0,"","XShort");
}
}

protected override void OnExecution(IExecution execution)
{
if (longOrder != null && longOrder == execution.Order)
{
if (execution.MarketPosition == MarketPosition.Long)
{
if (shortOrder != null)
CancelOrder(shortOrder);
shortOrder = null;

if (Close[0] > Low[0])
{
stopLossOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Stop,quantity,0,MIN(Low,2)[0] - 1*TickSize,"","LongSL");
}

profitTargetOrder = SubmitOrder(0,OrderAction.Sell,OrderType.Limit,quantity,target,0,"","LongPT");
}
}
else if (shortOrder != null && shortOrder == execution.Order)
{
if (execution.MarketPosition == MarketPosition.Short)
{
if (longOrder != null)
CancelOrder(longOrder);
longOrder = null;

if (Close[0] < High[0])
{
stopLossOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Stop,quantity,0,MAX(High,3)[0] + 1*TickSize,"","ShortSL");
}

profitTargetOrder = SubmitOrder(0,OrderAction.BuyToCover,OrderType.Limit,quantity,target,0,"","ShortPT");
}
}

// Resets the longOrder and shortOrder objects to null after the order has been filled
// if (execution.Order.OrderState != OrderState.PartFilled)
// {
// longOrder = null;
// shortOrder = null;
// }

// Reset our stop order and target orders' IOrder objects after our position is closed.
if ((stopLossOrder != null && stopLossOrder == execution.Order) || (profitTargetOrder != null && profitTargetOrder == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopLossOrder = null;
profitTargetOrder = null;
}
}
}

#region Properties
[Description("Quantity of the underlying asset")]
[GridCategory("Parameters")]
public int Quantity
{
get { return quantity; }
set { quantity = Math.Max(1, value); }
}

[Description("Target of this position")]
[GridCategory("Parameters")]
public int Target
{
get { return target; }
set { target = Math.Max(1, value); }
}

#endregion
}


Could anyone help me, please?

Thank you!

PS: Excuse my English.

Reply With Quote

Can you help answer these questions
from other members on NexusFi?
NT7 Indicator Script Troubleshooting - Camarilla Pivots
NinjaTrader
What broker to use for trading palladium futures
Commodities
MC PL editor upgrade
MultiCharts
How to apply profiles
Traders Hideout
REcommedations for programming help
Sierra Chart
 
  #2 (permalink)
 
xplorer's Avatar
 xplorer 
London UK
Site Moderator
 
Experience: Beginner
Platform: CQG
Broker: S5
Trading: Futures
Posts: 5,980 since Sep 2015
Thanks Given: 15,506
Thanks Received: 15,422

Hi stevien - sorry I'm not familiar enough with NT to answer your specific question, but I would have thought that the platform must allow for some sort or bracket order where you automatically set a stop loss and a target?

Reply With Quote
  #3 (permalink)
 
stoicbuddha's Avatar
 stoicbuddha 
Seattle, WA
 
Experience: Intermediate
Platform: NinjaTrader 8
Broker: AMP/CQG
Trading: Indices
Frequency: Every few days
Duration: Minutes
Posts: 96 since Feb 2012
Thanks Given: 1,052
Thanks Received: 96


I may be wrong but shouldn't use to be using ChangeOrder() instead of SubmitOrder() when trying to set the stop loss/breakeven in the Unmanaged approach as mentioned here?

Visit my NexusFi Trade Journal Reply With Quote
  #4 (permalink)
 bltdavid 
San Jose, CA, USA
 
Experience: Intermediate
Platform: NinjaTrader
Posts: 112 since Dec 2013
Thanks Given: 35
Thanks Received: 103


happyrenko View Post
I may be wrong but shouldn't use to be using ChangeOrder() instead of SubmitOrder() when trying to set the stop loss/breakeven in the Unmanaged approach as mentioned here?

Uh, no.

Regardless of OrderType, you always use SubmitOrder() at least one time. You
always want to save the reference to the IOrder object returned by SubmitOrder.

Then, if you need to update the order later (aka, move the stop, to achieve either
a BreakEven or Trailing effect) you then call ChangeOrder() and pass the saved
reference from SubmitOrder() as the first argument.

So, when entering the stop loss order, you first use SubmitOrder. You save that IOrder
reference returned by SubmitOrder, and use it with ChangeOrder later ... if you need to.

In other words, you would not call ChangeOrder unless you first had an IOrder reference
returned to you from SubmitOrder.

Reply With Quote
  #5 (permalink)
 
stoicbuddha's Avatar
 stoicbuddha 
Seattle, WA
 
Experience: Intermediate
Platform: NinjaTrader 8
Broker: AMP/CQG
Trading: Indices
Frequency: Every few days
Duration: Minutes
Posts: 96 since Feb 2012
Thanks Given: 1,052
Thanks Received: 96


bltdavid View Post
Uh, no.

Regardless of OrderType, you always use SubmitOrder() at least one time. You
always want to save the reference to the IOrder object returned by SubmitOrder.

Then, if you need to update the order later (aka, move the stop, to achieve either
a BreakEven or Trailing effect) you then call ChangeOrder() and pass the saved
reference from SubmitOrder() as the first argument.

So, when entering the stop loss order, you first use SubmitOrder. You save that IOrder
reference returned by SubmitOrder, and use it with ChangeOrder later ... if you need to.

In other words, you would not call ChangeOrder unless you first had an IOrder reference
returned to you from SubmitOrder.

Yes, I meant use SubmitOrder() initially and ChangeOrder() later to fix the ability to change the Stop Loss later.

Visit my NexusFi Trade Journal Reply With Quote
  #6 (permalink)
d.allen101
Chicago, IL
 
Posts: 4 since Jun 2009
Thanks Given: 1
Thanks Received: 1

Hey Stevien I'm having the EXACT same issue. I'm assuming you now know how to close an open order using Unmanaged, SubmitOrder. How did you go about closing open orders?

Thanks in advance.

Reply With Quote




Last Updated on November 2, 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