Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

SetProfitTarget executing before BarsSinceEntryExecution rule on Short Side

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

    SetProfitTarget executing before BarsSinceEntryExecution rule on Short Side

    I have a LOC that will set a profit target if price is under entry price & will set a stop loss if price is over entry price, respective towards short side.

    But for some reason the SetProfitTarget() Rule will execute before the BarsSinceExecution(), while the SetStopLoss Rule works properly. Even if I remove the SetStopLoss Rule, the SetProfitTarget still doesn't work.

    //**** at bottom, everything else is for reference if it impacts the bottom problem

    Code:
    protected override void OnBarUpdate()
    {
    if (Position.MarketPosition == MarketPosition.Flat)
    {
    // Set stop loss to 6 ticks in case of reversals
    SetStopLoss("ShortEntry", CalculationMode.Ticks, 6, false);
    
    }
    
    // Declares position Short
    else if (Position.MarketPosition == MarketPosition.Short)
    {
    // if price is more than entry by 5 ticks, raise Stop Loss to 1 tick
    if (Position.AveragePrice >= Close[0] + (5 * TickSize))
    
    {
    SetStopLoss("ShortEntry", CalculationMode.Ticks, -1, false);
    }
    
    // if price is over entry by 9 ticks (2.25 points), raise Stop Loss to 1.25 points (5 ticks)
    if (Position.AveragePrice >= Close[0] + (9 * TickSize))
    
    {
    SetStopLoss("ShortEntry", CalculationMode.Ticks, -5, false);
    }
    
    }
    // Going Short by Cross Below
    if (CrossBelow(EMA1, SMA1, 1))
    {
    // Enter Short
    EnterShort(Convert.ToInt32(DefaultQuantity), "ShortEntry");
    }
    
    // Needs both statements to be true in order to execute
    // This rule will prevent false signals during ranges/reversals
    if (Position.AveragePrice >= Close[0] + (5 * TickSize) && CrossAbove(EMA1, SMA1, 1))
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "ShortEntry", "");
    }
    
    //********************************************************************************************************************
    
    
    if (BarsSinceEntryExecution() >= 8 && (Position.AveragePrice >= Close[0]))
    {
    // if current price is under entry price -> Set 1 tick StopLoss
    SetStopLoss("ShortEntry", CalculationMode.Ticks, -1, false);
    }
    
    // Why is this executing before 8 bars?
    else
    {
    SetProfitTarget("ShortEntry", CalculationMode.Ticks, 1, false);
    }
    }
    
    //********************************************************************************************************************
    I am unsure of what route to take of:
    1. Call the method with OnStateChange
    2. Call the method with OnBarUpdate
    3. StopTargetHandling
    4. Or something else


    I see the documentation states that I can call the method OnStateChange() or OnBarUpdate(), but because I already declared the position flat at the beginning through OnBarUpdate() so I don't see that as the problem here, unless it's with OnStateChange. (Then how do you do that?)

    I also read How to close a position under Manage Approach about to use a ByStrategyPosition or a PerEntryExecution. Would this fix the problem? If so how does it & where would I put it?

    Again I'm not sure what the problem is/what route to take.

    Please explain which route & why it fixes it.

    Thanks in advance.
    Last edited by sentient254; 02-23-2021, 03:34 PM.

    #2
    Hello sentient254, thanks for your post.

    The else part is executing because it's running directly after if (BarsSinceEntryExecution ...) evaluates to false. If you want to do either or past 8 bars, do

    Code:
    if(BarsSinceEntryExecution() >= 8 && (Position.AveragePrice >= Close[0]))
    {
       stop loss
    }
    
    else if( BarsSinceEntryExecution() >= 8 && (Position.AveragePrice >= Close[0]))
    {
       profit target 
    }
    From what I see you want to set either protective stops 8 bars since the entry execution based on some price conditions at the time 8 bars have passed.

    Please let me know if this does not resolve your inquiry.
    Chris L.NinjaTrader Customer Service

    Comment


      #3
      I forgot to add the else if statement because I've been going at this trial & error & I came across it. What I came across with the else if statement is this code by switching the Close & Position.AveragePrice around to attend for the SetProfitTarget, as my rule is:

      - If Last closing is under price (reversal for short side) set a profit target 1 tick above entry

      Code:
      else if (BarsSinceEntryExecution() >= 8 && (Close[0] >= Position.AveragePrice))
      {
      SetProfitTarget("ShortEntry", CalculationMode.Ticks, 1, false);
      }
      But the SetProfitTarget Bypasses the 8 bars since entry execution rule, as in the picture With SetProfitTarget:

      The Without SetProfitTarget is just the SetStopLoss Rule by itself, waiting for the 8 tick rule:


      So this LOC doesn't work, & I don't know why. Like you said, I need to somehow rule the Last Closing is less than/before Entry signal, but this is where I'm at a dead end.
      Attached Files
      Last edited by sentient254; 02-23-2021, 04:53 PM.

      Comment


        #4
        Hello sentient254, thanks for your reply.

        Have you taken debugging steps to see what the strategy is doing on each bar iteration? Regrettably, I will not be able to spend time debugging or assisting with custom strategy logic.
        I would recommend going through the strategy and adding Prints throughout. e.g.

        if(BarsSinceEntryExecution() >= 8 && (Position.AveragePrice >= Close[0]))
        {
        Print("Stop Loss Condition Reached");
        Print("Bars Since Entry: " + BarsSinceEntryExecution());
        Print("Average Price: " + Position.AveragePrice);
        Print("Close: " + Close[0]);
        stop loss
        }

        else if( BarsSinceEntryExecution() >= 8 && (Position.AveragePrice >= Close[0]))
        {
        Print("Profit Target Condition Reached");
        profit target
        }
        Chris L.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by kabott, Today, 03:17 AM
        0 responses
        1 view
        0 likes
        Last Post kabott
        by kabott
         
        Started by Jacklugate, Today, 02:36 AM
        0 responses
        1 view
        0 likes
        Last Post Jacklugate  
        Started by Jmgheosjrue, Today, 02:23 AM
        0 responses
        1 view
        0 likes
        Last Post Jmgheosjrue  
        Started by jodiesutton, Today, 01:55 AM
        0 responses
        1 view
        0 likes
        Last Post jodiesutton  
        Started by azuul, Today, 01:49 AM
        1 response
        11 views
        0 likes
        Last Post azuul
        by azuul
         
        Working...
        X