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

Strategy to limit to only one trade per day

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

    Strategy to limit to only one trade per day

    I am building this strategy and cannot seem to limit this to one order per day. Can someone show me what I have wrong here?

    #region Using declarations
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Gui;
    using NinjaTrader.Gui.Chart;
    using NinjaTrader.Gui.SuperDom;
    using NinjaTrader.Gui.Tools;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    #endregion

    //This namespace holds Strategies in this folder and is required. Do not change it.
    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class ATO : Strategy
    {
    private int HighestBarsAgo;
    private int LowestBarsAgo;
    private double HighestPrice;
    private double LowestPrice;
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "ATO";
    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 = 7;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    LookBackPeriod = 15;
    HighestBarsAgo = 1;
    LowestBarsAgo = 1;
    HighestPrice = 1;
    LowestPrice = 1;
    //ProfitTargetTicks = 12;

    }
    else if (State == State.Configure)
    {
    SetTrailStop(CalculationMode.Ticks, 20);
    //SetParabolicStop("Enter ATO Long", CalculationMode.Ticks, 20, false, 0.03, 0.3, 0.01);
    // SetParabolicStop("Enter ATO Short", CalculationMode.Ticks, 20, false, 0.03, 0.3, 0.01);
    //SetParabolicStop(CalculationMode.Ticks, 20);
    //SetStopLoss(CalculationMode.Ticks, StopLossTicks);
    //SetProfitTarget(CalculationMode.Ticks, 10);
    //SetStopLoss(CalculationMode.Ticks, 20);
    }
    }

    protected override void OnBarUpdate()
    {
    //Add your custom strategy logic here.
    //Add your custom strategy logic here.
    if (BarsInProgress != 0)
    return;
    // Resets the stop loss to the original value when all positions are closed

    // set initial highest/lowest price to first bar of session so we have a baseline
    if(Bars.IsFirstBarOfSession)
    {
    HighestPrice = High[0];
    LowestPrice = Low[0];
    }

    if (CurrentBars[0] < LookBackPeriod)
    return;


    if (ToTime(Time[0]) >= 62000 && ToTime(Time[0]) <= 65000)
    {
    HighestPrice = MAX(High, 7)[0];
    Print("The current MAX value is " + HighestPrice.ToString());
    LowestPrice = MIN(Low, 7)[0];
    Print("The current MIN value is " + LowestPrice.ToString());
    }

    // Set 2
    //if (BarsSinceEntryExecution(0, @"ATO Enter Long", 1) == 1)
    //BarsSinceExitExecution() == -1)

    if (ToTime(Time[0]) >= 65001 && ToTime(Time[0]) <= 82500 && Close[0] > HighestPrice && BarsSinceEntryExecution(0, @"Enter ATO Long", 1) != 1)
    {


    EnterLongLimit(HighestPrice, "Enter ATO Long");
    Print("ATO Long " + HighestPrice.ToString());

    }


    if (ToTime(Time[0]) >= 65001 && ToTime(Time[0]) <= 82500 && Close[0] < LowestPrice && BarsSinceEntryExecution(0, @"Enter ATO Short", 1) != 1)
    {


    EnterShortLimit(LowestPrice, "Enter ATO Short");
    Print("ATO Short " + LowestPrice.ToString());

    }
    }
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="LookBackPeriod", Order=1, GroupName="Parameters")]
    public int LookBackPeriod
    { get; set; }
    #endregion

    }
    }

    #2
    Hello Jonathan.Lee,

    Use a bool declared in the scope of the class like dailyOrderSubmitted.

    When the order is submitted, set this to true.

    When Bars.IsFirstBarOfSession is true, set this to false to allow for a new order on a new day.


    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.
    Chelsea B.NinjaTrader Customer Service

    Comment


      #3
      Hi,

      And about avoid to a strategy run again, because set a bool variable works, but if one turn on the strategy again on other chart the same strategy will send another order because the bool variable will be on his original value.

      Is there a way to query NJ if some order from this signal was sended or executed today?

      Thanks in advance!

      Comment


        #4
        Originally posted by earmarques View Post
        Hi,

        And about avoid to a strategy run again, because set a bool variable works, but if one turn on the strategy again on other chart the same strategy will send another order because the bool variable will be on his original value.

        Is there a way to query NJ if some order from this signal was sended or executed today?

        Thanks in advance!
        You could consider exposing a value to other scripts. For example, we have a reference sample that demonstrates "Exposing indicator values that are not plots" and you could consider exposing a value in this way:


        Another option would be to combine your strategy logic from the separate charts into one strategy so it is aware of position, order, and execution information for both instruments involved. Instead of running the strategy on two charts, you could run it on one chart and have the second instrument added programmatically via AddDataSeries():


        For more information regarding Multi-Time Frame & Instruments:


        Please let us know if we may be of further assistance.
        Emily C.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by George21, Today, 10:07 AM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_ChristopherJ  
        Started by geddyisodin, Yesterday, 05:20 AM
        9 responses
        50 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Started by DayTradingDEMON, Today, 09:28 AM
        3 responses
        20 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Stanfillirenfro, Today, 07:23 AM
        9 responses
        23 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by navyguy06, Today, 09:28 AM
        1 response
        9 views
        0 likes
        Last Post NinjaTrader_Gaby  
        Working...
        X