• If this is your first visit, you will have to register before you can post. To view messages, please scroll below and select the forum that you would like to visits. Questions? Be sure to check out the Forum FAQ.

Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Daily loss limit examples

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

  • NinjaTrader_Jim
    replied
    Hello Trader17,

    I noticed that you also have a ticket open regarding this item with my colleague, Kate, over email. We ask that you continue working with the current technician assigned to your ticket without opening a new ticket. This helps us to ensure that the same technicians are not working on the same items which improves our ability to serve all of our clients.

    Thanks. I was going to leave Line 53 as is with Calculate set to Each Tick and just use Is First Tick of Bar in the condition logic to restrict it from calculating entry logic on each tick. And Script will still recognize the first bar of session with calculate set to calculate on each tick, correct?
    We should be clear that our role is not to provide recommendation for modifying single lines of code. Our role is to provide educational information on how to use the software. As with learning any new language there will be a necessary level of testing to understand how to work with that language. This sort of hypothetical question would be best answered by testing with prints to see the behavior yourself.

    Or to reset the PnL only once while using Calculate on Each Tick if I use Is First Tick of Bar and First Bar of Session keeps it from resetting on every tick in the first session bar as you mentioned. So as long as a tick is traded in the new session it assumes it is the first bar of session without waiting for it to close and we are calculating on each tick it will reset the PnL to zero only once, correct?
    The first bar of a session is determined by the Trading Hours template. The first tick of that bar will have IsFirstTickOfBar == true and Bars.IsFirstBarOfSession == true. This is another item that would be best answered by testing with prints to observe the behavior.

    Now when this is used in a strategy it calculates the PnL on all trades for the day including historical trades. What if I want to start it for just a couple of hours anytim I wish and have the PnL start from zero at that point? Anything in Script that can prevent calculating historical trades for the day till I start it?
    You can have the strategy return from historical processing in OnBarUpdate with if (State == State.Historical) return;

    Please understand that while we are here to assist you on your journey, our support services are not equipped to handle large numbers of questions that involve confirming a concept, and many such items are best understood through testing. If you have any additional questions, please attach the debug prints you are using and the output received so we can offer further direction to help you test these items to better understand these concepts.

    We look forward to being of further assistance.

    Leave a comment:


  • Trader17
    replied
    Now when this is used in a strategy it calculates the PnL on all trades for the day including historical trades. What if I want to start it for just a couple of hours anytim I wish and have the PnL start from zero at that point? Anything in Script that can prevent calculating historical trades for the day till I start it?
    Thank you.

    Leave a comment:


  • Trader17
    replied
    Originally posted by NinjaTrader_Jim View Post
    Hello Trader17,

    If you add a check for IsFirstTickOfBar to the condition on line 53 in Chelsea's script and change the script to use Calculate.OnPriceChange or Calculate.OnEachTick, then you will restrict the code to only reset the daily PnL once, and not multiple times per bar.

    We look forward to being of further assistance.
    Thanks. I was going to leave Line 53 as is with Calculate set to Each Tick and just use Is First Tick of Bar in the condition logic to restrict it from calculating entry logic on each tick. And Script will still recognize the first bar of session with calculate set to calculate on each tick, correct?

    Or to reset the PnL only once while using Calculate on Each Tick if I use Is First Tick of Bar and First Bar of Session keeps it from resetting on every tick in the first session bar as you mentioned. So as long as a tick is traded in the new session it assumes it is the first bar of session without waiting for it to close and we are calculating on each tick it will reset the PnL to zero only once, correct?

    Thank you.
    Last edited by Trader17; 04-17-2019, 12:55 PM.

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Trader17,

    There will be countless questions in regards to what is possible with C#/NinjaScript and what is correct syntax. It will be necessary for you as an individual working on the script to test your questions to see if what you think you have written is correct or if it is not. For that reason, I will leave the question presented in #32 as an exercise to you. Please set up test conditions similar to my reply in which you have quoted. Testing this will be a straightforward answer to your question that you can set up on your end.

    Thank you. In this statement for Line 53 I would leave it as is because the PnL is reset each day on the first session bar, correct? And I add IsFirstTickOfBar in the logic itself to prevent calculation more than once for the last closed bar. Correct?
    If you add a check for IsFirstTickOfBar to the condition on line 53 in Chelsea's script and change the script to use Calculate.OnPriceChange or Calculate.OnEachTick, then you will restrict the code to only reset the daily PnL once, and not multiple times per bar.

    Many of these questions will be best answered by testing on your own. Our role is not to provide examples that perform multiple tasks so you can copy and use in your custom works, but to educate you on how each concept works. My recommendation would be to start with simple test scripts so you can get used to working with scripts that calculate OnEachTick or OnPriceChange, and then once you have mastered these concepts, combine the behavior with the Daily Loss example.

    We look forward to being of further assistance.

    Leave a comment:


  • Trader17
    replied
    Originally posted by NinjaTrader_Jim View Post
    Hello Trader17,

    Bools return a true or false value and checking if (YourBool == true) is equivalent to checking if(YourBool). It can be very helpful and will at times be necessary to test out the concept to observe the behavior. For example, you could create a couple conditions that print a message if the condition becomes true.

    if (IsFirstTickOfBar) Print("Condition 1 This is true"");

    if (IsFirstTickOfBar == true) Print("Condition 2 This is true");

    We look forward to being of further assistance.
    So are both correct for Ninja Script? I know == True is meaningless as it is what is returned upon checking the logic.
    Thank you.

    Leave a comment:


  • Trader17
    replied
    Originally posted by NinjaTrader_Jim View Post
    Hello Trader17,


    If you change the strategy to Calculate.OnEachTick or OnPriceChange, line 53 if (Bars.IsFirstBarOfSession) will be true for each tick of that bar. I would suggest adding an IsFirstTickOfBar check with that condition to ensure it only happens once on that bar.

    Please let us know if you have any questions.
    Thank you. In this statement for Line 53 I would leave it as is because the PnL is reset each day on the first session bar, correct? And I add IsFirstTickOfBar in the logic itself to prevent calculation more than once for the last closed bar. Correct?

    Thanks a lot.

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Trader17,

    Thanks for your patience.

    So in the Script by Chelsea I basically change it ti calculate on each tick or price change and both use the common term IsFirstTickOfBar? And while doing so now the bar currently forming will take "0" as its index and the bar that just closed will take "1' off which I need to calculate the strategy. Correct? As when we use Calculate on bar close then "0" is the one that just closed.
    Changing the script to Calculate.OnEachTick or Calculate.OnPriceChange will have the script's OnBarUpdate method iterate with each new tick or price change. BarsAgo 0 values for Price Series and plots (Close[0]) will update with each new iteration instead of having OnBarUpdate iterate once when the bar closes (Calculate.OnBarClose.) If you would like to have some code calculate on bar closes when using Calculate.OnEachTick or Calculate.OnPriceChange, you will want to use BarsAgo 1 references on the first tick of a new bar. See IsFirstTickOfBar linked in post #16.


    And in this code the first line that uses the Close[0] technically is not a close of the bar forming as I will use OnTickUpdate but just the term "close" is used, correct? How do you get the code in a block in the post? Am I supposed to copy and paste it and put it between Code and /Code?
    Close[0] represents the data point for the close of the bar. When calculating on each tick or on price changes, this value is updated with each new tick or price change. Once the bar closes, then the index will shift (I.E. Close[0] becomes Close[1].) There is no OnTickUpdate method. You can use [CODE] blocks similar to [QUOTE] blocks to post formatted code.


    Also using the same code I guess I can also put a limit on Daily Profit as GetUnrealizedProfitLoss returns a positive number for profit and negative number for loss. So I can add another input like ProfitLimit similar to LossLimit if I need to stop trading after achieving a set amount of profit for the day, correct?

    How do I make a duplicate of this strategy to play around with?
    Yes, you will have full control over the strategy logic and could model similar behavior for a daily profit behavior. You can open your strategy in the NinjaScript Editor, right click and select Save As to create a duplicate strategy.

    We look forward to being of further assistance.

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Trader17,

    I have seen your messages.

    I am approaching the end of my shift and I will be replying tomorrow during our regular office hours.

    Thanks for your patience.

    Leave a comment:


  • Trader17
    replied
    Also using the same code I guess I can also put a limit on Daily Profit as GetUnrealizedProfitLoss returns a positive number for profit and negative number for loss. So I can add another input like ProfitLimit similar to LossLimit if I need to stop trading after achieving a set amount of profit for the day, correct?

    How do I make a duplicate of this strategy to play around with?

    Thanks a lot.
    Last edited by Trader17; 04-15-2019, 12:33 PM.

    Leave a comment:


  • Trader17
    replied
    Originally posted by NinjaTrader_Jim View Post
    Hello Trader17,

    You can then set Calculate.OnEachTick or OnPriceChange and then place your trading logic in a check for IsFirstTickOfBar. BarsAgo 0 indexes should then be BarsAgo 1 indexes since you are referencing the bar that had just closed within that logic.

    The block of code checking daily PnL would not have to be inside any logic for IsFirstTickOfBar and can still use BarsAgo 0 references because it is calculating on each tick or price change.

    Code:
    // if in a position and the realized day's PnL plus the position PnL is greater than the loss limit then exit the order
    if (Position.MarketPosition == MarketPosition.Long
    && (currentPnL + Position.GetUnrealizedProfitLoss(PerformanceUnit.Currency, Close[0])) <= -LossLimit)
    {
    //Print((currentPnL+Position.GetProfitLoss(Close[0], PerformanceUnit.Currency)) + " - " + -LossLimit);
    // print to the output window if the daily limit is hit in the middle of a trade
    Print("daily limit hit, exiting order " + Time[0].ToString());
    ExitLong("Daily Limit Exit", "long1");
    }
    We look forward to being of further assistance.
    And in this code the first line that uses the Close[0] technically is not a close of the bar forming as I will use OnTickUpdate but just the term "close" is used, correct? How do you get the code in a block in the post? Am I supposed to copy and paste it and put it between Code and /Code?

    Thanks.
    Last edited by Trader17; 04-15-2019, 12:32 PM.

    Leave a comment:


  • Trader17
    replied
    Thanks. Are both statements acceptable as correct in Ninja Script?

    So in the Script by Chelsea I basically change it ti calculate on each tick or price change and both use the common term IsFirstTickOfBar? And while doing so now the bar currently forming will take "0" as its index and the bar that just closed will take "1' off which I need to calculate the strategy. Correct? As when we use Calculate on bar close then "0" is the one that just closed.

    Thanks. Ninja Rocks!!!

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Trader17,

    Bools return a true or false value and checking if (YourBool == true) is equivalent to checking if(YourBool). It can be very helpful and will at times be necessary to test out the concept to observe the behavior. For example, you could create a couple conditions that print a message if the condition becomes true.

    if (IsFirstTickOfBar) Print("Condition 1 This is true"");

    if (IsFirstTickOfBar == true) Print("Condition 2 This is true");

    We look forward to being of further assistance.

    Leave a comment:


  • Trader17
    replied
    Thanks Jim. Saw the video too. In there I see in the logic you just used IsFirstTickOfBar. Can it also be written like this? if ( IsFirstTickOfBar == True ) or will I be wrong?
    Thanks.

    Leave a comment:


  • NinjaTrader_Jim
    replied
    Hello Trader17,

    When you use a different Calculate mode, the behavior changes for the NinjaScript for how OnBarUpdate develops new bars. A strategy will not update intrabar behind-the-scenes if you are using Calculate.OnBarClose, and it will only update for bar closes.

    If you simulate OnBarClose when using OnEachTick/OnPriceChange by checking previous bars on IsFirstTickOfBar, then this would be the case.

    Please let me know if I can be of further assistance.

    Leave a comment:


  • Trader17
    replied
    Thanks for the detailed example. Will try it myself and also hopefully wait till Chelsea updates his example too. Buy any strategy technically is constantly running the entire script on each tick and checking for things needed to be done at the close of a bar or intra bar, correct?
    Thank you.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by dalebru, Today, 01:51 AM
0 responses
2 views
0 likes
Last Post dalebru
by dalebru
 
Started by Austiner87, Yesterday, 09:33 PM
0 responses
9 views
0 likes
Last Post Austiner87  
Started by HiddenPhilosopher, Yesterday, 07:24 PM
0 responses
10 views
0 likes
Last Post HiddenPhilosopher  
Started by dkbyond, Yesterday, 06:43 PM
0 responses
12 views
0 likes
Last Post dkbyond
by dkbyond
 
Started by i2w8am9ii2, Yesterday, 06:05 PM
0 responses
5 views
0 likes
Last Post i2w8am9ii2  
Working...
X