Need help to modify EA With Triggerline

 

Hi There,

I try to code an EA to buy and sell with Triggerline indecator, It is work ok now but if the order is stoplose or take profit it open new order.

I wont EA to open one order for ech condetion, for ex, if triggerline red color open sell and not open new order even stoplose or takeprofit till chang color to blue.

code:

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

//| TriggerlineEA .mq4 |

//| |

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

#property copyright "TriggerlineEA"

//---- Trades limits

extern double TakeProfit=20;

extern double TrailingStop=35;

extern double StopLoss=20;

extern int HedgeLevel=6;

extern bool UseClose = true;

extern bool UseMACD = false;

extern double Expiration = 7200;

extern double TimeFrame=0 ;

extern int ShortEma = 4;

extern int LongEma = 24;

extern int CurrentBar = 1;

extern double Lots = 0.10;

//Trigger Settings

extern string Triggers_Parameters_1 = "Trigger Settings_1";

extern int Rperiod_1 = 10;

extern int LSMA_Period_1 = 5;

//Trigger Settings

extern string Triggers_Parameters_2 = "Trigger Settings_2";

extern int Rperiod_2 = 8;

extern int LSMA_Period_2 = 2;

//Trigger Settings

extern string Triggers_Parameters_3 = "Trigger Settings_3";

extern int Rperiod_3 = 6;

extern int LSMA_Period_3 = 2;

string ExpertName = "TriggerlineEA ";

int Magic = 506070;

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

int init()

{

return(0);

}

int deinit()

{

return(0);

}

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

int Signal()

{

double TrigSignal_1 =iCustom(NULL, TimeFrame, "Triggerlines",Rperiod_1,LSMA_Period_1,2,2);

if (TrigSignal_1 != EMPTY_VALUE) return (1); //Buy

if (TrigSignal_1 == EMPTY_VALUE) return (2); //Sell

return (0); //elsewhere

}

int Signal_2()

{

double TrigSignal_2 =iCustom(NULL, TimeFrame, "Triggerlines",Rperiod_2,LSMA_Period_2,2,2);

if (TrigSignal_2 != EMPTY_VALUE) return (1); //Buy

if (TrigSignal_2 == EMPTY_VALUE) return (2); //Sell

return (0); //elsewhere

}

int Signal_3()

{

double TrigSignal_3 =iCustom(NULL, TimeFrame, "Triggerlines",Rperiod_3,LSMA_Period_3,2,2);

if (TrigSignal_3 != EMPTY_VALUE) return (1); //Buy

if (TrigSignal_3 == EMPTY_VALUE) return (2); //Sell

return (0);

}

bool isNewSumbol(string current_symbol)

{

//loop through all the opened order and compare the symbols

int total = OrdersTotal();

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

string selected_symbol = OrderSymbol();

if (current_symbol == selected_symbol)

return (False);

}

return (True);

}

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

//| expert start function |

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

int start()

{

//----

int cnt, ticket, total;

double SEma, LEma;

if(Bars<100)

{

Print("bars less than 100");

return(0);

}

if(TakeProfit<10)

{

Print("TakeProfit less than 10");

return(0); // check TakeProfit

}

total = OrdersTotal();

if(total < 1 || isNewSumbol(Symbol()))

{

if( Signal() == 1 && Signal_2() == 1 && Signal_3() == 1 )

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*MarketInfo(Symbol(),MODE_POINT),Ask+TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,CurTime() + 3600,Green);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if( Signal() == 2 && Signal_2() == 2 && Signal_3() == 2 )

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*MarketInfo(Symbol(),MODE_POINT),Bid-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertName,Magic,0,Red);

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());

}

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

return(0);

}

return(0);

}

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

{

OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

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

{

if(OrderType()==OP_BUY) // long position is opened

{

if(UseClose)

{

if(Signal_3() == 2)

{

OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position

return(0); // exit

}

}

// check for trailing stop

if(TrailingStop>0)

{

if(Bid-OrderOpenPrice()>MarketInfo(Symbol(),MODE_POINT)*TrailingStop)

{

if(OrderStopLoss()<Bid-MarketInfo(Symbol(),MODE_POINT)*TrailingStop)

{

OrderModify(OrderTicket(),OrderOpenPrice(),Bid-MarketInfo(Symbol(),MODE_POINT)*TrailingStop,OrderTakeProfit(),0,Green);

return(0);

}

}

}

}

else // go to short position

{

if(UseClose)

{

if(Signal_3() == 1)

{

OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position

return(0); // exit

}

}

// check for trailing stop

if(TrailingStop>0)

{

if((OrderOpenPrice()-Ask)>(MarketInfo(Symbol(),MODE_POINT)*TrailingStop))

{

if((OrderStopLoss()>(Ask+MarketInfo(Symbol(),MODE_POINT)*TrailingStop)) || (OrderStopLoss()==0))

{

OrderModify(OrderTicket(),OrderOpenPrice(),Ask+MarketInfo(Symbol(),MODE_POINT)*TrailingStop,OrderTakeProfit(),0,Red);

return(0);

}

}

}

}

}

}

return(0);

}

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

 

This code change will rotate order type as trade signals rotate. This should stop mutilple same type orders opening while triggerline indicator remains same color.

Wackena

int OT;

if(total < 1 || isNewSumbol(Symbol()))

{

if( Signal() == 1 && Signal_2() == 1 && Signal_3() == 1 && OT!=1)

{

ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*MarketInfo(Symbol(),MODE_POINT),Ask+TakeP rofit*MarketInfo(Symbol(),MODE_POINT),ExpertName,M agic,CurTime() + 3600,Green);

OT=1;

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("BUY order opened : ",OrderOpenPrice());

}

else Print("Error opening BUY order : ",GetLastError());

return(0);

}

if( Signal() == 2 && Signal_2() == 2 && Signal_3() == 2 && OT!=2)

{

ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+S topLoss*MarketInfo(Symbol(),MODE_POINT),Bid-TakeProfit*MarketInfo(Symbol(),MODE_POINT),ExpertN ame,Magic,0,Red);

OT=2;

if(ticket>0)

{

if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES )) Print("SELL order opened : ",OrderOpenPrice());

}

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

return(0);

}

return(0);

}
 

Wackena,

Thanks very much for try to help me, But it is not work.

 
Safeer:
Wackena, Thanks very much for try to help me, But it is not work.

Just make sure you put the "int OT;" up in the Inputs section at the beginning of the EA.

Wackena

Reason: