How to code? - page 111

 
MrPip:
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

Exactly ... And also, sometimes the original comment coded in the EA is too long and will be troncated by the platform (not sure if the rest of the comment is in the "memory" but it is not display in comment column).

FerruFx

 

need help with my EA

hi,

i am working on an EA that opens long and short positions simultaneously and i am facing this problem...when both type of positions are open, say first one is short and second is long...the EA will not close the long position if the short is still open, it will wait for the short to be closed and then close the long when conditions are met and vice versa. herebelow is the part of the EA where it should close the open positions:

//---- Close Open Positions

for(int cnt=0; cnt<total; cnt++)

{

OrderSelect(cnt, SELECT_BY_POS);

{

//-- Close Long

if(OrderSymbol() == Symbol() && OrderType()==OP_BUY && rsi1>75 && rsi<75)

{

OrderClose(OrderTicket(),OrderLots(),Bid,5,Green);

LotL=Lot;

}

//-- Close Short

if(OrderSymbol() == Symbol() && OrderType()==OP_SELL && rsi125)

{

OrderClose(OrderTicket(),OrderLots(),Ask,5,Red);

LotS=Lot;

}

}

}

*where rsi1 is the previous rsi and rsi is the current reading

*LotL & LotS is a multiplier of the original Lot, i have set it to open a maximum of 3 positions each side, so when closing one type of open positions, LotL & LotS will be reset to the original size

the EA is working as intended when opening positions but the problem is with closing open positions, i think the EA in the loop part is not reading all of the open positions but only the first one... any help fixing this is really appreciated!!

cheers!

pete

 

I'm trying to add the "Pips to activate Trailing stop" feature. What I changed is in red. What else needs to be added?

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

extern int MagicNumber = 54321;

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 double Pips_Activate_TS = 5;

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 - 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 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((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(Open[0] > Open[0+1] &&

Open[0+1] >= Open[0+2] &&

Open[0+2]>= Open[0+3] &&

Open[0+3] >= Open[0+4] &&

Open[0+4] >= Open[0+5] &&

Open[0+5] >= Open[0+6]) Order = SIGNAL_SELL;

if(Open[0] < Open[0+1] &&

Open[0+1] <= Open[0+2] &&

Open[0+2]<= Open[0+3] &&

Open[0+3] <= Open[0+4] &&

Open[0+4] <= Open[0+5] &&

Open[0+5] <= Open[0+6]) Order = SIGNAL_BUY;

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

//| Signal End |

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

//Buy

if (Order == SIGNAL_BUY && ((EachTickMode && !TickCheck) || (!EachTickMode && (Bars != BarCount)))) {

if(DecideToOpenTrade(OP_BUY) && TradeSlotsAvailable()) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ")", MagicNumber, 0, DodgerBlue);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("BUY order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Ask, Digits) + " Open Buy");

} 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(DecideToOpenTrade(OP_SELL) && TradeSlotsAvailable()) {

//Check free margin

if (AccountFreeMargin() < (1000 * Lots)) {

Print("We have no money. Free Margin = ", AccountFreeMargin());

return(0);

}

if (UseStopLoss) StopLossLevel = Bid + StopLoss * Point; else StopLossLevel = 0.0;

if (UseTakeProfit) TakeProfitLevel = Bid - TakeProfit * Point; else TakeProfitLevel = 0.0;

Ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, StopLossLevel, TakeProfitLevel, "Sell(#" + MagicNumber + ")", MagicNumber, 0, DeepPink);

if(Ticket > 0) {

if (OrderSelect(Ticket, SELECT_BY_TICKET, MODE_TRADES)) {

Print("SELL order opened : ", OrderOpenPrice());

if (SignalMail) SendMail("[Signal Alert]", "[" + Symbol() + "] " + DoubleToStr(Bid, Digits) + " Open Sell");

} else {

Print("Error opening SELL order : ", GetLastError());

}

}

if (EachTickMode) TickCheck = True;

if (!EachTickMode) BarCount = Bars;

return(0);

}

}

if (!EachTickMode) BarCount = Bars;

return(0);

}

 
 
mifiglo:
Hello fellow traders,

I have this little problem developing an Expert Advisor from a Custom indicator , I've tried the "iCustom" function but my indicator always returns the same value.

It returns something like 21473..., like that

I want the EA to be able to recognise when the sell and buy arrow signals are generated and it should perform the corresponding trade action (i.e. buy when the indicator says up and sell when it says down)

Here is the code of the indicator I would so much appreciate it if someone can come up with a solution ASAP.

Please post also the code lines with your iCustom() function

FerruFx

 

sorry, this is al i've got...

 
 

Rsi Ea

This is my firs EA. It uses 2 rsi's crossing in 3 time frames. I have a problem with the ea taking trades against the crosses. Buying when it is supposed to sell, sell when suppose to buy. Could someone please look at the code and tell me where I went wrong. I haven't been able to see anything wrong.

I have attached the ea and a pic of the problem.

Files:
 

Need help for EA code!

Hi all,

As you know the fx market is 24 hours, I am not comfortable to trade overnight without sitting in front of PC. So I am thinking the best way is program to protect the trade if the price moves towards my favorite direction. For example, if I opened 5 lots in total, when the profit is above 50 pips, it automatically move stop loss to break even. When the profit is above 100 pips, it moves to protect 30 pips.When the profit is above 200 pips, it moves to protect 50 pips. and so on.

I tried to code my self. But it doesn't work when I drag it into my chart and set it trading live. Any body could help me to have a look of my code and fix it? Many thanks.

#property copyright "x li"

#property link ""

#property show_confirm

//---- input parameters

extern int target1 =50;

extern int target2 =100;

extern int target3 =150;

extern int target4 =200;

extern int target5 =300;

extern int protect1 =0;

extern int protect2 =5;

extern int protect3 =50;

extern int protect4 =100;

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

//| script "move stoploss to protect(50->0;100->5;150->50;200->50;300->100)" |

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

int start()

{

bool result;

double stop_loss,point,EntryPrice,profit;

int cmd,total,error,i;

//----

total=OrdersTotal();

point=MarketInfo(Symbol(),MODE_POINT);

//----

for(i=0; i<total; i++)

{

if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

{

cmd=OrderType();

EntryPrice=OrderOpenPrice();

//---- buy orders are considered

if(cmd==OP_BUY)

{

profit=Bid-EntryPrice;

if(profit>target1*point && profit<target2*point)

{

stop_loss=EntryPrice+protect1*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target2*point && profit<target3*point)

{

stop_loss=EntryPrice+protect2*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target3*point && profit<target4*point)

{

stop_loss=EntryPrice+protect3*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target4*point && profit<target5*point)

{

stop_loss=EntryPrice+protect3*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target5*point)

{

stop_loss=EntryPrice+protect4*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

}

//---- sell orders are considered

if(cmd==OP_SELL)

{

profit=EntryPrice-Ask;

if(profit>target1*point && profit<target2*point)

{

stop_loss=EntryPrice-protect1*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target2*point && profit<target3*point)

{

stop_loss=EntryPrice-protect2*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target3*point && profit<target4*point)

{

stop_loss=EntryPrice-protect3*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target4*point && profit<target5*point)

{

stop_loss=EntryPrice-protect3*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

if(profit>target5*point)

{

stop_loss=EntryPrice-protect4*point;

result=OrderModify(OrderTicket(),0,stop_loss,0,0,CLR_NONE);

}

}

}

else { Print( "Error when order select ", GetLastError()); break; }

}

//----

return(0);

}

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

 

Rsi Ea

I do not see why you use the same variable to open and to close trades, but it may work.

This may be the problem:

"Order" is a global variable. So at the end of each run through the Start function it will be set to the last value it was assigned, and use that value for the beginning of the next run through. It you make it local (move it inside Start) it may fix it. Else you will have to reset it after last use of it.

Good Hunting,

Big Be