Download MetaTrader 5

Expert Advisor: 2 Parabolic Sar Signals

To add comments, please log in or register
Franik
28
Franik  

I need help about the writing of an EA.

I have used 2 Parabolic Sar Indicator with different settings. I used the library "SignalSAR.mqh" to create the output, but in the library there are the conditions of buying and selling only for using 1 Indicator.

My project is (after the choice of the parameters of Indicators, Lots and etc ):

1- Fix the library "SignalSar.mqh" (I want only to receive the value of the 2 Sar Indicators without the execution of the selling and buying);

2- Create this cycle        -> IF there is no open order and  IF the value of SAR1 and SAR2 are below the current price -> go LONG: open an order

                                 -> IF there is no open order and  IF the value of SAR1 and SAR2 are above the current price -> go SHORT: open an oder

                                 -> IF there is 1 open order and IF (the value of SAR1 is below the current price and SAR2 is above the current price) or (SAR2 is below and SAR1 is above the current price) -> close the order

 

I don't know how to receive the 2 different values of the Indicator from the library and using them how I have written before.

I'll really appreciate your help and explanations. 

Alain Verleyen
Moderator
31182
Alain Verleyen  
You need a custom module for that.
Franik
28
Franik  
angevoyageur:
You need a custom module for that.

I found a way to do that. I used the iSar function to create an handle and then with copybuffer, transfer these values in 2 arrays set as a series because I want to manage the values of the last 2 bars of price and the current one.

I am attaching an image so you can see what I did and the error I receive.

As I wrote before I need an array set as a series so I can always have the values updated (the value of the current bar and the values of the 2 bars before the current one).

 

Thank you for your time. I'll wait your answer. 

Files:
Error.PNG 24 kb
Alain Verleyen
Moderator
31182
Alain Verleyen  
Franik:

I found a way to do that. I used the iSar function to create an handle and then with copybuffer, transfer these values in 2 arrays set as a series because I want to manage the values of the last 2 bars of price and the current one.

I am attaching an image so you can see what I did and the error I receive.

As I wrote before I need an array set as a series so I can always have the values updated (the value of the current bar and the values of the 2 bars before the current one).

 

Thank you for your time. I'll wait your answer. 

It's not an error, it's a warning. You can only use ArraySetAsSeries() for dynamic arrays. So you have to declare your arrays as :

   double SarVal1[];
Franik
28
Franik  
angevoyageur:

It's not an error, it's a warning. You can only use ArraySetAsSeries() for dynamic arrays. So you have to declare your arrays as :

Thank you very much!
I wrote in my code " double SarVal1[3] ". It's a week I am approaching to this field and I am in phase of learning, so I am sorry if these were "stupid questions".
I really thank you for your kindness.
Franik
28
Franik  

My last problem (I hope) is that my EA is able to open order and so position, but I don't succeed in closing them. 

In the last part of the source code there are the conditions to modify stop loss and take profit (because I didn't find anything on Internet how to close positions) . My goal is:

--- If the numer of positions is >1 and if the symbol is the current one and If the values of Sar are correct and If the position type is Sell/Buy (there are 2 cases) -> Close the position

 

I am attaching the source code so you can see what I did.

Thank you!! ;) 

Files:
Alain Verleyen
Moderator
31182
Alain Verleyen  
Franik:

My last problem (I hope) is that my EA is able to open order and so position, but I don't succeed in closing them. 

In the last part of the source code there are the conditions to modify stop loss and take profit (because I didn't find anything on Internet how to close positions) . My goal is:

--- If the numer of positions is >1 and if the symbol is the current one and If the values of Sar are correct and If the position type is Sell/Buy (there are 2 cases) -> Close the position

 

I am attaching the source code so you can see what I did.

Thank you!! ;) 

With MT5/mql5 a position is simply closed by sending a order request in the opposite direction with the same volume.

So if you have a 0.5 BUY position, you just have to send an order to SELL 0.5 of the same instrument.

Franik
28
Franik  
angevoyageur:

With MT5/mql5 a position is simply closed by sending a order request in the opposite direction with the same volume.

So if you have a 0.5 BUY position, you just have to send an order to SELL 0.5 of the same instrument.

Thank you for that advice.

Now I want to use a trailing system in my code. In the picture you can see what I did. I don't know what I wrong because in test I see that the EA doesn't change the stop loss.

Maybe you understand where is my error. 

Files:
Trailing.PNG 29 kb
Alain Verleyen
Moderator
31182
Alain Verleyen  
Franik:

Thank you for that advice.

Now I want to use a trailing system in my code. In the picture you can see what I did. I don't know what I wrong because in test I see that the EA doesn't change the stop loss.

Maybe you understand where is my error. 

You have to use PositionSelect() before using PoisitionGetXXX functions.
Franik
28
Franik  
angevoyageur:
You have to use PositionSelect() before using PoisitionGetXXX functions.

Thank you again. I have used PositionSelect() as you said, and after few adjustments (using also the freeze level to change the sl) I wrote this code for trailing, but in test my EA doesn't change the level of the stop loss.

Can you tell me what's wrong?

 Ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK);

   Bid = SymbolInfoDouble(_Symbol, SYMBOL_BID);

   Spread=int(SymbolInfoInteger(_Symbol,SYMBOL_SPREAD));

   Freeze=int(SymbolInfoInteger(_Symbol,SYMBOL_TRADE_FREEZE_LEVEL));

   if(PositionSelect(_Symbol)== true)

     {

      if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

          { 

            if(PositionGetDouble(POSITION_PRICE_CURRENT)>PositionGetDouble(POSITION_SL)+MathMax(Freeze,SL*_Point) +Spread+ Margine*_Point)

             {

               request.action = TRADE_ACTION_SLTP;

               request.symbol = _Symbol;

               request.sl =PositionGetDouble(POSITION_PRICE_CURRENT)-Freeze-Spread;

               OrderSend(request,result);

               if(result.retcode==10009 || result.retcode==10008) // request executed

                  Print("Moving Stop Loss of Buy position #",request.sl);

               else

                 {

                  Print(ResultRetcodeDescription(result.retcode));

                  return;

                 }

               return;

              }

              return;

          }

          

         if(PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

         {

            if(PositionGetDouble(POSITION_PRICE_CURRENT)<PositionGetDouble(POSITION_SL)-MathMax(Freeze,SL*_Point) -Spread- Margine*_Point)

            {

               request.action = TRADE_ACTION_SLTP;

               request.symbol = _Symbol;

               request.sl =PositionGetDouble(POSITION_PRICE_CURRENT)+Freeze+Spread;

               OrderSend(request,result);

              

               

               if(result.retcode==10009 || result.retcode==10008) // request executed

                  Print("Moving Stop Loss of Sell position #",request.sl);

               else

                 {

                  Print(ResultRetcodeDescription(result.retcode));

                  return;

                 }

              return;

            }

           return; 

         }

         return;

      } 
Alain Verleyen
Moderator
31182
Alain Verleyen  

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


12
To add comments, please log in or register