Sound Strategy, Faulty EA - Logic works, Program doesn't

 

Hello folks. After a very laborious day and an hour-long session of cross-editing with RaptorUK, my Expert Advisor placed one trade, in the very wrongest place ever and never took me out of it.

This is the strategy: (I tried this in Excel, it yielded 200% in two months with a maximum draw down of 19.8% - not bad)

H1 chart, 144 period Simple Moving Average (weekly trend), 24 period bollinger bands (daily trend).

First, The attached program is intended to identify three market conditions: Up, Down or Range (Sideways Channel)

An Up Trend is identified when the Daily trend's moving average is above the weekly trend and both are moving up.

A Down Trend is identified when the Daily trend's moving average is below the weekly trend and both are moving down.

A Range is identified when the weekly trend refuses to move more than 20 pips.


Second, the program trades based on the identified trend

Up Trend: Go Long one lot - liquidate when either the daily trend or weekly trend reverses

Down Trend: Go Short one lot - liquidate when either the daily trend or weekly trend reverses

Range: (this is a little more complicated)

If the Daily moving average is below the weekly and the price opens below the lower bollinger band then go long 0.1 lot after the price opens above the same band

(this is to be done each time this condition is met)

all positions are to be liquidated when the bid price hits the upper band.

If the Daily moving average is below the weekly and the price opens above the upper bollinger band then go short 0.1 lot after the price opens below the same band

(this is to be done each time this condition is met)

all positions are to be liquidated when the ask price hits the lower band.

That's about it. Simple, effective. My excel spreadsheet of this strategy yielded $6000 from $3000, tripling my money in two months over the summer on USDCAD.

But my EA is not making any sense. I thought I got all the logic right but I'm obviously missing something crucial.

If anyone is up to the challenge, I'd like some pointers on what I can do differently.

Here is the code...

Files:
 
trivates:

Hello folks. After a very laborious day and an hour-long session of cross-editing with RaptorUK, my Expert Advisor placed one trade, in the very wrongest place ever and never took me out of it.

This is the strategy: (I tried this in Excel, it yielded 200% in two months with a maximum draw down of 19.8% - not bad)

H1 chart, 144 period Simple Moving Average (weekly trend), 24 period bollinger bands (daily trend).

First, The attached program is intended to identify three market conditions: Up, Down or Range (Sideways Channel)

An Up Trend is identified when the Daily trend's moving average is above the weekly trend and both are moving up.

A Down Trend is identified when the Daily trend's moving average is below the weekly trend and both are moving down.

A Range is identified when the weekly trend refuses to move more than 20 pips.


Second, the program trades based on the identified trend

Up Trend: Go Long one lot - liquidate when either the daily trend or weekly trend reverses

Down Trend: Go Short one lot - liquidate when either the daily trend or weekly trend reverses

Range: (this is a little more complicated)

If the Daily moving average is below the weekly and the price opens below the lower bollinger band then go long 0.1 lot after the price opens above the same band

(this is to be done each time this condition is met)

all positions are to be liquidated when the bid price hits the upper band.

If the Daily moving average is below the weekly and the price opens above the upper bollinger band then go short 0.1 lot after the price opens below the same band

(this is to be done each time this condition is met)

all positions are to be liquidated when the ask price hits the lower band.

That's about it. Simple, effective. My excel spreadsheet of this strategy yielded $6000 from $3000, tripling my money in two months over the summer on USDCAD.

But my EA is not making any sense. I thought I got all the logic right but I'm obviously missing something crucial.

If anyone is up to the challenge, I'd like some pointers on what I can do differently.

Here is the code...


your parameters for MA are not correct, for example in Weekly MA you set to Timeframe 1 Hour (PERIOD_H1).

Check all your parameters in the iMAs.

More i was not looking till now ;-)

double DailyNow=iMA(NULL,PERIOD_H1,24,0,MODE_SMA,PRICE_WEIGHTED,0); //Fast moving average from current bar
double DailyEre=iMA(NULL,PERIOD_H1,24,0,MODE_SMA,PRICE_WEIGHTED,1); //Fast moving average from last bar
double WeeklyNow=iMA(NULL,PERIOD_H1,144,0,MODE_SMA,PRICE_WEIGHTED,0); //Slow moving average from current bar
double WeeklyEre=iMA(NULL,PERIOD_H1,144,0,MODE_SMA,PRICE_WEIGHTED,1);

 
EADeveloper:


your parameters for MA are not correct, for example in Weekly MA you set to Timeframe 1 Hour (PERIOD_H1).

Check all your parameters in the iMAs.

More i was not looking till now ;-)

TimeFrame is H1 but periods are 24 hrs for Daily and 144 for Weekly (6 days) . . so it might be correct. Maybe the shift is wrong though ?

 
 if(Liquidate==true) //Close out all positions, set InMarket to false, set UpTrend, DownTrend, GoLong and GoShort all to False

      {
         if (UpTrend==true)
            {
               OrderClose(65,1,Bid,Slippage,Red); //close UpTrend trade
               UpTrend=False;
            }
      
         if (DownTrend==true)
            {
               OrderClose(56,1,Ask,Slippage,Blue); //close DownTrend trade
               DownTrend=False;
            }
      
         if (GoLong==true)
            {
               for(i=1;i<=Magic;) OrderClose(i,0.1,Bid,Slippage,Red); //close Channel Long trades
               GoLong=False;
            }
  
         if (GoShort==true)
            {
               for(i=1;i<=Magic;) OrderClose(i,0.1,Ask,Slippage,Blue); //close Channel Short trades
               GoShort=False;
            }
         InMarket=False;
         i=1;
      }

You actually need to select which order your closing, unless of course every trade has a ticket number of 65 or 56 or you value of Magic then it will come back with an error and not actually close the trade. What I would suggest is store the ticket numbers along with what the trade is based on in a file and check it once a minute or so, then it will still work even if you change charts but you could just use an array:

string TradeArray[][2];

// On trade open
ArrayResize(TradeArray,ArraySize(TradeArray)+1);
TradeArray[ArraySize(TradeArray)-1][0] = ticket number;
TradeArray[ArraySize(TradeArray)-1][1] = description such as "trend buy", "range sell";

// During start()
 if(Liquidate==true)
{
for(int i=ArraySize(TradeArray);i>=0;i--)
{
if(!OrderSelect(StrToInteger(TradeArray[i][0]),SELECT_BY_TICKET))continue;
if(OrderCloseTime()>0){
for(int j=i;j<ArraySize(TradeArray);j++){
TradeArray[j]=TradeArray[j+1];
}
ArrayResize(TradeArray,ArraySize(TradeArray)-1);
continue;
}

if(TradeArray[i][1]=="TrendBuy"&&UpTrend==true){
OrderClose(StrToInteger(TradeArray[i][0]),1,Bid,Slippage,Red);
UpTrend = false;
}
//So on and so forth
}
}
 

EADeveloper:



your parameters for MA are not correct, for example in Weekly MA you set to Timeframe 1 Hour (PERIOD_H1).

Check all your parameters in the iMAs.

More i was not looking till now ;-)

double DailyNow=iMA(NULL,PERIOD_H1,24,0,MODE_SMA,PRICE_WEIGHTED,0); //Fast moving average from current bar
double DailyEre=iMA(NULL,PERIOD_H1,24,0,MODE_SMA,PRICE_WEIGHTED,1); //Fast moving average from last bar
double WeeklyNow=iMA(NULL,PERIOD_H1,144,0,MODE_SMA,PRICE_WEIGHTED,0); //Slow moving average from current bar
double WeeklyEre=iMA(NULL,PERIOD_H1,144,0,MODE_SMA,PRICE_WEIGHTED,1);


RaptorUK is correct to assume that the Period is correct but I`m getting a daily reading by looking at 24 bars and a weekly reading by looking at 144 bars. 144 bars is exactly six trading days, which, if I am not mistaken, is exactly how long the markets are open every week.
 
heelflip43:

You actually need to select which order your closing, unless of course every trade has a ticket number of 65 or 56 or you value of Magic then it will come back with an error and not actually close the trade. What I would suggest is store the ticket numbers along with what the trade is based on in a file and check it once a minute or so, then it will still work even if you change charts but you could just use an array:


In uptrend, only one trade is executed. I give it number 65 - a sufficiently high number that I don`t believe it will ever be reached by the int variable "Magic" in control of Channel trade ticket numbers. The down trend is treated similarly, but it gets ticket number 56.

When the market gets into a channel, ticket numbers start at one, Magic goes up by one after every channel trade entry and gets reset to 1 after liquidation.

 
trivates:


In uptrend, only one trade is executed. I give it number 65 - a sufficiently high number that I don`t believe it will ever be reached by the int variable "Magic" in control of Channel trade ticket numbers. The down trend is treated similarly, but it gets ticket number 56.

When the market gets into a channel, ticket numbers start at one, Magic goes up by one after every channel trade entry and gets reset to 1 after liquidation.

You don't give an Order a ticket number . . . it is assigned and you can read it.
 
RaptorUK:
You don't give an Order a ticket number . . . it is assigned and you can read it.

then that's my whole problem. how do you read an order number? Order placing is probably my weakest area. Logic is easy. Placing trades and getting out of them is where I'm having difficulty.
 
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES))continue;
Print(OrderTicket());
}
That will print out the ticket numbers for all the open trades from newest to oldest. I'm sure you can take it from there, just be sure to filter by symbol and magic number and OrderType.
 
trivates:

then that's my whole problem. how do you read an order number? Order placing is probably my weakest area. Logic is easy. Placing trades and getting out of them is where I'm having difficulty.
Just to add . . . read the Book or the documentation specific to Orders : https://docs.mql4.com/trading
 

Доброго времени суток уважаемые форумчане!

Меня зовут Герман, мне 23 года, я являюсь трейдером компании "Инстафорекс"

Помогите в поиске нужного скрипта! Скрипт нужен для сетки отложенных ордеров.

Reason: