Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Strategy stop working when executes ExitLong(), it has to do with SetProfitTarget().

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

    Strategy stop working when executes ExitLong(), it has to do with SetProfitTarget().

    This is a simple code, it is looking at SMA to EnterLong() and ExitLong() if it crosses above/below the SMA.
    However, I want to set a profit target, so if price goes above the SMA it enters long and it will set a profit target of around 8 ticks. However, if price does not reach 8 ticks and price crosses below the SMA it will exit the Long order.
    When I do backtesting, the EnterLong() works and it price hits the setprofittarget of 8ticks it gets out of the trade for a 8 tick profit. However, the issue is that if price does not reach 8 ticks and it goes below the SMA then the strategy does the ExitLong(), but then the strategy stop working and never enters back to EnterLong() every again. There must be something with the SetProfitTarget() that maybe creating an issue if it does not get trigger and then the ExitLong() gets trigger.

    I have attach the code on the attachment, but below is also part of the code. Also I got the same issue if I put the SetProfitTarget(CalculationMode.Ticks, 8); on the OnBarUpdate() instead of putting it in the State.Configure as it is below.

    else if (State == State.DataLoaded)
    {
    smatesting = SMA(smatestingnumber);
    x = SMA (smatestingnumber) [0];
    smatesting.Plots[0].Brush = Brushes.Goldenrod;
    AddChartIndicator(smatesting);
    }

    if(State == State.Configure)
    {
    SetProfitTarget(CalculationMode.Ticks, 8);
    }

    }

    protected override void OnBarUpdate()
    {
    //Add your custom strategy logic here.
    if(CrossAbove(smatesting,Close,1)) //What this is saying is "if price cross from above"
    {
    //CancelOrder();//("Long 1");
    ExitLong();
    } //then you want to close position
    if (CrossBelow(smatesting,Close,1))
    {
    EnterLong();
    // SetProfitTarget(CalculationMode.Ticks, 8);
    //put in here a enter long that says get out after x ticks/points
    }
    }
    Attached Files

    #2
    Hello zaro33,

    Thank you for your post.

    The issue you're running into here does have to do with using SetProfitTarget(), you're correct about that. When using the Managed Approach, (particularly, those using the Strategy Builder with their own hard Exit logic while using Set methods for profit target/stop loss), an Exit that logically gets submitted while the Stop/Target fills can cause a range of generally adverse behavior, including stopping trading at what seems like a random point.

    We can avoid this scenario one way by modifying our code to not enter on the same bar we might have had an overfill on (from the previous position) and to not allow for an overfill on the same bar we entered on (by not submitting our stop/target until the next bar). For example, let's assume I've left our SetProfitTarget() in State == State.Configure.


    Code:
                                // if we are in a flat position and it's been at least 1 bar since execution of an exit, and our cross is true, enter long
                            if (CrossBelow(smatesting,Close,1) && Position.MarketPosition == MarketPosition.Flat && (BarsSinceExitExecution() > 1 || BarsSinceExitExecution() == -1))
                            {
                                EnterLong();
                            }
                            // if we are in a long position and there are no current pending orders and our close went below the SMA, exit
                            if(CrossAbove(smatesting,Close,1) && Position.MarketPosition == MarketPosition.Long && Account.Orders.Count == 0)
                            {
                                ExitLong();                            
                            }
    The root of this behavior is with how Calculate.OnBarClose handles backtest/historical fills since it would only know the finished bar (not how it was built). This behavior is solely related to processing on historical data, as with backtesting, and does not occur on live data.

    Generally we would discourage the use of Set methods with Exit methods, especially if you're going to be backtesting your strategy.

    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Thank you so much. That worked perfectly.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Today, 05:17 AM
      0 responses
      44 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      126 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      65 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