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

Proper way to always be in the market (Long and Short)

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

    Proper way to always be in the market (Long and Short)

    I can't seem to get a strategy to always be in the market - Long and Short.

    If the Long position closes, a new short position should open and vice versa. This works fine for the Sim101 account but it does not work my Forex.com account and the strategy always goes out of sync.

    What could I be doing wrong? Sample code of strategy below:

    #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 USDCAD15min20BarHighShort : Strategy
    {



    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "USDCAD15min20BarHighShort";
    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;
    length =20;

    }
    else if (State == State.Configure)
    {
    }
    else if (State == State.DataLoaded)
    {

    }
    }

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

    if (CurrentBars[0] < 1)
    return;



    // Set 1
    if (Close[0] > MAX(High, length)[1])
    {
    EnterShort();
    }

    // Set 2
    if (Close[0] < MIN(Low, length)[1])
    {
    EnterLong();
    }

    }

    #region Properties
    [NinjaScriptProperty]
    [Range(1, int.MaxValue)]
    [Display(Name="length", Order=1, GroupName="Parameters")]
    public int length
    { get; set; }
    #endregion
    }
    }







    #2
    Hello omermirza,

    Thank you for your note.

    Do you receive an error message either on screen or in the log tab of the Control Center when this occurs? If so, what is the full text of the error you receive?

    Does the strategy shut down when this occurs, or does it continue to process?

    We'll need to take a look at your log and trace files to see what may be happening. Rather than have you post those here, since they may contain personally identifiable information, please send these directly through the platform.

    You can do this by going to the Control Center-> Help-> Email Support

    Ensuring 'Log and Trace Files' is checked will include these files. This is checked by default.

    Please reference the following ticket number in the body of the email: 3443726 ATTN Kate W.

    Thanks in advance; I look forward to assisting you further.
    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hi Kate,

      No error message and the strategy keeps running.

      I haven't run this strategy in a long time for this reason so I doubt the logs will show anything.

      Is there any problem in my code?

      Thanks,

      -Omer

      Comment


        #4
        Hello omermirza,

        Thank you for your reply.

        There is a known issue in which Forex.com connections may have issues reversing positions. We've added some code to help with this in NinjaTrader, but in order to diagnose if this may be the issue I would need to see an example of your logs from a time when this occurred.

        The only potential issue I see in the code itself is not specifying the forex lot size when submitting orders - generally with strategies for use with forex you'd either want to specify that you're going to use a specific position size (like 1000 for a micro lot, 10000 for a mini lot or 100000 for a standard lot) or using DefaultQuantity for the position size as then you could specify the lot quantity (or a multiple thereof) when you run the strategy.

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Ok thanks - I usually specify the position size when I enable the strategy.

          I'll try coding it into the strategy and test that out. If the issues persists, I will provide the logs.

          Thanks,

          -Omer

          Comment

          Latest Posts

          Collapse

          Topics Statistics Last Post
          Started by Barry Milan, Yesterday, 10:35 PM
          5 responses
          16 views
          0 likes
          Last Post NinjaTrader_Manfred  
          Started by DanielSanMartin, Yesterday, 02:37 PM
          2 responses
          13 views
          0 likes
          Last Post DanielSanMartin  
          Started by DJ888, 04-16-2024, 06:09 PM
          4 responses
          13 views
          0 likes
          Last Post DJ888
          by DJ888
           
          Started by terofs, Today, 04:18 PM
          0 responses
          12 views
          0 likes
          Last Post terofs
          by terofs
           
          Started by nandhumca, Today, 03:41 PM
          0 responses
          8 views
          0 likes
          Last Post nandhumca  
          Working...
          X