price gap ?

 

Hi,

I'm planning to write a code that could change current chart's every order's SL to a new level IF current currency reaches a specific price. (my purpose is: if some price reached, I'll add some position and have to adjust all SL.)

What will this have to be? EA or script?

I'm wondering: if I boot up computer after night, or if after some net failure, mt4 will have a data "gap" to fill in. And I don't want this code to do something wrong during this filling in. Any idea about this? Or can I prevent this EA from running automatically when starting up MT4?


Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

 
  1. A script is designed to run once and exit. You want to continually check for price reaching your level. EA
  2. Code the EA to handle the gap.
 

What will this have to be? EA or script?

EA is probably the best solution.

You prevent EAs running on startup.

 

> What will this have to be? EA or script?

You can use EA or scrpit too. EA runs until stopped, script runs once.


> I'm wondering: if I boot up computer after night, or if after some net failure, mt4 will have a data "gap" to fill in. And I don't want this code to do something wrong during this filling in. Any idea about this? Or can I prevent this EA from running automatically when starting up MT4?


I don't know how can be this programmed. The service desk replied that MT4 always loads the history, I should use RefreshRates().

But often the terminal does not load the history, not with RefreshRates and I never get the usually referred 4066 error message, not any other error message with GetLastError() - not for several seconds, minutes, ticks. The only way way I shut down the terminal, delete te incomplete hst files and restart and hope that next time it will be loaded. And the terminal will never load more than 2048 bars by itself. There is no way to know if the downloaded history is the same as on the server. IMHO this is a disaster.

Service Desk replied that I should ask the broker for chart differences, but they did not tell me where shall I find a broker support which checks every minute the hst files I send and reply if it is OK or not...

 

RefreshRates() is ONLY necessary after delays in start(), e.g. Computations that take seconds or more, sleep, between server calls, etc.

When it opens a chart (user initializated or on startup previous saved) it does a refresh from the broker and gets the latest 2048 by itself. A refresh is not RefreshRates()

The terminal then loads as much history as you have up to max bars in chart. The current chart is current (may have gaps) when start() is called.

Why do you care? Look at the current price and adjust your SL.
 

Thanks guys. I appreciate it.

I have an idea: compare the last 2 ticks, if difference is larger than 10 points, don't run for this/last tick.

>Why do you care? Look at the current price and adjust your SL.

Yeah. Just I may consider including function of adding position in this code, thus have to control error.

Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

 

Anybody, please take a look at my code.... price reached, but nothing happens. (the "for()" part is from another code, I didn't change much.)

extern double reach  = 9999;
extern double newSL  = 7777;

double secondtick = 0;                  // for last second tick
bool EAEnabled = true;                   // switch off this ea after SL changed
extern bool    comment   = true;        //display information on screen

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
     if (!EAEnabled)
   {
      return(0);
   }



   int Ticket;
  //   double value = NormalizeDouble(WindowPriceOnDropped(),Digits);
   double value = newSL;
   string txt=StringConcatenate("script exhibiting SL ",DoubleToStr(value,Digits)," start ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
 //  RefreshRates();
   
if( secondtick == Ask || MathAbs(secondtick-Ask)>10*Point ) return(0);                 // prevent price gap
if( (Ask>=reach && reach>secondtick) || (Ask<=reach && reach<secondtick) )           // cross over reach price
{   

   
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol()) continue;
      
         
      Ticket = OrderTicket();
      if(OrderType()==OP_BUY)     
      if(value<Ask) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value,OrderTakeProfit() ,OrderExpiration(),White))
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," BUY order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," 恹耱噔脲龛 stoploss BUY order ",Ticket);
      }
      
      if(OrderType()==OP_SELL)
      if(value>Bid) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value, OrderTakeProfit(),OrderExpiration(),White))   
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," SELL order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," stoploss SELL order ",Ticket);
      }
         
      if((OrderType()==OP_BUYSTOP) || (OrderType()==OP_BUYLIMIT))     
      if(value<OrderOpenPrice()) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value, OrderTakeProfit(),OrderExpiration(),White)) 
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," placing BUY order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," stoploss placing BUY order ",Ticket);
      }
       
      if((OrderType()==OP_SELLSTOP) || (OrderType()==OP_SELLLIMIT))
      if(value>OrderOpenPrice()) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value, OrderTakeProfit(),OrderExpiration(),White))                
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," placing SELL order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," stoploss placing SELL order ",Ticket);
      }
      if (comment) Comment(txt);
   }   
   if (comment) Comment(txt,"\nThe script has finished its work ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
   
   
   
   
 
EAEnabled = false;
 
}
 
 secondtick = Ask; 
 Print(EAEnabled)  ;
   return(0);
  }


Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

 
Also, the txt comment part is from another code, didn't change anything, nothing meaningful.
Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

 
if( secondtick == Ask 
The == operand. - MQL4 forum
 

Thank you, sir.

I rewrote the code, with several changes. But still not working.

extern double reach  = 9999;
extern double newSL  = 7777;

double secondtick = 0;                  // for last second tick
bool EAEnabled = true;                   // switch off this ea after SL changed
extern bool    comment   = true;        //display information on screen----- txt comment part is meaningless, from another code

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
     if (!EAEnabled)
   {
      return(0);
   }



   int Ticket;
  //   double value = NormalizeDouble(WindowPriceOnDropped(),Digits);
   double value = newSL;
   string txt=StringConcatenate("script exhibiting SL ",DoubleToStr(value,Digits)," start ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
 //  RefreshRates();
   
if( MathAbs(secondtick - Ask)<0.00001 || MathAbs(secondtick-Ask)>10*Point )        // prevent price gap
{
secondtick = Ask;
return(0);    
}             
if( (Ask>=reach && reach>secondtick) || (Ask<=reach && reach<secondtick) )           // cross over reach price
{   

   
   for(int i=OrdersTotal()-1;i>=0;i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
      if(OrderSymbol()!=Symbol()) continue;
      
         
      Ticket = OrderTicket();
      if(OrderType()==OP_BUY)     
      if(value<Ask) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value,OrderTakeProfit() ,OrderExpiration(),White))
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," BUY order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," 恹耱噔脲龛 stoploss BUY order ",Ticket);
      }
      
      if(OrderType()==OP_SELL)
      if(value>Bid) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value, OrderTakeProfit(),OrderExpiration(),White))   
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," SELL order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," stoploss SELL order ",Ticket);
      }
         
      if((OrderType()==OP_BUYSTOP) || (OrderType()==OP_BUYLIMIT))     
      if(value<OrderOpenPrice()) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value, OrderTakeProfit(),OrderExpiration(),White)) 
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," placing BUY order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," stoploss placing BUY order ",Ticket);
      }
       
      if((OrderType()==OP_SELLSTOP) || (OrderType()==OP_SELLLIMIT))
      if(value>OrderOpenPrice()) 
      {
         if (OrderModify(Ticket,OrderOpenPrice(),value, OrderTakeProfit(),OrderExpiration(),White))                
            txt = StringConcatenate(txt,"\nSet stoploss ",DoubleToStr(value,Digits)," placing SELL order ",Ticket);
         else txt = StringConcatenate(txt,"\nError ",GetLastError()," stoploss placing SELL order ",Ticket);
      }
      if (comment) Comment(txt);
   }   
   if (comment) Comment(txt,"\nThe script has finished its work ",TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS));
   
   
   
   
 
EAEnabled = false;
  Print(EAEnabled)  ;                // for debug
}
 
 secondtick = Ask; 

   return(0);
  }


Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

 

How do I edit my post in this forum?

I find that my code doesn't work sometimes, missing some crossover.

Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Detect language Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh YiddishAfrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latin Latvian Lithuanian Macedonian Malay Maltese Norwegian Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish
English (auto-detected) » Chinese (Simplified)

Reason: