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

TradeCounter in custom strategy not working?

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

    TradeCounter in custom strategy not working?

    I'm trying to implement a trade counter in my custom strategy to serve 2 purposes:
    1.To make sure I take no more than 2 trades per current bar (of any bar type)
    2. To perform a different type/quantity on the 2nd trade executed on the current bar

    The initial strategy was created with Strategy Wizard and worked fine without my added code when I unlocked. All code typed in blue & italic is what I added to the code after unlocking it:


    namespace NinjaTrader.NinjaScript.Strategies
    {
    public class MyStrategy : Strategy
    {
    public int TradesTaken;

    protected override void OnStateChange()
    {
    if (State == State.SetDefaults)
    {
    Description = @"Enter the description for your new custom Strategy here.";
    Name = "MyStrategy";
    Calculate = Calculate.OnEachTick;
    EntriesPerDirection = 1;
    EntryHandling = EntryHandling.AllEntries;
    IsExitOnSessionCloseStrategy = false;
    ExitOnSessionCloseSeconds = 30;
    IsFillLimitOnTouch = true;
    MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
    OrderFillResolution = OrderFillResolution.High;
    OrderFillResolutionType = BarsPeriodType.Tick;
    OrderFillResolutionValue = 1;
    Slippage = 0;
    StartBehavior = StartBehavior.ImmediatelySubmit;
    TimeInForce = TimeInForce.Gtc;
    TraceOrders = false;
    RealtimeErrorHandling = RealtimeErrorHandling.IgnoreAllErrors;
    StopTargetHandling = StopTargetHandling.PerEntryExecution;
    BarsRequiredToTrade = 0;
    // Disable this property for performance gains in Strategy Analyzer optimizations
    // See the Help Guide for additional information
    IsInstantiatedOnEachOptimizationIteration = true;
    }
    else if (State == State.Configure)
    {
    SetProfitTarget("", CalculationMode.Ticks, 31);

    }
    }

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

    if (CurrentBars[0] < 1)
    return;

    if (IsFirstTickOfBar)
    {
    TradesTaken = 0;
    }



    // Set 1
    if (Condition 1)
    && (Condition 2)
    && (Condition 3)
    && (TradesTaken == 0)
    {
    EnterLongLimit(Convert.ToInt32(DefaultQuantity), (Close[1] + (45 * TickSize)) , "");
    TradesTaken++;
    }

    // Set 2
    if (Condition 1)
    && (Condition 2)
    && (Condition 3)
    && (TradesTaken == 0)
    {
    EnterShortLimit(Convert.ToInt32(DefaultQuantity), (Close[1] + (-30 * TickSize)) , "");
    TradesTaken++;
    }

    // Set 3
    if (Condition 1)
    && (Condition 2)
    && (Condition 3)
    && (Condition 4)
    && (TradesTaken == 1)
    {
    ExitShort(Convert.ToInt32(DefaultQuantity), "", "");
    EnterLongLimit((Convert.ToInt32(DefaultQuantity * 3) ), (Close[1] + (45 * TickSize)) , "");
    TradesTaken++;
    }

    // Set 4
    if (Condition 1)
    && (Condition 2)
    && (Condition 3)
    && (Condition 4)
    && (TradesTaken == 1)

    {
    ExitLong(Convert.ToInt32(DefaultQuantity), "", "");
    EnterShortLimit((Convert.ToInt32(DefaultQuantity * 3)), (Close[1] + (-30 * TickSize)) , "");
    TradesTaken++;
    }



    I've tried replacing "public int" with "private int", I've tried assigning "public/private int TradesTaken = 0", I've tried defining a "TradesTaken" field and properties
    I tried "TradesTaken += 1" and "++TradesTaken"

    When I remove the parts of code I added, it will perform set 1 and 2 (more than twice of course). I don't bother testing for sets 3 & 4 because they're useless without having the ability of tracking how many trades I've taken.

    Any help would be greatly appreciated.
    Thank you.
    Last edited by daghost_88; 04-07-2021, 07:15 PM.

    #2
    Hello daghost_88,

    Thanks for the post.

    Can you confirm what is not working? Are you saying that the sets 1 and 2 don't happen at all with the code you added?

    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello daghost_88,

      Thanks for the post.

      Can you confirm what is not working? Are you saying that the sets 1 and 2 don't happen at all with the code you added?

      I look forward to being of further assistance.
      You are correct. I "think" I have the script working now. I kept all my code in it's current place and syntax, but I added "TradesTaken = 0;" to the encapsulated segment following "else if (State == State.Configure)......" which is part of the protected override void OnStateChange() method. Do you know why that made the script finally recognize my variable? Im new to coding so I don't know much about declaring and assigning variables.

      Thank you

      Comment


        #4
        Hello daghost_88,

        I don't see anything in what you provided that should have caused that, int defaults to 0 as a C# type default. Its possible that one of the conditions was equating true before the BarsRequiredToTrade which would present as no order submitted but your variable would be incremented.

        The best way to review the case would be to use a Print before the conditions to see what the value is on each bar, you could do the following to chase down the exact cause if you wanted to:

        Code:
        if (CurrentBars[0] < 1)
        return;
        
        if (IsFirstTickOfBar)
        {
        TradesTaken = 0;
        }
        
        Print(Time[0] + " " + TradesTaken);

        I look forward to being of further assistance.
        JesseNinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by mattbsea, Today, 05:44 PM
        0 responses
        4 views
        0 likes
        Last Post mattbsea  
        Started by RideMe, 04-07-2024, 04:54 PM
        6 responses
        31 views
        0 likes
        Last Post RideMe
        by RideMe
         
        Started by tkaboris, Today, 05:13 PM
        0 responses
        2 views
        0 likes
        Last Post tkaboris  
        Started by GussJ, 03-04-2020, 03:11 PM
        16 responses
        3,282 views
        0 likes
        Last Post Leafcutter  
        Started by WHICKED, Today, 12:45 PM
        2 responses
        20 views
        0 likes
        Last Post WHICKED
        by WHICKED
         
        Working...
        X