As a sugestion to trace your code… Print something After every line of code in the for loop… After first line Print(“1”)… second line Print(“2”)… and so on… for the Sell side you cam Prnt a b c…. After every OrderOpen or OrderModify you print something also… just to SEE Where your code stops and why…
Looking at the journal it says OrderSend Error 130, also a lot of OrderModify Error - 130, Trailing Buy Error - Invalids Stops.
Means the trailing didn't work and so does the New Open Order.
And also this is my First open position of Buy's code:
void BuyOrder1()
{
double SL1 = Ask - SLoss*PipValue*Point;
if (SLoss == 0) SL1 = 0;
double TP1 = Ask + TProfit*PipValue*Point;
if (TProfit == 0) TP1 = 0;
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);
else
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);
if (ticket > -1)
{
if (true)
{
bool sel = OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL1, TP1, 0, Blue);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
else
{
Print("OrderSendBuy() error - ", ErrorDescription(GetLastError()));
}
}
Looking at the journal it says OrderSend Error 130, also a lot of OrderModify Error - 130, Trailing Buy Error - Invalids Stops.
Means the trailing didn't work and so does the New Open Order.
And also this is my First open position of Buy's code:
void BuyOrder1()
{
double SL1 = Ask - SLoss*PipValue*Point;
if (SLoss == 0) SL1 = 0;
double TP1 = Ask + TProfit*PipValue*Point;
if (TProfit == 0) TP1 = 0;
int ticket = -1;
if (true)
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);
else
ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 4, SL1, TP1, "Alpha Buy", 1, 0, Blue);
if (ticket > -1)
{
if (true)
{
bool sel = OrderSelect(ticket, SELECT_BY_TICKET);
bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL1, TP1, 0, Blue);
if (ret == false)
Print("OrderModify() error - ", ErrorDescription(GetLastError()));
}
}
else
{
Print("OrderSendBuy() error - ", ErrorDescription(GetLastError()));
}
}
What is PipValue?
PipValue is 1... here is the code:
double PipValue=1; // this variable is here to support 5-digit brokers
Btw this is the code I need to make....
After OP_BUY (MagicNumber is 1) or OP_SELL (MagicNumber is 2) is opened...
Then I need to Select ALL OP_BUY and modify all the SL by the TrailSL variable...
Also if the OP_SELL is opened... I need to select ALL OP_SELL and modify all the OP_SELL's SL by the TrailSL variable.
What I need for this to work is to SELECT ALL the position Opened either it Buy or Sell using their each magic number and modify ALL the opened to Trailing SL.
After that, In seperate command... I need a code to:
Select LAST OP_BUY and after the last OP_BUY go into profit (decided using the pips traveled in the variable i called distance) then it need to Open 1 more OP_BUY with the same MagicNumber...
also for the OP_SELL is the same...
so this is what I think....
Void TrailAllBuyOrder()
double SL1= Ask-SLoss*PipValue*Point;
double TP1= Ask+TProfit*PipValue*Point;
for (int i=OrdersTotal()-1; i>= 0; i--)
if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType () == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
{
OrderModify(ALL _TICKET_FOR _OP_BUY, OrderOpenPrice(), SL1, TP1, 0, clrNONE);
}
else
{
Print("TrailBuyError() error --", ErrorDescription (GetLastError()));
}
}
void TradeUpBuy()
double SL1= Ask-SLoss*PipValue*Point;
double TP1= Ask+TProfit*PipValue*Point;
double LastBuyOrderPrice = 0.0;
double LastSellOrderPrice = 0.0;
for(int i = (OrdersTotal()-1); i >= 0; i --)
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if (OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1)
{
double CurBuyOrderPrice = OrderOpenPrice();
{
if(CurBuyOrderPrice > LastBuyOrderPrice)
{
CurBuyOrderPrice = LastBuyOrderPrice;
LastBuyOrderPrice = OrderOpenPrice();
{
if(OrderType() == OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber() == 1 && (LastBuyOrderPrice+Distance*PipValue*Point)<= Distance*PipValue*Point)
{
OrderSend(Symbol(), OP_BUY, TrailLots, Bid, 4, SL1, TP1, "Trail Up Buy", 1, 0, Blue);
}
else
{
Print ("TradeUpBuy() error -", Errordescription (GetLastError()));
}
}
}
}
}
Please correct my code... I am a novice in Programming, still learning... I hope u understand what I'm trying to code here...
- Fajar Sentoso #:. I hope u understand
This is an international English forum; Post in English.
Please don't write ur - it's "you are" or "your" - MQL4 programming forum (2014) -
Please edit your posts and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
General rules and best pratices of the Forum. - General - MQL5 programming forum (2019)
Messages Editor -
void TradeUpBuy() double SL1= Ask-SLoss*PipValue*Point;
That's not how you define a function. Do not post code that will not even compile.
MT4: Learn to code it.
MT5: Begin learning to code it.If you don't learn MQL4/5, there is no common language for us to communicate. If we tell you what you need, you can't code it. If we give you the code, you don't know how to integrate it into your code.
-
Use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
Code debugging - Developing programs - MetaEditor Help
Error Handling and Logging in MQL5 - MQL5 Articles (2015)
Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, so my target is to create Trade Up with trailing stop of all open order either it OP_BUY or OP_SELL after several "Distance" of pips going profit.
So here is my code:
I Attach of my backtest report using this code.
As you can see it only open 0.05 Lots position (meaning its only open the first position)
My Settings are:
First Open Position Lot is 0.05
After certain "Distance" traveled into a profit, a new order will be open with 0.10 Lot along with trailing stop to all open trade position.
Example:
Starting Lot: 0.05
SL: 500
Trail Lot: 0.10
Distance: 500
Trailing_Stop: 500
Trailing Gap: 500
So if the first position opened as OP_BUY at Symbol(XAUUSD) at Price 1800.00
the SL would be at 1795.00
and due to distance set to 500 point meaning a new Buy order will be opened if the Ask reach 1805.00 with SL at 1800.00 and at the same time the trailing will move the SL of the first order to 1800.00
if the price going up again to 1810.00 then a new Buy Order with 0.10 lot will be opened again with SL at 1805.00 and at the same time all SL of previous order will be move to 1805.00 by the trailing feature.
Is there something wrong with my code? Plz help I'am novice at programming languange. Thanks a lot!