Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

$EURUSD strategy development

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

    $EURUSD strategy development

    Hi all,

    I am new on NT and trying to learn how to code strategy on it using C#. I am using the wizard to generate the code so I can than change it and learning doing it.
    I tried to develop a strategy (file attached) for a very simple system to use as a learning procedure. What I want it to do is:

    1. Enter a long position when you have a green candle after a red candle. The position should be open on the close of the green candle and I think my problem in the code has to do with this.
    2. Close the position on next candle close (the day after the entry candle) or close it on a stop loss placed on the previous red candle low.
    3. Enter a short position when you have a red candle after a green candle. The position should be open on the close of the red candle.
    4. Close the position on next candle close or close it on a stop loss placed on the previous green candle high.

    My 1st thought was that this would be an easy exercise but I couldn't do it correctly because when I backtest it I get trades on places I wouldn't expect it. Problems of a beginner...

    Can you please show me where I made the mistakes on the strategy development?

    I am testing the strategy on $EURUSD using weekly bars from 01-01-2009 until today.

    Here's the backtest menu with the parameters I'm using (image attached)

    Here are the trades I get using the code I sent you (image(1) attached)

    And here are the trades I expected from the strategy for the same period (image (2) attached)

    Just to remember the rules so it will be easy for you to understand the previous chart:

    1. Long when you have a green candle after a red candle
    2. Short when you have a red candle after a green candle
    3. Stop loss long is the low of the red candle
    4. Stop loss short is the high of the green candle
    5. Time stop is the next candle close

    Hope someone can give me some help on my learning journey.

    Regards,

    ACP
    Attached Files

    #2
    ajcpereira,

    If you haven't yet been welcomed to the forum, welcome!

    I am noticing you have the following :

    // Condition set 3
    if (ToTime(Time[0]) > ToTime(Time[1]))
    {
    ExitLong("Close long position", "Long");
    ExitShort("Close short position", "Short");
    }

    This condition would always be true, so your ExitLong and ExitShort would be called every bar.

    What are you trying to do with this set? I may be able to offer a better solution.
    Adam P.NinjaTrader Customer Service

    Comment


      #3
      Hi Adam P.

      What I am trying to do with that is to get out of the trade on the close of the candle after the position was opened. If I have an entry signal in candle 0, I open a long/short position that should be closed on candle 1 on it's close if initial stop was not hit.

      Hope your solution can solve my problem (if this is the only problem).

      ACP

      Comment


        #4
        ajcpereira,

        There may be a misunderstanding of backtesting vs. real time operation here, however that was the first thing I noticed that may cause an issue.

        You can use BarsSinceEntry() to accomplish what you want.

        if ( BarsSinceEntry() >= 1)
        {
        //exit orders
        }

        I would suggest testing with this and then let me know if you still see any discrepancies to what you would expect, as it would help me narrow things down.
        Adam P.NinjaTrader Customer Service

        Comment


          #5
          Hi Adam,

          That solved the problem on the time exit but one problem is still present. As I'm using the conditions:

          if (Close[1] < Open[1]
          && Close[0] > Open[0])
          {
          EnterLong(1000, "Long");
          }

          // Condition set 2
          if (Close[1] > Open[1]
          && Close[0] < Open[0])
          {
          EnterShort(1000, "Short");
          }

          The value is only verified after the candle is closed and so the position is only open on the next candle open. How can I define that if nearly the close this conditions are verified the trade should be done on that bar close?

          Thanks for your help,

          ACP

          Comment


            #6
            ajcpereira,

            For this you would need to use something that has intrabar granularity.

            You can submit orders to different Bars objects. This allows you the flexibility of submitting orders to different timeframes. Like in live trading, taking entry conditions from a 5min chart means executing your order as soon as possible instead of waiting until the next 5min bar starts building. You can achieve this by


            With calculate on bar close set to true, the logic runs right after the current bar closes, with it false it would run tick-by-tick. You may need to adjust how you are checking for your red/green bars however as a bar could have 1 tick higher than close and it would still satisfy your conditions.

            There unfortunately isn't any way to check for the last tick of a bar using supported code, but for example :

            if ( FirstTickOfBar )
            {

            }

            This checks to see if you are in the next bar at the first tick of that bar.
            Adam P.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by sjsj2732, Yesterday, 04:31 AM
            0 responses
            33 views
            0 likes
            Last Post sjsj2732  
            Started by NullPointStrategies, 03-13-2026, 05:17 AM
            0 responses
            286 views
            0 likes
            Last Post NullPointStrategies  
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            286 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            133 views
            1 like
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            91 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Working...
            X