Download MetaTrader 5
To add comments, please log in or register
Did you like the article? Share it with your friends!
Mccallum28
245
Mccallum28 2014.05.12 09:47 

I have an indicator that repaints, I would like to open a position x bars from the signal bar, could someone show me how do code this.


Also, the signal is triggered each tic of the signal bar, how can i get it to signal at the close of the bar only?


Thanks


//+------------------------------------------------------------------+
//|                                            Tester.mq4            |
//|                                                                  |
//+------------------------------------------------------------------+
#property strict

// #include <stdlib.mqh> why do you need this file included

extern int dist2=6;
extern int SignalBar=2;
input double lots=3; 
input double sl=150;
input double tp=350;
extern int MagicNumber=45645;
int Buy_Pos = 0, Sell_Pos = 0;
int ticket;
bool IsNewBar=false;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
//---
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+


void OnTick()
  {

   
   for(int i = OrdersTotal() -1; i >= 0; i--)
      {
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
         {
         if (OrderType() == OP_BUY)
            {
            Buy_Pos++;
            }
         if (OrderType() == OP_SELL)
            {
            Sell_Pos++;
            }
         }
      }
      
   bool uptrend=iCustom(NULL,0,"super_signals_v2_alert",0,0)!=EMPTY_VALUE;
   bool downtrend=iCustom(NULL,0,"super_signals_v2_alert",1,0)!=EMPTY_VALUE;
   
      
   if(uptrend && Sell_Pos == 0)
      if (IsNewBar=true)
   
      {
      ticket = OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + sl * Point, Ask - tp * Point,NULL, MagicNumber,0, Red);
      if (ticket < 0)
         Alert("Can't open Sell Order, Error code ", GetLastError());
  
      }
   
   if(downtrend && Buy_Pos == 0) 
      if (IsNewBar=true)
      
      {
      ticket = OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - sl * Point, Bid + tp,NULL, MagicNumber, 0, Blue); 
      if (ticket < 0)
         Alert("Can't open Buy Order, Error code ", GetLastError());
   
      }
   
//---
  }
qjol
3243
qjol 2014.05.12 10:14  

please dont double post

your question has been answered there already

Mccallum28
245
Mccallum28 2014.05.12 11:35  
its a different qustion
qjol
3243
qjol 2014.05.12 11:48  

maybe you don't remember but i do

1: (https://www.mql5.com/en/forum/151484/page3#962873)

ryangillSL:

Also how would i open the trade on the open of the next bar from an indicator signal?


2: (https://www.mql5.com/en/forum/151484/page3#962879)


ryangillSL:
i.e to open the trade x bars after signal is triggered?


and the answer is given to you over there

if (newBar)
   {
   your code
   }

https://www.mql5.com/en/forum/137985

Mccallum28
245
Mccallum28 2014.05.12 13:22  

OK i got it,

so signal is trigered on bar, i want to wait 1 bar and open on next bar, would this do it?


if(uptrend && Sell_Pos == 0 && NewBar[1])

Ticket

qjol
3243
qjol 2014.05.12 15:23  

no but you can modify the function to suit your requirements using Time or iTime or PeriodSeconds or iBarShift etc. (there is plenty more)

or you can play around with the iCustom function example:

   bool uptrend=iCustom(NULL,0,"super_signals_v2_alert",0,0)!=EMPTY_VALUE;
   bool downtrend=iCustom(NULL,0,"super_signals_v2_alert",1,0)!=EMPTY_VALUE;

or

   bool uptrend=iCustom(NULL,0,"super_signals_v2_alert",0,1)!=EMPTY_VALUE;
   bool downtrend=iCustom(NULL,0,"super_signals_v2_alert",1,1)!=EMPTY_VALUE;

or

   bool uptrend=iCustom(NULL,0,"super_signals_v2_alert",0,2)!=EMPTY_VALUE;
   bool downtrend=iCustom(NULL,0,"super_signals_v2_alert",1,2)!=EMPTY_VALUE;


etc.

Mccallum28
245
Mccallum28 2014.05.12 17:36  

Thats great, thank you for your help....

/
To add comments, please log in or register