Hi
Here below is metatrader expert I have been using. I am having trouble getting it to work right since I changed it to improve it.
I moved 'close half' to after the second SL level and now it just goes mad and closes immediately half each time then all within few seconds.
I cannot understand out why. It must be the logic I use for this.
Its purpose if that if trade goes against you, at - 15 pips set a SL to - 20 pips. This is level 1.
If it goes in your favour at + 20 move SL to BE + 2 AND close half the position. This would be level 2.
At + 30 pips move remaining position SL to + 25 pips - level 3
Then trail in steps.
Unfortunately what mine does is change order immediately, and close instantly the position then close it all, before I can blink.
Can someone help me as to what i have done wrong.
I wonder if someone could also show me code I can use so it manages all orders I have open, no magic nos etc, just apply to everything
thank you very much in advance
PG
//| e-Smart_Tralling |
//+--------------------------------------------------------+
extern bool UseOneAccount = true; // not sure how this applies to all orders
extern bool UseCloseOneHalf = true;
extern int LevelProfit1 = -130; // if price hit - 13
extern int LevelMoving1 = -200; // move SL to -20
extern int LevelProfit2 = 200; // if price +ve
extern int LevelMoving2 = 10; // move SL to + 10
extern int LevelProfit3 = 550;
extern int LevelMoving3 = 300;
extern int TrailingStop = 10;
extern int TrailingStep = 50;
extern int Slippage = 2;
extern bool ShowComment = true;
extern bool UseSound = true;
string var_132 = "expert.wav";
//+------------------------------------------------------------------+
void deinit()
{
Comment("");
}
//+------------------------------------------------------------------+
void start()
{
double point;
int digits;
string msg = "";
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (UseOneAccount || (OrderSymbol() == Symbol()))
{
ThreeLevelSystemOfOutput();
digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
point = MarketInfo(OrderSymbol(),MODE_POINT);
msg = msg + OrderSymbol() + " Öåíà: " + DoubleToStr(OrderOpenPrice(),digits) + " SL = " + DoubleToStr(
OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";
}
}
}
if (ShowComment) Comment(msg);
}
//+------------------------------------------------------------------+
void ThreeLevelSystemOfOutput()
{
int profit = ProfitPosition();
int sl = StopLossInPoint();
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);
if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1)) ModifyStopLossInPoint(LevelMoving1);
if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2);
{
ModifyStopLossInPoint(LevelMoving1);
if (UseCloseOneHalf) CloseOneHalf();
}
if ((profit > LevelProfit3) && (sl < LevelMoving3)) ModifyStopLossInPoint(LevelMoving3);
if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();
}
//+------------------------------------------------------------------+
void CloseOneHalf()
{
bool result = false;
double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;
if (lots > 0.0)
{
if (OrderType() == OP_BUY)
{
result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE);
}
if (OrderType() == OP_SELL)
{
result = OrderClose(OrderTicket(),lots,Ask,Slippage,CLR_NONE);
}
if (result && UseSound) PlaySound(var_132);
}
}
//+------------------------------------------------------------------+
void TrailingPositions()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
if (bid - OrderOpenPrice() > TrailingStop * point)
{
if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)
{
ModifyStopLoss(bid - TrailingStop * point);
return;
}
}
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
if (OrderOpenPrice() - ask > TrailingStop * point)
{
if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))
{
ModifyStopLoss(ask + TrailingStop * point);
}
}
}
}
//+------------------------------------------------------------------+
void ModifyStopLoss(double sl)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}
//+------------------------------------------------------------------+
void ModifyStopLossInPoint(int stoploss)
{
bool result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;
result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}
//+------------------------------------------------------------------+
int ProfitPosition()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double profit = 0;
if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
profit = (bid - OrderOpenPrice()) / point;
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
profit = (OrderOpenPrice() - ask) / point;
}
return(MathRound(profit));
}
//+------------------------------------------------------------------+
int StopLossInPoint()
{
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double sl = 0;
if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;
if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;
if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;
return(MathRound(sl));
}
Here below is metatrader expert I have been using. I am having trouble getting it to work right since I changed it to improve it.
I moved 'close half' to after the second SL level and now it just goes mad and closes immediately half each time then all within few seconds.
I cannot understand out why. It must be the logic I use for this.
Its purpose if that if trade goes against you, at - 15 pips set a SL to - 20 pips. This is level 1.
If it goes in your favour at + 20 move SL to BE + 2 AND close half the position. This would be level 2.
At + 30 pips move remaining position SL to + 25 pips - level 3
Then trail in steps.
Unfortunately what mine does is change order immediately, and close instantly the position then close it all, before I can blink.
Can someone help me as to what i have done wrong.
I wonder if someone could also show me code I can use so it manages all orders I have open, no magic nos etc, just apply to everything
thank you very much in advance
PG
//| e-Smart_Tralling |
//+--------------------------------------------------------+
extern bool UseOneAccount = true; // not sure how this applies to all orders
extern bool UseCloseOneHalf = true;
extern int LevelProfit1 = -130; // if price hit - 13
extern int LevelMoving1 = -200; // move SL to -20
extern int LevelProfit2 = 200; // if price +ve
extern int LevelMoving2 = 10; // move SL to + 10
extern int LevelProfit3 = 550;
extern int LevelMoving3 = 300;
extern int TrailingStop = 10;
extern int TrailingStep = 50;
extern int Slippage = 2;
extern bool ShowComment = true;
extern bool UseSound = true;
string var_132 = "expert.wav";
//+------------------------------------------------------------------+
void deinit()
{
Comment("");
}
//+------------------------------------------------------------------+
void start()
{
double point;
int digits;
string msg = "";
for (int i = 0; i < OrdersTotal(); i++)
{
if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
{
if (UseOneAccount || (OrderSymbol() == Symbol()))
{
ThreeLevelSystemOfOutput();
digits = MarketInfo(OrderSymbol(),MODE_DIGITS);
point = MarketInfo(OrderSymbol(),MODE_POINT);
msg = msg + OrderSymbol() + " Öåíà: " + DoubleToStr(OrderOpenPrice(),digits) + " SL = " + DoubleToStr(
OrderStopLoss(),digits) + " (" + StopLossInPoint() + ")\n";
}
}
}
if (ShowComment) Comment(msg);
}
//+------------------------------------------------------------------+
void ThreeLevelSystemOfOutput()
{
int profit = ProfitPosition();
int sl = StopLossInPoint();
int spread = MarketInfo(OrderSymbol(),MODE_SPREAD);
if ((profit > LevelProfit1) && (profit <= LevelProfit2) && (sl < LevelMoving1)) ModifyStopLossInPoint(LevelMoving1);
if ((profit > LevelProfit2) && (profit <= LevelProfit3) && (sl < LevelMoving2)) ModifyStopLossInPoint(LevelMoving2);
{
ModifyStopLossInPoint(LevelMoving1);
if (UseCloseOneHalf) CloseOneHalf();
}
if ((profit > LevelProfit3) && (sl < LevelMoving3)) ModifyStopLossInPoint(LevelMoving3);
if (profit > LevelMoving3 + TrailingStop + TrailingStep) TrailingPositions();
}
//+------------------------------------------------------------------+
void CloseOneHalf()
{
bool result = false;
double lots = MathCeil(OrderLots() / 2.0 * 10.0) / 10.0;
if (lots > 0.0)
{
if (OrderType() == OP_BUY)
{
result = OrderClose(OrderTicket(),lots,Bid,Slippage,CLR_NONE);
}
if (OrderType() == OP_SELL)
{
result = OrderClose(OrderTicket(),lots,Ask,Slippage,CLR_NONE);
}
if (result && UseSound) PlaySound(var_132);
}
}
//+------------------------------------------------------------------+
void TrailingPositions()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
if (bid - OrderOpenPrice() > TrailingStop * point)
{
if (OrderStopLoss() < bid - (TrailingStop + TrailingStep - 1) * point)
{
ModifyStopLoss(bid - TrailingStop * point);
return;
}
}
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
if (OrderOpenPrice() - ask > TrailingStop * point)
{
if ((OrderStopLoss() > ask + (TrailingStop + TrailingStep - 1) * point) || (OrderStopLoss() == 0))
{
ModifyStopLoss(ask + TrailingStop * point);
}
}
}
}
//+------------------------------------------------------------------+
void ModifyStopLoss(double sl)
{
bool result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}
//+------------------------------------------------------------------+
void ModifyStopLossInPoint(int stoploss)
{
bool result;
double sl = 0;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
if (OrderType() == OP_BUY) sl = OrderOpenPrice() + stoploss * point;
if (OrderType() == OP_SELL) sl = OrderOpenPrice() - stoploss * point;
result = OrderModify(OrderTicket(),OrderOpenPrice(),sl,OrderTakeProfit(),0,CLR_NONE);
if (result && UseSound) PlaySound(var_132);
}
//+------------------------------------------------------------------+
int ProfitPosition()
{
double bid;
double ask;
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double profit = 0;
if (OrderType() == OP_BUY)
{
bid = MarketInfo(OrderSymbol(),MODE_BID);
profit = (bid - OrderOpenPrice()) / point;
}
if (OrderType() == OP_SELL)
{
ask = MarketInfo(OrderSymbol(),MODE_ASK);
profit = (OrderOpenPrice() - ask) / point;
}
return(MathRound(profit));
}
//+------------------------------------------------------------------+
int StopLossInPoint()
{
double point = MarketInfo(OrderSymbol(),MODE_POINT);
double sl = 0;
if (OrderType() == OP_BUY) sl = (OrderStopLoss() - OrderOpenPrice()) / point;
if (OrderType() == OP_SELL) sl = (OrderOpenPrice() - OrderStopLoss()) / point;
if (OrderStopLoss() == 0.0) sl = -OrderOpenPrice() / point;
return(MathRound(sl));
}
Attached File(s)
eTrail.mq4
5 KB
|
227 downloads