Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exiting a position when a certain condition is met.

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

    Exiting a position when a certain condition is met.

    Hello,

    I am building an strategy that places two market orders with their respective stoploss. I want one of the two orders to be issued when it has reached a certain price and the other when it meets a certain coded condition, according to the following script:​

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    SetStopLoss (@"Compra1", CalculationMode.Price, stop, false);
    SetStopLoss (@"Compra2", CalculationMode.Price, stop, false);
    SetProfitTarget (@"Compra1", CalculationMode.Price, objetivo);
    EnterLong (@"Compra1");
    EnterLong (@"Compra2");
    }

    if (/*condition*/)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"Salida1", @"Compra2");
    }


    I have also tried the following alternative, but when the Target of one of the positions is executed and the logic of the strategy triggers an entry again, it adds another new position to the existing one, which is something I want to avoid:

    {
    SetStopLoss (@"Compra1", CalculationMode.Price, stop, false);
    SetStopLoss (@"Compra2", CalculationMode.Price, stop, false);
    SetProfitTarget (@"Compra1", CalculationMode.Price, objetivo);
    EnterLong (@"Compra1");
    EnterLong (@"Compra2");
    }

    if (/*condition*/)
    {
    ExitLong(Convert.ToInt32(DefaultQuantity), @"Salida1", @"Compra2");
    }


    that's the reason why I added the if (Position.MarketPosition == MarketPosition.Flat) sentence but it doesn´t work as I explained in the begining of the message.

    How could I code it properly?

    Thanks in advance!​​​

    #2
    Hello BIOK.NT,

    Are you certain that your exit condition is becoming true? You should be able to submit a market exit to close a position while you have targets active.

    Comment


      #3
      Yes, the condition is met. With the same condition, in the first case it gives me the correct position and in the second case (with the condition if (Position.MarketPosition == MarketPosition.Flat)​), it doesn´t.

      Comment


        #4
        Hello BIOK.NT,

        In your second case you are submitting more entries again as part of that condition so that would be expected for the position to also change.

        How did you confirm the exit condition was true when using only ExitLong?

        Comment


          #5
          The condition that is met in the first case (where it executes the ExitLong() as I want) is the same condition as in the second case. That's how I know that the condition is met. I would have to do the same as in the first case but without sending any more new orders to market.

          If I'm not misinterpreting your answers; would it be well coded and it's just a matter of logic that it doesn't act as expected?

          Thanks again.​

          Comment


            #6
            Hello BIOK.NT,

            As far as I can see that should work to exit the position, I would suggest using TraceOrders to check if the exit is being ignored.

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            112 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            60 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            38 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            43 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            79 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X