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

Martingale Strategy

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

    Martingale Strategy

    Hello I annex my attempt at martingale strategy, I still don't know how to double the amount when the stoploss or profit is executed, if a trade closes and opens another immediately it does not double the position. If somebody can help me simplify this I appreciate it, I will leave the script so that newbies like me can improve it.

    I use MES 15min

    Thanks
    Attached Files

    #2
    Hello walter739,

    Thank you for your post.

    You would likely need to track the stop loss and profit target orders in order variables, then monitor for them to be filled in OnOrderUpdate. When you see that the stop or profit target is filled, you can then execute logic to double the position.

    Here's a link to our help guide that may be helpful which demonstrates one method of tracking stop losses and profit targets:



    Please let us know if we may be of further assistance to you.

    Kate W.NinjaTrader Customer Service

    Comment


      #3
      Hello Kate thanks for answer, I see the Monitoring Stop-Loss and Profit Target Orders, but this example not work in a strategy, i enable the script and not working in the chart., for me is difficult understand how integrated to my script, Please do you have one example script o strategy with i can work in the chart for see how works with the print ouput ?

      I read sample order update for begin understand, but only work with charts in tick replay. For this method only work in tick replay?

      Regards

      Comment


        #4
        Hello walter739,

        Thank you for your reply.

        The example strategy will only function in real time, so you'd need to be running it on a chart using the Sim101 account with live data coming in to see what it does.

        SampleOnOrderUpdate does not need tick replay to function - what exactly are you referring to?

        Thanks in advance; I look forward to assisting you further.
        Kate W.NinjaTrader Customer Service

        Comment


          #5
          Hello I'm facing a problem making the martingale betting sequence , it starts noramally then it stats adding size randomlly 2 then 4 then back to 2 then it goes 4 again , it doesn't work as i expected please can i get some help




          if (SystemPerformance.RealTimeTrades.Count > 1)

          {
          // Check to make sure there is at least one trade in the collection
          Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];

          // Calculate the PnL for the last completed real-time trade
          double lastProfitCurrency = lastTrade.ProfitCurrency;

          // Store the quantity of the last completed real-time trade
          double lastTradeQty = lastTrade.Quantity;


          switch (MartingaleType)
          {

          case EnumMartingaleType.None:
          NextSize = Size;
          break;
          case EnumMartingaleType.Standard:
          if (lastTrade.ProfitCurrency < 0 )
          return;
          {
          NextSize = lastTrade.Quantity * MartingaleMult;
          }
          if (lastTrade.ProfitCurrency > 0 )
          {
          NextSize = Size;
          }

          break;
          }
          }​

          Comment


            #6
            Hello ossamaelouadih,

            Thanks for your note.

            Based on the code you shared I do not see anything standing out that would be an issue.

            Have you added debugging prints to your strategy to understand exactly how Size and NextSize is calculating as your strategy runs?

            Generally, when a strategy is not behaving as expected, debugging prints need to be used to see exactly how the strategy is processing logic. Below is a link to a forum post that demonstrates how to use prints to understand behavior.

            https://ninjatrader.com/support/foru...121#post791121

            Prints will appear in the NinjaScript Output window (New > NinjaScript Output window).

            Also, enable TraceOrders which will let us know if any orders are being ignored and not being submitted when the condition to place the orders is evaluating as true.​

            I am happy to assist you with analyzing the output from the NinjaScript Output window.

            Let us know if we may assist further.
            Brandon H.NinjaTrader Customer Service

            Comment


              #7
              Hi
              Size is the intial size i put it as a parameter in the properties then i added NextSize as an int
              and added Next size to the order management and now I added TraceOrders and Prints

              if(longCondition[0] = longCond[0] & CondIni[1] == -1)
              {
              Buy[0] = Low[0] + (-1 * TickSize);
              EnterLong(Convert.ToInt32(NextSize), "Long");
              }

              if(shortCondition[0] = shortCond[0] & CondIni[1] == 1)
              {
              Sell[0] = High[0] + (+1 * TickSize);
              EnterShort(Convert.ToInt32(NextSize), "Short");
              }

              SetStopLoss("Long", CalculationMode.Price, LowTarget[0], false);
              SetStopLoss("Short", CalculationMode.Price, HighTarget[0], false);

              if (SystemPerformance.RealTimeTrades.Count > 1)

              {
              // Check to make sure there is at least one trade in the collection
              Trade lastTrade = SystemPerformance.RealTimeTrades[SystemPerformance.RealTimeTrades.Count - 1];

              // Calculate the PnL for the last completed real-time trade
              double lastProfitCurrency = lastTrade.ProfitCurrency;

              // Store the quantity of the last completed real-time trade
              double lastTradeQty = lastTrade.Quantity;
              double lastPositinQty = Position.Quantity;


              switch (MartingaleType)
              {

              case EnumMartingaleType.None:
              NextSize = Size;
              break;
              case EnumMartingaleType.Standard:
              if (lastTrade.ProfitCurrency < 0 )
              return;
              {
              NextSize = lastTrade.Quantity * MartingaleMult;
              Print(NextSize);
              Print(Size);
              }
              if (lastTrade.ProfitCurrency > 0 )
              {
              NextSize = Size;
              Print(NextSize);
              Print(Size);
              }

              break;
              }
              }​

              Comment


                #8
                ​this is what i got when i enabled it , I will wait to, see a loosing tradeClick image for larger version  Name:	image.png Views:	0 Size:	29.7 KB ID:	1220810

                Comment


                  #9
                  this is what i got after a loosing tradeClick image for larger version

Name:	image.png
Views:	490
Size:	197.0 KB
ID:	1220814

                  Comment


                    #10
                    Hello ossamaelouadih,

                    Thanks for your notes.

                    In the screenshots you shared, I do not see any prints occurring in the Output window that prints out Size and NextSize. If you do not see the prints you created printing to the Output window, this means that the logic where you added prints to your script is not being reached.

                    You would need to further debug your strategy by adding more prints to determine why that logic in your script is not being reached. Prints would need to be added outside each condition that is used to change the NextSize variable in you script. That way you can determine exactly how those conditions are evaluating.

                    Debugging Tips - https://ninjatrader.com/support/help...script_cod.htm

                    Please let me know if I may assist further.
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      I added Prints to Long and short conditions and it printing NextSize as 0 and Size as 1Click image for larger version

Name:	image.png
Views:	481
Size:	809.4 KB
ID:	1220843

                      Comment


                        #12
                        Hello ossamaelouadih,

                        Thanks for your note.

                        If you added prints to a different part of your script and see those prints appearing in the Output window, this indicates that specific line of logic is being reached and your NextSize variable is evaluating as 0 and Size is evaluating to 1 when that specific logic is processed.

                        You would need to thoroughly debug your entire script to see exactly what logic is being reached in your strategy and what logic is not triggering. Once you fully understand how each line of logic in the strategy is behaving, you could modify your logic accordingly so the script functions more as you expect it to.

                        Note that in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                        Let me know if I may further assist.
                        Brandon H.NinjaTrader Customer Service

                        Comment


                          #13
                          Thank you Mr BrandonH For your helpfull Guidance After some Trials i figuered out that the problem is in getting the right laast quantity , as you see the last three trades were loosers but it keeps entring two contracts because they got the Last quantity as 1 contract from the code, Please let me know if there is a way to get the last size correctlyClick image for larger version  Name:	image.png Views:	0 Size:	138.3 KB ID:	1220923
                          Last edited by ossamaelouadih; 10-25-2022, 05:10 PM.

                          Comment


                            #14
                            Please I coudn't sleep thinking of this Can i get some direct help

                            Comment


                              #15
                              Originally posted by NinjaTrader_BrandonH View Post
                              Hello ossamaelouadih,

                              Thanks for your note.

                              If you added prints to a different part of your script and see those prints appearing in the Output window, this indicates that specific line of logic is being reached and your NextSize variable is evaluating as 0 and Size is evaluating to 1 when that specific logic is processed.

                              You would need to thoroughly debug your entire script to see exactly what logic is being reached in your strategy and what logic is not triggering. Once you fully understand how each line of logic in the strategy is behaving, you could modify your logic accordingly so the script functions more as you expect it to.

                              Note that in the support department at NinjaTrader it is against our policy to create, debug, or modify, code or logic for our clients. Further, we do not provide C# programming education services in our support. This is so that we can maintain a high level of service for all of our clients as well as our partners.

                              Let me know if I may further assist.
                              Can i get some help??

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by Rapine Heihei, 04-23-2024, 07:51 PM
                              2 responses
                              30 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by Shansen, 08-30-2019, 10:18 PM
                              24 responses
                              943 views
                              0 likes
                              Last Post spwizard  
                              Started by Max238, Today, 01:28 AM
                              0 responses
                              9 views
                              0 likes
                              Last Post Max238
                              by Max238
                               
                              Started by rocketman7, Today, 01:00 AM
                              0 responses
                              5 views
                              0 likes
                              Last Post rocketman7  
                              Started by wzgy0920, 04-20-2024, 06:09 PM
                              2 responses
                              28 views
                              0 likes
                              Last Post wzgy0920  
                              Working...
                              X