How to code? - page 110

 

Hi.

I am looking forward to learning your tricks and showing you mine.

 
CodeMuncher:
Hi. I am looking forward to learning your tricks and showing you mine.

You'll find a lot of great stuff on this forum.

Read and use the search feature without any restriction!!!

Feel free to ask anything ... A lot of great contributors/helpers here.

FerruFx

 
fireslayer26:
In this code, it has the trailing stop set for 45 pips. But the trailing stop doesnt seem to Activate until it moves 45 pips. How would I need to change it to have the trailing stop activated when the trade is placed?

extern string Remark1 = "== Main Settings ==";

extern int MagicNumber = 0;

extern bool SignalMail = False;

extern bool EachTickMode = true;

extern double Lots = 4;

extern int Slippage = 2;

extern bool UseStopLoss = false;

extern int StopLoss = 100;

extern bool UseTakeProfit = false;

extern int TakeProfit = 15;

extern bool UseTrailingStop = true;

extern int TrailingStop = 45;

extern bool MoveStopOnce = False;

extern int MoveStopWhenPrice = 50;

extern int MoveStopTo = 1;

extern int MaxConcurrentTrades = 2;

//Version 2.01

int BarCount;

int Current;

bool TickCheck = False;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init() {

BarCount = Bars;

if (EachTickMode) Current = 0; else Current = 1;

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit() {

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

int Order = SIGNAL_NONE;

int Total, Ticket;

double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;

Total = OrdersTotal();

Order = SIGNAL_NONE;

//+------------------------------------------------------------------+

//| Variable Begin |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| 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() && OrderMagicNumber() == MagicNumber) {

IsTrade = True;

if(OrderType() == OP_BUY) {

//Close

//+------------------------------------------------------------------+

//| Signal Begin(Exit Buy) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| 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;

}

//MoveOnce

if(MoveStopOnce && MoveStopWhenPrice > 0) {

if(Bid - OrderOpenPrice() >= Point * MoveStopWhenPrice) {

if(OrderStopLoss() < OrderOpenPrice() + Point * MoveStopTo) {

OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + Point * MoveStopTo, OrderTakeProfit(), 0, Red);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if(Bid - OrderStopLoss() > 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) |

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//| 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;

}

//MoveOnce

if(MoveStopOnce && MoveStopWhenPrice > 0) {

if(OrderOpenPrice() - Ask >= Point * MoveStopWhenPrice) {

if(OrderStopLoss() > OrderOpenPrice() - Point * MoveStopTo) {

OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() - Point * MoveStopTo, OrderTakeProfit(), 0, Red);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

//Trailing stop

if(UseTrailingStop && TrailingStop > 0) {

if(((OrderStopLoss - Ask) > (Point * TrailingStop)) || OrderStopLoss() == 0) {

if((OrderStopLoss() > (Ask + Point * TrailingStop)) || (OrderStopLoss() == 0)) {

OrderModify(OrderTicket(), OrderOpenPrice(), Ask + Point * TrailingStop, OrderTakeProfit(), 0, DarkOrange);

if (!EachTickMode) BarCount = Bars;

continue;

}

}

}

}

}

}

Changed in Red

 

I made the changes in red, but now the EA doesnt seem to want to work. I'll post the whole EA for you to take a look at. Thanks!

Files:
williams_ea.mq4  12 kb
 

Make sure you put () after orderstoploss

 

Cool, that did it!

How about this indicator....

Could it be modified to have the prices show different colors depending on how they open? Like if it opens higher than the previous bar the price would be green, if lower, then red, and if even then yellow? With these colors being selectable? Also, could you extend it to show 10 openings instead of just 6?

Also on the EA we just fixed, How would I put an input setting like, "Pips to Activate Trailing Stop". I have this in another EA but unsure how to code it.

Files:
period_open.mq4  23 kb
 

OrderComment()

OrderTicket()

OrderLots()

OrderStopLoss()

OrderTakeProfit()

OrderOpenTime()

Those are all potential order-filtering tools.

What else about those specific trades makes them unique?

 
Ronald Raygun:
OrderComment()

OrderTicket()

OrderLots()

OrderStopLoss()

OrderTakeProfit()

OrderOpenTime()

Those are all potential order-filtering tools.

What else about those specific trades makes them unique?

Two of the most important for filtering orders are OrderSymbol() and OrderMagicNumber(). Another of note is OrderComment().

The best idea is to enter one of these into the Metaeditor, then click on the word asnd then press F1. Doing so will display the help and list all of the order functions.

Happy programming,

Hiachiever

 

Using OrderComment

Be careful when using OrderComment() to identify trades. Sometimes the broker will add characters to the comment.

It is best to use

if (StringFind(OrderComment(), UserComment, 0) > 0)

{

// order identified by UserComment found in OrderComment

}

instead of

if (OrderComment() == UserComment)

{

// order might be identified by UserComment

// if OrderComment was not changed by broker

}

Robert

 

Close trades at beakeven

..............