Mql4 Code Trailing Stop Loss not Moving on time
Mql4 Code Trailing Stop Loss not Moving on time
Loading saved threads...
Hari prasath · External communityPost link
External question — Stack Overflow Stack Exchange
Author: Hari prasath
Original post: https://stackoverflow.com/questions/77356719
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
//+------------------------------------------------------------------+
//| CANDLE_HIGH_LOW.mq4 |
//| Hariprasath |
//| |
//+------------------------------------------------------------------+
#property copyright "Hariprasath"
#property link ""
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
double Lots = 0.01;
double minStopLossLevel = MarketInfo(_Symbol, MODE_STOPLEVEL) * Point;
double bidPrice = MarketInfo(Symbol(), MODE_BID);
double askPrice = MarketInfo(Symbol(), MODE_ASK);
double priceDifferece = askPrice-bidPrice;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnTick()
{
//---
gettingPriceData();
creatingLines();
placeOrder();
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double gettingRecentHigh()
{
if(High[1] > High[2])
{
return High[1];
}
else
return High[2];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double gettingRecentLow()
{
if(Low[1] > Low[2])
{
return Low[2];
}
else
return Low[1];
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void placeOrder()
{
double HighValue = gettingRecentHigh();
double LowValue = gettingRecentLow();
if(Ask > HighValue)
{
buyOrder();
buyTrailingStopLoss();
}
if(Bid<LowValue)
{
sellOrder();
selltrailingStopLoss();
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void gettingPriceData()
{
double recentHigh = gettingRecentHigh();
double previousLow = gettingRecentLow();
double AccountEquityBalance = AccountInfoDouble(ACCOUNT_EQUITY);
double AccountProfitBalance = AccountInfoDouble(ACCOUNT_PROFIT);
//Display price
ObjectCreate("bidPrice",OBJ_LABEL,0,0,0);
ObjectSet("bidPrice",OBJPROP_CORNER,CORNER_LEFT_UPPER);
ObjectSet("bidPrice",OBJPROP_XDISTANCE,20);
ObjectSet("bidPrice",OBJPROP_YDISTANCE,10);
ObjectSetText("bidPrice","Bid Price : " + DoubleToStr(bidPrice,Digits),15,"Arial",White);
ObjectCreate("askPrice",OBJ_LABEL,0,0,0);
ObjectSet("askPrice",OBJPROP_CORNER,CORNER_LEFT_UPPER);
ObjectSet("askPrice",OBJPROP_XDISTANCE,20);
ObjectSet("askPrice",OBJPROP_YDISTANCE,40);
ObjectSetText("askPrice","Ask Price : " + DoubleToStr(askPrice,Digits),15,"Arial",White);
ObjectCreate("recentHigh",OBJ_LABEL,0,0,0);
ObjectSet("recentHigh",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
ObjectSet("recentHigh",OBJPROP_XDISTANCE,20);
ObjectSet("recentHigh",OBJPROP_YDISTANCE,10);
ObjectSetText("recentHigh","Recent High : " + DoubleToStr(recentHigh,Digits),15,"Arial",White);
ObjectCreate("previousLow",OBJ_LABEL,0,0,0);
ObjectSet("previousLow",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
ObjectSet("previousLow",OBJPROP_XDISTANCE,20);
ObjectSet("previousLow",OBJPROP_YDISTANCE,40);
ObjectSetText("previousLow","Previous Low : " + DoubleToStr(previousLow,Digits),15,"Arial",White);
ObjectCreate("minStopLossLevel",OBJ_LABEL,0,0,0);
ObjectSet("minStopLossLevel",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
ObjectSet("minStopLossLevel",OBJPROP_XDISTANCE,20);
ObjectSet("minStopLossLevel",OBJPROP_YDISTANCE,70);
ObjectSetText("minStopLossLevel","MinStopLossLevel : " + DoubleToStr(minStopLossLevel,Digits),15,"Arial",White);
ObjectCreate("priceDifferece",OBJ_LABEL,0,0,0);
ObjectSet("priceDifferece",OBJPROP_CORNER,CORNER_RIGHT_UPPER);
ObjectSet("priceDifferece",OBJPROP_XDISTANCE,20);
ObjectSet("priceDifferece",OBJPROP_YDISTANCE,100);
ObjectSetText("priceDifferece","PriceDifferece : " + DoubleToStr(priceDifferece,Digits),15,"Arial",White);
ObjectCreate("AccountEquityBalance",OBJ_LABEL,0,0,0);
ObjectSet("AccountEquityBalance",OBJPROP_CORNER,CORNER_LEFT_LOWER);
ObjectSet("AccountEquityBalance",OBJPROP_XDISTANCE,50);
ObjectSet("AccountEquityBalance",OBJPROP_YDISTANCE,10);
ObjectSetText("AccountEquityBalance","Account Equity Balance : " + DoubleToStr(AccountEquityBalance,Digits),15,"Arial",White);
ObjectCreate("AccountProfitBalance",OBJ_LABEL,0,0,0);
ObjectSet("AccountProfitBalance",OBJPROP_CORNER,CORNER_LEFT_LOWER);
ObjectSet("AccountProfitBalance",OBJPROP_XDISTANCE,50);
ObjectSet("AccountProfitBalance",OBJPROP_YDISTANCE,30);
ObjectSetText("AccountProfitBalance","Account Profit Balance : " + DoubleToStr(AccountProfitBalance,Digits),15,"Arial",White);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void creatingLines()
{
//Delete If existing lien
ObjectDelete("RecentHighLine");
//Creating line
ObjectCreate("RecentHighLine",OBJ_HLINE,0,Time[0], gettingRecentHigh());
ObjectSet("RecentHighLine", OBJPROP_COLOR, clrGreen); // Line color
ObjectSet("RecentHighLine", OBJPROP_WIDTH, 2); // Line width
ObjectSet("RecentHighLine", OBJPROP_RAY_LEFT, false); // Do not extend to the left
ObjectSet("RecentHighLine", OBJPROP_RAY_RIGHT, true); // Extend to the right
//Delete If existing lien
ObjectDelete("PreviousLowLine");
//Creating line
ObjectCreate("PreviousLowLine",OBJ_HLINE,0,Time[0], gettingRecentLow());
ObjectSet("PreviousLowLine", OBJPROP_COLOR, clrRed); // Line color
ObjectSet("PreviousLowLine", OBJPROP_WIDTH, 2); // Line width
ObjectSet("PreviousLowLine", OBJPROP_RAY_LEFT, false); // Do not extend to the left
ObjectSet("PreviousLowLine", OBJPROP_RAY_RIGHT, true); // Extend to the right
}
//+------------------------------------------------------------------+
void buyOrder()
{
if(OrdersTotal() < 1)
{
// To place order
int buyTicket = OrderSend(
_Symbol, //Currency Pair
OP_BUY, // Order Type
Lots, // Lot Size
Ask, // Buying price
3, // Slipage or tolorance
0, //Stop loss
0, //Profit
"BUY ORDER PLACED", // Comments
0, // Magic Number
0, // Expiation
Green //Arrow allowed
);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void buyTrailingStopLoss()
{
double HighValue = gettingRecentHigh();
double LowValue = gettingRecentLow();
// To count total orders
for(int a = OrdersTotal()-1; a>=0; a--)
{
//To select the orders
if(OrderSelect(a,SELECT_BY_POS,MODE_TRADES))
//Check order with current order
if(OrderSymbol() == Symbol())
//To find the buy orders
if(OrderType() == OP_BUY)
//Fix the stop loss
// if(OrderStopLoss() < Ask)
// Check and update the stop loss
if(OrderStopLoss() != LowValue)
//We modify the stoploss
OrderModify(
OrderTicket(), //For the current order
OrderOpenPrice(), // Opended for the open price
LowValue, //Set stop loss
OrderTakeProfit(), //Order Take profit
0, //Expiration
Red
);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void sellOrder()
{
if(OrdersTotal() < 1)
{
// To place order
int sellTicket = OrderSend(
_Symbol, //Currency Pair
OP_SELL, // Order Type
Lots, // Lot Size
Bid, // Buying price
3, // Slipage or tolorance
0, //Stop loss
0, //Profit
"SELL ORDER PLACED", // Comments
0, // Magic Number
0, // Expiation
Red //Arrow allowed
);
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void selltrailingStopLoss()
{
double HighValue = gettingRecentHigh();
double LowValue = gettingRecentLow();
// To count total orders
for(int b = OrdersTotal()-1; b>=0; b--)
{
//To select the orders
if(OrderSelect(b,SELECT_BY_POS,MODE_TRADES))
//Check order with current order
if(OrderSymbol() == Symbol())
//To find the buy orders
if(OrderType() == OP_SELL)
//Fix the stop loss
// if(OrderStopLoss() > Bid)
//We modify the stoploss
// Check and update the stop loss
if(OrderStopLoss() != HighValue)
Alert("SELL WORKING");
OrderModify(
OrderTicket(), //For the current order
OrderOpenPrice(), // Opended for the open price
HighValue, //Set stop loss
OrderTakeProfit(), //Order Take profit
0, //Expiration
Red
);
}
}
//+------------------------------------------------------------------+
Stoploss should be moved to green line but is not working properly working lately.
Like wise for buy order also expected SL is for red line but its coming below on that value
How to handle this kind of issues?
Quote
Report
Mark SdS · External communityPost link
External answer — Stack Overflow Stack Exchange
Author: Mark SdS
Original post: https://stackoverflow.com/a/77370355
License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/
Adaptation: HTML converted to plain text; contact email addresses removed.
It looks like your trailing stop routine will never be executed since it is iterating backwards from
OrdersTotal()-1
:
// To count total orders
for(int b = OrdersTotal()-1; b>=0; b--)
and your
OrdersTotal()
will never be larger than 1:
void sellOrder()
{
if(OrdersTotal() < 1)
{
// To place order (...)
Besides that your code seems very redundant: the functions
gettingRecentHigh()
and
gettingRecentLow()
are both executed three times on each tick. Better store their results in a global variable and call the functions once per tick. Refer to the global var when needed elsewhere.
Quote
Report
Post Reply
Quoted from Forex.com.bd-Editorial External answer — Stack Overflow Stack Exchange Author: Mark SdS Source score (net votes, not local likes): 0 Original post: https://stackoverflow.com/a/77370355 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. It looks like your trailing stop routine will never be executed since it is iterating backwards from OrdersTotal()-1 : // To count total orders for(int b = OrdersTotal()-1; b>=0; b--) and your OrdersTotal() will never be larger than 1: void sellOrder() { if(OrdersTotal() < 1) { // To place order (...) Besides that your code seems very redundant: the functions gettingRecentHigh() and gettingRecentLow() are both executed three times on each tick. Better store their results in a global variable and call the functions once per tick. Refer to the global var when needed elsewhere.
Checking account access…