Help with coding?

 

below is the coding I have for entering a position but I would like to possibly make another type of trailing stop... basically I would have a stoploss of maybe 100 pips but I want to make it so once it goes up a certain amount (I would make another extern int for this) it would adjust the stoploss to a defined level. Like a trailing stop but... I would want the actual trailing to be... maybe 10 pips. I hope this makes sense. Also I would like it to adjust the lot amount based on the equity of the account. I have an idea on how to code the latter but... it's not quite clear to me.


//+------------------------------------------------------------------+
//| Variable End |
//+------------------------------------------------------------------+

//Check position
bool IsTrade = False;

for (int i = 0; i < Total; i ++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if(OrderType() <= OP_SELL && OrderSymbol() == Symbol()) {
IsTrade = True;
if(OrderType() == OP_BUY) {
//Close

//+------------------------------------------------------------------+
//| Signal Begin(Exit Buy) |
//+------------------------------------------------------------------+

if (CloseBuy1_1 < CloseBuy1_2) Order = SIGNAL_CLOSEBUY;


//+------------------------------------------------------------------+
//| Signal End(Exit Buy) |
//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
//Close

//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+

if (CloseSell1_1 > CloseSell1_2) Order = SIGNAL_CLOSESELL;


//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
}
}
}

//+------------------------------------------------------------------+
//| Signal Begin(Entry) |
//+------------------------------------------------------------------+

if (Buy1_1 < Buy1_2 && Buy2_1 < Buy2_2 && Buy3_1 < Buy3_2 && Buy4_1 < Buy4_2) Order = SIGNAL_BUY;

if (Sell1_1 > Sell1_2 && Sell2_1 > Sell2_2 && Sell3_1 > Sell3_2 && Sell4_1 > Sell4_2) Order = SIGNAL_SELL;


//+------------------------------------------------------------------+
//| Signal End |
//+------------------------------------------------------------------+

//Buy
if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(OrdersTotal() < MaxOrders) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}

StopLossLevel = Ask - StopLoss * Point;
TakeProfitLevel = Ask + TakeProfit * Point;
Ticket = OrderSend(Symbol(), OP_BUY, Lots,
NormalizeDouble(Ask,digit),
Slippage,
NormalizeDouble(StopLossLevel,digit),
NormalizeDouble(TakeProfitLevel,digit),
"Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);
if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) Print("BUY order opened : ", OrderOpenPrice()); else Print("Error opening BUY order : ", GetLastError());
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}
}

//Sell
if (Order == SIGNAL_SELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
if(OrdersTotal() < MaxOrders) {
//Check free margin
if (AccountFreeMargin() < (1000 * Lots)) {
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
StopLossLevel = Bid + StopLoss * Point;
TakeProfitLevel = Bid - TakeProfit * Point;
Ticket = OrderSend(Symbol(), OP_SELL, Lots,
NormalizeDouble(Bid,digit),
Slippage,
NormalizeDouble(StopLossLevel,digit),
NormalizeDouble(TakeProfitLevel,digit),
"Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);



if(Ticket > 0) {
if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) Print("SELL order opened : ", OrderOpenPrice()); else Print("Error opening SELL order : ", GetLastError());
}
if (EachTickMode) TickCheck = True;
if (!EachTickMode) BarCount = Bars;
return(0);
}

}}//+------------------------------------------------------------------+

 

I suppose I should reword parts of this. Basically I want to have the EA open a trade... lets says long EJ at 133.00 and the stop loss is set at 132.00 trailing is 10 pips but the EA initially waits until 133.20 to move the stoploss to 133.10 and the trailing stop does the rest; or make it into the new trailing stop... whichever is easier to code. Obviously I would like to play with the numbers... but for this is all for instance.

And as for the open lots... I believe I am overthinking all of it. Perhaps 1 lot for every 5k in the account?

And on a side note I would also see if I can see if it will only open a trade during certain hours.

 

I was hoping to get some help on this as I really don't know very much about MQL coding. I am very new at this

 
Skog23 wrote >>

I suppose I should reword parts of this. Basically I want to have the EA open a trade... lets says long EJ at 133.00 and the stop loss is set at 132.00 trailing is 10 pips but the EA initially waits until 133.20 to move the stoploss to 133.10 and the trailing stop does the rest; or make it into the new trailing stop... whichever is easier to code. Obviously I would like to play with the numbers... but for this is all for instance.

And as for the open lots... I believe I am overthinking all of it. Perhaps 1 lot for every 5k in the account?

And on a side note I would also see if I can see if it will only open a trade during certain hours.

Although I have received no responses... I will at least mention I have figured out how to make it adjust lot amounts per free margin... and what time frames to trade in... Now I just need to figure out how to make my own trailing stop of some sort. I really would like to possibly give myself room of about 100-150 pips and once I am up about 10 pips it would move to either b/e or 5 pips in profit and at that point have a trailing stop of 5-10 points if possible. I know this sounds strange but it seems to work for me in manual...

 
Skog23 wrote >>

Although I have received no responses... I will at least mention I have figured out how to make it adjust lot amounts per free margin... and what time frames to trade in... Now I just need to figure out how to make my own trailing stop of some sort. I really would like to possibly give myself room of about 100-150 pips and once I am up about 10 pips it would move to either b/e or 5 pips in profit and at that point have a trailing stop of 5-10 points if possible. I know this sounds strange but it seems to work for me in manual...

if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}

I am not sure how to adjust this to do what I am requesting... or if I would need a separate section so it does not simply loop repeatedly and mess with the price.

 
Skog23:

Although I have received no responses... I will at least mention I have figured out how to make it adjust lot amounts per free margin... and what time frames to trade in... Now I just need to figure out how to make my own trailing stop of some sort. I really would like to possibly give myself room of about 100-150 pips and once I am up about 10 pips it would move to either b/e or 5 pips in profit and at that point have a trailing stop of 5-10 points if possible. I know this sounds strange but it seems to work for me in manual...

Hi there,


Good day,


I'm somehow new to the forum as well, but maybe I can help you with this.


For Trailing Stop function, I have this function you can insert in your code.


This part below you put it in your code as an individual function ( not in Start(), int(), deint() functions):


void trail()
{
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderSymbol()==Symbol() )
{
if (OrderType() == OP_BUY) {
if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}
}
} else if (OrderType() == OP_SELL) {
if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) ||
(OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
          }
        }
      }  
    }
  }
}

And this one:

//insert this function on top of your EA as an external variables ... 

extern bool trail = False;
extern double TrailingStop = 10;

// also insert this one in Start() function

if (trail) trail();
 

I hope this is useful.

Best wishes,
 
scarface wrote >>

Hi there,

Good day,

I'm somehow new to the forum as well, but maybe I can help you with this.

For Trailing Stop function, I have this function you can insert in your code.

This part below you put it in your code as an individual function ( not in Start(), int(), deint() functions):

And this one:

So... with this I could have a stoploss of 100 pips and once I am profitting 10 pips I can set a breakeven and have the trailing stop take it from there? I don't see that in the coding... I could be missing it though

 


if (Order == SIGNAL_CLOSEBUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, MediumSeaGreen);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Close Buy");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if(Bid - OrderOpenPrice() > Point * TrailingStop) {
if(OrderStopLoss() < Bid - Point * TrailingStop) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - Point * TrailingStop, OrderTakeProfit(), 0, MediumSeaGreen);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
//Move stoploss to Break even
if(UseStopLoss && StopLoss > 0 && breakeven < 1) {
if(Bid - OrderOpenPrice() >= 10 * BE) {{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Aqua);
Print ("Moved stop to Break even: ",OrderOpenPrice());
breakeven += 1;
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
} else {
//Close

//+------------------------------------------------------------------+
//| Signal Begin(Exit Sell) |
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//| Signal End(Exit Sell) |
//+------------------------------------------------------------------+

if (Order == SIGNAL_CLOSESELL && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {
OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, DarkOrange);
if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Close Sell");
if (!EachTickMode) BarCount = Bars;
IsTrade = False;
continue;
}
//Trailing stop
if(UseTrailingStop && TrailingStop > 0) {
if((OrderOpenPrice() - Ask) > (Point * TrailingStop)) {
if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
//Move stoploss to Break even
if(UseStopLoss && StopLoss > 0 && breakeven < 1) {
if(OrderOpenPrice() - Ask >= 10 * BE) {{
OrderModify(OrderTicket(), OrderOpenPrice(), OrderOpenPrice(), OrderTakeProfit(), 0, Aqua);
Print ("Moved stop to Break even: ",OrderOpenPrice());
breakeven += 1;
if (!EachTickMode) BarCount = Bars;
continue;
}
}
}
}
}
}

The above is what I have... BE value is currently 10... so once 10 points in profit it will attempt to break even.

One issue I am having right now though is in backtesting. I have a couple trades that went south but it shouldn't have... for example I had a long position that went to being up 15 pips but the breakeven portion didn't kick in... also, in the journal... the Print function isn't working in the breakeven portion...