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 Template

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

    Strategy Template

    Hi I am new to Ninjascript and C#, I have been making strategies on a java based platform for about a year and I am trying to build my template that i use to start new strategies.

    essentially it is just enabling basic trading principles like exponential risk scaling, strategy trading hours, strategy equity monitoring etc

    It is to be used with strategies that employ scaling entries and exits.

    My problem is that it wont backtest just a basic entry and exit signal, I was hoping someone could tell me why.

    I uploaded the exported file, incase copy pasting code here is not the right way to do it.

    TIA

    Code:
    #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 templatekjm : Strategy
    {
    private bool HomeRunStatus;
    private int RiskAdjustedPositionSize;
    private bool ProfitableStrategy;
    private bool Pretradechecklistpassorfail;
    private bool MondayToFriday;
    private bool Tradetimeok;
    
    
    
    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Strategy Template";
    Name = "templatekjm";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 500;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Day;
    TraceOrders = true;
    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;
    StopLoss = 12;
    TakeProfit = 12;
    StartingCapital = 1000;
    RiskScale = 500;
    ExponetialRiskLimit = 0.5;
    StartTime = DateTime.Parse("10:00", System.Globalization.CultureInfo.InvariantCulture) ;
    EndTIme = DateTime.Parse("06:00", System.Globalization.CultureInfo.InvariantCulture) ;
    HomeRunStatus = false;
    RiskAdjustedPositionSize = StartingCapital / RiskScale;
    ProfitableStrategy = true;
    Pretradechecklistpassorfail = true;
    MondayToFriday = true;
    Tradetimeok = true;
    }
    else if (State == State.Configure)
    {
    
    }
    }
    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    {
    if (BarsInProgress != 0)
    return;
    
    // Set 1
    if (SystemPerformance.AllTrades.TradesPerformance.Cur rency.CumProfit + StartingCapital >= StartingCapital)
    {
    ProfitableStrategy = true;
    }
    
    // Set 2
    if (
    // nottradingdays
    ((DayOfWeek.Saturday == DayOfWeek.Saturday)
    || (DayOfWeek.Sunday == DayOfWeek.Sunday)))
    {
    MondayToFriday = false;
    }
    
    if (CurrentBars[0] < 0)
    return;
    
    // Set 3
    if ((Times[0][0].TimeOfDay >= StartTime.TimeOfDay)
    && (Times[0][0].TimeOfDay < EndTIme.TimeOfDay))
    {
    Tradetimeok = true;
    }
    
    // Set 4
    if ((MondayToFriday == false)
    && (Tradetimeok == true)
    && (ProfitableStrategy == true))
    {
    Pretradechecklistpassorfail = true;
    }
    {
    if (BarsInProgress != 0)
    return;
    
    if (CurrentBars[0] < 1)
    return;
    
    // entry1
    if (CrossAbove(Close, High, 1)
    && (Tradetimeok = true)
    &&(ProfitableStrategy == true)
    &&(Pretradechecklistpassorfail = true))
    {
    EnterLong(Convert.ToInt32(RiskAdjustedPositionSize ), @"enterlong");
    }
    
    // exit1
    if ((GetCurrentBid(0) > Position.AveragePrice)
    && (CrossBelow(Close, Low, 1)))
    {
    ExitLong(Convert.ToInt32(Position.Quantity), @"exitlong", @"enterlong");
    }
    }
    }
    
    }
    
    
    
    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="StopLoss", Description="Ticks of stop loss", Order=1, GroupName="Parameters")]
    public int StopLoss
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="TakeProfit", Description="Ticks of take profit", Order=2, GroupName="Parameters")]
    public int TakeProfit
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="StartingCapital", Order=3, GroupName="Parameters")]
    public int StartingCapital
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="RiskScale", Order=4, GroupName="Parameters")]
    public int RiskScale
    { get; set; }
    
    [NinjaScriptProperty]
    [Range(0.5, double.MaxValue)]
    [Display(Name="ExponetialRiskLimit", Description="% of risk per trade on equity", Order=5, GroupName="Parameters")]
    public double ExponetialRiskLimit
    { get; set; }
    
    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="StartTime", Order=6, GroupName="Parameters")]
    public DateTime StartTime
    { get; set; }
    
    [NinjaScriptProperty]
    [PropertyEditor("NinjaTrader.Gui.Tools.TimeEditorKe y")]
    [Display(Name="EndTIme", Order=7, GroupName="Parameters")]
    public DateTime EndTIme
    { get; set; }
    #endregion
    
    }
    }
    Attached Files

    #2
    I did find one mistake in line 113 I had after 10AM and before 6AM which need to be
    after 10am || before6am

    (this is more relevant to my timezone +10)

    still not backtesting a simple entry like close above close 1

    not sure if it would default to true so changed
    also change line 100 on to

    Code:
     // Set 2
    if (
    // nottradingdays
    ((DayOfWeek.Saturday == DayOfWeek.Saturday)
    || (DayOfWeek.Sunday == DayOfWeek.Sunday)))
    {
    MondayToFriday = false;
    }
    else if (
    // nottradingdays
    ((DayOfWeek.Monday == DayOfWeek.Monday)
    || (DayOfWeek.Tuesday == DayOfWeek.Tuesday)
    || (DayOfWeek.Wednesday == DayOfWeek.Wednesday)
    || (DayOfWeek.Thursday == DayOfWeek.Thursday)
    || (DayOfWeek.Friday == DayOfWeek.Friday)))
    {
    MondayToFriday = true;
    }
    can probably also use
    if MondayToFriday = false;
    Return

    or something but not sure, just trying to get it to place a trade at this point
    Last edited by BurnOutTrader; 01-21-2022, 10:59 PM.

    Comment


      #3
      I thiink its may be because i need int in these lines, but ive chnaged so much im starting a fresh one
      RiskAdjustedPositionSize = StartingCapital / RiskScale;

      also just realised i can get rid of monday to friday as stratgy will close before close every day anyway
      Last edited by BurnOutTrader; 01-21-2022, 11:42 PM.

      Comment


        #4
        Hello BurnOutTrader,

        You will need to do some debugging to understand what is going on with the logic.

        Use Print() to print information to the NinjaScriptOutput window.

        Print the time of the bar above the EnterLong() call. Do you see output from that appearing in the output window?

        If not, print the time of the bar and all values used in the condition that submits the order above (outside) of that condition.

        Enable TraceOrders to see what is happening with an order after it is submitted.

        Below I am providing a link to a forum post that demonstrates using print to understand behavior.



        Chelsea B.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Tim-c, Today, 02:10 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by Taddypole, Today, 02:47 PM
        0 responses
        2 views
        0 likes
        Last Post Taddypole  
        Started by chbruno, 04-24-2024, 04:10 PM
        4 responses
        50 views
        0 likes
        Last Post chbruno
        by chbruno
         
        Started by TraderG23, 12-08-2023, 07:56 AM
        10 responses
        400 views
        1 like
        Last Post beobast
        by beobast
         
        Started by lorem, Yesterday, 09:18 AM
        5 responses
        25 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Working...
        X