How to make Break even trigger more than one time in one entry - mql4

I now trying to make Break Even Code trigger more than one time,
example EA entry is 1.28000 and stop loss 1.28500
if current price reach 1.175000(50pips), sl move to break even such as to 1.28000(5pips).
EA will not make more modify order after condition are meet.
so how to trigger break even again if price reach 1.17000(100pips), sl move to (1.175000)(50 pips)
and again price reach 1.165000(150pips),sl move to 1.17000(100pips)
I want to make
BE_B_M(sl move to(example:5))
and
BE_B_T(price reach(example:50))
as variable and every time price reach target variable change to next value
so became
BE_B_M(sl move to(example:50)) and BE_B_T(price reach(example:100))
The entire code is as follows
extern double BE_T_1 = 50;
extern double BE_M_1 = 5;
extern double BE_T_2 = 100;
extern double BE_M_2 = 50;
extern double BE_T_3 = 150;
extern double BE_M_3 = 100;
double BE_S_M;
double BE_S_T;
void MOVE_BE_1()
{
for(int b=OrdersTotal()-1;b>=0;b--)
{
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=M_Number)continue;
if(OrderSymbol()==Symbol())
if(OrderType()==OP_BUY)
if(Bid-OrderOpenPrice()>BE_S_T*Pips)
if(OrderOpenPrice()>OrderStopLoss())
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
Print("eror");
}
for(int s=OrdersTotal()-1;s>=0;s--)
{
if(OrderSelect(s,SELECT_BY_POS,MODE_TRADES))
if(OrderMagicNumber()!=M_Number)continue;
if(OrderSymbol()==Symbol())
if(OrderType()==OP_SELL)
if(OrderOpenPrice()-Ask>BE_S_T*Pips)
if(OrderOpenPrice()<OrderStopLoss())
if(!OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-(BE_S_M*Pips),OrderTakeProfit(),0,CLR_NONE))
Print("eror");
}
}
i expect sl will move after price reach every 50pips from entry

Here you can put all the 3 break even levels on one function.
Note: you can use 1 for-loop for both OP_BUY and OP_SELL
Here is my OnInit()
// Global variable
double point;
int OnInit()
{
if(Digits == 5 || Digits == 3) point=Point*10;
else point=Point;
return(INIT_SUCCEEDED);
}
Here is the BreakEven() function
//+------------------------------------------------------------------+
//| Break even the trade at 3 levels |
//+------------------------------------------------------------------+
void BreakEven()
{
// get the stop level for that symbol
double stopLevel = SymbolInfoInteger(Symbol(),SYMBOL_TRADE_STOPS_LEVEL)*Point;
for(int i=OrdersTotal()-1;i>=0;i--)
{
if(!OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) continue;
if(OrderMagicNumber()!=M_Number)continue;
if(OrderSymbol()!=Symbol())continue;
if(OrderType()==OP_BUY)
{
double profitPips=Bid-OrderOpenPrice();
double newSL=OrderStopLoss();
if(profitPips>=BE_T_1*point && OrderStopLoss()<OrderOpenPrice()) // Break Even
{
newSL=OrderOpenPrice()+BE_M_1*point;
}
else if(profitPips>=BE_T_3*point) // 150/100
{
newSL=OrderOpenPrice()+BE_M_3*point;
}
else if(profitPips>=BE_T_2*point) // 100/50
{
newSL=OrderOpenPrice()+BE_M_2*point;
}
if(newSL>=OrderStopLoss()+Point && newSL<Bid-stopLevel)
if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
Print("Error at BE: ",GetLastError());
}
else if(OrderType()==OP_SELL)
{
double profitPips=OrderOpenPrice()-Ask;
double newSL=OrderStopLoss();
if(profitPips>=BE_T_1*point && (OrderStopLoss()>OrderOpenPrice() || OrderStopLoss()==0)) // Break Even
{
newSL=OrderOpenPrice()-BE_M_1*point;
}
else if(profitPips>=BE_T_3*point) // 150/100
{
newSL=OrderOpenPrice()-BE_M_3*point;
}
else if(profitPips>=BE_T_2*point) // 100/50
{
newSL=OrderOpenPrice()-BE_M_2*point;
}
if(newSL<=OrderStopLoss()-Point && newSL>Ask+stopLevel)
if(!OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(newSL,Digits),OrderTakeProfit(),0))
Print("Error at BE: ",GetLastError());
}
}
}
I didn't test this myself in a trade, but it should work.

Related

EA Opens more orders than expected in MQL4

Everything seems fine. But EA usually opens multiple trades in the same second... The way I built it is very linear and i can't seem to spot the logical mistake. It is basically a random martingale EA to test stuff out. Any indicator (that's why I called it random, haven't decided myself) can be put in there.
Basic idea is that it has an upper and lower threshold which determines when it is in buy zone and when at sell zone. Once it is in either zone, if trend goes against it (determined by indicator's value, not symbol's price) it opens another trade with the same SL/TP of the initial order. Also it checks whether initial trade still runs so it does not open other ones and once the initial trade is open. After that the criterias about the rest of the trades (that go against the trade are different).
The problem is that it opens multiple trades at times that it shouldn't, or like 3-4 trades within the same second or two. Any idea why this happens?
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
input int stepValue = 5;
input double lotsize = 0.01;
input int stoploss = 2000;
input int takeprofit = 140;
input int slippage = 10;
input double upper_border = 60.0;
input double lower_border = 40.0;
const string EAComment = "Xind";
string mode = "";
bool first_trade = false;
int InitTicket = 1;
double X = 0.0;
double X_Last = 0.0;
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//---
first_trade = false;
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//---
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//---
SearchSignal();
if (mode == "Buy")
{
if (first_trade == false)
{
Buy();
}
if (first_trade == true)
{
MartinCheck();
CloseCheck();
}
}
if (mode == "Sell")
{
if (first_trade == false)
{
Sell();
}
if (first_trade == true)
{
MartinCheck();
CloseCheck();
}
}
}
//+------------------------------------------------------------------+
void Buy()
{
X_Last = X;
first_trade = true;
InitTicket = OrderSend(NULL,OP_BUY,lotsize,Ask,slippage,Ask-stoploss*Point,Ask+takeprofit*Point,EAComment,1,0,clrDarkBlue);
}
//---
void Sell()
{
X_Last = X;
first_trade = true;
InitTicket = OrderSend(NULL,OP_SELL,lotsize,Bid,slippage,Bid+stoploss*Point,Bid-takeprofit*Point,EAComment,1,0,clrDarkRed);
}
//---
void MartinBuy()
{
if (OrderSelect(InitTicket, SELECT_BY_TICKET) == true)
{
double new_SL = OrderStopLoss();
double new_TP = OrderTakeProfit();
int dont_care = OrderSend(NULL,OP_BUY,lotsize,Ask,slippage,new_SL,new_TP,EAComment+" martin",1,0,clrDarkBlue);
}
}
//---
void MartinSell()
{
if (OrderSelect(InitTicket, SELECT_BY_TICKET) == true)
{
double new_SL = OrderStopLoss();
double new_TP = OrderTakeProfit();
int dont_care = OrderSend(NULL,OP_SELL,lotsize,Bid,slippage,new_SL,new_TP,EAComment+" martin",1,0,clrDarkRed);
}
}
//---
void SearchSignal()
{
X = 0.0; //where 0.0, put here the iCustom for external indicators, or some built-in indicator
if (X >= upper_border)
{
mode = "Sell";
}
else if (X <= lower_border)
{
mode = "Buy";
}
else
{
mode = "";
first_trade = false;
InitTicket = 1;
X_Last = 0.0;
}
}
//---
void CloseCheck()
{
if (OrderSelect(InitTicket, SELECT_BY_TICKET))
{
if (OrderCloseTime() == 0)
{
first_trade = true;
}
else if (OrderCloseTime() != 0)
{
first_trade = false;
}
else
{
return;
}
}
}
//---
void MartinCheck()
{
if (mode == "Buy")
{
if ((X_Last - stepValue) >= X)
{
X_Last = X;
MartinBuy();
}
}
if (mode == "Sell")
{
if ((X_Last + stepValue) <= X)
{
X_Last = X;
MartinSell();
}
}
}
The layout of your code makes it possible for several processes to happen in sequence all on the same tick which I assume you do not want. Try changing your code initially to this and work from there:
void OnTick()
{
SearchSignal();
if(mode=="Buy")
{
if(!first_trade) Buy();
else
{
MartinCheck();
CloseCheck();
}
}
else if(mode=="Sell")
{
if(!first_trade) Sell();
else
{
MartinCheck();
CloseCheck();
}
}
}
Remember to use if(...) else to stop executing all functions when it should only be an either/or situation.

Why does this EA (in early development) not produce an alert, or an error?

#property strict
string subfolder = "ipc\\";
int last_read = 0;
int t = 0;
struct trade_message
{
int time; // time
string asset; // asset
string direction; // direction
double open_price; // open
double stop_price; // open
double close_price;// open
float fraction; // fraction
string comment; // comment
string status; // status
};
trade_message messages[];
int OnInit()
{
int FH = FileOpen(subfolder+"processedtime.log",FILE_BIN);
if(FH >=0)
{
last_read = FileReadInteger(FH,4);
FileClose(FH);
}
return(INIT_SUCCEEDED);
}
void OnTick()
{
int FH=FileOpen(subfolder+"data.csv",FILE_READ|FILE_CSV, ","); //open file
int p=0;
while(!FileIsEnding(FH))
{
t = StringToInteger(FileReadString(FH));
if(t<=last_read)
{
break;
}
do
{
messages[p].time = t; // time
messages[p].direction = FileReadString(FH); // direction
messages[p].open_price = StringToDouble(FileReadString(FH)); // open
messages[p].stop_price = StringToDouble(FileReadString(FH)); // stop
messages[p].close_price = StringToDouble(FileReadString(FH)); // close
messages[p].fraction = StringToDouble(FileReadString(FH)); // fraction (?float)
messages[p].comment = FileReadString(FH); // comment
messages[p].status = FileReadString(FH); // status
Alert(messages[p].comment);
}
while(!FileIsLineEnding(FH));
p++;
Alert("P = ",p,"; Array length = ", ArraySize(messages));
}
FileClose(FH);
last_read = t;
FileDelete(subfolder+"processedtime.log");
FH = FileOpen(subfolder+"processedtime.log",FILE_BIN);
FileWriteInteger(FH,t,4);
FileClose(FH);
ArrayFree(messages);
}
The code is in tick function in order to test it before taking it out to a function.
The data.csv file is:
Timestamp
Asset
Direction
Price
Stop
Profit
Fraction
Comment
Status
xxx
yyy
SHORT
13240
13240
13220
0.5
yyy SHORT 13240 - taken half at 13220 and stop to breakeven
U
xxx
yyy
SHORT
13240
13262
13040
1.0
55%
DP
The processedtime.log is not being created.
So 2 problems, both of my own making.
Not skipping the header row, simply remedied by inserting
do f = FileReadString(FH);
while(!FileIsLineEnding(FH));
after FileOpen() (easier than altering my source for data.csv), and
forgetting about the asset part of my structure, thus ensuring my structures got out of sync with my lines!! Remedied by adding messages[p].asset = FileReadString(FH); between messages[p].time = t and messages[p].direction = FileReadString(FH);

How to calculate indicator buffer only once per day mql4

I have an indicator which gives one signal [Down Buffer]... I wish to only give signal once per day... Like after the first signal it will not paint any signal for the rest of the day! I have tested with below code it's now not painting at all?
//--- indicator buffers
double Buffer1[];
int day = 0;
int OnInit()
{
........................
}
int OnCalculate(......)
{
//--- main loop
for(int i = limit-1; i >= 0; i--)
{
//Indicator Buffer 1
if( here goes my condition )
{
if( day != Day() )
{
Buffer1[i] = High[i] + iATR(NULL, PERIOD_CURRENT, 14, i); //Set indicator value at Candlestick High + Average True Range
day = Day();
}
}
else
{
Buffer1[i] = EMPTY_VALUE;
}
}
return(rates_total);
}
What is wrong with my code? It's now not showing any signal at all...
Note : I have removed some of the code to make it simple...
Use the following function to check whether it is a new day.
It returns true if it is a new day, else returns false.
bool IsNewDay(){
static datetime prevDay = -1;
if( iTime(_Symbol, PERIOD_D1, 0) == prevDay ) return false;
prevDay = iTime(_Symbol, PERIOD_D1, 0);
return true;
}

Modify Pending Order at Mql4

I am trying to figure out an EA which updates its pending orders if current price pass the SL level of pending order both for buy and sell orders. As you can see below pic current price is below the SL point of pending orders. Therefore I want to modify new updated pending orders with openning price of from those SL point (+30 & -30 point) and not only for window currency but also for other currencies which are in the pending order list.
I tried below code but it didn't work out. Could you help me?
//------------------------------------------------------------------------------- 1 --
int start()
{
string Symb=Symbol(); // Symbol
//------------------------------------------------------------------------------- 2 --
for(int i=1; i<=OrdersTotal(); i++)
{
if (OrderSelect(i-1,SELECT_BY_POS)==true)
{
//---------------------------------------------------------------------- 3 --
if (OrderSymbol()== Symb) continue;
if (OrderType()==OP_BUYSTOP || OrderType()==OP_SELLSTOP) continue; // Market order
//---------------------------------------------------------------------- 4 --
{
int Tip =OrderType();
int Ticket=OrderTicket();
double Price =OrderOpenPrice();
double SL =OrderStopLoss();
double TP =OrderTakeProfit();
}
}
}
//------------------------------------------------------------------------------- 5 --
if (Tip==0)
{
Alert("For ",Symb," no pending orders available");
return;
}
//------------------------------------------------------------------------------- 6 --
while(true)
{
RefreshRates(); // Update data
//------------------------------------------------------------------------- 7 --
double c_bid =MarketInfo(Symb,MODE_BID); // Request for the value of Bid
double c_ask =MarketInfo(Symb,MODE_ASK); // Request for the value of Ask
//------------------------------------------------------------------------- 8 --
string Text=""; // Not to be modified
double New_SL=0;
double New_TP=0;
switch(Tip) // By order type
{
case 4: // BuyStopt
if (NormalizeDouble(SL,Digits) > // If it is further than by
NormalizeDouble(c_ask,Digits))//..the preset value
{
double New_Price=SL+30*Point; // Its new price
if (NormalizeDouble(SL,Digits)>0)
New_SL=New_Price-(Price-SL); // New StopLoss
if (NormalizeDouble(TP,Digits)>0)
New_TP=New_Price+(TP-Price); // New TakeProfit
Text= "BuyStopt "; // Modify it.
}
break; // Exit 'switch'
case 5: // SellStop
if (NormalizeDouble(SL,Digits) < // If it is further than by
NormalizeDouble(c_bid,Digits))//..the preset value
{
New_Price=SL-30*Point; // Its new price
if (NormalizeDouble(SL,Digits)>0)
New_SL=New_Price+(SL-Price); // New StopLoss
if (NormalizeDouble(TP,Digits)>0)
New_TP=New_Price-(Price-TP); // New TakeProfit
Text= "SellStop "; // Modify it.
}
}
if (NormalizeDouble(New_SL,Digits)<0) // Checking SL
New_SL=0;
if (NormalizeDouble(New_TP,Digits)<0) // Checking TP
New_TP=0;
if (Text=="") // If it is not modified
{
Alert("No conditions for modification.");
break; // Exit 'while'
}
//------------------------------------------------------------------------ 10 --
Alert ("Modification ",Text,Ticket,". Awaiting response..");
bool Ans=OrderModify(Ticket,New_Price,New_SL,New_TP,0);//Modify it!
//------------------------------------------------------------------------ 11 --
if (Ans==true) // Got it! :)
{
Alert ("Modified order ",Text," ",Ticket," :)");
break; // Exit the closing cycle
}
//------------------------------------------------------------------------ 12 --
int Error=GetLastError(); // Failed :(
switch(Error) // Overcomable errors
{
case 4: Alert("Trade server is busy. Retrying..");
Sleep(3000); // Simple solution
continue; // At the next iteration
case 137:Alert("Broker is busy. Retrying..");
Sleep(3000); // Simple solution
continue; // At the next iteration
case 146:Alert("Trading subsystem is busy. Retrying..");
Sleep(500); // Simple solution
continue; // At the next iteration
}
switch(Error) // Critical errors
{
case 2 : Alert("Common error.");
break; // Exit 'switch'
case 64: Alert("Account is blocked.");
break; // Exit 'switch'
case 133:Alert("Trading is prohibited");
break; // Exit 'switch'
case 139:Alert("Order is blocked and is being processed");
break; // Exit 'switch'
case 145:Alert("Modification prohibited. ",
"Order is too close to the market");
break; // Exit 'switch'
default: Alert("Occurred error ",Error);//Other alternatives
}
break; // Exit the closing cycle
} // End of closing cycle
//------------------------------------------------------------------------------ 13 --
Alert ("The script has completed its operations -----------------------");
return; // Exit start()
}
//------------------------------------------------------------------------------ 14 --
Welcome to the Worlds of MQL-4.56789...
Since the early days of the MQL4 language, the specifications 've changed a lot.
Your script suffers from a "recent"-New-MQL4.56789... change, where scope of validity of declared variables got restricted to a syntax-constructor-only ( this was not in the MQL4 original ).
So, declaring an int Tip et al but inside an if(){...}-block causes no Tip-variable to remain visible outside the {...}-block-constructor, not being know there anymore.
Next, your code requires a complete re-design, so as to meet your requirement of :
"not only for window currency but also for other currencies which are in the pending order list."
// ------------------------------------------------------------------------------- 2 --
for( int i=1; i <= OrdersTotal(); i++ )
{
if ( True == OrderSelect( i-1, SELECT_BY_POS ) )
{
//---------------------------------------------------------------------- 3 --
if ( OrderSymbol() == Symb ) continue; // ------------------------^ JIT/LOOP
if ( OrderType() == OP_BUYSTOP
|| OrderType() == OP_SELLSTOP
) continue; //-----------------------------------------------^ JIT/LOOP
// btw. was a pending { BUYSTOP || SELLSTOP }
//---------------------------------------------------------------------- 4 --
{ // a rest goes here :
int Tip = OrderType();
int Ticket = OrderTicket();
double Price = OrderOpenPrice();
double SL = OrderStopLoss();
double TP = OrderTakeProfit();
}
// ------------------------------ BUT ALL THESE GOT FORGOTTEN RIGHT HERE...
}
}
// ------------------------------------ OK, 've LOOPED THE WHOLE DB-POOL,
// BUT DID NOTHING SO FAR
// AND NOTHING SO FORTH
So, something evolved from this will help to settle the script on the rock-solid grounds :
#define MASK "(for i==(%3d)) got OrderTKT(%20d) of TYPE{BUY|SELL|BUYLIMIT|SELLLIMIT|BUYSTOP|SELLSTOP}=[%d] in (%10s)-MARKET ... WILL LOOP for a NEXT(?) ONE"
while ( !IsStopped() ) // ----------------------------------------------------------- 1 --
{ // (almost) INFINITE SCRIPT SERVICE LOOP
// as (almost) HEADLESS Sir NICHOLAS :)
string TradeSYMBOL;
int Tip;
int Ticket;
double Price;
double SL;
double TP;
if ( 1 > OrdersTotal() ) { Sleep( 333 ); continue; } // NOP / NAP ----------^ NOP/LOOP
// ------------------------------------------------------------------------------ 2 --
for( int i=0; i < OrdersTotal(); i++ ) // DB-POOL LOOP
{
if ( OrderSelect( i, SELECT_BY_POS ) )
{
//---------------------------------------------------------------------- 3 --
if ( OrderType() != OP_BUYSTOP
&& OrderType() != OP_SELLSTOP
) {
PrintFormat( MASK, i, OrderTicket(), OrderType(), OrderSymbol() );
continue; //-------------------------------------------------^ JIT/LOOP
} // btw. ! a pending { BUYSTOP || SELLSTOP }
//---------------------------------------------------------------------- 4 --
else
{ // a { BUYSTOP || SELLSTOP } go here :
// -------------------------------------------------------------------- SET :
Tip = OrderType();
Ticket = OrderTicket();
Price = OrderOpenPrice();
SL = OrderStopLoss();
TP = OrderTakeProfit();
TradeSYMBOL = OrderSymbol();
// ------------------------------------------------------------- PROCESS it :
RefreshRates();
...
// Alert() - a blocking GUI operation, quite dangerous in auto-trading
// any
// OrderModify() - has also to respect the Broker-defined {Stop|Freeze}Level
...
} // { BUYSTOP || SELLSTOP }
} // .SELECT
} // .DB-POOL LOOP________________________________________________________
} // .SERVICE LOOP........................................................

Arduino project Servo glitch (memory game)

I recently got into Arduino with a Rex Qualis Arduino Uno R3 and I am trying to build a project that would beat the Simon memory game (or Repeat the Beat).
It waits for user response through one of four buttons then adds that to the list, executes the list, then waits for user input on the next move.
Everything works how it's supposed to, but the weirdest things happen on execution:
On the first loop after full execution, Servo 1 will execute its move function without authorization.
On the second loop after full execution, Servo 2 will execute its move function and so on.
After the fourth loop, execution, and servo 4 executing its move function, it doesn't happen again. I don't know why it cycles through all the servos one by one in the first four loops then is fine after but it kinda breaks my project.
Is there a problem in my code that redirects to the move functions or something? All help is appreciated. Here is the code for reference:
//Simon killer
//Da Cube
#include <Servo.h>
//Declare buttons
int button1 = 4;
int button2 = 5;
int button3 = 6;
int button4 = 7;
//Declare servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;
int moves[100]; //Memory up to 100
int x = 0;
int y = 1;
void setup() {
pinMode(button1, INPUT_PULLUP); //Button setup
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(button4, INPUT_PULLUP);
servo1.attach(8); //Servo setup
servo2.attach(9);
servo3.attach(10);
servo4.attach(11);
moveServo1();//System check
moveServo2();
moveServo3();
moveServo4();
}
//move functions
void moveServo1() {
servo1.write(5);
delay(500);
servo1.write(45);
delay(500);
}
void moveServo2() {
servo2.write(5);
delay(500);
servo2.write(45);
delay(500);
}
void moveServo3() {
servo3.write(175);
delay(500);
servo3.write(135);
delay(500);
}
void moveServo4() {
servo4.write(5);
delay(500);
servo4.write(45);
delay(500);
}
void loop() {
//Read Input by button
while (x < y) {
if (digitalRead(button1) == LOW) {
moves[x] = 1;
x++;
} else if (digitalRead(button2) == LOW) {
moves[x] = 2;
x++;
} else if (digitalRead(button3) == LOW) {
moves[x] = 3;
x++;
} else if (digitalRead(button4) == LOW) {
moves[x] = 4;
x++;
}
}
y++;
//Decode Memory Array
for (int i = 0; i < (sizeof(moves)); i++) {
switch (moves[i]) {
case 1:
moveServo1();
break;
case 2:
moveServo2();
break;
case 3:
moveServo3();
break;
case 4:
moveServo4();
break;
}
}
}
First i would check to see if the code that makes the Servos move 1-4 isn't the one in the setup loop.
moveServo1();//System check
moveServo2();
moveServo3();
moveServo4();
Here you make a servo sistem check, which means every time you power up the arduino, the first servo will move, then the second and so on and only then the void loop starts...comment these lines and see if that helps

Resources