Announcement

Collapse
No announcement yet.

MACD backtesting?

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

  • MACD backtesting?

    I have a MACD efs which I have tweaked a little bit from suggestions on this bulletin board as well as some of my own ideas. However, I now want to convert it into a backtesting file. All I want to do is make a trade everytime an alert goes off, which shouldn't be too hard, but I'm not sure where to start. If you could help me at all with some of the code to do this, I would really appreciate it! Thanks!
    Attached Files

  • #2
    jstulgis
    The attached revision of your efs includes the commands to trigger the trades
    Alex
    Attached Files

    Comment


    • #3
      Thanks Alexis, that looks pretty good when I run it. I just had a couple more questions for you and was hoping you could point me in the right direction. The backtest you sent triggers trades everytime the MACD lines cross, which is perfect. Is there anyway to write in to make sure that positions are closed at the end of everyday? Basically I want to make it so everyday starts fresh, with the first position of the day coming on the first cross of the day. Also, when I backtested I only got the last 2 days of data, is there a way to tell Esignal which days you want to backtest? Thanks!!
      Attached Files

      Comment


      • #4
        jstulgis
        If using the Formula Wizard you will need to add some conditions and make changes to the existing ones.
        As to the addition here is an example for closing longs (this would be Set3)

        (getHour()*100)+getMinute() >= 1600
        Strategy.isLong() == true

        then the following will happen every time
        Strategy.doSell(insert your parameters)

        As to the existing conditions you will need to change them so that a trade happens only on a cross. So, for example the long side would need to be as follows

        (getHour()*100)+getMinute() >= 930
        (getHour()*100)+getMinute() < 1600
        Strategy.isLong() == false
        vMACD20_43.getValue(MACDStudy.SIGNAL) < vMACD20_43.getValue(MACDStudy.MACD)
        vMACD20_43.getValue(MACDStudy.SIGNAL,-1) > vMACD20_43.getValue(MACDStudy.MACD,-1)

        then the following will happen every time
        Strategy.doLong(insert your parameters)

        Notice that the Strategy command no longer executes the "first time" but is now set to "every time" since the discriminator is provided by a more detailed definition of the cross.

        As to the number of days to backtest you control that through the Time Template
        Hope this helps
        Alex

        Comment


        • #5
          Hi Alexis, thanks again for your post. I took it home tonight and tried to make the changes that you said but it didn't seem to work. I tried to just do the first part (that you said would be set 3) and that didn't seem to make any difference. And then when I tried to do the rest, there were no trades done when I ran the backtest. I'll attach it here and hopefully you can point out where I went wrong. Basically the first backtest was great, all I want to change is that I want to be flat at the end of each day. So if a crossover got me long at 2:30, then if it still had me long at the close of the day I would want a trade sent to get me flat. Thanks so much!

          jeff
          Attached Files

          Comment


          • #6
            jeff
            With regards to the issue of no trades being triggered this is due to an error in your conditions.
            In Set1 (and also in Set2 with reversed operators) you have the following

            vMACD20_43.getValue(MACDStudy.SIGNAL) > vMACD20_43.getValue(MACDStudy.SIGNAL) &&
            vMACD20_43.getValue(MACDStudy.SIGNAL, -1) < vMACD20_43.getValue(MACDStudy.SIGNAL, -1)


            instead of

            vMACD20_43.getValue(MACDStudy.SIGNAL) > vMACD20_43.getValue(MACDStudy.MACD) &&
            vMACD20_43.getValue(MACDStudy.SIGNAL, -1) < vMACD20_43.getValue(MACDStudy.MACD, -1)


            With regards instead to the closing of trades at the end of the day that depends on the market hours of the symbol you are using in testing, the interval and the Time Template. When you set the closing time you need to ensure that there is going to be at least a bar that has a time stamp equal or greater than the cutoff time. In my example I assumed the symbol was ES or NQ with a Time Template from 9:30-16:15 ET and an interval no higher than 15 minutes.
            Hope this helps
            Alex

            Comment

            Working...
            X