Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Feel like TrailingStop not work

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

    Feel like TrailingStop not work

    I code a strategy but It doesnt work with the trailingstop when I backtest. I dont know why. Any logic or syntax wrong? a another problem is ninja trader strategy dosent work with value < 0,0001 right?

    Click image for larger version  Name:	Screenshot 2023-01-11 221741.png Views:	0 Size:	46.4 KB ID:	1230816


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

    if (CurrentBars[0] < 3)
    return;

    // Set 1
    if (CCI1[2] > CCI1[1]
    && CCI1[1] < CCI1[0]
    && CCI1[1] < -100)


    {
    EnterLong(3000, "long");
    c =1;
    SetProfitTarget("long",CalculationMode.Pips, 30);
    SetStopLoss("long", CalculationMode.Pips, 30, false);
    }


    // Set 2
    if (CCI1[2] < CCI1[1]
    && CCI1[1] > CCI1[0]
    && CCI1[1] > 100)

    {
    EnterShort(3000, "short");
    c=1;
    SetProfitTarget("short",CalculationMode.Pips, 30);
    SetStopLoss("short", CalculationMode.Pips, 30, false);
    }




    // Set 3
    if (SystemPerformance.AllTrades.Count > 1)
    {
    Trade lastTrade = SystemPerformance.AllTrades[SystemPerformance.AllTrades.Count - 1];
    if ( lastTrade.ProfitPips <0 && lastTrade.Entry.Name == "short" && c==1)
    {
    EnterLong(3000,"RLong");

    SetTrailStop("RLong",CalculationMode.Pips, 30,false);
    c--;
    }
    if ( lastTrade.ProfitPips <0 && lastTrade.Entry.Name== "long")
    {
    EnterShort(3000,"RShort");

    SetTrailStop("RShort",CalculationMode.Pips, 30,false);
    c--;
    }
    }


    }
    }
    }​​

    #2
    Hello baolinh09,

    Thanks for your post.

    "ninja trader strategy dosent work with value < 0,0001 right?"

    What value are you referring to exactly? Are you referring to an order quantity or trailing stop value?​

    Set methods prep NinjaTrader to submit protective orders for an Entry order and should be called before the Entry method. For example:

    SetTrailStop("RShort",CalculationMode.Pips, 30,false);
    EnterShort(3000,"RShort");


    Should you call this method to dynamically change the trail stop price in the strategy OnBarUpdate() method, you should always reset the trail stop price / offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your trail stop order on your next open position. This could be seen in the 'Tips' section of the SetTrailStop() help guide page linked ​below.

    SetTrailStop(): https://ninjatrader.com/support/help...ttrailstop.htm

    Please review the help guide document on the differences on real-time vs backtest (historical).
    http://ninjatrader.com/support/helpG...ime_vs_bac.htm

    When backtesting on historical data, only the Open, High, Low, and Close will be available and there will be no intra-bar data. This means actions cannot happen intra-bar, fills cannot happen intra-bar. All prices and actions come from and occur when the bar closes as this is all the information that is known.

    Because of this, OnBarUpdate will only update 'On bar close' as it does not have the intra-bar information necessary for 'On price change' or 'On each tick' and the script will not have the intra-bar information to accurately fill an order at the exact price and time.

    Below is a link to the help guide on Calculate.
    https://ninjatrader.com/support/help.../calculate.htm

    To improve the accuracy of a backtest, you may use Tick Replay along with an added 1-tick series to have logic processed intra-bar and have orders filled intrabar. Tick Replay would be used to have the logic process OnEachTick or OnPriceChange with historical data, but this does not allow for intra-bar order fills. You would need to add a single-tick data series and submit orders to that single-tick data series for a strategy that uses Tick Replay.

    Additional information may be found in this NinjaTrader Forum post: https://ninjatrader.com/support/foru...mance?t=102504

    Ultimately, if a script is not behaving as expected, debugging prints should be added to the script to see exactly how it is behaving.

    Below is a link to a forum post that demonstrates how to use prints to understand behavior.

    https://ninjatrader.com/support/foru...121#post791121

    Please let us know if we may be of further assistance to you.​
    Brandon H.NinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by Segwin, 05-07-2018, 02:15 PM
    14 responses
    1,789 views
    0 likes
    Last Post aligator  
    Started by Jimmyk, 01-26-2018, 05:19 AM
    6 responses
    837 views
    0 likes
    Last Post emuns
    by emuns
     
    Started by jxs_xrj, 01-12-2020, 09:49 AM
    6 responses
    3,293 views
    1 like
    Last Post jgualdronc  
    Started by Touch-Ups, Today, 10:36 AM
    0 responses
    13 views
    0 likes
    Last Post Touch-Ups  
    Started by geddyisodin, 04-25-2024, 05:20 AM
    11 responses
    63 views
    0 likes
    Last Post halgo_boulder  
    Working...
    X