Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Martingale Bet Sizing

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

    #16
    Martingale in Ninjtrader 8

    I see there is some Martingale code samples and snipits for ninjatrader 7 but I cant find anything for Ninjtrader 8, Anyone have a code sample of doubling down a limited number of times which I know isn't a pure Martingale. Anything to get me started in the right direction would be appreciated.

    Thanks Quantismo

    Comment


      #17
      Hello quantismo,

      Thank you for the question.

      The NT7 syntax and concepts should mostly translate into valid NT8 syntax with the exceptions of some naming. You can find the code-breaking changes guide here, this is helpful if you will be converting the NT7 code to NT8:

      It looks like the previous samples in this thread are all fairly simple, aside from the structure of the file and some naming it should also work in nt8 with some modification.

      The only items I can see from the sample in this post would be Performance is now SystemPerformance: https://ninjatrader.com/support/help...ub=Performance

      And the file structure has changed some in NT8. To get the right file structure, you could generate a new strategy in NT8 and then copy and paste the OnBarUpdate code into the new empty OnBarUpdate of the new file. Additionally, the private variables would need to be copied.

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

      Comment


        #18
        Is there a way to reset the losing trade count to zero ? to make a loop for example: if you have two loosing trades the counter would enter a long postion and also reset to zero after two loosing trades

        if (SystemPerformance.AllTrades.LosingTrades.Count == 2)
        Thanks Quantismo

        Comment


          #19
          Hello,

          Thank you for the reply.

          There is not a way to reset the value, but you could store a variable and reset that based on the current value of the property.

          We have a sample that does something similar to check PnL across multiple sessions. you could use a similar approach to set variables to the performance values. A second set of variables could be used to get the current count minus the stored amount. The difference would be that instead of using the first bar of the session, you would reset the variables at your timing.



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

          Comment


            #20
            • Hello Yassine.Chetouani,

              Thank you for writing in.

              I have created a sample script attached to this post that will double your order quantity if the previous trade was a loser. Otherwise, it will change the quantity to 1 if a winner or if it broke even.

              Please, let me know if you have any questions.

            Hi can I use Martingaletest strategy into NT 8? Also can I integrate it into each separate strategy or its works with one platform that collects all trades?

            Thx

            Comment


              #21
              Originally posted by Yassine.Chetouani View Post
              Dear Mr.ZacharyG,

              Thank you for giving time to consider this.

              I want to inform you that I tried to link my code to the order quantity management by creating the related order quantity 7 conditions from the file you sent me. somehow it doesn't work.
              Without the martingale or quantity management conditions the code works cause it is a simple one, so the problem is not with the platform nor the condition, I checked every other possibility, even create a very simple code considering condition by conditions with a close/open comparison , didn't work also, and I didn't have any other choice than contacting you again. So sorry for the disturb.

              Please, can you help me to figure out how to solve it. or how to link the conditions I created for order quantity to the buy/sell actions as I predict it might be the problem, I attached the full strategy "Nominal Bar" with this mail to give you more idea about what I am trying to do. next to the simplest code "Nominal Bar2", Please feel free to modify on my code I sent you.

              Besides, As I told you that after the 7th loop of the sequence the count should return to 0 since I will be loosing a lot of ticks and there is a possibility to recover after the trend changes so I wrote code lines that I wish you can check since you have more expertise in this subject..

              Feel free to ask any more details, I will be here to provide you more information if you need Sir.
              Thank you again.
              Yassine.
              Hello is it for NT 7? Can I test it for NT 8?

              Comment


                #22
                Hello
                • Zachary G.
                Can u make same for NT 8? And is it possible to run that strategy after 2 nd loss?

                Originally posted by NinjaTrader_ZacharyG View Post
                Hello Yassine.Chetouani,

                Thank you for writing in.

                I have created a sample script attached to this post that will double your order quantity if the previous trade was a loser. Otherwise, it will change the quantity to 1 if a winner or if it broke even.

                Please, let me know if you have any questions.

                Comment


                  #23
                  Hello Sir do u have same example for NT 8

                  thx


                  Originally posted by traderpards View Post
                  This might help

                  A couple of years ago I helped someone with this very thing on another forum. I don't remember too much from that long ago but apparently for some reason, I thought it was interesting enough to separate the Martingale functionality into a base class that you could apply to any strategy. If it will help, you're welcome to it. As far as how to hook it up into your strategy, I did do that for someone else on the same forum but it's a pretty complicated strategy. You can probably just go to the Martingale section to see what you have to do but the second file is a real, working example for what it's worth. Sorry there's so much to wade through.

                  You'll need to know about inheritance but don't worry if you don't. Just look up "Object Oriented programming inheritance" in your favorite search engine and you can learn all you need in a couple of hours.

                  This Martingale base class will double up on trades 4 times before it resets back to one. I'm not sure why we chose 4 but it probably had to do with how little money we would ever possibly want to lose. If you want it to go seven times, you can probably add that easily enough.

                  All you really need to do is inherit from MartingaleBase and call the base class in OnInitialize(). You can then copy doLong() and doShort() exactly and in your code just call doLong() when your signals indicate long and doShort() when your signals indicate short.

                  This was actually kind of fun to do...

                  Comment


                    #24
                    Originally posted by traderpards View Post
                    Hi Yassine,

                    I was actually drawn to this post by the words "Martingale" which means that you double up on the losers. I remembered that I did something like that for someone else a couple of years ago and wanted to offer you that. After I made my post, I noticed that you are doing something different.

                    I couldn't tell from your posts what is actually wrong with your strategy - what unexpected behavior are you seeing? I told my kid I'd do something with him after I finished this post so if you do that I'll see if I can do something later this afternoon. In the meantime, I swapped out that awful example in my previous post with a far simpler one. Hopefully you'll find that more useful.
                    Hi Sir can u help me with creating Martingale system for NT 8?

                    Comment


                      #25
                      Originally posted by traderpards View Post
                      Hi Yassine,

                      I was actually drawn to this post by the words "Martingale" which means that you double up on the losers. I remembered that I did something like that for someone else a couple of years ago and wanted to offer you that. After I made my post, I noticed that you are doing something different.

                      I couldn't tell from your posts what is actually wrong with your strategy - what unexpected behavior are you seeing? I told my kid I'd do something with him after I finished this post so if you do that I'll see if I can do something later this afternoon. In the meantime, I swapped out that awful example in my previous post with a far simpler one. Hopefully you'll find that more useful.
                      Hi Sir can u help me with creating Martingale system for NT 8?

                      Comment


                        #26
                        I would like a copy please anyone

                        Comment


                          #27
                          Sorry.. I never got the notification for your interest... Here is my base class. I trust you're familiar with how to implement base classes in Ninjatrader; i.e. how to inherit from them...
                          Attached Files

                          Comment


                            #28
                            traderpards
                            Hey mate I know this is a really old post but any chance you could help me load your martingale base to my strategy, I've been trying for a couple hours now and can't seem to figure out how to get it working or what I need to replace the long/short position entering with

                            Comment


                              #29
                              Originally posted by RookieKiwi View Post
                              traderpards
                              Hey mate I know this is a really old post but any chance you could help me load your martingale base to my strategy, I've been trying for a couple hours now and can't seem to figure out how to get it working or what I need to replace the long/short position entering with
                              Sure... post what you have and I'll finish it for you... Or try this:
                              1. Inherit from MartingaleBase
                              2. Call the base class in both OnStateChange and OnBarUpdate

                              That's pretty much it. If that doesn't work post your strat and I'll finish what I forgot to tell you.
                              Last edited by traderpards; 03-04-2022, 08:14 AM.

                              Comment


                                #30
                                Originally posted by traderpards View Post

                                Sure... post what you have and I'll finish it for you... Or try this:
                                1. Inherit from MartingaleBase
                                2. Call the base class in both OnStateChange and OnBarUpdate

                                That's pretty much it. If that doesn't work post your strat and I'll finish what I forgot to tell you.
                                Im really sorry for this i tried to make it work on my own but ill admit my skills arent very good when it comes to C# i wasnt able to figure out how to set it up correctly and where to call it, i made a simple Stochastics strategy for testing attached here, i tried to use getQty as the entershort/enterlong but wasnt able to figure out how to call the rest of your base to use it correctly, i think i was able to figure out the inheritance but the rest of it is like another language for me ​ could you take a look?, also to add more then 8 do i just add more double up cases and continue the same way that was already done?​

                                For some reason the forum wont let me attach .cs files so just included a demo here

                                Code:
                                namespace NinjaTrader.NinjaScript.Strategies
                                {
                                public class testdemo : MartingaleBase
                                {
                                private Stochastics Stochastics1;
                                
                                protected override void OnStateChange()
                                {
                                getQty();
                                
                                if (State == State.SetDefaults)
                                {
                                Description = @"Enter the description for your new custom Strategy here.";
                                Name = "testdemo";
                                Calculate = Calculate.OnBarClose;
                                EntriesPerDirection = 1;
                                EntryHandling = EntryHandling.AllEntries;
                                IsExitOnSessionCloseStrategy = true;
                                ExitOnSessionCloseSeconds = 30;
                                IsFillLimitOnTouch = false;
                                MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix;
                                OrderFillResolution = OrderFillResolution.Standard;
                                Slippage = 0;
                                StartBehavior = StartBehavior.WaitUntilFlat;
                                TimeInForce = TimeInForce.Gtc;
                                TraceOrders = false;
                                RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose;
                                StopTargetHandling = StopTargetHandling.PerEntryExecution;
                                BarsRequiredToTrade = 20;
                                // Disable this property for performance gains in Strategy Analyzer optimizations
                                // See the Help Guide for additional information
                                IsInstantiatedOnEachOptimizationIteration = true;
                                }
                                else if (State == State.Configure)
                                {
                                }
                                else if (State == State.DataLoaded)
                                {
                                Stochastics1 = Stochastics(Close, 1, 15, 1);
                                SetProfitTarget("", CalculationMode.Ticks, 20);
                                SetStopLoss("", CalculationMode.Ticks, 10, false);
                                }
                                }
                                
                                protected override void OnBarUpdate()
                                {
                                getQty();
                                
                                if (BarsInProgress != 0)
                                return;
                                
                                if (CurrentBars[0] < 1)
                                return;
                                
                                // Set 1
                                if (CrossAbove(Stochastics1.K, 30, 1))
                                {
                                EnterLong(getQty(), "");
                                }
                                
                                // Set 2
                                if (CrossBelow(Stochastics1.K, 70, 1))
                                {
                                EnterShort(getQty(), "");
                                }
                                
                                }
                                }
                                }

                                Comment

                                Latest Posts

                                Collapse

                                Topics Statistics Last Post
                                Started by xiinteractive, 04-09-2024, 08:08 AM
                                2 responses
                                10 views
                                0 likes
                                Last Post xiinteractive  
                                Started by Irukandji, Today, 09:34 AM
                                1 response
                                3 views
                                0 likes
                                Last Post NinjaTrader_Clayton  
                                Started by RubenCazorla, Today, 09:07 AM
                                1 response
                                5 views
                                0 likes
                                Last Post RubenCazorla  
                                Started by TraderBCL, Today, 04:38 AM
                                3 responses
                                25 views
                                0 likes
                                Last Post NinjaTrader_Jesse  
                                Started by WeyldFalcon, 08-07-2020, 06:13 AM
                                11 responses
                                1,423 views
                                0 likes
                                Last Post jculp
                                by jculp
                                 
                                Working...
                                X