Topic: Help on a strategy

I have a strategy where I use moving averages.

In the strategy builder we don't need to specify long or short positions as we need to specify only the conditions and strategy builder takes it as a long position and it automatically decides the short positions.

I am entering a long position at at the Moving Average* (Simple, Low, 5, 0) when some other conditions are met. The short positions are also entering at the Moving Average* (Simple, Low, 5, 0) but I want to enter the short positions at the Moving Average* (Simple, High, 5, 0).

Can someone help me how to do this?

Thank you.

Re: Help on a strategy

Try that custom indicator I made for you.

Edit

Indicator removed due to a bug.

Re: Help on a strategy

This version of the indicator applies the "Use previous bar value" option correctly.

Post's attachments

Moving Average High-Low.cs 8.4 kb, 24 downloads since 2010-10-04 

You don't have the permssions to download the attachments of this post.

Re: Help on a strategy

Thank you very much for the custom indicator. I have placed it under the custom indicators folder and it is showing up for the opening logic condition. But for the closing logic condition it is not showing up. Do I have to save it in any other folders?

Thank you.

Re: Help on a strategy

hello.i am new here.i would like to have a indicator to buy if moving avg(5 day,simple)crosses (7 day,expo).
pls,help

6 (edited by powerstar 2010-10-18 02:53:59)

Re: Help on a strategy

Hi,

How can I set a trade alert for me to receive an e-mail or SMS on details of trades done whenever there is a trade either open or close ?

Thanks

Yes thanks for that but the broker says that the EA must include the e-mail alert instructions. So how do we include the e-mail instructions to the EA via the Forex Stratergy Builder ? This is the problem.

Many thanks in advance

Re: Help on a strategy

Yes thanks for that but the broker says that the EA must include the e-mail alert instructions. So how do we include the e-mail instructions to the EA via the Forex Stratergy Builder ? This is the problem.

Many thanks in advance

Re: Help on a strategy

Is there a way to change the break even so that it stops at a 2 to 3 pip positive position

Re: Help on a strategy

Thanks Footon,    I think it would be great if that were an option,  I was also wondering if there were an option for the trailing stop to start trailing in profit only,  to lock in some kind of profit instead of slipping into negative territory...

Thanks

Re: Help on a strategy

Here is how to make Break Even closing on several points profit.

Find the "External variables" section on line 80 and add this external variable:

extern int Break_Even_Shift = 5;

It will be visible when starting the expert and you can set the Break Even shift in points. The default is 5, but you can change it.

After that find the "void SetBreakEvenStop(string symbol)" function on line 1031 and replace it with that one:

///
/// Sets a BreakEven stop of current positions. 
/// 
void SetBreakEvenStop(string symbol)
{
    if (SetAggregatePosition(symbol) <= 0)
        return;

    double breakeven = MarketInfo(symbol, MODE_STOPLEVEL);
    if (breakeven < BreakEven)
        breakeven = BreakEven;
        
    double breakprice = 0; // Break Even price including commission.   
    double commission = 0; // Commission in pips.
    if (PositionCommission != 0)
        commission = MathAbs(PositionCommission) / MarketInfo(symbol, MODE_TICKVALUE);
        
    double point  = MarketInfo(symbol, MODE_POINT);
    double digits = MarketInfo(symbol, MODE_DIGITS);
    
    if (PositionType == OP_BUY)
    {
        double bid = MarketInfo(symbol, MODE_BID);
        breakprice = NormalizeDouble(PositionOpenPrice + point * Break_Even_Shift + point * commission / PositionLots, digits);
        if (bid - breakprice >= point * breakeven)
            if (PositionStopLoss < breakprice)
            {
                SetStopLossAndTakeProfit(symbol, breakprice, PositionTakeProfit);
                Print("Break Even (", BreakEven, " pips) set Stop Loss to ",  breakprice, ", Bid = ", bid);
            }
    }
    else if (PositionType == OP_SELL)
    {
        double ask = MarketInfo(symbol, MODE_ASK);
        breakprice = NormalizeDouble(PositionOpenPrice - point * Break_Even_Shift - point * commission / PositionLots, digits);
        if (breakprice - ask >= point * breakeven)
            if (PositionStopLoss == 0 || PositionStopLoss > breakprice)
            {
                SetStopLossAndTakeProfit(symbol, breakprice, PositionTakeProfit);
                Print("Break Even (", BreakEven, " pips) set Stop Loss to ",  breakprice, ", Ask = ", ask);
            }
    }
}

There are two modifications only. For long position:

breakprice = NormalizeDouble(PositionOpenPrice + point * commission / PositionLots, digits);
changed to:
breakprice = NormalizeDouble(PositionOpenPrice + point * Break_Even_Shift + point * commission / PositionLots, digits);

And for short position:

breakprice = NormalizeDouble(PositionOpenPrice - point * commission / PositionLots, digits);
changed to:
breakprice = NormalizeDouble(PositionOpenPrice - point * Break_Even_Shift - point * commission / PositionLots, digits);

Re: Help on a strategy

Hi,
Many thanks for finding that EA for me. I tried it on demo and it actually send the mail alert but the contents are not filled and all zero. It gives a date with the year 1970 and I do not know from where it is fetching the year 1970. My computer date is correct as well as the metatrader explorer have been changed. Here is the sample message which I received on my hand phone.

GBPUSD    OpenTime: 1970.01.01 00:00   Close Time: 1970.01.01 00:00                 
Order Type BUY   Open 0.0000   Close 0.0000  Profit (0.0000)

I would appreciate if you could kindly check this and let me know.

On the other hand if there is a drop down item in the FSB ( forex Stratergy Builder) which could be inserted easily in to the EA would be very attractive. Can you please suggest this point to the owner please.

Thank you

Re: Help on a strategy

powerstar wrote:

GBPUSD    OpenTime: 1970.01.01 00:00   Close Time: 1970.01.01 00:00                 
Order Type BUY   Open 0.0000   Close 0.0000  Profit (0.0000)

Thank you

Just a guess (without actually looking at any of the code) but it looks like the variables for passing the time and order info are not being set. The 1970 date is like the default for time objects.

Re: Help on a strategy

Hi,

Here is the coding, please note not only the date is not functioning, the price details and the profits are also not updating.

Thanks

//+------------------------------------------------------------------+
//|                                               MailAlert TEST.mq4 |
//|                                                      Nicholishen |
//|                                                                  |
//+------------------------------------------------------------------+
//http://www.forex-tsd.com/metatrader-4/1125-code-send-email-alerts-when-closed-trades.html
#property copyright "Nicholishen"
#property link      ""
int k;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
for(int i=0;i<1000;i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         if(OrderSymbol()==Symbol()  ){
            k++;
         }
      }else{
         break;
      } 
   }
   Comment("Init() Trades Count = ",k);
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

int f =0;
   for(int i=0;i<10000;i++){
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
         if(OrderSymbol()==Symbol() /*&& OrderMagicNumber()==MAGICMA && OrderComment() ==Cmt(Period())*/ ){
            f++;
         
         }
      }else{
         break;
      } 
   }
   Comment(" Trades in History ",f," Init cnt ",k  );
   if(k < f){
      string ordertyp;
     
      OrderSelect(f,SELECT_BY_POS,MODE_HISTORY);
      if(OrderType()==0)ordertyp="BUY";
      if(OrderType()==1)ordertyp="SELL";
     // SendMail("HI","HI");
      SendMail("CLOSED TRADE","  "+Symbol()+"    OpenTime: "+TimeToStr(OrderOpenTime())+"   Close Time: "+TimeToStr(OrderCloseTime())+"                     "+
      "Order Type "+ordertyp+"   Open "+DoubleToStr(OrderOpenPrice(),4)+"   Close "+DoubleToStr(OrderClosePrice(),4)+"  Profit ("+DoubleToStr(OrderProfit(),4)+")" );
      k++;
   }
return;
}


//----
 
//+------------------------------------------------------------------+

Re: Help on a strategy

Popov,  Thanks for all the help and for all the great work!!   

I am a little confused though..
When you say
"After that find the "void SetBreakEvenStop(string symbol)" function on line 1031 and replace it with that one:"

what am I replacing it with?

Thanks again..    big_smile

Re: Help on a strategy

what am I replacing it with?

Replace the whole function
void SetBreakEvenStop(string symbol)
with the code shown below.

Re: Help on a strategy

Thanks Again,  I think I got it

Re: Help on a strategy

Hi footon,

Can you please post what I should do to correct the above problem of sending traded history by mail as I am a new commer to Mt4.

Thanks