Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Why is my strategy not placing order

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

    Why is my strategy not placing order

    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class StochRSIStrategy : Strategy
    {
    private StochRSI StochRSI1;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Buy and Sell based on Stochastic RSI";
    Name = "StochRSIStrategy";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = false;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.Standard;
    Slippage = 0;
    StartBehavior = StartBehavior.WaitUntilFlat;
    TimeInForce = TimeInForce.Day;
    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;
    Buy_Stoch_RSI = 0.5;
    Sell_Stoch_RSI = 0.8;
    }
    else if (State == State.Configure)
    {
    AddDataSeries("ES 12-22", Data.BarsPeriodType.Minute, 3, Data.MarketDataType.Last);
    }
    else if (State == State.DataLoaded)
    {
    StochRSI1 = StochRSI(Closes[1], 14);
    SetProfitTarget(@"4", CalculationMode.Ticks, 0);
    SetStopLoss(@"12", CalculationMode.Ticks, 0, false);
    SetTrailStop(@"2", CalculationMode.Ticks, 0, false);
    }
    }

    protected override void OnBarUpdate()
    {
    if (BarsInProgress != 0)
    return;

    if (CurrentBars[0] < 1
    || CurrentBars[1] < 1)
    return;

    // Set 1
    if (CrossAbove(StochRSI1, Buy_Stoch_RSI, 1))
    {
    EnterLong(3, @"");
    }

    // Set 2
    if (CrossBelow(StochRSI1, Sell_Stoch_RSI, 1))
    {
    EnterShort(3, "");
    }

    }

    region Properties
    [NinjaScriptProperty]
    [Range(0.5, double.MaxValue)]
    [Display(Name="Buy_Stoch_RSI", Order=1, GroupName="Parameters")]
    public double Buy_Stoch_RSI
    { get; set; }

    [NinjaScriptProperty]
    [Range(0.8, double.MaxValue)]
    [Display(Name="Sell_Stoch_RSI", Order=2, GroupName="Parameters")]
    public double Sell_Stoch_RSI
    { get; set; }
    #endregion

    }
    }​

    #2
    All I want is if Stochastic RSI crosses above, it should place order with 4 ticks profit and 12 ticks stop loss with trail stop of 2 ticks. I am new the Ninjatrader. Pls help.

    Comment


      #3
      I want to go short if it crosses below 0.8 in the similar way. So crosses above 0.5 long and crosses below 0.8 short. Thanks for the help!

      Comment


        #4
        Hi abshaik, thanks for posting and welcome. The first issue I'm seeing is the incorrect use of the fromEntrySignal. Your entry orders do not have signal names, and the fromEntrySignal in SetStopLoss/SetProfitTarget is a number. The stop loss value is also 0, if you want to set it at 12 ticks set that to 12 To target any entry, keep the signal and fromEntrySignal names blank. E.g.

        SetProfitTargetCalculationMode.Ticks, 12); //this will be applied to any entry with no signal name.
        and
        EnterLong(3);

        Another way to debug and test your strategy is to add Print() within the entry singal to see if its being reached:

        if (CrossAbove(StochRSI1, Buy_Stoch_RSI, 1))
        {
        Print("Long Condition True " + Time[0]); //helps to see the data coming into the strategy.
        EnterLong(3, @"");
        }​


        Kind regards,
        -ChrisL
        Chris L.NinjaTrader Customer Service

        Comment


          #5
          Hi, I don't know the coding so I did this with the strategy builder. Can you please generate the code if possible for my condition and post it here please? I need to enter 3 long positions when stochastic RSI crosses above 0.5 and have the profit target of 4 ticks and stop loss of 12 ticks. If possible 2 ticks of trail stop. Similarly enter into short position when stochastic crosses below 0.8 with same profit, stop loss and trail stops. Really appreciate your help.

          Comment


            #6
            I am getting this error when modified ..
            Click image for larger version

Name:	image.png
Views:	74
Size:	53.2 KB
ID:	1216762

            Comment


              #7
              Hi abshaik, thanks for your reply. There are missing parenthesis in the code and the compiler needs exactly correct syntax to work. My ability to help one-on-one with fixing syntax will be limited to this post, so I recommend first getting comfortable with C# programming and getting the syntax correct (e.g. how to create and use a variable, fully understand what a function/method is in C#, how to call a function/method, and understand classes and members of classes)

              The SetStopLoss/SetProfitTarget methods should have an opening and closing parentheses:

              SetProfitTarget(CalculationMode.Ticks, 4);
              SetStopLoss(CalculationMode.Ticks, 12);

              We also have many pre-made reference samples, and some examples on using the condition builder:





              Kind regards,
              -ChrisL

              Chris L.NinjaTrader Customer Service

              Comment

              Latest Posts

              Collapse

              Topics Statistics Last Post
              Started by PaulMohn, Today, 05:00 AM
              0 responses
              5 views
              0 likes
              Last Post PaulMohn  
              Started by ZenCortexAuCost, Today, 04:24 AM
              0 responses
              5 views
              0 likes
              Last Post ZenCortexAuCost  
              Started by ZenCortexAuCost, Today, 04:22 AM
              0 responses
              2 views
              0 likes
              Last Post ZenCortexAuCost  
              Started by SantoshXX, Today, 03:09 AM
              0 responses
              16 views
              0 likes
              Last Post SantoshXX  
              Started by DanielTynera, Today, 01:14 AM
              0 responses
              3 views
              0 likes
              Last Post DanielTynera  
              Working...
              X