Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Problem with Position.AveragePrice

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

    Problem with Position.AveragePrice

    // Set 1
    if (
    ((Position.MarketPosition == MarketPosition.Long)
    && ((Position.AveragePrice - (StopLoss * TickSize)) <= (Low[0] + (-1 * TickSize)) )
    && ((Position.AveragePrice + (1 * TickSize)) > Low[0])
    && (Breakeven == false)))
    {
    SetStopLoss(@"Long" , CalculationMode.Ticks, StopLoss, false);
    }
    // Set 2
    if (
    ((Position.MarketPosition == MarketPosition.Long)
    && ((Position.AveragePrice - (StopLoss * TickSize)) <= (Low[0] + (-1 * TickSize)) )
    && ((Position.AveragePrice + (1 * TickSize)) > Low[0])
    && (Breakeven == false)))
    {
    SetStopLoss(@"Long" ,CalculationMode.Ticks, 6, false);
    }
    // Set 3
    if (
    ((Position.MarketPosition == MarketPosition.Long)
    && ((Position.AveragePrice + (1 * TickSize)) <= (Low[0]) )
    && (Breakeven == false)))
    {
    SetStopLoss(@"Long" ,CalculationMode.Ticks, 2 , false);
    Breakeven = true;
    }
    // Set 4
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    Breakeven = false;
    }​


    The problems I am having is in Playback.

    On Current candle 0, my order executes but with Set 3 Stoploss, the Original StopLoss is 13 , not 2SL as it is in set 3.

    The next Candle closes , and then the StopLoss is updated to Set 2 Stoploss of 6SL.

    The next candle closes and the StopLoss is back to Set 3 of 2SL, and never resets again.
    If Set 3 is valid, then the flag should have been flipped on "Breakeven" to true in Set 3 , and Set 2 could have never been executed.
    When I remove Set 4 all together , Set 3 holds and the SL never moves. I am so confused.

    Please help.

    Basically I am simply trying to follow the bottom of the candle until the LOW is greater than BE +1 , that is all. And if the initial SL is greater than the first candle LOW.






    #2
    In addition , I only set the SL to 2 and 6 in the above example for testing , because I am having problems with Calculationmode.Price to follow the bottom of the candle. I will address that when I solve this issue.

    Comment


      #3
      Hello sdpadre3219,

      I would suggest to use prints here to identify when each of your conditions are becoming true, based on that observation you could change the conditions as needed so they happen in the order that you are expecting.

      Comment


        #4
        Will, do , I just thought at first glance , someone might find a syntax error, and find an issue. In my next try, I will also calculate for the candle low as was intended from the start.

        Comment


          #5
          }
          else if (State == State.Configure)
          {
          SetProfitTarget(@"Long", CalculationMode.Ticks, 20);
          SetStopLoss(@"Long", CalculationMode.Price, SL, false);
          }
          }

          protected override void OnBarUpdate()
          {
          if (BarsInProgress != 0)
          return;

          if (CurrentBars[0] < 1)
          return;

          // Set 1
          if (Position.MarketPosition == MarketPosition.Flat)
          {
          SL = Low[0];
          }

          // Set 2
          if ((High[0] < High[1])
          && (Position.MarketPosition == MarketPosition.Flat)
          && (State == State.Realtime))
          {
          EnterLongStopMarket(Convert.ToInt32(DefaultQuantit y), (High[0] + (1 * TickSize)) , @"Long");
          }

          }

          I can not even get this to work. I have tried Calculate on Tick , Price etc. How do I simply get the StopLoss to the low of the current candle. I did AveragePosition - Low etc.

          Nothing. Maybe this work on pricechange and not on Barclose.

          I really can not go any further until I can complete the syntax for something as simply as placing a Stop Loss and the low of a candle. ​

          BTW this code has nothing to do with original. This was done just to open a long position and get the stoploss to the LOW

          Comment


            #6
            Hello sdpadre3219,

            The low of the current candle is Low[0]. The code you have shown is not using your variable, its using (High[0] + (1 * TickSize)). You would need to select the SL variable as the price of the order if you want it to be set to the low.

            Keep in mind that with how your condition is currently configured it is possible that the order would be expired before filled. If the current bars high becomes higher than the previous bar that would stop the condition from being true and then the order would be expired. I mention this in case you see the order submitted but then later disappears and does not enter into a position.

            Comment


              #7
              All of what you explain above is OK, it is just to enter the order, No problems there.(Set 2) executes flawlessly . Its once it is Executed and the Profit Target Set. Which it does 20 ticks. I can not get the Stop loss set to the low of the candle, SET 1 , which I have currently getting updated every bar close. Thats all.

              The HIGH you mention above is simply to get into the order. I just need to set the stop loss now. I can easily do it with an "int" calc as ticks to lets say 12. Easy, Its getting it to the LOW of the current candle.

              Thank you for helping.

              Slightly a novice at this. But have programmed before. (25 + years ago, lol)

              I have even tried using CalculationAverage.Price - Low[0] but it will not save it as a "double"

              Having the same problem as this, but can not fix it.

              i have a strategy that enters short and has a stop loss 1 tick above the high of the entry bar for the position with a profit target order of 4 ticks. it seems to have arbitrary orders where the stop loss order does not execute and the position is held until &quot;Exit on session close&quot; for a massive loss. is there a way

              Last edited by sdpadre3219; 08-30-2023, 03:07 PM.

              Comment


                #8
                Hello sdpadre3219,

                The stops and targets that are set from OnStateChange cannot use bar prices, you would need to call SetStopLoss from OnBarUpdate.

                If you are manually coding you can just call SetStopLoss in the set where you want to update it and supply the price you wanted. If you are using the builder you would need to remove the stop from the stops and targets page and instead submit a stop market order from the set. You can see a sample of how to dynamical price targets in the builder in the following link:

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by NullPointStrategies, Today, 05:17 AM
                0 responses
                53 views
                0 likes
                Last Post NullPointStrategies  
                Started by argusthome, 03-08-2026, 10:06 AM
                0 responses
                130 views
                0 likes
                Last Post argusthome  
                Started by NabilKhattabi, 03-06-2026, 11:18 AM
                0 responses
                70 views
                0 likes
                Last Post NabilKhattabi  
                Started by Deep42, 03-06-2026, 12:28 AM
                0 responses
                44 views
                0 likes
                Last Post Deep42
                by Deep42
                 
                Started by TheRealMorford, 03-05-2026, 06:15 PM
                0 responses
                49 views
                0 likes
                Last Post TheRealMorford  
                Working...
                X