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

Simple strat

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

    Simple strat

    Hi,

    I am trying to program/back test a simple strat that enters at the exact time twice a day. I am using the builder and the issue I am running into is that It enters time after time again. I want to enter at 2 different times with 2 different entries and then after those are done it waits until the next time slot, here is the code below I am sure its a simple fix but I cannot figure it out and after trying multiple times I figured I'd ask for a little help. Any suggestions/fixes would be great thank you


    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class Buyclosesellopen : Strategy
    {
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"everynight the system will buy the close of the previous day and sell the open at the rth open ";
    Name = "Buyclosesellopen";
    Calculate = Calculate.OnBarClose;
    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;
    Close = DateTime.Parse("16:30", System.Globalization.CultureInfo.InvariantCulture) ;
    Open = DateTime.Parse("09:30", System.Globalization.CultureInfo.InvariantCulture) ;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute, 1);
    SetProfitTarget("", CalculationMode.Ticks, 40);
    SetStopLoss("", CalculationMode.Ticks, 40, false);
    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    // Set 1
    if (new TimeSpan(16, 45, 0) == Close.TimeOfDay)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"");
    }

    // Set 2
    if (new TimeSpan(9, 30, 0) == Open.TimeOfDay)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), "");
    }

    }

    #region Properties
    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="Close", Description="buy", Order=1, GroupName="Parameters")]
    public DateTime Close
    { get; set; }

    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="Open", Description="sell", Order=2, GroupName="Parameters")]
    public DateTime Open
    { get; set; }
    #endregion

    }
    }

    #2
    Hello minip,

    Thanks for the post.

    A couple of items come to attention here, first you are using the names Open and Close for your times. These should change to something else because Open and Close already exist in the script as the price series. You could rename these to OpenTime and CloseTime. I mention this because if you later try and make a condition using the close price this will likely cause an error.

    The second item is the condition you made, you are comparing the Timespan against your Input time which are the same times. You can remove the timespan here, you instead need to make the condition as your input == Time -> Time series.

    In the builder that would be
    Left: User input -> your input name
    Center: equals
    Right: Time -> Time series

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Jesse,

      Thanks for the help two more small tweaks I'd like your help on, currently it is closing out of trades at the end of the session. How could I make it so that it stays in the trade until the stop loss or profit level it hit. Also how would i set up an auto break even + x. Similar to how you would use this in a ATM stop strategy.

      Thanks for the help

      Comment


        #4
        Hello minip,

        Are you asking how to leave it past the session into the next session?

        There is a sample of what would be needed to do a breakeven in the following post: https://ninjatrader.com/support/foru...rategy-builder
        Also for manually coded scripts you can see this sample: https://ninjatrader.com/support/help...and_onexec.htm

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by algospoke, Yesterday, 06:40 PM
        2 responses
        19 views
        0 likes
        Last Post algospoke  
        Started by ghoul, Today, 06:02 PM
        3 responses
        14 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by jeronymite, 04-12-2024, 04:26 PM
        3 responses
        44 views
        0 likes
        Last Post jeronymite  
        Started by Barry Milan, Yesterday, 10:35 PM
        7 responses
        20 views
        0 likes
        Last Post NinjaTrader_Manfred  
        Started by AttiM, 02-14-2024, 05:20 PM
        10 responses
        180 views
        0 likes
        Last Post jeronymite  
        Working...
        X