Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Entering Limit order and Cancel after 1 Bar

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

    Entering Limit order and Cancel after 1 Bar

    Hi NinjaTrader Staff

    First, I am already struggeling to Enter a limit order. For some reason it does not submit it.

    Second problem is canceling the order if it is not filled after 1 Bar, therefore I am coding everything in the section OnBarUpdate().

    Already apologizing in advance, some stuff might be irrelevant.

    #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 GOD : Strategy
    {
    private EMA EMA1;
    private EMA EMA50;
    private SMA SMA1;
    private int periodEMA;
    private int periodSMA;
    private double constant1;
    private double constant2;
    private double selflongconstant1;
    private double selflongconstant2;
    private double priorSum;
    private double sum;
    private double Price;
    private double selfSMA;
    private double selfEMA;
    private int selflongperiodEMA;
    private double selflongEMA;
    private double ConvergencePrice;
    private Order ConvergenceEntryPrice = null;


    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "GOD";
    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;
    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {
    EMA1 = EMA(Close, 11);
    SMA1 = SMA(Close, 14);
    EMA50 = EMA(50);
    EMA1.Plots[0].Brush = Brushes.Turquoise;
    SMA1.Plots[0].Brush = Brushes.Snow;
    AddChartIndicator(EMA1);
    AddChartIndicator(SMA1);
    periodEMA = 11;
    periodSMA = 14;
    selflongperiodEMA = 200;
    constant1 = 2.0 / (1 + periodEMA);
    constant2 = 1 - (2.0 / (1 + periodEMA));
    selflongconstant1 = 2.0 / (1 + selflongperiodEMA);
    selflongconstant2 = 1 - (2.0 / (1 + selflongperiodEMA));
    priorSum = 0;
    sum = 0;
    Price = 0;
    selfSMA = 0;
    selfEMA = 0;
    selflongEMA = 0;
    ConvergencePrice = 0;
    }
    }

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

    if (CurrentBars[0] < 20) return;


    CancelOrder(ConvergenceEntryPrice);

    // SMA
    {
    if (BarsArray[0].BarsType.IsRemoveLastBarSupported)
    {
    if (CurrentBar == 0)
    Value[0] = Input[0];
    else
    {
    double last = Value[1] * Math.Min(CurrentBar, periodSMA);

    if (CurrentBar >= periodSMA)
    Value[0] = (last + Input[0] - Input[periodSMA]) / Math.Min(CurrentBar, periodSMA);
    else
    Value[0] = ((last + Input[0]) / (Math.Min(CurrentBar, periodSMA) + 1));
    }
    }
    else
    {

    if (IsFirstTickOfBar)
    priorSum = sum;


    if (priorSum == 0)
    {
    sum = Input[0] + Input[1] + Input[2] + Input[3] + Input[4] + Input[5] + Input[6] + Input[7] + Input[8] + Input[9] + Input[10] + Input[11] + Input[12] + Input[13];
    }
    else
    {
    sum = priorSum + Input[0] - Input[14];
    selfSMA = sum / periodSMA;
    // SMA = sum - Input[13] + Price / periodSMA;
    selfEMA = Input[0]*constant1 + EMA1[1]*constant2;
    // EMA = Price*constant1 + selfEMA * constant2;
    Price = (sum - Input[13] - selfEMA * constant2 * periodSMA)/(constant1*periodSMA-1);
    //Print(selfEMA);
    //Print(selfSMA);
    Print("The Cross Price is");
    Print(Price);
    selflongEMA = Input[0]*selflongconstant1 + EMA50[0]*selflongconstant2;
    // EMA = ConvergencePrice * constant1 + selflongEMA * constant2
    ConvergencePrice = (selflongEMA * selflongconstant2) / (1 - selflongconstant1);
    Print(EMA50[0]);
    Print("The Convergence Price is");
    Print(ConvergencePrice);
    if (EMA50[0] > Input[0])
    {
    EnterShortLimit(3, ConvergencePrice, "ConvergenceEntryPrice");
    Print("hello");
    }
    if (EMA50[0] < Input[0])
    {
    EnterLongLimit(3, ConvergencePrice, "ConvergenceEntryPrice");
    }
    }



    }
    }
    }
    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.Name == "ConvergenceEntryPrice")
    {
    ConvergenceEntryPrice = order;
    }

    Print(orderState);




    }



    #2
    Hello Slaywer,

    Thanks for your post.

    In order to determine why a strategy has or has not taken an action, debugging steps will need to be taken. This involves creating a testable context to reproduce the issue, and then to test the strategy with debugging prints added to monitor your conditions so you can see why they became true or did not become true.

    In order to keep orders alive, they must be called with each new bar update, or you can use the IsLiveUntilCancelled overloads.

    Keeping Orders Alive example - https://ninjatrader.com/support/help...ders_alive.htm

    Order methods - https://ninjatrader.com/support/help...er_methods.htm

    Debugging Tips

    In order to better understand how the code is executing, I recommend adding debugging prints to your strategy. By adding prints for the values used to evaluate your conditions in outside of those conditions, you can observe if logic is becoming true and allowing your order submission methods to fire.

    If you are seeing your order submission logic being reached, but it is not executing, there may be an issue where the strategy is hitting an internal rule that is not allowing you to re-enter. TraceOrders can be enabled in the strategy (set in State.SetDefaults or in the Strategy Builder under Default Properties) so it prints its order feedback, and you may also observe the log tab of the Control Center for additional hints.

    Some items to check when you are taking debugging steps:
    1. Are your conditions allowing your order submission methods to be reached?
    2. Is the strategy currently in a position that is preventing it from submitting another entry order?
    3. Do you see any errors in the log tab of the Control Center or does Trace Orders give you any feedback?
    I've included some tips for debugging below. The Playback Connection can be used to replay data as if it were realtime.

    Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

    TraceOrders - https://ninjatrader.com/support/help...aceorders2.htm

    Debugging in the Strategy Builder - https://drive.google.com/file/d/1mTq...w?usp=drivesdk

    Playback Connection - https://ninjatrader.com/support/help...connection.htm

    Debugging Demo - https://drive.google.com/file/d/1rOz...w?usp=drivesdk

    We look forward to assisting.
    JimNinjaTrader Customer Service

    Comment

    Latest Posts

    Collapse

    Topics Statistics Last Post
    Started by judysamnt7, 03-13-2023, 09:11 AM
    4 responses
    59 views
    0 likes
    Last Post DynamicTest  
    Started by ScottWalsh, Today, 06:52 PM
    4 responses
    36 views
    0 likes
    Last Post ScottWalsh  
    Started by olisav57, Today, 07:39 PM
    0 responses
    7 views
    0 likes
    Last Post olisav57  
    Started by trilliantrader, Today, 03:01 PM
    2 responses
    22 views
    0 likes
    Last Post helpwanted  
    Started by cre8able, Today, 07:24 PM
    0 responses
    10 views
    0 likes
    Last Post cre8able  
    Working...
    X