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

Automatic Trend Line Detection, im trying to set pfofit and target to this strategy.

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

    Automatic Trend Line Detection, im trying to set pfofit and target to this strategy.

    #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 AutoTrendHDemoNT8 : Strategy
    {
    #region Variables
    private AutoTrendHNT8 ATH;
    // Constants
    int cBreakUp=1; int cBreakDown =-1;
    int cRising =1; int cFalling =-1; int cFlat=0;
    int cLong =1; int cShort =-1;
    bool debug = false;
    #endregion

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Demonstration of AutoTrendH Indicator.";
    Name = "AutoTrendHDemoNT8";
    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;
    Alert = true;
    ShowHistory = true;
    LimitHistory = false;
    LimitHistoricalLookback = 60;
    Strength = 15;
    StopProfit = 10;
    StopShock = 0;
    StopEven = 0;
    StopLoss = 0;
    StopReverse = 0;
    Quantity = 1000;
    }
    else if (State == State.Configure)
    {

    DefaultQuantity = Quantity;
    }
    else if (State == State.DataLoaded)
    {
    ATH = AutoTrendHNT8(Alert, Strength, ShowHistory, LimitHistory, LimitHistoricalLookback, Brushes.Red, Brushes.Green, Brushes.Red, Brushes.Green);
    }
    }

    protected override void OnBarUpdate()
    {
    //PRELOAD
    //I use this as my NT is not set to account for the tick price difference in the JPY's
    if ((State == State.Historical) && (Instrument.FullName == "$USDJPY") || (Instrument.FullName=="$EURJPY"))
    {
    DefaultQuantity=(int)(Quantity*.01);
    }
    //Preload the AutoTrendH values this bar for later use in the strategy
    int trendDirection = ATH.Direction; //1=TrendUp, -1=TrendDown, 0=New trend not yet determined
    double trendPrice = ATH.TrendPrice; //Tick value at rightmost bar of current trend line
    int trendSignal = ATH.Signal; //1=resistance break, -1=support break

    //ENTRYS
    // If trending and price is following trend, enter in direction of trend
    if ((trendDirection==cRising) && (Close[0]>trendPrice))
    EnterLong("TrdLg");
    if ((trendDirection==cFalling) && (Close[0]<trendPrice))
    EnterShort("TrdSh");

    // If price breaks through trend, reverse position.
    if ((trendDirection==cRising) && (Close[0]<trendPrice))
    EnterShort("BrkSh");
    if ((trendDirection==cFalling) && (Close[0]>trendPrice))
    EnterLong( "BrkLg");
    }

    #region Properties
    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name="Alert", Description="Sets audible and logs alert if set to true", Order=1, GroupName="NinjaScriptStrategyParameters")]
    public bool Alert
    { get; set; }

    [NinjaScriptProperty]
    [Display(ResourceType = typeof(Custom.Resource), Name="ShowHistory", Description="Saves trendlines of auto-generated Trends", Order=2, GroupName="NinjaScriptStrategyParameters")]
    public bool ShowHistory
    { get; set; }

    [NinjaScriptProperty]
    [Display(Name="LimitHistory", Description="Limit Historical Trendlines & Breaks", Order=3, GroupName="Parameters")]
    public bool LimitHistory
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(Name="LimitHistoricalLookback", Description="Historical Lookback Period to Limit Trendlines & Breaks", Order=4, GroupName="Parameters")]
    public int LimitHistoricalLookback
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="Strength", Description="Sets the granularity of trend detection (smaller # = finer trend detection", Order=5, GroupName="NinjaScriptStrategyParameters")]
    public int Strength
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="StopProfit", Description="Lock in profits after StopProfit pips/ticks", Order=6, GroupName="NinjaScriptStrategyParameters")]
    public int StopProfit
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="StopShock", Description="1 Minute timeframe catastrophic loss stop", Order=7, GroupName="NinjaScriptStrategyParameters")]
    public int StopShock
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="StopEven", Description="Move stop to breakeven after StopEven pips/ticks", Order=8, GroupName="NinjaScriptStrategyParameters")]
    public int StopEven
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="StopLoss", Description="Initial StopLoss on entry", Order=9, GroupName="NinjaScriptStrategyParameters")]
    public int StopLoss
    { get; set; }

    [NinjaScriptProperty]
    [Range(0, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="StopReverse", Description="Reverse position after StopReverse pips/ticks", Order=10, GroupName="NinjaScriptStrategyParameters")]
    public int StopReverse
    { get; set; }

    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(ResourceType = typeof(Custom.Resource), Name="Quantity", Description="Number of shares/contracts/Lots to buy", Order=11, GroupName="NinjaScriptStrategyParameters")]
    public int Quantity
    { get; set; }
    #endregion

    }
    }

    #2
    Hello hakanshuker,

    Thank you for your post.

    There's a number of different options for setting your stops and profit targets depending on what you want them to do.

    Here is one example from our help guide that goes over how to set a basic stop and target based on your entry price and then move the stop loss to breakeven after a certain number of ticks in your favor:



    Here's a more advanced example that shows using OnOrderUpdate and OnExecution to submit your profit target and stop loss orders:



    Also, here are links to our help guide regarding SetStopLoss() and SetProfitTarget():





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

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by kujista, Today, 06:23 AM
    0 responses
    1 view
    0 likes
    Last Post kujista
    by kujista
     
    Started by traderqz, Yesterday, 04:32 PM
    1 response
    12 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Started by f.saeidi, Today, 05:56 AM
    1 response
    5 views
    0 likes
    Last Post Jltarrau  
    Started by Jltarrau, Today, 05:57 AM
    0 responses
    5 views
    0 likes
    Last Post Jltarrau  
    Started by Stanfillirenfro, Yesterday, 09:19 AM
    7 responses
    53 views
    0 likes
    Last Post NinjaTrader_Gaby  
    Working...
    X