Help on Trix Trend Line

 

Hello,

I'm using indicator: THV (Turhovich) Trix Divergence version 4 to read the trend. I also discover how to use this indicator into my algorithm with iCustom() function. Unfortunately, there's a problem occurred with this indicator. I also attached THVTrixDiv.mq4 below.

There are 6 buffers. First until fourth buffer belong to the line of Trix and the rest of buffers belong to arrow. I don't pay attention too much for this arrow value since I only use the trix line.

As you know, this indicator shown in Separate Window with two line shows which direction the trend will be. One line for Fast Period and the other one is for Slow Period. Each of this line consist two values which is Green and Red. Green for Trend Up and vice-versa. And when line Green is shown, the value of Red is empty as known as empty_value.

So, to use them in EA I have to write in iCustom() function like below:

double
SlowGreen = iCustom (Symbol(), 0, "THVTrixDiv", SlowPeriod, 0, 0),
SlowRed = iCustom (Symbol(), 0, "THVTrixDiv", SlowPeriod, 1, 0),
FastGreen = iCustom (Symbol(), 0, "THVTrixDiv", FastPeriod, 2, 0),
FastRed = iCustom (Symbol(), 0, "THVTrixDiv", FastPeriod, 3, 0);

My objective is simple. Open Long Position when all the Trix is Green or Short Position when all Trix is Red. The idea of Take Profit is when several points captured. After TP all I want to do is make it idle until Trix change color (or value).

Here the example:

When both of Trix is Green, then Ordersend() buy is commence (shown with Green Arrow Down and Light Turquoise Arrow Left)

After several points, Orderclose() is run to take profit (shown by Turquoise Arrow Left).

By the time the order is closed, both of Trix still green (shown by Red Arrow Up). While the Trix (I chose to focus more on the FS) still Green (shown with Red Circle), the EA should be in standby.

The trouble came when I cannot make it idle. I already used While() or For() as a looping. I've tried those function and it can't work as well. This is the algorithm When I use While() function in some call function:

void LoopingGreen()
   {
      int m = 0;
      double a = empty_value,
             Green0 = iCustom (Symbol(), 0, "THVTrixDiv", FastPeriod, 2, 0);
      
      while (Green0 != a)
         {
            m++;
         }
   }

So, after Orderclose() for long position, EA will continue to LoopingGreen() function. The idea is logic but it can't work. It made the MetaTrader freeze (forward and backtest). I also change:

double a = empty_value;

to

int a = empty_value;

to

int a = 2147483647;

to

double a = 2147483647;
It's useless because still cause freeze. I think there's some problem with empty_value but I can't figure it out yet.

Maybe I use wrong parameter on iCustom() function or maybe there's something I missed how to use algorithm when including empty_value variable. Please help me. Thanks before for your attention.

Regards,

Aras


This is my simple source code:

int start()
{
   double
   STGreen = iCustom (Symbol(), 0, "THVTrixDiv", SlowTrixPeriod, 0, 0),
   STRed   = iCustom (Symbol(), 0, "THVTrixDiv", SlowTrixPeriod, 1, 0),     
   FTGreen = iCustom (Symbol(), 0, "THVTrixDiv", FastTrixPeriod, 2, 0),
   FTRed   = iCustom (Symbol(), 0, "THVTrixDiv", FastTrixPeriod, 3, 0);

   if (OrdersTotal() < 1)
      {
            if(STGreen != EMPTY_VALUE && FTGreen != EMPTY_VALUE)
               {
                  Alert ("BUY");
                  OrderSend (Symbol(), OP_BUY, Lot, Ask, Slippage, 0, 0, BuyComment, 1404, 0, Blue);
               }
                           
            else if (STRed != EMPTY_VALUE && FTRed != EMPTY_VALUE)
               {
                  Alert ("SELL");
                  OrderSend (Symbol(), OP_SELL, Lot, Bid, Slippage, 0, 0, SellComment, 0304, 0, Blue);
               }
      }
   else
      {
         for (int i = OrdersTotal() - 1; i >= 0; i--)
            {
               if (OrderSelect(0, SELECT_BY_POS, MODE_TRADES) == true)
                  {
                     double CloseBuy  = OrderOpenPrice() + TakeProfit * Point,
                            CloseSell = OrderOpenPrice() - TakeProfit * Point,
                            LossBuy   = OrderOpenPrice() - StopLoss * Point,
                            LossSell  = OrderOpenPrice() + StopLoss * Point;

                     if (OrderType() == OP_BUY)
                        {
                           if (Bid >= CloseBuy || Bid <= LossBuy)
                              {
                                 OrderClose (OrderTicket(), OrderLots(), Bid, Slippage, Green);
                                 LoopingGreen();
                              }  
                        }
                        
                     if (OrderType() == OP_SELL)
                        {
                           if (Ask <= CloseSell || Ask >= LossSell)
                              {
                                 OrderClose (OrderTicket(), OrderLots(), Ask, Slippage, Red);
                                 LoopingRed();
                              }
                        }
                  }
            }
      }

   return;
}

void LoopingGreen()
   {
      int m = 0;
      double a = empty_value,
             Green = iCustom (Symbol(), 0, "THVTrixDiv", FastPeriod, 2, 0);
      
      while (Green != a)
         {
            m++;
         }
   }

void LoopingRed()
   {
      int m = 0;
      double a = empty_value,
             Red = iCustom (Symbol(), 0, "THVTrixDiv", FastPeriod, 2, 0);
      
      while (Red != a)
         {
            m++;
         }
   }

Files:
thvtrixdiv.mq4  26 kb
 
  1. rex_loner:
    So, after Orderclose() for long position, EA will continue to LoopingGreen() function. The idea is logic but it can't work. It made the MetaTrader freeze (forward and backtest). I also change:

    Of course it does because of void LoopingGreen() No sleep, no refresh.

    Instead try this form

    int start(){
       ...
       if (count > 0){
           // handle open orders...
           return(0);
       }
       int newDIR = ... // set OP_BUY/OP_Sell per your criteria
    
       static int prevDIR=-1; // Open a new trade
       if (newDIR == prevDIR) return(0); // Wait until direction change
       prevDIR = newDIR;
       // open new trade...

  2. for (int i = OrdersTotal() - 1; i >= 0; i--)
                {
                   if (OrderSelect(0, SELECT_BY_POS, MODE_TRADES) == true)
    If (TRUE == true) is redundant.
    This makes the EA incompatible with every other, including itself on other charts. Always use magic number.
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
    

  3. if (OrdersTotal() < 1)
    Again incompatible.
    int count=0;
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
            OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol() ){              // and my pair.
            count++;
        }
    if (count == 0){

  4. OrderSend (Symbol(), OP_SELL, Lot, Bid, Slippage, 0, 0, SellComment, 0304, 0, Blue);
    CloseBuy  = OrderOpenPrice() + TakeProfit * Point,
    EAs must adjust for 5 digit brokers, TP, SL, AND slippage.
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 

thanks for the reply,

I understand for number 2 until 4. But for:

int start(){
   ...
   if (count > 0){
       // handle open orders...
       return(0);
   }
   int newDIR = ... // set OP_BUY/OP_Sell per your criteria

   static int prevDIR=-1; // Open a new trade
   if (newDIR == prevDIR) return(0); // Wait until direction change
   prevDIR = newDIR;
   // open new trade...

I'm still getting confuse about setting the OP_BUY or OP_SELL on int newDIR. Based on what criteria I have to set them?

And for your statement saying: "Of course it does because of void LoopingGreen() No sleep, no refresh." about my looping problem. What do you mean with no Sleep() and no Refresh()? Do I have to use those function in Looping?

Can you please make some simple example for me so I can fully understand? Thank you for your attention

 

rex_loner:

I'm still getting confuse about setting the OP_BUY or OP_SELL on int newDIR. Based on what criteria I have to set them?

And for your statement saying: "Of course it does because of void LoopingGreen() No sleep, no refresh." about my looping problem. What do you mean with no Sleep() and no Refresh()? Do I have to use those function in Looping?

  1. Your EA is already deciding to buy or to sell. Instead of
    if (condition){ orderSend(buy) }
    else (condition2){ orderSend(sell) }
    I meant something like:
    int new=-1
    if (condition) new=1
    elseif (condition2) new=2
    
    if (new == prev) return
    if (new==1){ orderSend(buy)
    elseif(new==2){ orderSend(sell) }

  2. Not in all loops. Your loop was to WAIT for a condition, but the loop had no sleep and didn't refresh so it was just repeatedly asking the same question and getting the same result. There is no need for that normally, JUST return and reevaluate on the next tick/next bar
 
thanks for the reply.. I do understand now about the logic of waiting the signal. What about if I insist to use While() function to 'wait' the signal? How to write this 'wait' algorithm with While() or For() function?
 
rex_loner:
thanks for the reply.. I do understand now about the logic of waiting the signal. What about if I insist to use While() function to 'wait' the signal? How to write this 'wait' algorithm?
Hi rex,

You asked - "Maybe I use wrong parameter on iCustom() function or maybe there's something I missed how to use algorithm when including empty_value variable. Please help me. Thanks before for your attention."

You show only one parameter for each iCustom - just the fast or slow period and no other parameters.

The THVTrixDiv has approx 50 extern settings that should ALL be included in your EA iCustom statements.

You can include ALL the externs in your iCustoms....OR... To make it easy to use this indicator in your EA,

You can copy the indicator to a test file (THVTrixDiv_Test) and remove or minimize the externs - you don't need them for your EA iCustom calls.

Leave the FastPeriod/SlowPeriod externs if you still want to change them in your EA for testing...but make sure you include them both in each iCustom statement, not just one or the other.

Example - SlowGreen and the other values needs BOTH periods...(both parameters)...

SlowGreen = iCustom (Symbol(), 0, "THVTrixDiv", SlowPeriod,FastPeriod 0, 0),

To change the EXTERN's.. .

Delete or Blank out (//) all the "externs" but leave the values -

Externs - Example Code:

extern color HighLine_Color = FireBrick;
extern color ZeroLine_Color = DimGray;
extern color LowLine_Color = DarkGreen;

Change all externs to //extern:

//extern
color HighLine_Color = FireBrick;
//extern
color ZeroLine_Color = DimGray;
//extern
color LowLine_Color = DarkGreen;

OR delete externs completely:

color HighLine_Color = FireBrick;
color ZeroLine_Color = DimGray;
color LowLine_Color = DarkGreen;

Recompile the indicator and you are ready to go. Use the remaining extern parameters and new indicator name in your EA iCustoms.

EMPTY_BUFFER

Not sure what problem you are having with the EMPTY_BUFFER...but I don't think you need to initialize any values, just use them as they are or change them to zero (0):


if (Value1 = 2147483647) Do something...;

if (Value1 = 2147483647) Value1 = 0; if (Value1 = 0) Do something...;

Sleep() or Refresh()

You asked - "I do understand now about the logic of waiting the signal. What about if I insist to use While() function to 'wait' the signal? How to write this 'wait' algorithm? "

I don't know enough coding to help with Sleep() or Refresh(), but you should not need to do that for this indicator? Your EA should retrieve the current indicator values and keep checking the indicator by Time or Tick anyway.

Print or Comment Your Indicator Values

One thing that will help you a lot is to Print or Comment your indicator values in your EA so you can actually see you are getting the correct values.

Hope this helps,

Robert




 

thanks Robert,

I can do better with the indicator now since I remove all the externs I don't need. I can save more memory capacity.

In regards of the empty_value, I face the problem about how to looping them. Let's say, when this indicator, Trix, shows us a FastGreen line then EA send order to buy. After TakeProfit, somehow FastGreen line still there and I want to make it idle as long as this line still green. I already give the picture on the first post.

So, the point is, I've been thinking to make the EA idle after Buy and as long as the FastGreen still available I want to make EA to wait until FastGreen change to FastRed with While() looping function. Then when FastRed is available, it's time to Sell and after TakeProfit, make the EA idle as long as the FastRed, and so on.

When I tried to write the simple algorithm like this:

//... after orderclose, let's say after Buy Position

int m;
while (FastGreen != empty_value)  // as long as the FastGreen is available then..
{
  m++;                            // .. counting the tick
}

it's just like failed.. my Strategy Tester is freeze and when I tried it in Forward Test, it's not working. So, if you do have any other idea please help me. I'm still trying to understand the idea from WHRoeder.

 
Don't loop for waiting, just return and re-evaluate conditions next tick. Use global or static variables to remember previous conditions.
 

WHRoeder, so I'm trying to write like this:

if (OrdersTotal() > 0)
      {
         OrderClose()    // close when condition 
         ...
         return(0);
      }

   int new = -1;
   if (Condition Buy)
      {
         new = 1;
      }
   else if (Condition Sell)
      {
         new = 2;
      }
      
   if (new == 1)
      {
         OrderBuy
      }
   if (new == 2)
      {
         OrderSell
      }

where do I put my static variable? is it after OrderClose()? where do I have to put assignment operator of:

 static int prevDIR=-1; // Open a new trade ---> is this real to open new trade? how come?
   if (newDIR == prevDIR) return(0); // Wait until direction change
   prevDIR = newDIR;
 
nobody else can help me? someone please...