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

Profit targets, stop losses, playing sounds not working correctly

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

    Profit targets, stop losses, playing sounds not working correctly

    Hi I hope I can find some help and we can all share a solution as a community...

    I am including the relevant bits of my code. I am experiencing no issues in the debugger. Strategy runs. And behaves. Just not as expected.

    I set profit target at 500, stop loss at 2000.

    Given the following code, I am seeing results like profit not being taken at 500. I saw profit go today to 700, then back down to 300 before it became realized.
    I see stop losses exiting in the hundreds, so it exits too early if I am telling it to wait until 2000.

    No sound is played, ever.

    The positions are not closed at the end of the day, but the end of the day alerts work and no more orders are placed.

    What am I doing wrong?

    I see no errors in my Log. But I do wish the log was easier to read. I also wish the executions tab showed profit/loss for each row. That seems like a HUGE glaring omission, like forgetting to include a "Send" button in an email app huge.

    Thanks for any and all advice.

    This is where I am assigning my profit target and stop loss for each entry label, using user inputs (defaults are 500 target and 2000 loss)

    Code:
    SetProfitTarget(@"Buy Long", CalculationMode.Currency, ProfitTarget);
    SetStopLoss(@"Buy Long", CalculationMode.Currency, StopLoss, false);
    SetProfitTarget(@"Buy Short", CalculationMode.Currency, ProfitTargetShort);
    SetStopLoss(@"Buy Short", CalculationMode.Currency, StopLossShort, false);
    This piece is enclosed in a condition, which works correctly except for the missing audio...

    Code:
    EnterLong(Convert.ToInt32(DefaultQuantity), @"Buy Long");
    Alert(@"SmokySignals_1", Priority.High, @"Buy Long" + Convert.ToString(Close[0]) + " " + Convert.ToString(High[0]) + " " + Convert.ToString(Low[0]), @"", 30, Brushes.Chartreuse, Brushes.Black);
    if (Instrument.MasterInstrument.Name == "TSLA"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-TSLA-Long.wav");
    }
    else if(Instrument.MasterInstrument.Name == "AAPL"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-AAPL-Long.wav");
    }
    else if(Instrument.MasterInstrument.Name == "FB"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-FB-Long.wav");
    }
    else if(Instrument.MasterInstrument.Name == "QS"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-QS-Long.wav");
    }
    This piece is also enclosed in a condition, and it all works correctly except for the missing audio...

    Code:
    EnterShort(Convert.ToInt32(DefaultQuantity), @"Buy Short");
    Alert(@"SmokySignals_1", Priority.High, @"Buy Short" + Convert.ToString(Close[0]) + " " + Convert.ToString(High[0]) + " " + Convert.ToString(Low[0]), @"", 30, Brushes.DodgerBlue, Brushes.Black);
    if (Instrument.MasterInstrument.Name == "TSLA"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-TSLA-Short.wav");
    }
    else if(Instrument.MasterInstrument.Name == "AAPL"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-AAPL-Short.wav");
    }
    else if(Instrument.MasterInstrument.Name == "FB"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-FB-Short.wav");
    }
    else if(Instrument.MasterInstrument.Name == "QS"){
    PlaySound(@"C:\Program Files (x86)\NinjaTrader 8\sounds\Buy-QS-Short.wav");
    }
    From this code I get the alerts at the correct time, and no more positions are taken, but the existing positions are not closed and allowed to run until market close. I have tried setting IsExitOnSessionCloseStrategy to true and to false and neither has an impact.

    Code:
    if (DateTime.Now > EndTime){
    ExitLong(@"Buy Long");
    ExitShort(@"Buy Short");
    Alert(@"SmokySignals_1", Priority.High, @"Closing - End of Day " + Convert.ToString(Close[0]) + " " + Convert.ToString(High[0]) + " " + Convert.ToString(Low[0]), @"", 30, Brushes.Pink, Brushes.Black);
    }
    This is an example showing the odd stop loss realized amounts and being clearly past profit target for many bars with no reaction
    Click image for larger version

Name:	2020-12-29_11-11-30.png
Views:	227
Size:	4.1 KB
ID:	1134321
    Last edited by Smoky; 12-29-2020, 12:41 PM.

    #2
    Hello Smoky,

    Thank you for your post.
    I set profit target at 500, profit loss at 2000.

    Given the following code, I am seeing results like profit not being taken at 500. I saw profit go today to 700, then back down to 300 before it became realized.

    I see that your Profit Target is not taking profit at the value specified. Did the Profit Target order submit properly?

    Please note that it is suggested to call SetProfit target from within the strategy OnStateChange() method if your profit target price/offset is static. You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the target price while in an open position. Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position

    SetProfitTarget - https://ninjatrader.com/support/help...ofittarget.htm

    I see stop losses exiting in the hundreds, so it exits too early if I am telling it to wait until 2000.

    I understand that your Stop Loss is filling rapidly. Please reduce the script by commenting out or removing code not related to the behavior while still demonstrating the issue occurring. Then, please send us your reduced script that demonstrates the issue so we may investigate further.

    First, its great to create a copy of the script so that the original work is kept safe. Below is a link to a video that demonstrates making a copy of the script. Once the copy is made you would reduce the copy of the script by removing all code not related to the behavior while still demonstrating the issue.
    https://www.youtube.com/watch?v=BA0W...utu.be&t=8m15s

    No sound is played, ever.

    Are you able to confirm that the PlaySound lines of code are getting hit by adding prints above the PlaySound lines of code and monitoring a NinjaScript Output window (New > NinjaScript Output). This may occur if the sound files are not proper PCM WAV files. Do regular sound files work if you use them for PlaySound?

    If you are able to isolate these issues to separate test scripts it will allow you to focus on each issue at hand and help us better assist you. This could be done by making a reduced copy of the script for each issue.

    I look forward to assisting further.



    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thanks fore the quick reply! Responses inline...

      Originally posted by NinjaTrader_BrandonH View Post
      Hello Smoky,

      Thank you for your post.
      I set profit target at 500, profit loss at 2000.

      Given the following code, I am seeing results like profit not being taken at 500. I saw profit go today to 700, then back down to 300 before it became realized.



      I see that your Profit Target is not taking profit at the value specified. Did the Profit Target order submit properly?

      Please note that it is suggested to call SetProfit target from within the strategy OnStateChange() method if your profit target price/offset is static. You may call this method from within the strategy OnBarUpdate() method should you wish to dynamically change the target price while in an open position. Should you call this method to dynamically change the target price in the strategy OnBarUpdate() method, you should always reset the target price/offset value when your strategy is flat otherwise, the last price/offset value set will be used to generate your profit target order on your next open position

      SetProfitTarget - https://ninjatrader.com/support/help...ofittarget.htm
      I am setting all of my profit targets and stop losses inside OnStateChange(). They are based on user inputs.


      Originally posted by NinjaTrader_BrandonH View Post
      I see stop losses exiting in the hundreds, so it exits too early if I am telling it to wait until 2000.

      Originally posted by NinjaTrader_BrandonH View Post

      I understand that your Stop Loss is filling rapidly. Please reduce the script by commenting out or removing code not related to the behavior while still demonstrating the issue occurring. Then, please send us your reduced script that demonstrates the issue so we may investigate further.

      First, its great to create a copy of the script so that the original work is kept safe. Below is a link to a video that demonstrates making a copy of the script. Once the copy is made you would reduce the copy of the script by removing all code not related to the behavior while still demonstrating the issue.
      https://www.youtube.com/watch?v=BA0W...utu.be&t=8m15s
      What I attached in my first post is the reduced code. I don't understand the request I guess. I did not share my entire script, only the relevant bits (your request is what I had in mind when I shaped my original post, so please clarify what you are asking and I will try to follow through). The video link you provided seems to be about preserving the strategy builder state. I am not using strategy builder in this case so that is not a concern.

      No sound is played, ever.




      Originally posted by NinjaTrader_BrandonH View Post
      Are you able to confirm that the PlaySound lines of code are getting hit by adding prints above the PlaySound lines of code and monitoring a NinjaScript Output window (New > NinjaScript Output). This may occur if the sound files are not proper PCM WAV files. Do regular sound files work if you use them for PlaySound?
      I can confirm the lines are being hit because code on either side of the sounds- within the same condition- are working. Can you tell me what are the constraints around the required wav format? I did generate wav files but I may have gone too high on the sample rate, or used stereo when I should have used mono. Do you have a link to the requirements? Update re: sound, I did confirm that my wav files are not compatible with NT 8 by replacing the default connect sound with one of my files and it does not play. So I will wait to find out what is the required format. I can't find it after searching but I will look again.

      Thanks!



      Last edited by Smoky; 12-29-2020, 03:02 PM.

      Comment


        #4
        Hello Smoky,

        Thank you for your note.

        To test the behavior you are seeing for the protected orders on our end, you will need to send us a reduced and exported version of the script. To copy a script, right-click on the script in the NinjaScript Editor and choose 'Save As'. Creating a copy of the script will allow you to reduce the copy script while keeping your original script safe.

        Exporting a NinjaScript - https://ninjatrader.com/support/help...tAsSourceFiles

        Please see the PlaySound help guide link below regarding the format required for the sound file.


        I look forward to assisting further.
        Brandon H.NinjaTrader Customer Service

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by JoMoon2024, Today, 06:56 AM
        0 responses
        6 views
        0 likes
        Last Post JoMoon2024  
        Started by Haiasi, 04-25-2024, 06:53 PM
        2 responses
        19 views
        0 likes
        Last Post Massinisa  
        Started by Creamers, Today, 05:32 AM
        0 responses
        6 views
        0 likes
        Last Post Creamers  
        Started by Segwin, 05-07-2018, 02:15 PM
        12 responses
        1,786 views
        0 likes
        Last Post Leafcutter  
        Started by poplagelu, Today, 05:00 AM
        0 responses
        3 views
        0 likes
        Last Post poplagelu  
        Working...
        X