Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Error with MBTrading Running this ScaleIn Algo

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

    Error with MBTrading Running this ScaleIn Algo

    I just recently starting using ninja a few weeks ago and took it live last week to test on a demo account at MBTrading using their live demo servers. Things were going peachy until I started to get some errors regarding positions not being able to be closed out and orders not being able to be canceled and also orders being under the "minimum" size limit when they certainly are not. If there is anyone else using a do{}, for(), or while() loop to place scale orders for FOREX please respond to this post. I am also going to past in the code that i wrote for the strategy here too in case anyone wants to see how i structured the loop. I included the stuff I fiiddled around with and commented out as well.. only look at what is not commented to see what is actually being submitted. The only issue that I think I might have had is that I was running the previous release before this most recent one.

    I am now running version 7.0.1000.2

    #region Using declarations
    using System;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Xml.Serialization;
    using NinjaTrader.Cbi;
    using NinjaTrader.Data;
    using NinjaTrader.Indicator;
    using NinjaTrader.Strategy;
    #endregion

    // This namespace holds all strategies and is required. Do not change it.
    namespace NinjaTrader.Strategy
    {
    /// <summary>
    /// Reference sample demonstrating how to plot from within a strategy
    /// </summary>
    [Description("Reference sample demonstrating how to plot from within a strategy")]
    public class ScaleInOrderLOOPwCANCELS : Strategy
    {
    #region Variables
    private int Sma = 20;
    private double LongLevel = .0010;
    private double ShortLevel = -.0010;
    private int ordercount = 5;
    private int takeprofit = 100;
    private int stoploss = 100;
    #endregion

    /// <summary>
    /// This method is used to configure the strategy and is called once before any strategy method is called.
    /// </summary>
    protected override void Initialize()
    {
    CalculateOnBarClose = true;

    // Create a multi-time frame strategy
    // Add(PeriodType.Minute, 3);

    /* Add our blank placeholder indicators. The parameter we pass in is used to distinguish the two
    indicators from each other. */
    // Add(StrategyPlot(0));
    // Add(StrategyPlot(1));

    Add(StrategyPlot(2));
    Add(StrategyPlot(3));
    Add(StrategyPlot(4));

    int orderID = 1;

    Add(SMA(Close, Sma));
    Add(DistancefromSMA(Sma));

    // Set the color for the indicator plots
    // StrategyPlot(0).Plots[0].Pen.Color = Color.Blue;
    // StrategyPlot(1).Plots[0].Pen.Color = Color.YellowGreen;

    StrategyPlot(2).Plots[0].Pen.Color = Color.DarkGreen;
    StrategyPlot(3).Plots[0].Pen.Color = Color.DarkRed;
    StrategyPlot(4).Plots[0].Pen.Color = Color.DarkCyan;

    // Set the panel which the plots will be placed on. 1 = price panel, 2 = panel under the price panel, etc.
    // StrategyPlot(0).PanelUI = 1;
    // StrategyPlot(1).PanelUI = 1;

    StrategyPlot(2).PanelUI = 2;
    StrategyPlot(3).PanelUI = 3;
    StrategyPlot(4).PanelUI = 4;

    // For more options please see the forum tip titled "Adding Indicators to Strategies"
    // http://www.ninjatrader-support.com/v...ead.php?t=3228
    }

    /// <summary>
    /// Called on each bar update event (incoming tick)
    /// </summary>
    protected override void OnBarUpdate()
    {
    /* Set the values of our indicators. One of them is set to the High of the primary bar series while the
    other is set to the High of the secondary bar series. This effectively creates a multi-time frame plot. */
    // StrategyPlot(0).Value.Set(High[0]);
    // StrategyPlot(1).Value.Set(Highs[1][0]);

    double profitloss = Position.GetProfitLoss(Close[0], PerformanceUnit.Currency);
    double realized = Performance.AllTrades.TradesPerformance.Currency.C umProfit;
    int position = Position.Quantity / 1000;

    StrategyPlot(2).Value.Set(profitloss);
    StrategyPlot(3).Value.Set(realized);
    StrategyPlot(4).Value.Set(position);

    double distance = Close[0] - SMA(Close, Sma)[0];
    double scale = 10*TickSize;
    int orderID = 1;



    if (Close[0] > SMA(Close, Sma)[0])
    // (CrossAbove(Close, SMA(Close, Sma), 5))
    // &&
    // Open[3] > Close[3] && Open [2] > Close[2]
    // && Open[1] > Close[1] && Open[0] > Close[0])
    {
    distance = Close[0] - SMA(Close, Sma)[0];
    scale = 10*TickSize;
    orderID = 1;
    IOrder myorder = null;

    while(scale <= distance)
    {
    EnterLongLimit(1000,SMA(Close, Sma)[0] + distance, "OrderID " + orderID.ToString());
    distance = distance - scale;
    orderID++;
    }
    SetProfitTarget(CalculationMode.Ticks, 80);
    SetStopLoss(CalculationMode.Price, SMA(Close, Sma)[0] - 40*TickSize);
    }

    // if (Close[0] < SMA(Close, Sma)[0] && Position.MarketPosition == MarketPosition.Long)
    // {
    // ExitLong();
    // }

    //Start of Short Algorithm
    if (Close[0] < SMA(Close, Sma)[0])
    // (CrossAbove(Close, SMA(Close, Sma), 5))
    // &&
    // Open[3] > Close[3] && Open [2] > Close[2]
    // && Open[1] > Close[1] && Open[0] > Close[0])
    {
    distance = SMA(Close, Sma)[0] - Close[0];
    scale = 10*TickSize;
    orderID = 100;

    while(scale <= distance)
    {
    EnterShortLimit(1000,SMA(Close, Sma)[0] - distance, "OrderID " + orderID.ToString());
    distance = distance - scale;
    orderID++;
    }
    SetProfitTarget(CalculationMode.Ticks, 80);
    SetStopLoss(CalculationMode.Price, SMA(Close, Sma)[0] + 40*TickSize);
    }
    // if (Close[0] > SMA(Close, Sma)[0] && Position.MarketPosition == MarketPosition.Short)
    // {
    // ExitShort();
    // }
    //
    // orderID = 1;
    // while(orderID <= 100)
    // {
    // SetProfitTarget("OrderID" + orderID, CalculationMode.Ticks, 10);
    // orderID++;
    // }

    // if (SMA(Close, Sma)[0] < Close[0] && Open[0] < Close[0] && Position.MarketPosition == MarketPosition.Long && Position.Quantity >= 5000)
    // {
    // orderID = 1;
    // scale = 10*TickSize;
    // distance = Close[0] - SMA(Close, Sma)[0];
    // while(scale <= distance)
    // {
    // ExitLongLimit(0, true, 1000,SMA(Close, Sma)[0] + distance + 10*TickSize, "ScaleOut", "OrderID" + orderID.ToString());
    // ExitLongLimit(SMA(Close, Sma)[0] + distance + 10*TickSize, "OrderID" + orderID.ToString());
    // orderID++;
    // }
    // }


    }

    #region Properties
    #endregion
    }
    }

    #2
    forexlevel, thanks for sharing your script work here - for the error message on NT7 R2 you saw: can you please contact us via Help > Mail to Support to send over the diagnostic files for review so we can assist?

    Thanks,
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I am not familiar with Forex trading, but in equity trading, You cannot have SetProfitTarget and SetStopLoss together at the same time. If you do then the errors you have described occur. The problem is that two limit orders on the same instrument are not allowed by FINRA.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by judysamnt7, 03-13-2023, 09:11 AM
      4 responses
      57 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
      19 views
      0 likes
      Last Post helpwanted  
      Started by cre8able, Today, 07:24 PM
      0 responses
      9 views
      0 likes
      Last Post cre8able  
      Working...
      X