Announcement

Collapse
No announcement yet.

EMA cross over failing to run despite...

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

  • EMA cross over failing to run despite...

    Dear eSignal,

    I was a subscriber in 04-05 followed by some unsuccessful trading a hiatus and now a return. Fortunately I saved my old code on my external drive. However despite the code coming from a template one genius Alexis Montenegro put on the net it no longer runs. I mean it passes the sythax checks but produces nonsensical strategy results. Note its intended for daily intervals in currencies and at the moment is setup specifically for the Euro. The closest to understanding the problem is that the "Strategy.doShort(... etc command does no longer recognise the specification of where or when with a bar to place a trade ie on line 222 ignores ...high() - entryfromlowhigh... and instread just executes Every trade at midnight. If anyone can make this what is to someone with experience Very basic code run I'd really appreciate it. I have tried searching knowledge base for synthax and for formalas on this most basic of strategies but to date this only leads to much time pushing on a string.

    So hopefully Thanks in advance,

    exponentials
    Attached Files

  • #2
    Hi Exponentials,

    I built an MA crossover backtester a while ago for the S&P futures market, which I have quickly adapted for what I think you are looking for.

    It’s not complete as it only goes short.

    The EFS goes short the first time the fast Ema crosses below the slow Ema, and has a profit target and a stoploss.

    I used the USD/EURO future (6E 1!) and it seems to work.

    Anyway I hope this works.

    Yours sincerely
    Aengus
    Attached Files

    Comment


    • #3
      Midnight entries

      Dear Aengus,

      Thank you for your help on the code. Ive been doing some reading and come up with the attached but still the strategy analyser persists in opening and closing at the midnight hour. I see this happens on your own code when I ruin it on the euro. Even though I specify entry at for longs the low of the next bar plus a number of pips and I specify fill type stop, next bar, vice versa for shorts. What gives??

      May be if I can get Alexis to give it the glad eye we can be on the road.

      Thanks

      expontentials

      NB Attached code intended for spot currency - Euro denomination
      Attached Files

      Comment


      • #4
        LIMIT entry on THISBAR for intra bar entry specification?? but doesnt work

        Dear eSignal,

        Ive been reading through the tutorials and came accross what I thought was the answer to my problems regarding trying to specify a intra bar entry price based off a low for longs a high for shorts. But the use of THISBAR and a fill type of LIMIT doesnt seem to work. (please see code attached) intended for use onthe Euro spot currency. Can someone please advise.

        Thank you,

        exponentials
        Attached Files

        Comment


        • #5
          Exponentials,

          I have had a look at your code and think I have spotted two things.

          The first is that if you are running it on a daily bar chart, where the close of each bar is midnight and that is why your entry’s and exits are all at midnight, try running it on an hourly chart, you’ll see all the entry’s and exits are at the close of the hourly bars.

          I don’t trade on either of these time charts or currencies so I don’t know whether you could chart the time templates to suit you’re trading times, as in trying to set up the close of bars to be at 9am or something.

          A problem I was having with my backtests vs real-time trading was false signals occurring in real-time before the close of a bar so I changed all my signals to the open of the bars as they cannot change in real time, maybe you could set up your signal the same way and change the time template to Daily 9am to 9pm or when ever volume is the greatest.

          Anyway this might be something you could consider to move away from midnight.


          The second thing I noticed is that you have your signs the wrong way round on your stops.
          It should be “+” on your shorts and “-“on your Longs.

          This seems to be stopping you out on the same bar as entry.

          I have reattached your formula with the “+ and –“ changed and I have change the ema to “open”
          I have run it on the EURO/USD future (6E 1!) on a time template of 9am to 9pm over 100 days and got these results:


          Hope this helps.
          Aengus
          Attached Files

          Comment


          • #6
            cant post results, sorry

            Comment


            • #7
              I posted my problem over a week ago but no instructor has offered a hand so Im faced with paying a programmer to fix what is to an instructor is a very simple problem. Does anyone know how you get an instructors attention??

              exponenitals

              Im settling for the entry on the current bar rather then the next day as it seems the efs does not allow this with its intrabar specifier .LIMIT and .THISBAR. but now nothing works as not trades show.

              *----------------------------------------------------------
              Sample Profit Target/Trailing Stop Code
              ----------------------------------------------------------

              Declare new global variables for the code. Notice we are
              Only adding one new variables to handle the stop loss
              Function - nStopLevel.
              nStopLevel = our stop/trailing stop level.

              ----------------------------------------------------------*/

              var nNewTrade; // New Trade Trigger 0 = OFF / 1 = ON
              var nsignal; // returns the direction of the trading signal
              //var nTradeEntryPrice;
              var ProfitTarget1 = .0400;
              var nStopLevel;
              var nDistanceOffLowHighStop = .0050;
              var nDistanceOffLowHighEntry = .0040;
              //xEMAfast = ema(7);
              //xEMAslow = ema(29);
              //var study1;
              //var study2;



              function preMain()
              {
              setPriceStudy(true);
              setStudyTitle("Sample Profit Code");
              setCursorLabelName("PT CODE");
              }


              function main() {



              /*----------------------------------------------------------------
              // If new trade, get entry price - used for our profit target
              ----------------------------------------------------------
              This portion of the code identifies if a new trade has been issued
              and records the entry price of our trade. If no new trade has been
              triggered (nNewTrade == 1), then this portion of the code is ignored.
              ----------------------------------------------------------*/

              xEMAfast = ema(7);
              xEMAslow = ema(29);


              var vMAfast = xEMAfast.getValue(0);
              var vMAslow = xEMAslow.getValue(0);
              //var bExitFlag = false;

              /* if(study1 == null && study2 == null)
              {
              //study1 = new MAStudy(7, 0, "Close", MAStudy.EXPONENTIAL);
              //study2 = new MAStudy(29,0, "Close", MAStudy.EXPONENTIAL);
              }
              */


              //var vMAfast = study1.getValue(MAStudy.MA);
              //var vMAslow = study2.getValue(MAStudy.MA);

              /*
              if(vMAfast == null || vMAslow == null)

              if (Strategy.isInTrade() == true && (nNewTrade == 1)) {
              // This sets the expected entry price of the current short trade
              if (Strategy.isShort ==true)
              {
              nTradeEntryPrice = high(0) - nDistanceOffLowHighEntry;
              }
              else
              {
              nTradeEntryPrice = low(0) + nDistanceOffLowHighEntry;
              }
              // This switches off the nNewTrade variable
              nNewTrade = 0; // Turn off NEW TRADE switch
              }
              */

              /*----------------------------------------------------------------
              // Test for Profit Target Breach (ProfitTarget1)
              ----------------------------------------------------------
              This portion of the code identifies if our profit target has
              been reached and exits our trades.
              ----------------------------------------------------------*/

              if (Strategy.isInTrade() == true && (Strategy.isShort() == true)) {
              // Check if the profit target has been reached/breached
              if (low() <= (nTradeEntryPrice - ProfitTarget1)) {
              // Profit Target Breached, Execute Cover order.
              Strategy.doCover("Short PT1 Exit", Strategy.LIMIT, Strategy.THISBAR,
              Strategy.ALL, (nTradeEntryPrice - ProfitTarget1));
              }
              }

              if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
              // Check if the profit target has been reached/breached
              if (high() >= (nTradeEntryPrice + ProfitTarget1)) {
              // Profit Target Breached, Execute Sell order.
              Strategy.doSell("Long PT1 Exit", Strategy.LIMIT, Strategy.THISBAR,
              Strategy.ALL, (nTradeEntryPrice + ProfitTarget1));
              }
              }


              /*----------------------------------------------------------
              This portion of the code tests for a stop level breach and
              executes trades accordingly.
              ----------------------------------------------------------*/
              if (Strategy.isInTrade() == true && (Strategy.isShort() == true)) {
              // Check if the profit target has been reached/breached
              if (high() >= nStopLevel) {
              // Stop Breached, Execute Cover order.
              Strategy.doCover("Short Stop Exit", Strategy.LIMIT, Strategy.THISBAR,
              Strategy.ALL, nStopLevel);
              }
              }

              if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
              // Check if the profit target has been reached/breached
              if (low() <= nStopLevel) {
              // Stop Breached, Execute Sell order.
              Strategy.doSell("Long Stop Exit", Strategy.LIMIT, Strategy.THISBAR,
              Strategy.ALL, nStopLevel);
              }
              }


              /*----------------------------------------------------------
              This portion of the code calculates and sets the new stop levels.
              ----------------------------------------------------------*/
              /*dont need trailing stops
              if (Strategy.isInTrade() == true && (Strategy.isShort() == true)) {
              nStopLevel = high(-1) + 0.25;
              }

              if (Strategy.isInTrade() == true && (Strategy.isLong() == true)) {
              nStopLevel = low(-1) - 0.25;
              }
              */

              /*----------------------------------------------------------------
              // Identify new trade signals
              ----------------------------------------------------------------- */

              if (Strategy.isInTrade() == false) {
              if (vMAfast > vMAslow) {
              nNewTrade = 1; // New Trade Trigger
              nsignal = 1; // Buy Signal - Trade Type
              }
              if (vMAfast < vMAslow) {
              nNewTrade = 1; // New Trade Trigger
              nsignal = -1; // Sell Signal - Trade Type
              }
              }


              /*----------------------------------------------------------------
              // Execute Trades ONLY if nNewTrade is triggered ....
              ----------------------------------------------------------------- */

              if (nNewTrade == 1) { //Execute New Trade
              // new or reversed trade position
              if (Strategy.isInTrade() == true) {
              if ((nsignal > 0) && (Strategy.isShort() == true)) {
              var nTradeEntryPrice = (low(0) + nDistanceOffLowHighEntry);
              Strategy.doCover("Exit Short", Strategy.MARKET, Strategy.THISBAR);
              Strategy.doLong("Rev Long", Strategy.LIMIT, Strategy.THISBAR,Strategy.ALL , nTradeEntryPrice);
              nStopLevel = (low(0) - nDistanceOffLowHighStop);
              }
              if ((nsignal < 0) && (Strategy.isLong() == true)) {
              var nTradeEntryPrice = (high(0) - nDistanceOffLowHighEntry);
              Strategy.doSell("Exit Long", Strategy.MARKET, Strategy.THISBAR);
              Strategy.doShort("Rev Short", Strategy.LIMIT, Strategy.THISBAR,Strategy.ALL, nTradeEntryPrice);
              nStopLevel = (high(0) + nDistanceOffLowHighStop);
              }
              } else {
              if ((nsignal > 0)) {
              var nTradeEntryPrice = (low(0) + nDistanceOffLowHighEntry);
              Strategy.doLong("Go Long", Strategy.LIMIT, Strategy.THISBAR,Strategy.ALL, nTradeEntryPrice);
              nStopLevel = (low(0) - nDistanceOffLowHighStop);
              }
              if ((nsignal < 0)) {
              var nTradeEntryPrice = (high(0) - nDistanceOffLowHighEntry);
              Strategy.doShort("Go Short", Strategy.LIMIT, Strategy.THISBAR,Strategy.ALL, nTradeEntryPrice);
              nStopLevel = (high(0) + nDistanceOffLowHighStop);
              }
              } // end if IN TRADE
              } //
              if (Strategy.isShort() == true) setBarBgColor(Color.black);
              if (Strategy.isLong() == true) setBarBgColor(Color.blue);

              }

              Comment

              Working...
              X