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

Max Daily Trades

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

    Max Daily Trades

    Hello! Im trying to do some custom code on the Ninja Editor. I managed to set a limit of 2 trades taken per day...but of course when I use the Strategy Analyzer to evaluate a period (for instance 30days) I notice that it only takes 2 trades within 30 days.

    What am I missing here? I want the counter to ''reset'' at market close everyday, or at ''certain'' time of the day. So that the strategy can take 2 more trades the next day, and so on...

    Here i will show you the code I have so far... Please can someone give me the part of the code Im missing for this to work? Thankyou..

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class SimpleCross : Strategy
    {
    double allTrades;
    private EMA EMA1;
    private SMA SMA1;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "SimpleCross";
    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;
    SMAs = 20;
    EMAs = 9;
    Start = DateTime.Parse("15:30", System.Globalization.CultureInfo.InvariantCulture) ;
    End = DateTime.Parse("20:00", System.Globalization.CultureInfo.InvariantCulture) ;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, Convert.ToInt32(EMAs));
    SMA1 = SMA(Close, Convert.ToInt32(SMAs));
    SetProfitTarget(CalculationMode.Currency, 600);
    SetStopLoss(CalculationMode.Currency, 300);
    }
    }

    protected override void OnBarUpdate()
    {

    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1)
    return;

    double allTrades = SystemPerformance.AllTrades.TradesPerformance.Trad esPerDay;


    // Set 1
    if (CrossAbove(EMA1, SMA1, 1) && allTrades < 2)
    {
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Long");
    ExitShort(Convert.ToInt32(DefaultQuantity), close, "");
    allTrades ++;
    }

    // Set 2
    if (CrossBelow(EMA1, SMA1, 1) && allTrades < 2)
    {
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Short");
    ExitLong(Convert.ToInt32(DefaultQuantity), close, "");
    allTrades ++;
    }

    #2
    Hello camtrading,

    As a tip, to export a NinjaTrader 8 NinjaScript so this can be shared and imported by the recipient do the following:
    1. Click Tools -> Export -> NinjaScript...
    2. Click the 'add' link -> check the box(es) for the script(s) and reference(s) you want to include
    3. Click the 'Export' button
    4. Enter a unique name for the file in the value for 'File name:'
    5. Choose a save location -> click Save
    6. Click OK to clear the export location message
    By default your exported file will be in the following location:
    • (My) Documents/NinjaTrader 8/bin/Custom/ExportNinjaScript/<export_file_name.zip>
    Below is a link to the help guide on Exporting NinjaScripts.



    If you are wanting to reset on each new day, set the allTrades counter int back to 0 when Bars.IsFirstBarOfSession is true.

    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Thank you Chelsea. I had a look at it and gave it a try. I must be doing something wrong. This is the conditional I used:

      bool newDay = Bars.IsFirstBarOfSession;

      if (newDay)
      {
      allTrades =0;
      }

      And it wont work.

      Comment


        #4
        Hello camtrading,

        You can add a print and test in a separate script to make sure the Bars.IsFirstBarIOfSession check works.

        For example:

        Code:
        protected override void OnBarUpdate()
        {
            bool newDay = Bars.IsFirstBarOfSession;
        
            if (newDay)
            {
                Print(String.Format("newDay is true on bar {0} with time: {1}", CurrentBar, Time[0]));
            }
        }
        Will have output like:

        newDay is true on bar 0 with time: 6/23/2020 7:30:05 AM
        newDay is true on bar 4680 with time: 6/24/2020 7:30:05 AM
        newDay is true on bar 9360 with time: 6/25/2020 7:30:05 AM
        You can use debugging prints like what it is used in the example snippet above that confirms if the bool becomes true elsewhere in your script in order to check your logic.

        Regarding your allTrades variable, you are creating this in the scope of OnBarUpdate and assigning it with the value from SystemPerformance.AllTrades.TradesPerformance.Trad esPerDay on each bar. If you use Bars.IsFirstBarOfSession to reset the variable, it will only reset the variable on the first bar of the new session. Other bars will still have the value reset. Taking steps to use debugging pritns will help you monitor your logic and variables to see why actions have been taken or have not been taken. I have included some debugging tips at the end of my message.

        Also to note, you should not call both ExitLong and EnterShort to reverse a position. You just have to call EnterLong() when you are short or EnterShort() when you are long to reverse positions. Combining Enter and Exit methods here can result in unintended behavior. The SampleMACrossover strategy that comes with NinjaTrader can demonstrate creating crossovers, and the script demonstrated in the Strategy Builder 301 tutorial demonstrates this as well.

        Strategy Builder 301 - https://www.youtube.com/watch?v=HCyt90GAs9k

        I've included some tips for debugging below. The Playback Connection can be used to replay data as if it were realtime.

        Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm
        TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm
        Debugging in the Strategy Builder - https://drive.google.com/file/d/1mTq...w?usp=drivesdk
        Playback Connection - https://ninjatrader.com/support/help...connection.htm
        Debugging Demo - https://drive.google.com/file/d/1rOz...w?usp=drivesdk

        We look forward to assisting.
        JimNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by PaulMohn, Today, 12:36 PM
        0 responses
        2 views
        0 likes
        Last Post PaulMohn  
        Started by love2code2trade, 04-17-2024, 01:45 PM
        4 responses
        38 views
        0 likes
        Last Post love2code2trade  
        Started by alifarahani, Today, 09:40 AM
        2 responses
        14 views
        0 likes
        Last Post alifarahani  
        Started by junkone, Today, 11:37 AM
        3 responses
        20 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by frankthearm, Yesterday, 09:08 AM
        12 responses
        44 views
        0 likes
        Last Post NinjaTrader_Clayton  
        Working...
        X