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

SystemPerformance + order signal name + Print() = ?

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

  • Amedeus
    replied
    Hey everyone,

    if someone faces undesired prints when uncommenting State.Terminated for a backtest,
    here is what to do :
    Print() within OnStateChange() when State.Terminated - NinjaTrader Support Forum

    cheers

    Leave a comment:


  • Amedeus
    replied
    hey everyone, hey Jesse,

    Attached is a sample of what we were trying to accomplish :
    a simple print of multiple specific entries, this is an example with one only.

    This is more of a tool that can be helpful in cases where we want to test and compare multiple entries during a backtest.
    Or just backtest directly on the chart.
    But it does not provide the details and visuals that SystemPerformance and the analyzer can provide, unless doing it all by hand.

    Thank you very much Jesse, you were of great help.
    It was pretty basic I guess but not for everyone. And it was a very interesting basic thing to do.

    Have a great week-end,
    and happy backtesting to all.
    Attached Files
    Last edited by Amedeus; 08-13-2021, 03:08 PM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello


    But looking at the ending math verification print from the attached picture -b, it seems that the strategy is adding one item to the list every bar.
    If not every bar, there is another reason. Do you have a clue about why ?
    This again realates to how you need to use the list. OnBarUpdate is called for every bar so if you are adding to the list in OnBarUpdate it will do that for every bar. You would need to clear the list before using it again or move the code to a more relevant override for the goal.

    Like in in the attached picture -a, we did have a try with the BarsSinceExitExecution() method, but the output showed us this was not it.
    How could we make this list be implemented only after each exit execution ? not the next bar.
    You could use OnExecutionUpdate if you are trying to observe execution events: https://ninjatrader.com/support/help...xecutionUpdate


    On bar 411, a second trade is added to the Bminus list, on execution update.
    Any good idea on how to stop this ? (the commented lines are either erroring out or still giving us a wrong count)
    If the trade is a duplicate you could check if the trade exists in your list before adding it. You would otherwise need to see why the OnExecution event is being called and what order that was for to determine how to fix the conditions.

    I must mention,
    I tried to add the OnAddTrade() void to this sample, below all other voids, because it kind of sounded like a potential appropriate method for our use.
    But the editor did not want to compile, see attached picture. the message was : no suitable method found to override.
    What does this mean ? and would OnAddTrade() be suitable for our w/l print purpose ?
    That is not an override for indicators/strategies but is for a performance metric for the strategy analyzer. In the help guide you can look at the top of the page to see the section of help guide you are in, you can also see that on the left side navigation tree.

    I look forward to being of further assistance.

    Leave a comment:


  • Amedeus
    replied
    hey again,

    attached may be what the end of our little debug looks like.

    The piece of performance code we did was finally placed at the end of OnExecutionUpdate(), we still are in a copy of the OnOrderUpdateSample.

    the code was rearranged to make it even more clear in the output. The bars from the output are the 1st 3 trades, on execution update.

    On bar 411, a second trade is added to the Bminus list, on execution update.
    Any good idea on how to stop this ? (the commented lines are either erroring out or still giving us a wrong count)




    I must mention,
    I tried to add the OnAddTrade() void to this sample, below all other voids, because it kind of sounded like a potential appropriate method for our use.
    But the editor did not want to compile, see attached picture. the message was : no suitable method found to override.
    What does this mean ? and would OnAddTrade() be suitable for our w/l print purpose ?
    Attached Files
    Last edited by Amedeus; 08-13-2021, 07:37 AM.

    Leave a comment:


  • Amedeus
    replied

    hey Jesse, thanks a lot,

    It is just meant to print multiple orders perf data like w/l after a backtest for example, or basic count camparison, nothing more for now.
    I've had a look at a few tutorials on lists, there is some I understand, but there also is a lot I don't follow. Your indications helped.

    We went back in OnBarUpdate(), allthough I'am not convinced an every bar calculation is the best, it'll do for now.
    There were multiple adjustments to the same logic previously presented.

    We have a count now.
    But looking at the ending math verification print from the attached picture -b, it seems that the strategy is adding one item to the list every bar.
    If not every bar, there is another reason. Do you have a clue about why ?

    Like in in the attached picture -a, we did have a try with the BarsSinceExitExecution() method, but the output showed us this was not it.
    How could we make this list be implemented only after each exit execution ? not the next bar.
    Attached Files

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello

    This simple entryApluslist list does not print any count.
    Would you please let us know why it does not, if you do know already ?
    I wouldn't know why based on this sample, are you certain the condition to add to the list is happening?

    we can not see the Print("entryApluslist is : " + entryApluslist); part.
    That would indicate the condition was not true so that print never happened. You could use prints to see what the variable values are at that time to better understand what needs to change with the condition.

    Would a better idea than this list come to your (or anyone around) mind for this purpose ?
    There are a ton of possibilities regarding lists. Ours could involve a function part for the simple math..? Would this be unnecessary for a w/l ratio ?
    I'm not certain what your overall goal requires to say if there is something better. A List is a collection of objects, if you needed to collect some objects and then do some action with those objects like math a List would be suitable for that purpose. Lists are a part of C# so you can learn more about their uses by searching online for C# List tutorials.


    Also, We have no clue about where is a good spot in our NT script to incorporate this list code.
    Should this be done in OnBarUpdate(), OnOrderUpdate(), OnExecutonUpdate(), OnPositionUpdate(), or evenyually in another special dedicated void of ours ???
    In this example we chose to do it in onPositionUpdate() because it was empty and it did not sound too nonsensical. What do you think of that ?
    That would really depend on what you need this code for, if something in the script needs it then you would put it in the relevant section for the code that needs it. If you are just trying to do prints you could use OnBarUpdate for simplicity.


    we can see there are 1K+ trades in this example.
    let'say we test 5 entries, each having such lists with a couple hundreds variables listed,
    how of an impact would adding this many lists, have on the performance of the machine ?
    For simple data it would not likely be that much effort for the PC. It would depend on how you use the lists and what overall ends up being done. The actual storage of data wont take much resources.

    I look forward to being of further assistance.

    Leave a comment:


  • Amedeus
    replied

    hey Jesse, thank you very much,

    100 was just a test, it has been tested with 1 but there wss not a print either... that could tell us something about this method...


    However, since we cannot filter SystemPerformance for strategies, we might as well create our own list(s).
    I like very much this idea of yours.

    We do not just want a count per entrues, but also print a simple math w/l ratio for each entries.

    So far, this (code below) is what we've attempted to create : one list per condition. (here actually, just the first condition)

    This simple entryApluslist list does not print any count.
    Would you please let us know why it does not, if you do know already ?

    Would a better idea than this list come to your (or anyone around) mind for this purpose ?
    There are a ton of possibilities regarding lists. Ours could involve a function part for the simple math..? Would this be unnecessary for a w/l ratio ?

    Also, We have no clue about where is a good spot in our NT script to incorporate this list code.
    Should this be done in OnBarUpdate(), OnOrderUpdate(), OnExecutonUpdate(), OnPositionUpdate(), or evenyually in another special dedicated void of ours ???
    In this example we chose to do it in onPositionUpdate() because it was empty and it did not sound too nonsensical. What do you think of that ?

    Code:
    protected override void OnPositionUpdate(Cbi.Position position, double averagePrice, int quantity, Cbi.MarketPosition marketPosition)
    {
    
    if (SystemPerformance.AllTrades.Count > 1)
    {
    Print("");
    Print("");
    Print("Instrument is : " + Instrument);
    // Print out the total # of AllTrades
    Print("SystemPerformance.AllTrades.TradesPerforman ce.TradesCount is : " + SystemPerformance.AllTrades.TradesPerformance.Trad esCount);
    Print("----");
    
    TradeCollection myTotalofTrades = SystemPerformance.AllTrades;
    Print(myTotalofTrades+"\t named myTotalofTrades has a count of :\t" + myTotalofTrades.TradesCount + "\t trades");
    
    Trade lastTrade = myTotalofTrades.Last();
    string lastTradeName = lastTrade.Entry.Order.Name;
    
    Print("lastTradeName is :\t" + lastTradeName + ",\t or is :\t" + lastTrade);
    Print("lastTrade.ProfitTicks is : " + lastTrade.ProfitTicks);
    Print("--------");
    
    
    // create 2 lists with the purpose of printing a win/loss count per trade name,
    // eventually with a w/l ratio calculated at the end, this may involve a function
    List<double> entryApluslist = new List<double>();
    // var entryAminuslist = new List<int, double>();
    
    if (lastTradeName == "entryA"
    && lastTrade.ProfitTicks > 0)
    {
    entryApluslist.Add(lastTrade.ProfitTicks);
    Print("entryApluslist is : " + entryApluslist);
    Print("entryApluslist.Count is : " + entryApluslist.Count);
    }
    
    
    if (lastTradeName == "entryA"
    && lastTrade.ProfitTicks < 0)
    {
    
    //Print("entryAminuslist is : " + entryAminuslist);
    //Print("entryAminuslist.Count is : " + entryAminuslist.Count);
    }
    
    
    
    else if (lastTradeName == "entryB")
    {
    }
    }// if (SystemPerformance.AllTrades.Count > 1)
    
    
    }// OnPositionUpdate()

    the output of 2.5 bars for this code is :
    Code:
    Instrument is : MES 09-21 Globex
    SystemPerformance.AllTrades.TradesPerformance.Trad esCount is : 1149
    ----
    NinjaTrader.Cbi.TradeCollection named myTotalofTrades has a count of : 1149 trades
    lastTradeName is : entryA, or is : instrument='MES 09-21' entryPrice=4440 exitPrice=4437 quantity=1 marketPosition=Long entryTime='12/08/2021 07:34:16' exitTime='12/08/2021 08:38:39' profitCurrency=-16,02
    lastTrade.ProfitTicks is : -12,816
    --------
    
    
    Instrument is : MES 09-21 Globex
    SystemPerformance.AllTrades.TradesPerformance.Trad esCount is : 1149
    ----
    NinjaTrader.Cbi.TradeCollection named myTotalofTrades has a count of : 1149 trades
    lastTradeName is : entryA, or is : instrument='MES 09-21' entryPrice=4440 exitPrice=4437 quantity=1 marketPosition=Long entryTime='12/08/2021 07:34:16' exitTime='12/08/2021 08:38:39' profitCurrency=-16,02
    lastTrade.ProfitTicks is : -12,816
    --------
    
    
    Instrument is : MES 09-21 Globex
    SystemPerformance.AllTrades.TradesPerformance.Trad esCount is : 1150
    we can not see the Print("entryApluslist is : " + entryApluslist); part.
    If we place it below outside the condition, it prints this for one bar :
    Code:
    Instrument is : MES 09-21 Globex
    SystemPerformance.AllTrades.TradesPerformance.Trad esCount is : 1150
    ----
    NinjaTrader.Cbi.TradeCollection named myTotalofTrades has a count of : 1150 trades
    lastTradeName is : entryB, or is : instrument='MES 09-21' entryPrice=4439,75 exitPrice=4436 quantity=1 marketPosition=Long entryTime='12/08/2021 00:07:52' exitTime='12/08/2021 08:40:18' profitCurrency=-19,77
    lastTrade.ProfitTicks is : -15,816
    --------
    entryApluslist is : System.Collections.Generic.List`1[System.Double]
    entryApluslist.Count is : 0

    Lastly, another question :
    we can see there are 1K+ trades in this example.
    let'say we test 5 entries, each having such lists with a couple hundreds variables listed,
    how of an impact would adding this many lists, have on the performance of the machine ?
    Last edited by Amedeus; 08-12-2021, 05:59 AM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello Amedeus,

    You can use the GetTrades method to return a specific trade collection from that method, the strategies main trade collection cannot be filtered directly. You can see an example of using GetTrades in the help guide here: https://ninjatrader.com/support/help...tsub=GetTrades

    In what you provided earlier you used 100 for the instance, are you trying to find the 100th past trade or the last trade? If you mean the last trade that would be 1 and not 100. You can read about this in the above link.

    I look forward to being of further assistance.


    Leave a comment:


  • Amedeus
    replied
    Hey Jesse, thank you,

    The collection its self cant be sorted by signal names.
    The overall performance though could not be filtered to only contain specific orders with specific signal names.
    What am missing about GetTrades() then ?
    entrySignalName from the GetTrades() help guide page, what does it relates to ? the EnterLong signal name ? or the created string from your sample ? (NinjaTrader 8)
    I have not been able to print anything using GetTrades(), writing any of those.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello Amedeus,

    In the sample I provided I had just shown you how to access the Order which has the signal names, you could also use a loop or any other C# collection code like LINQ to search for specific orders in the trade collection. The trade collection is just a C# collection.

    In any use with the TradesPerformance you will have to drill down to the order object if you wanted to find the name of the order in the collection. The collection its self cant be sorted by signal names but you could find the orders you wanted and make your own list of those orders. The overall performance though could not be filtered to only contain specific orders with specific signal names.


    I look forward to being of further assistance.

    Leave a comment:


  • Amedeus
    replied
    Hey Jesse, thank you very much,

    I am just trying to access a count and a collection of trades whose entry order are named entryA and entryB. And we know the exits names too.

    In your example the trade is the first trade of the collection.
    How do we define a trade not from the position it has in the AllTrades collection, but with just the name of its entry and exit order ?

    Attached is a .txt sample of what we have been attempting to do, for us to have matter to work on.
    It basically is the SampleOnOrderUpdate with two orders.

    the specific trade collections still output 0...
    here is one bar of its output, when lastrade was TradeA :
    Code:
    Instrument is : MES 09-21 Globex
    SystemPerformance.AllTrades.TradesPerformance.Trad esCount is : 1185
    myTotalofTrades is : NinjaTrader.Cbi.TradeCollection
    myTotalofTrades.TradesCount is : 1185
    firstTrade is : instrument='MES 09-21' entryPrice=4294,5 exitPrice=4290,5 quantity=1 marketPosition=Long entryTime='08/07/2021 12:09:17' exitTime='08/07/2021 12:10:11' profitCurrency=-21,02
    lastTrade is : instrument='MES 09-21' entryPrice=4424,5 exitPrice=4430,5 quantity=1 marketPosition=Long entryTime='06/08/2021 21:50:13' exitTime='06/08/2021 22:14:58' profitCurrency=28,98
    tradeA is :
    TradeA.TradeNumber is : 1184
    TradeA is : instrument='MES 09-21' entryPrice=4424,5 exitPrice=4430,5 quantity=1 marketPosition=Long entryTime='06/08/2021 21:50:13' exitTime='06/08/2021 22:14:58' profitCurrency=28,98
    myentryATradesColl has a total of 0 trades.
    myentryBTradesColl has a total of 0 trades.
    Attached Files
    Last edited by Amedeus; 08-07-2021, 05:37 AM.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello Amedeus,

    First() is a C# method from the library called LINQ, its just getting the first element of the collection. That would be similar to trades[0], 0 being the first index of the collection.
    Is this a custom method to link entry and exits ?
    That's not specific to NinjaScript, it can work on any collection in C#.


    I was not able to call OnOrderUpdate() or OnExecutionUpdate() from an custom function.
    You shouldn't ever need to call either of these methods, they get called automatically by the order events. Do you mean you can't add these methods to a custom class? Could you provide more details here?


    I look forward to being of further assistance.

    Leave a comment:


  • Amedeus
    replied

    Hey Jesse, thank you very much,

    I'am not following the : First().
    Is this a custom method to link entry and exits ?

    I was not able to call OnOrderUpdate() or OnExecutionUpdate() from an custom function.

    Leave a comment:


  • NinjaTrader_Jesse
    replied
    Hello Amedeus,

    To get a from entry signal name you need an order, the signal names apply to individual orders and not a Trade which can consist of multiple orders.


    Code:
    TradeCollection trades = SystemPerformance.AllTrades;
    Trade firstTrade = trades.First();
    string entryOrderFromEntrySignal = firstTrade.Entry.Order.FromEntrySignal
    In the above example the entry order is used to get the from entry signal name.

    I look forward to being of further assistance.

    Leave a comment:


  • Amedeus
    replied
    Hey everyone,

    It seems that SystemPerformance could help for this with the GetTrades() method. : NinjaTrader 8

    Yet, when we try to print separate entries collections counts, the counts from the output does not match (does not equal AllTrades.Count).

    This code :
    Code:
    Print("");
    Print("Instrument is : " + Instrument);
    // Print out the total # of AllTrades
    Print("SystemPerformance.AllTrades.TradesPerforman ce.TradesCount is: " + SystemPerformance.AllTrades.TradesPerformance.Trad esCount);
    
    
    // help guide example
    // <TradeCollection>.GetTrades(string instrument, string entrySignalName, int instance)
    // TradeCollection myTrades = SystemPerformance.AllTrades.GetTrades("MSFT", "myEntrySignal", 1);
    // Print("The last position was comprised of " + myTrades.Count + " trades.");
    TradeCollection myTrades = SystemPerformance.AllTrades.GetTrades("MES 09-21", "entryB", 100);
    Print("The last position was comprised of " + myTrades.Count + " trades.");
    
    TradeCollection myTradeACollection = SystemPerformance.AllTrades.GetTrades("MES 09-21 Globex", "entryA", 3);
    Print("myTradeACollection.Count is : " + myTradeACollection.Count );
    
    TradeCollection myTradeBCollection = SystemPerformance.AllTrades.GetTrades("MES 09-21 Globex", "entryB", SystemPerformance.AllTrades.TradesPerformance.Trad esCount);
    Print("myTradeBCollection.Count is : " + myTradeBCollection.Count );
    outputs this :
    Code:
    Instrument is : MES 09-21 Globex
    SystemPerformance.AllTrades.TradesPerformance.Trad esCount is: 1028
    The last position was comprised of 1 trades.
    myTradeACollection.Count is : 1
    myTradeBCollection.Count is : 0
    1028 trades, all under a signal name, but so few in the specific collections...
    The GetTrades() method must be written wrong, is this right ?
    Has anyone any idea of what could not be taken into consideration ?
    Last edited by Amedeus; 08-06-2021, 07:00 AM.

    Leave a comment:

Latest Posts

Collapse

Topics Statistics Last Post
Started by nnwakaihe, Today, 08:19 AM
0 responses
3 views
0 likes
Last Post nnwakaihe  
Started by SuperDude123, Yesterday, 08:31 AM
2 responses
75 views
0 likes
Last Post SuperDude123  
Started by TNA22, Today, 04:28 AM
3 responses
27 views
0 likes
Last Post NinjaTrader_Jason  
Started by maltin, Yesterday, 04:39 PM
1 response
61 views
0 likes
Last Post NinjaTrader_Jason  
Started by jjanguda, Yesterday, 03:54 PM
1 response
52 views
0 likes
Last Post NinjaTrader_Jason  
Working...
X