//+------------------------------------------------------------------+
//| 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
);
}
}
//+------------------------------------------------------------------+
止损应移至绿线,但最近无法正常工作。
同样,买入订单也预期 SL 为红线,但其低于该值
遇到此类问题如何处理?
看起来你的追踪止损例程永远不会被执行,因为它是从
OrdersTotal()-1
向后迭代的:
// To count total orders
for(int b = OrdersTotal()-1; b>=0; b--)
并且你的
OrdersTotal()
永远不会大于 1:
void sellOrder()
{
if(OrdersTotal() < 1)
{
// To place order (...)
此外,您的代码看起来非常多余:函数
gettingRecentHigh()
和 gettingRecentLow()
都在每个刻度上执行三次。最好将其结果存储在全局变量中,并在每个周期调用一次函数。其他地方需要时请参阅全局变量。