Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

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 nicthe, Today, 07:38 AM
      2 responses
      15 views
      0 likes
      Last Post WaleeTheRobot  
      Started by stalt, 12-28-2015, 01:36 PM
      7 responses
      1,549 views
      0 likes
      Last Post NinjaTrader_Eduardo  
      Started by marcus2300, Today, 12:38 PM
      0 responses
      2 views
      0 likes
      Last Post marcus2300  
      Started by Rogers101, Today, 11:30 AM
      0 responses
      12 views
      0 likes
      Last Post Rogers101  
      Started by MGHORBEL, Today, 11:16 AM
      0 responses
      5 views
      0 likes
      Last Post MGHORBEL  
      Working...
      X