Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Getting order duplicates when I shouldn't be

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

    Getting order duplicates when I shouldn't be

    Re-Edit : I can't solve this issue for some reason I thought I had it fixed before but I'm still getting duplicates when I shouldn't be.


    Code producing errors is attached, As well as the chart with orders shown.

    I didn't have this issue before and should not even be able to have this issue because I have an Int that is set to the current bar number whenever an order is submitted. These orders also accumulate until the bar is closed & all of these scenarios are triggered on the OnPriceChange() event.

    I'm very confused how this can occur considering I check if the current bar has already been traded before submitting any orders & then set it to the current bar if a trade is made.
    Relevant code is "2-2D" and "2-2U"

    P.S I know there is a lot of redundant code and return statements, I've been trying to get this kink out for hours.

    Code:
    #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
    
    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.Data;
    using NinjaTrader.NinjaScript;
    using NinjaTrader.Core.FloatingPoint;
    using NinjaTrader.NinjaScript.Indicators;
    using NinjaTrader.NinjaScript.DrawingTools;
    
    namespace NinjaTrader.NinjaScript.Strategies
    
    {
    public class TheStrat : Strategy
    {
    private Boolean FTFC = false;
    private int tradeSize = 1;
    private Boolean inPositionLong = false;
    private int tradedBar = 0;
    private int tradedBarGoThree = 0;
    private ATR atr;
    private double defaultTp = 0.75;
    private Boolean inLongonetwo = false;
    private Boolean inLongtwotwo = false;
    private Boolean inLongthree = false;
    private Boolean inLongthreetwo = false;
    private Boolean inShortonetwo = false;
    private Boolean inShorttwotwo = false;
    private Boolean inShortthree = false;
    private Boolean inShortthreetwo = false;
    private VOLMA volumeMa;
    /*####################################*/
    
    private Boolean oneTwoSetup = false;
    
    
    
    
    protected override void OnStateChange()
    {
    
    if (State == State.SetDefaults)
    {
    Description = "Multi-timeframe analysis with actionable signals automatically traded.";
    Name = "TheStrat";
    Calculate = Calculate.OnPriceChange;
    // This strategy has been designed to take advantage of performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    }
    else if (State == State.Configure)
    {
    AddDataSeries(Data.BarsPeriodType.Minute,15);
    AddDataSeries(Data.BarsPeriodType.Minute,60);
    AddDataSeries(Data.BarsPeriodType.Day, 1);
    //AddDataSeries(Data.BarsPeriodType.Week,1);
    
    }
    else if (State == State.DataLoaded)
    {
    tradedBar = 0;
    volumeMa = VOLMA(14);
    AddChartIndicator(volumeMa);
    }
    
    
    }
    
    
    public Boolean isInside(int bar)
    {
    if(High[bar] <= High[bar+1])
    {
    if(Low[bar] >= Low[bar+1])
    {
    return true;
    
    }
    
    
    }
    
    return false;
    
    
    }
    public Boolean twoDown(int bar)
    {
    
    if(High[bar] < High[bar+1])
    {
    if(Low[bar] < Low[bar+1])
    {
    return true;
    }
    return false;
    }
    return false;
    
    }
    
    public Boolean twoUp(int bar)
    {
    if(High[bar] > High[bar+1])
    {
    if(Low[bar] > Low[bar+1])
    {
    
    
    return true;
    
    
    }
    return false;
    }
    return false;
    
    }
    
    public Boolean threeDown(int bar)
    {
    if(High[bar] > High[bar+1])
    {
    if(Low[bar] < Low[bar+1])
    {
    if(Open[bar] > Close[bar])
    {
    return true;
    }
    }
    
    }
    return false;
    
    }
    
    
    
    public Boolean threeUp(int bar)
    {
    if(High[bar] > High[bar+1])
    {
    if(Low[bar] < Low[bar+1])
    {
    if(Open[bar] < Close[bar])
    {
    return true;
    }
    }
    }
    return false;
    }
    
    public Boolean volumeCheck()
    {
    if(volumeMa[1] >= 15000)
    {
    return true;
    }
    return false;
    }
    
    public Boolean ftfcUp()
    {
    
    return false;
    }
    public Boolean ftfcDown()
    {
    return true;
    }
    
    
    protected override void OnBarUpdate()
    {
    
    
    if (CurrentBar < BarsRequiredToTrade) {
    return;
    }
    if(!volumeCheck())
    {
    if(inLongonetwo)
    {
    ExitLong("1-2U");
    inLongonetwo = false;
    
    }
    if(inLongtwotwo)
    {
    ExitLong("2-2U");
    inLongtwotwo = false;
    }
    if(inLongthree)
    {
    ExitLong("3U");
    inLongthree = false;
    }
    if(inLongthreetwo)
    {
    ExitLong("3-2U");
    inLongthreetwo = false;
    }
    
    if(inShortonetwo)
    {
    ExitShort("1-2D");
    inShortonetwo = false;
    }
    if(inShorttwotwo)
    {
    ExitShort("2-2D");
    inShorttwotwo = false;
    }
    if(inShortthree)
    {
    ExitShort("3D");
    inShortthree = false;
    }
    if(inShortthreetwo)
    {
    ExitShort("3-2D");
    inShortthreetwo = false;
    }
    return;
    
    
    }
    
    
    /*Long Trades*/
    
    if(isInside(1))
    {
    if(High[0] > High[1])
    {
    if(Low[0] > Low[1])
    {
    if(tradedBar != CurrentBar)
    {
    
    tradedBar = CurrentBar;
    EnterLong(tradeSize, "1-2U");
    inLongonetwo = true;
    
    
    
    }
    }
    }
    }
    if(twoDown(1))
    {
    if(twoUp(0))
    {
    if(tradedBar != CurrentBar)
    {
    
    tradedBar = CurrentBar;
    EnterLong(tradeSize, "2-2U");
    inLongtwotwo = true;
    }
    }
    }
    if(threeUp(0))
    {
    if(tradedBarGoThree != CurrentBar)
    {
    
    tradedBarGoThree = CurrentBar;
    tradedBar = CurrentBar;
    EnterLong(tradeSize, "3U");
    inLongthree = true;
    
    
    }
    
    }
    if(threeUp(1))
    {
    if(twoUp(0))
    {
    if(tradedBar != CurrentBar)
    {
    
    tradedBar = CurrentBar;
    EnterLong(tradeSize, "3-2U");
    inLongthreetwo = true;
    }
    }
    }
    
    /*Short Trades*/
    if(isInside(1))
    {
    if(twoDown(0))
    {
    if(High[0] > High[1]) { return;}
    
    if(tradedBar != CurrentBar)
    {
    
    tradedBar = CurrentBar;
    EnterShort(tradeSize, "1-2D");
    inShortonetwo = true;
    }
    
    }
    
    }
    if(twoUp(1))
    {
    if(twoDown(0))
    {
    if(tradedBar != CurrentBar)
    {
    
    tradedBar = CurrentBar;
    
    EnterShort(tradeSize, "2-2D");
    inShorttwotwo = true;
    }
    }
    }
    
    if(threeDown(0))
    {
    if(tradedBarGoThree != CurrentBar)
    {
    
    tradedBarGoThree = CurrentBar;
    tradedBar = CurrentBar;
    
    if(inShortthree == true) { return; }
    EnterShort(tradeSize, "3D");
    inShortthree = true;
    
    }
    }
    
    if(threeDown(1))
    {
    if(twoDown(0))
    {
    if(tradedBar != CurrentBar)
    {
    
    tradedBar = CurrentBar;
    EnterShort(tradeSize, "3-2D");
    inShortthreetwo = true;
    }
    }
    
    }
    
    if(IsFirstTickOfBar)
    {
    
    }
    }
    }
    }
    Attached Files
    Last edited by AlexsOptions; 02-25-2021, 09:30 PM. Reason: Thought I fixed it but I didn't

    #2
    Are you running with Calculate set to OnEachTick?

    Comment


      #3
      Originally posted by bltdavid View Post
      Are you running with Calculate set to OnEachTick?
      No, I'm using OnPriceChange because on each tick was not needed and slowed it down quite a bit.

      Comment


        #4
        Hello AlexsOptions,

        Thanks for your post.

        You have a multi-time frame strategy and that will require additional coding to best handle. Each data series will call the OnBarUpdate() on each price change that occurs and your code needs to know which data series to work with. Please review the help guide here: https://ninjatrader.com/support/help...nstruments.htm
        Paul H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by Newant, Today, 08:13 AM
        1 response
        16 views
        0 likes
        Last Post NinjaTrader_Kate  
        Started by alejandro888, Yesterday, 10:14 PM
        1 response
        11 views
        0 likes
        Last Post NinjaTrader_ChrisL  
        Started by devro, Yesterday, 07:00 PM
        1 response
        7 views
        0 likes
        Last Post NinjaTrader_BrandonH  
        Started by mr_trade, Today, 07:13 AM
        0 responses
        16 views
        0 likes
        Last Post mr_trade  
        Started by gchan, Yesterday, 05:48 PM
        1 response
        12 views
        0 likes
        Last Post NinjaTrader_PaulH  
        Working...
        X