need help with stop loss please (simple EA)

 

It my first EA and I have problem to set a stop loss, the stop loss works on the buy orders but does not work on the sell orders, it's so weird

this is the code. this EA working with MA its really simple and make profit lol

//+------------------------------------------------------------------+
//| izzy.mq4 |
//| sell when MA crossing without SL and TP (working) |
//| http://ftsa.co.il/ |
//+------------------------------------------------------------------+
#property link ""
#include <stderror.mqh>
#include <stdlib.mqh>
#define IDLE 0
#define SIGNAL_BUY 1
#define SIGNAL_SELL 2
#define SIGNAL_TRBUY 3
#define SIGNAL_TRSELL 4
#define SIGNAL_P 5
#define SIGNAL_L 6
bool trade=false;
int tickets=0;
int ticketb=0;
int Order = IDLE;
bool flagbuy = false;
bool flagsell = false;
double MA18;
double MA9;


extern double lotSize=0.1;
extern int TP=45000000;
extern int SL=4000;
extern int Per18=52;
extern int Per9=9;
extern int shift=1;
extern int MagicNumber=7272;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
if (OrdersTotal()==0)
{ trade=false;
Order = IDLE;
}

if (OrdersTotal()!=0)
{
for (int i=1; i<=OrdersTotal(); i++) //Cycle for all orders..
{ //displayed in the terminal
if(OrderSelect(i-1,SELECT_BY_POS)==true)//If there is the next one
{
if (OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{
trade=true;

break;
}
else {trade=false;}
}
}

}

double spred; //fix the price so it will open order when it's really touching the bands
spred=(Ask-Bid);
int time_test= Period();// define what is the time period we are using

//////////////////// INDICATORS\
MA18 =iMA(NULL,0,Per18,0,MODE_SMA,PRICE_CLOSE,shift);
MA9=iMA(NULL,0,Per9,0,MODE_SMA,PRICE_CLOSE,shift);
Comment("MA18 = ",MA18," MA9= ",MA9, "\nbid= ", Bid,"\nask= ", Ask, "\nspred= ",spred); // show the price of the boll high


//////////////////CHECK FOR BUY

if (trade==false && Order==IDLE && flagbuy== false)
{

if ( MA9>MA18 )

{Order=SIGNAL_BUY;
flagbuy=true;
}
}
//////////////////////////////


//////////////////CHECK FOR SELL

if (trade==false && Order==IDLE && flagsell==false)
{

if ( MA9<MA18 )

{Order=SIGNAL_SELL;
flagsell=true;
}
}

/////////////////////////BUY
if (trade==false)
{
if (Order == SIGNAL_BUY )
{

ticketb=OrderSend(Symbol(),OP_BUY,lotSize,Ask,3, 0, 0,"izzy",MagicNumber,0,Green); //open


if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
OrderModify(ticketb,OrderOpenPrice(),Ask- SL*Point,Ask+ (TP * Point),0,Green);
Order = SIGNAL_TRBUY;
if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
{
Alert("LONG ORDER OPENED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
Order = SIGNAL_TRBUY;



}
else
{
Print("OrderSelect returned the error of ",GetLastError());
Alert("EROR LONG ORDER IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
Order= IDLE;
}
}
}
/////////////////////////////////// END

/////////////////////////sell
if (trade==false)
{
if (Order == SIGNAL_SELL )
{

tickets=OrderSend(Symbol(),OP_SELL,lotSize,Bid,3,0,0,"izzy",MagicNumber,0,Green); // open sell

if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
OrderModify(tickets,OrderOpenPrice(),Ask+(SL*Point),Bid-(TP*Point),0,Green);


if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
{
Alert("SHORT ORDER OPENED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
Order = SIGNAL_TRSELL;


}
else
{
Print("OrderSelect returned the error of ",GetLastError());
Alert("ERROR SHORT ORDER IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
Order= IDLE;
}
}
}
/////////////////////////////////// END

/////////////////check order status buy

if (Order==SIGNAL_TRBUY)
{
if ( MA9<MA18 ){
if(OrderSelect( ticketb, SELECT_BY_TICKET )==true)
bool anbuy= OrderClose(ticketb,lotSize,Bid,3,Red);

}
if(anbuy== true)
{
Order=IDLE;
trade=false;
Alert("ORDER CLOSED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",ticketb);
}
else
{

Print("OrderSelect returned the error of when trying to close ",GetLastError());
Alert("ERROR ORDER closed IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
}
}



if (Order==SIGNAL_TRSELL)
{

if ( MA9>MA18 ){
if(OrderSelect( tickets, SELECT_BY_TICKET )==true)
bool ansell= OrderClose(tickets,lotSize,Ask,3,Green);

}
if(ansell== true)
{
Order=IDLE;
trade=false;
Alert("ORDER CLOSED IN izzy"," Symbol= ",Symbol()," Period=",Period(), " Ticket= ",tickets);
}
else
{

Print("OrderSelect returned the error of when trying to close ",GetLastError());
Alert("EROR ORDER closed IN izzy "," Symbol= ",Symbol()," Period=",Period()," EROR= ",GetLastError());
}
}

if (MA9<MA18)
flagbuy=false;

if ( MA9>MA18 )
flagsell= false;

///////////////////////////////////

//////////////////////////////
//----
return(0);
}
//+------------------------------------------------------------------+

 

can be

OrderModify(tickets,OrderOpenPrice(),Bid+(SL*Point),Bid-(TP*Point),0,Green);
 

no i tried it doesnt work, any other suggestions

thank you

 

I don't know what security you trade, but a TP on a sell

extern int TP=45000000;

that takes the TP into a negative number might not work.

 
phy:

I don't know what security you trade, but a TP on a sell

extern int TP=45000000;

that takes the TP into a negative number might not work.


Oops how I did not think about it

please note for the next time:

 

OMG how i havent see it thank you so much i tried to not use the TP so i used a big number but it seems like it makes error

you can use it maks profit

thanks again

 

No TP, just use zero.

On a Sell, TP and SL are relative to the Ask

Reason: