Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Unmanaged Orders with Fix StopLoss and Target

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Unmanaged Orders with Fix StopLoss and Target

    Hi All,

    I am fairly new to programming and I need some help with a simple strategy.
    The basics are: I want to place a trade in the begining of every hour, based on the close of the previous bar. Really simple: if Red --> Short, if Green --> Long. Same size for all the trades and same amount of pips for stoploss and take profit.

    I only want to trade between 7 and 4pm, therefore 10 trades in total per day.

    So far so good, my problem comes when I have to put my stop and target pending orders in. Since I cannot use Managed Orders, (due to the fact that buy and sell positions come during the day, and I do not want to cancel all current positions when the direction is reversed).

    Can someone help me how to sort out the stop loss and target pending orders for these trades? I will copy a part of the code, I know I am missing a lot of stuff.
    Code:
    if (ToTime(Time[0]) >= 70000 && ToTime(Time[0]) <= 160000)
                if (Close[1] <= Close[0])
    			if (Historical == false)
    			{
    				SubmitOrder (0, OrderAction.Buy, OrderType.Market, DefaultQuantity, 0, 0,"","Longpos");
    				SubmitOrder (0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, GetCurrentAsk() + (Target * TickSize), 0, "longpending", "LongTarget");
    				SubmitOrder (0, OrderAction.Sell, OrderType.Stop, DefaultQuantity, GetCurrentAsk() - (StopLoss * TickSize), 0, "longpending", "LongStop");
    			}
               
    
    			// Condition set 2
    			if (ToTime(Time[0]) >= 70000 && ToTime(Time[0]) <= 160000)
                if (Close[1] > Close[0])
    			if (Historical == false)
                {
                    SubmitOrder (0, OrderAction.Sell, OrderType.Market, DefaultQuantity, GetCurrentBid() - (Target * TickSize), GetCurrentBid() + (StopLoss * TickSize),"","Shortpos");
    				SubmitOrder (0, OrderAction.Buy, OrderType.Limit,DefaultQuantity,  GetCurrentAsk() - (Target * TickSize), 0, "shortpending", "ShortTarget");
    				SubmitOrder (0, OrderAction.Buy, OrderType.Stop, DefaultQuantity, GetCurrentAsk() + (StopLoss * TickSize), 0, "shortpending", "ShortStop");
    Basically, I would like to know how can I create a unique OCO ID for every new position, becuase I guess this way, it groups them together, and if one of these orders gets triggered then all the other pending orders are gonna be gone.

    Thanks.
    Last edited by marklekeny; 04-08-2015, 05:25 AM.

    #2
    Well it seems like I sorted out the problem now, I am just gonna share it here because someone might benefit from it.

    Basically, I dig around a bit and decided to use
    Code:
    string orderIdshort = GetAtmStrategyUniqueId();
    string orderIdlong = GetAtmStrategyUniqueId()
    To create a random string after trade execution, and using these strings as the OCO ID.

    Updated:
    Code:
    protected override void OnBarUpdate()
            {
                // Condition set 1
    			if (ToTime(Time[0]) >= 70000 && ToTime(Time[0]) <= 160000)
                if (Close[1] <= Close[0])
    			if (Historical == false)
    			{
    				SubmitOrder (0, OrderAction.Buy, OrderType.Market, DefaultQuantity, 0, 0,"","Longpos");
    				string orderIdlong = GetAtmStrategyUniqueId();
    				SubmitOrder (0, OrderAction.Sell, OrderType.Limit, DefaultQuantity, GetCurrentBid() + (Target * TickSize), 0,orderIdlong, "LongTarget");
    				SubmitOrder (0, OrderAction.Sell, OrderType.Stop, DefaultQuantity, 0, GetCurrentBid() - (StopLoss * TickSize), orderIdlong, "LongStop");
    			}
               
    
    			// Condition set 2
    			if (ToTime(Time[0]) >= 70000 && ToTime(Time[0]) <= 160000)
                if (Close[1] > Close[0])
    			if (Historical == false)
                {
                    SubmitOrder (0, OrderAction.Sell, OrderType.Market, DefaultQuantity, 0, 0,"","Shortpos");
    				string orderIdshort = GetAtmStrategyUniqueId();
    				SubmitOrder (0, OrderAction.Buy, OrderType.Limit,DefaultQuantity,  GetCurrentAsk() - (Target * TickSize), 0, orderIdshort, "ShortTarget");
    				SubmitOrder (0, OrderAction.Buy, OrderType.Stop, DefaultQuantity, 0, GetCurrentAsk() + (StopLoss * TickSize), orderIdshort, "ShortStop");
                }
    
            }

    Comment


      #3
      Thank you for the update Marklekeny!

      Glad everything is working for you
      Cal H.NinjaTrader Customer Service

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      633 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      364 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      105 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      567 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      568 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X