Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Trying to develop my first script and am honestly lost

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

    Trying to develop my first script and am honestly lost

    This is what I've got so far. I'm getting 0 compiling errors, however, when I backtest, it won't get any data. Theoretically, it should be a very simple code that enters a trade on a reversal.

    using System;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.Strategies;

    public class reveralsbottest4 : Strategy
    {
    private double accountSize = 500;
    private double commission = 1.50;
    private double maxAccountLoss = 0.1;
    private double trailingStopPercent = 0.2;
    private Order buyOrder;
    private Order sellOrder;
    private Pivots pivots;
    private Pivots PivotsRange;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Name = "reveralsbottest4";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.UniqueEntries;
    IsExitOnSessionCloseStrategy = true;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 2;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
    BarsRequiredToTrade = 20;
    }
    else if (State == State.Configure)
    {
    AddDataSeries(BarsPeriodType.Minute, 10);
    }
    else if (State == State.DataLoaded)
    {
    double value = Pivots(PivotRange.Daily, HLCCalculationMode.CalcFromIntradayData, 0, 0, 0, 20).Pp[0];
    AddChartIndicator(pivots);
    }
    }
    protected override void OnBarUpdate()
    {
    if (CurrentBars[0] < BarsRequiredToTrade)
    return;

    if (BarsInProgress != 0)
    return;

    if (Position.MarketPosition == MarketPosition.Flat)
    {
    if (Close[0] > pivots.R1[0])
    {
    buyOrder = EnterLongStopMarket(Close[0] + TickSize);
    }
    else if (Close[0] < pivots.S1[0])
    {
    sellOrder = EnterShortStopMarket(Close[0] - TickSize);
    }
    }

    if (Position.MarketPosition != MarketPosition.Flat)
    {
    double profit = Position.Quantity * (Close[0] - Position.AveragePrice) * (Position.MarketPosition == MarketPosition.Long ? 1 : -1) * Bars.Instrument.MasterInstrument.PointValue - commission;
    double maxLoss = -accountSize * maxAccountLoss;

    if (profit > 0)
    {
    SetStopLoss(CalculationMode.Percent, trailingStopPercent);
    }
    else if (profit <= maxLoss)
    {
    ExitLong();
    ExitShort();
    }
    }
    }

    protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
    {
    if (execution.Order == buyOrder)
    {
    buyOrder = null;
    }
    else if (execution.Order == sellOrder)
    {
    sellOrder = null;
    }
    }

    protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice,int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
    {
    if (order.OrderState == OrderState.Rejected)
    {
    Log(string.Format("Order Rejected: {0}", order.ToString()), LogLevel.Error);
    }
    }
    }

    #2
    Hello RehmaZ,

    Thank you for your post and welcome to the NinjaTrader forum community!

    Although you mentioned there are 0 compile errors, please also check the Log tab of the Control Center for any errors when you run the backtest. If you are not seeing any trades taken, I suggest adding print statements to your script that include all of the values used in your conditions and any variables used in your script. You may then monitor the NinjaScript Output window and get additional information that helps you to better understand your script's behavior based on the print statements. Here is an example of a helpful print statement based on your entry conditions:
    Code:
    Print(String.Format("{0} MarketPosition: {1} Close[0]: {2} pivots.R1[0]: {3} pivots.S1[0]: {4}", Time[0], Position.MarketPosition, Close[0], pivots.R1[0], pivots.S1[0]));
    You can add a print similar to this both outside of your entry conditions and inside of your entry conditions so you can monitor the values used when evaluating the conditions. For more details on using prints to debug your scripts:


    On another note, it looks like you are trying to call SetStopLoss() after you already enter a position. The Set() methods should be called prior to the position being opened, as they will be triggered as soon as the entry is executed. For more information, please see the link below: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 fx.practic, 10-15-2013, 12:53 AM
    5 responses
    5,404 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by Shai Samuel, 07-02-2022, 02:46 PM
    4 responses
    95 views
    0 likes
    Last Post Bidder
    by Bidder
     
    Started by DJ888, Yesterday, 10:57 PM
    0 responses
    8 views
    0 likes
    Last Post DJ888
    by DJ888
     
    Started by MacDad, 02-25-2024, 11:48 PM
    7 responses
    159 views
    0 likes
    Last Post loganjarosz123  
    Started by Belfortbucks, Yesterday, 09:29 PM
    0 responses
    8 views
    0 likes
    Last Post Belfortbucks  
    Working...
    X