Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

A few signsls on one candle

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

    A few signsls on one candle

    Hello everybody

    I have Strategy with multi MA logic and SL and PT. SL and PT a can choose in the Settings but they have a static positions on the chart.
    I modified my strategy for SL and PT and now I can move SL and PT on the chart, so my SL and PT are free to move. I used OnOrderUpdate and OnExecutionUpdate.
    Now I have problem when I see on the chart a few crossovers on one candle( a few signals for opening a few positions) - strategy placing just only one SL and PT (only for one position) and I have (for example) 3 Open Orders and just one SL and PT for one Order.
    I understand, that I need to unique name for each order and I added to the each name number of condition, but it not helps..

    Could you explain me how I must be implement order processing so that all executed orders have their own SL and PT?

    My part of the code with OnOrderUpdate and OnExecutionUpdate:​


    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (order.Name == "Long" + II + CurrentBar) // Long orders
    {
    entryOrderL = order;
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    entryOrderL = null;
    sumFilled = 0;
    }
    }
    else if (order.Name == "Short" + II + CurrentBar) // Short orders
    {
    entryOrderS = order;
    if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
    {
    entryOrderS = null;
    sumFilled = 0;
    }
    }
    }

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (entryOrderL != null && entryOrderL == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    sumFilled += execution.Quantity;
    if (stop[numberPairTemp] != 0)
    {
    if (execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrderL = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - stop[numberPairTemp] * TickSize, "StopL" + II + executionBarsL[0], "Long" + II + CurrentBar);
    targetOrderL = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + profit[numberPairTemp] * TickSize, "TargetL" + II + executionBarsL[0], "Long" + II + CurrentBar);
    }

    else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
    {
    stopOrderL = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - stop[numberPairTemp] * TickSize, "StopL" + II + executionBarsL[0], "Long" + II + CurrentBar);
    targetOrderL = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + profit[numberPairTemp] * TickSize, "TargetL" + II + executionBarsL[0], "Long" + II + CurrentBar);
    }
    }

    if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
    {
    entryOrderL = null;
    sumFilled = 0;
    numberPairTemp = 0;
    }
    }
    }

    else if (entryOrderS != null && entryOrderS == execution.Order)
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
    {
    sumFilled += execution.Quantity;
    if (profit[numberPairTemp] != 0)
    {
    if (execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrderS = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + stop[numberPairTemp] * TickSize, "StopS" + II + executionBarsL[0], "Short" + II + CurrentBar);
    targetOrderS = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - profit[numberPairTemp] * TickSize, "TargetS" + II + executionBarsL[0], "Short" + II + CurrentBar);
    }

    else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled) // Update exit order (for Short) quantities once orderstate turns to filled
    {
    // Stop-Loss order for OrderState.Filled
    stopOrderS = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + stop[numberPairTemp] * TickSize, "StopS" + II + executionBarsL[0], "Short" + II + CurrentBar);
    targetOrderS = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - profit[numberPairTemp] * TickSize, "TargetS" + II + executionBarsL[0], "Short" + II + CurrentBar);
    }
    }

    if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
    {
    entryOrderS = null;
    sumFilled = 0;
    numberPairTemp = 0;
    }
    }
    }

    if ((stopOrderL != null && stopOrderL == execution.Order) || (targetOrderL != null && targetOrderL == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrderL = null;
    targetOrderL = null;
    }
    }

    if ((stopOrderS != null && stopOrderS == execution.Order) || (targetOrderS != null && targetOrderS == execution.Order))
    {
    if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
    {
    stopOrderS = null;
    targetOrderS = null;
    }
    }
    }​

    Thanks!​

    #2
    Hello, thanks for writing in. These signal names you are using would not compile with the use of the || operator:

    if (order.Name == "Long" + II + CurrentBar)

    You need to use a different way of defining a unique signal name, this is the only reason your stop loss and profit target are not working as expected. You can use the CurrentBar value and keep a "counter" variable in your code, then each time an order is submitted, create a string that includes the Current bar value and the counter value to make them unique e.g. ("MyLongSignal"+CurrentBar+ "-" + Counter), make sure to increment the counter each time you generate a new string.

    Comment


      #3
      Hello Chris

      It's not || operator, it's two capital letter "i"

      I'll try to add Counter, thank you!

      Comment


        #4
        Counter not solve problem.

        You can see screenshot in attachment: All orders with different names, open position quantity is 4 contracts, but just only for 1 contract SL and PT...
        Attached Files

        Comment


          #5
          Hi, the first thing you need to do is use the Print() method in your strategy to debug this. The system is more than capable of setting up multiple stops and targets for multiple entries, it is just a matter of setting up the stop and target such that it targets each entry. Using Print, you will be able to see the flow of your code and it will help you see why you are only getting one pair of stops and targets. Unfortunately, I will not be able to review and debug your code. This is so I can provide fast help to all requests in a timely matter. See here for more information on debuggin using Print().

          Comment


            #6
            Thanks Chris, I use Print

            Comment


              #7
              I saw Log and I see that If strategy have a three signals on one candle - OnOrderUpdate run for each signal and print information in the Log, but OnExecutionUpdate - run only one time after last of the order, not after every order which going in OnOrderUpdate.
              In "Right Logic" must be run OnOrderUpdate for the first signal, run OnExecutionUpdate for the first signal, after that the same for the second signal and after that for third...​

              Comment


                #8
                Hi, We have an existing example that can submit a stop and target in OnExecution/OnOrderUpdate and you can essentially duplicate the code that is taking place here to accommodate 2 or 3 entries. As long as you can map an Order object to the entries and stop/target you can have a strategy that enters the market more than one time, at different times, and each entry can have its own stop and target.



                Please also make sure you have set "EntryHandling" to "EntryHandling.UniqueEntries" so that the strategy will enter as many times as it needs to (given each signal name is unique).

                Comment


                  #9
                  I saw this information and my code is based on this example

                  Comment


                    #10
                    Hello Chris.

                    Could you explaine me why I don't see state change for order "Shor2" on my screenshot.
                    If I understand correctly - when strategy place order - I must be see first state for order with this name like Submitted. BUT I see nothing for it.
                    Order was place ond position is open but I don't see information about it.
                    In this situation was a two signasl on one candle. Strategy opened positions correctly but I see information only about last order​
                    Attached Files

                    Comment


                      #11
                      Hi, this would imply that your OnOrderUpdate method is not picking up that signal correctly. In the "OnOrderUpdate/OnExecutionUpdate" example, the entry order is detected in OnExecutionUpdate, and only the order reference is set in OnOrderUpdate.

                      //OnBarUpdate
                      EnterLong(1, "MyEntry");

                      //OnOrderUpdate
                      if (order.Name == "MyEntry")
                      {
                      entryOrder = order;

                      //OnExecutionUpdate
                      ​if (entryOrder != null && entryOrder == execution.Order)
                      {
                      if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                      {​
                      //Stop and target are set up here.
                      }

                      Comment


                        #12
                        Hi Chris.
                        I have the same form of the code. It was in the sample file "SampleOnOrderUpdate".

                        in my variant I have a two parts (different for Long and for Short):

                        //OnOrderUpdate
                        if (order.Name == "Long" + II + CurrentBar")
                        {
                        entryOrderL = order;

                        else if (order.Name == "Short" + II + CurrentBar)
                        {
                        entryOrderS = order;


                        //OnExecutionUpdate
                        ​if (entryOrderL != null && entryOrderL == execution.Order)
                        {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {​
                        //Stop and target are set up here.

                        stopOrderL = ExitLongStopMarket()
                        targetOrderL = ExitLongLimit()

                        }
                        else if (entryOrderS != null && entryOrderS == execution.Order)
                        {
                        if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
                        {​
                        //Stop and target are set up here.

                        stopOrderS = ExitShortStopMarket()
                        targetOrderS = ExitShortLimit()

                        }

                        ​Really, I don't understand why my code put only last SL and PT in situation when strategy opening positions for two or more signals on one candle...
                        I inserted prints, looked Log ... and I don't understand why after order "Filled" don't open SL and PT and going to open next position...

                        I know that this methods ( ExitLongStopMarket(), ExitLongLimit() ) are ignored if a long position does not exist. Therefore I think that maybe need to insert some pause​ between
                        signals for opening positions? I understand that it gives delay, but I'll have safe position.​​

                        Comment


                          #13
                          Hi, I apologize deeply but I will not be able to review or debug your code for you. The only time period that the order entry system needs to wait is for the order execution to be seen in OnExecutionUpdate. Once the entry order is in State.Filled or PartFilled in OnExecutionUpdate, then it is safe to submit exits order. This is already demonstrated in the OnOrderUpdate/OnExecutionUpdate examples.

                          Kind regards,

                          -ChrisL

                          Comment


                            #14
                            Hi Chris.
                            I don't want that you will check my code, I want to understand what can be interferes with the correct execution order of the order state.

                            I inserted Print "state order" in OnOrderUpdate and turn on TraceOrders
                            and I don't see state "Filled" for first orders in my example. Just for last order and SL and PT for last order too.
                            Why OnOrderUpdate don't print state "Filled" for other orders?
                            So I understand, that If OnOrderUpdate not show order state "Filled" - therefore BOT not entry in OnExecutionUpdate and don't put SL and PT.​
                            Attached Files

                            Comment


                              #15
                              In Log I see state "filled" for this orders and all position are opened (in my example for 4 orders)

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by NullPointStrategies, Today, 05:17 AM
                              0 responses
                              51 views
                              0 likes
                              Last Post NullPointStrategies  
                              Started by argusthome, 03-08-2026, 10:06 AM
                              0 responses
                              128 views
                              0 likes
                              Last Post argusthome  
                              Started by NabilKhattabi, 03-06-2026, 11:18 AM
                              0 responses
                              69 views
                              0 likes
                              Last Post NabilKhattabi  
                              Started by Deep42, 03-06-2026, 12:28 AM
                              0 responses
                              42 views
                              0 likes
                              Last Post Deep42
                              by Deep42
                               
                              Started by TheRealMorford, 03-05-2026, 06:15 PM
                              0 responses
                              46 views
                              0 likes
                              Last Post TheRealMorford  
                              Working...
                              X