Hi. I'm new to MT5 coding. I migrated from MT4 and found out really quick that MT5 is totally different from MT4. anyways, this is my problem. My codes are below. the code is placed inside OnTick() but it stops after one of the OrderSend() are triggered. Please help me identify what is the problem here. I can't seem to find it. Thanks.
I'm expecting it to keep looping (every time a tick is received). do 1, 2, 3 and keep looping in 4. I expect to see a message " Stop Order Placed. Waiting for it to be triggered" every time a tick is received.
what happens is that once an ordersend() is triggered, the whole thing just stops.
// Variablesint i;
ulong ticket;
bool ans;
string Target = "EURUSD";
int Direction;
double Price;
double Lot;
double SL;
bool Stop_Order;
double Lots_Size_to_Pad = 0.01;
MqlTradeResult result = {0};
MqlTradeRequest request = {0};
double Pips_Clearance;
double Spread;
double Trail_Clearance;
MqlTick Tick;
//+------------------------------------------------------------------+intOnInit()
{
//---Print (" ------------------ START OF EA ------------------ ");
//---return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+voidOnTick()
{
// 1. check for an open position. if there are no open position, do nothing.// Get all the Details about the Open Positions. // Target = "EURUSD";PositionSelect(Target); // this line will select EURUS
Direction = PositionGetInteger(POSITION_TYPE); // Get the Direction for the current position. Direction of 0 = BUY, 1 = SELL
Price = NormalizeDouble(PositionGetDouble(POSITION_PRICE_OPEN),5); // Get the Open Price
Lot = PositionGetDouble(POSITION_VOLUME); // Get the Volume or Lot Size
SL = PositionGetDouble(POSITION_SL); // Get the Stop Loss value.// Print (" Symbol = ", Target," Direction is = ", Direction," Price is = ", Price," Lot Size is = ", Lot," SL is = ", SL);// create "Tick" to be able to call bid and ask SymbolInfoTick(Target,Tick);
// invoke codes only if there is an open position.if (Lot !=0)
{
// 2. if there is an open position, check for an already exisitng stop order. if a stop order is already open, delete it if it is wrong.
Stop_Order = false;
for (i=0; i <= OrdersTotal()-1; i++)
{
if ( Target = OrderGetString(ORDER_SYMBOL) )
{
// Pair Pending Order Found!
Stop_Order = true; // Marker to identify that a Stop was seen and confirmed correct.
ans=OrderSelect(OrderGetTicket(i)); // Select the order matching the Target.if (ans = false) Print ("Error Seen = ", GetLastError());
// Check if the Pending Order is correct. Correct means:// Open Price are the same.// Direction is the same. Sell + 4 = Sell STop, Buy + 4 = Buy Stopif ( Direction + 4 == OrderGetInteger(ORDER_TYPE) && Price == OrderGetDouble(ORDER_PRICE_OPEN) && Lots_Size_to_Pad == OrderGetDouble(ORDER_VOLUME_CURRENT) )
{
Print (" Pending order is correct.");
}
else
{
Print ("Order seen but it is incorrect. Order Number ", OrderGetTicket(i) );
// Here is where we remove the old stop order and place a new one.
request.order = OrderGetTicket(i);
request.action = TRADE_ACTION_REMOVE;
ans=OrderSend(request,result);
if (ans = false) Print ("Error Seen = ", GetLastError());
Print (OrderGetTicket(i) , " has seen been deleted.");
}
}
}
Pips_Clearance = NormalizeDouble(20 * Point(),5);
// 3. if there is no stop order, place one.if ( (Tick.bid - Pips_Clearance > Price && Direction == 1 && Stop_Order == false) || (Price > Tick.ask + Pips_Clearance && Direction == 0 && Stop_Order == false) )
{
// Place a Stop Order here.
request.action = TRADE_ACTION_PENDING;
request.symbol = Target;
request.volume = Lots_Size_to_Pad;
request.price = Price;
request.tp = 0;
request.deviation = 0.5;
if (Direction == 0) request.type = ORDER_TYPE_BUY_STOP;
if (Direction == 1) request.type = ORDER_TYPE_SELL_STOP;
request.type_filling = 0;
ans=OrderSend(request,result);
if (ans = false) Print ("Error Seen = ", GetLastError());
Print ("No Orders seen. Place a Correct one now. Target ", request.symbol, " Price = ", request.price, " SL = ", request.sl);
Sleep(1000);
}
// 4. wait for the stop order to get triggered. NO ACTION REQUIRED.if (Stop_Order == true)
{
Print (" Stop Order Placed. Waiting for it to be triggered");
}
// 5. once a threashold is reached, move sl to be.if ( ( (Tick.ask < Price - Pips_Clearance ) && Direction == 1 && SL == 0) || ((Tick.bid > Price + Pips_Clearance) && Direction == 0 && SL == 0) )
// if price is 2pips + freeze points into the money, move SL to BE.
{
if (Direction == 0) request.sl = Price ;
if (Direction == 1) request.sl = Price ;
request.action = TRADE_ACTION_SLTP;
request.symbol = Target;
request.tp = 0;
//ans=OrderSend(request,result);//if (ans = false) Print ("Error Seen = ", GetLastError());Print (" Move SL to BE. SL = ",request.sl);
}
Spread = NormalizeDouble(SymbolInfoInteger(Target,SYMBOL_SPREAD) * 10 * Point(),5);
Trail_Clearance = NormalizeDouble(70 * Point(),5) ;
// 6. once a trailing buffer is reached, move sl forward and trail SL.if ( ( (Tick.ask < Price - Trail_Clearance) && Direction == 1 && SL == 0 && SL > Tick.ask + Trail_Clearance ) || ((Tick.bid > Price + Trail_Clearance) && Direction == 0 && SL == 0 && SL < Tick.bid - Trail_Clearance) )
{
// setup all the needed variables.if (Direction == 0) request.sl = Tick.bid - Trail_Clearance;
if (Direction == 1) request.sl = Tick.ask + Trail_Clearance;
request.action = TRADE_ACTION_SLTP;
request.symbol = Target;
request.tp = 0;
ans = OrderSend(request,result);
if (ans = false) Print ("Error Seen = ", GetLastError());
Print ("Target ", request.symbol, " New SL = ", request.sl);
}
}
}
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
Hi. I'm new to MT5 coding. I migrated from MT4 and found out really quick that MT5 is totally different from MT4. anyways, this is my problem. My codes are below. the code is placed inside OnTick() but it stops after one of the OrderSend() are triggered. Please help me identify what is the problem here. I can't seem to find it. Thanks.
I'm expecting it to keep looping (every time a tick is received). do 1, 2, 3 and keep looping in 4. I expect to see a message " Stop Order Placed. Waiting for it to be triggered" every time a tick is received.
what happens is that once an ordersend() is triggered, the whole thing just stops.