Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

What's wrong with this customer's ExitOnClose procedure ( unmanaged )

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

    What's wrong with this customer's ExitOnClose procedure ( unmanaged )

    Hi NT,

    I have here a simple script, under unmanaged approach, which seeks to performance a simple "ExitOnClose". This is just a draft:
    Code:
    protected override void OnBarUpdate()										
            {	
    			if ( (ToTime(Time[0]) >= 165900 && ToTime(Time[0]) <= 171458) )
    			{
    				if (sEntryOrder != null )					
    				{					
    					CancelOrder(sEntryOrder);	 // if short order alive, cancel it			
    				}
    				if (lEntryOrder != null )					
    				{					
    					CancelOrder(lEntryOrder);	// if long order alive, cancel it	
    				}
    				if (Position.MarketPosition == MarketPosition.Long && Position.Quantity >0 )
    				{	
    					if (sEntryOrder == null && lEntryOrder == null )				
    					{				
    						sEntryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "EXIT LONG ON CLOSE");			
    					}
    				}
    				else if (Position.MarketPosition == MarketPosition.Short && Position.Quantity >0 )
    				{
    					if (sEntryOrder == null && lEntryOrder == null )				
    					{				
    						lEntryOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Market, Position.Quantity, 0, 0, "", "EXIT SHORT ON CLOSE");			
    					}
    				}
    				else
    				{;
    					lEntryOrder = null;
    					slEntryOrder = null;
    					sEntryOrder = null;
    					ssEntryOrder = null;
    					return;
    				}	
    				lEntryOrder = null;
    				slEntryOrder = null;
    				sEntryOrder = null;
    				ssEntryOrder = null;
    				return;
    			}
          .... otherwise ( trade normally )
    Practical example: The Strategy was short 100.000 EURUSD, then this happened when the time came ( check the trace file )

    Question:
    - Why does this happen, cause the loop is faster than the processing of the order?
    - Is it because the Return instruction that breaks the flow?

    Suggestions for solution would be great and highly appreciated

    Thanks in advance
    Attached Files
    Last edited by pstrusi; 12-22-2015, 04:19 AM.

    #2
    Hello pstrusi,

    Thank you for your post.

    So I may investigate this matter further, please detail what occurred in the trace that was unexpected.
    If possible, please provide the Log events when the item of concern occurred.

    Just to be sure, you have a ';' in a place that it should not be. You state this is a draft so the following is likely not in the full code.
    Code:
    				else
    				{[B];[/B] // ; in an incorrect place.
    					lEntryOrder = null;
    					slEntryOrder = null;
    					sEntryOrder = null;
    					ssEntryOrder = null;
    					return;
    				}

    Comment


      #3
      Thanks for your attention Patrick.

      Yes, the ";" is on that place as an involuntary syntax error cause it's a draft, it's not the cause or the issue itself at all.

      The issue is pretty simple. As you can see in that procedure, the goal, when the time comes, is to cancel any live orders, detect the strategy's current position and then submit the right order to go Flat before close, that's it. In theory it should work that way but it shouldn't, here what happens:

      In the earlier example, the strategy has a EURUSD 100.000 Short position, so the when it's 16:59:00, the strategy check for live orders to cancel, there's not any around, then it detects there's an opened short and submit a BuyToCover market order; but it sends the same BuyToCover order six times, after that it sends 3 Sell market orders of 200.000 and it continues with other cycle of non-sense.

      Obviously I'm missing some basic knowledge regarding to loops time and processing orders time synchronization. That's why I've attached the trace file, so an expert like you can see in seconds what's actually happening. I'll try to compare that trace file with other in order to see differences. Thanks in advance

      Comment


        #4
        if (Position.MarketPosition == MarketPosition.Long && Position.Quantity >0 )
        {
        if (sEntryOrder == null && lEntryOrder == null )
        {
        sEntryOrder = SubmitOrder(0, OrderAction.Sell, OrderType.Market, Position.Quantity, 0, 0, "", "EXIT LONG ON CLOSE");
        }
        }
        else if (Position.MarketPosition == MarketPosition.Short && Position.Quantity >0 )
        {
        if (sEntryOrder == null && lEntryOrder == null )
        {
        lEntryOrder = SubmitOrder(0, OrderAction.BuyToCover, OrderType.Market, Position.Quantity, 0, 0, "", "EXIT SHORT ON CLOSE");
        }
        }
        These orders are often not going to be submitted, reason being that the orders filters have not been nullified if the orders still existed. I do not know if you have nullified them in other circumstances, but they are certainly not nullified by your order-cancelling blocks.

        Comment


          #5
          Thanks Koganam for your comment. Yes, I'm aware that's something logic basic that I'm missing here. I'll investigate more on this as soon I finish another script.

          Once again, thanks for your help

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by helpwanted, Today, 03:06 AM
          0 responses
          3 views
          0 likes
          Last Post helpwanted  
          Started by Brevo, Today, 01:45 AM
          0 responses
          6 views
          0 likes
          Last Post Brevo
          by Brevo
           
          Started by aussugardefender, Today, 01:07 AM
          0 responses
          5 views
          0 likes
          Last Post aussugardefender  
          Started by pvincent, 06-23-2022, 12:53 PM
          14 responses
          242 views
          0 likes
          Last Post Nyman
          by Nyman
           
          Started by TraderG23, 12-08-2023, 07:56 AM
          9 responses
          384 views
          1 like
          Last Post Gavini
          by Gavini
           
          Working...
          X