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

Setting Profit Target Correctly

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

    Setting Profit Target Correctly

    Hi All,

    New to coding completely. I have written something that plots the full gap for me in CME futures RTH from the close of the last 30 minute candle to the open. I'm working to replicate a very basic strategy that takes entry at open(in the direction of the gap) when the gap is under 25pts and targets half of the said gap. This is all for practice, but I cannot get the correct definitions for my profit target to be and ABS value of the gap size. It just ignores my profit target at the moment and only exits on close or at a stop loss. I have tried multiple different ways to write this code and I've done a bunch of self exploration to no avail. Can someone please help me understand where my code is wrong? I have included the code below which shows the two ways I have tried to calculate this gap as my profit target.

    Thanks.

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class TryNumber2 : Strategy
    {
    private bool StopTrading = false;
    private bool doitonce = true;
    protected override void OnStateChange()

    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "TryNumber2";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 20;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    }
    }

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


    StopTrading = false;

    // Make sure not in a position
    if (Position.MarketPosition == MarketPosition.Flat && StopTrading == false)
    {
    // Check if gap up
    if (PriorDayOHLC().PriorOpen[0] < Open[0])
    {
    EnterShort(2);

    if (Position.MarketPosition == MarketPosition.Short && doitonce)
    {
    // Set Profit Target at half the gap size
    SetProfitTarget(CalculationMode.Price, Math.Abs(PriorDayOHLC().PriorClose[0] - Open.GetValueAt(0) / 2));

    // Set Trail stop at $250
    SetStopLoss(CalculationMode.Ticks,40);

    StopTrading = true;
    }
    }
    // Check if gap down
    if (PriorDayOHLC().PriorClose[0] > Open[0])
    {
    EnterLong(2);

    // Set Profit Target at half the gap size
    double gapSize = Open[0] - PriorDayOHLC().PriorClose[0];
    double profitTarget = gapSize / 2;
    SetProfitTarget(CalculationMode.Price, profitTarget);

    // Set Trail stop at $250
    SetStopLoss(CalculationMode.Ticks, 40);

    StopTrading = true;
    }
    }

    Draw.Line(this, "Gap"+CurrentBar, false, Bars.BarsSinceNewTradingDay, PriorDayOHLC().PriorClose[0] , 0, PriorDayOHLC().PriorClose[0], Brushes.Green, DashStyleHelper.Solid, 3);
    }


    }
    }​

    #2
    Hello KenSavoie,

    Welcome to the NinjaTrader forums!

    Use prints and TraceOrders to get a better understanding of what is being calculated.


    "It just ignores my profit target at the moment"

    I see you are using Math.Abs(PriorDayOHLC().PriorClose[0] - Open.GetValueAt(0) / 2) as the price for the profit target.

    Make a print that shows what this value is and why:

    Print(string.Format("{0} | PriorDayOHLC().PriorClose[0]: {1} - (Open.GetValueAt(0): {2} / 2): {3} = {4}", Time[0], PriorDayOHLC().PriorClose[0], Open.GetValueAt(0), (Open.GetValueAt(0) / 2), (PriorDayOHLC().PriorClose[0] - Open.GetValueAt(0) / 2) ));


    One note, set methods cannot be unset. This means you need to set these when the position is flat back to a number of ticks.

    The ProfitChaseStopTrailSetMethodsExample_NT8 example linked below demonstrates on line 100.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi Chelsea,

      thank you for your advice. I input the Print function and ran it through the output. I noticed that the Open.Getvalueat(0) is always pulling the first bar of the session I'm running the backtest on. It prints the same value each time at 3881. The PriorDayOHLC returns the correct value I'm looking for. Is there an equation or argument that would return the value of the first bar of each session open?

      Comment


        #4
        Hello KenSavoie,

        <Series>.GetValueAt(int barIndex) uses an absolute bar index (not a barsAgo index).


        A value of 0 would be the very first bar loaded in historical data on the far left of the chart. A value of CurrentBar would be the current bars value.

        For the bar of the session that would be the CurrentBar number minus the Bars.BarsSinceNewTradingDay.


        Open.GetValueAt(CurrentBar - Bars.BarsSinceNewTradingDay)

        Or if you wanted to use the barsAgo index

        Open[Bars.BarsSinceNewTradingDay]
        Chelsea B.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