I'm new and need programming help.
This EA is suppose to place Reverse orders only. So if my last order was a Buy the next one have to be a Sell. It works in back-test but does not in forward testing.
I believe when the Meta-Trader is Re-Started by Windows, my variable (static int Flip_Switch=0) resets back to Zero. I cannot figure out how to set the logic or variable and make the 1st order happen and Not set that switch to 0. Below is my code:
//+Global Varables
int Ticket;
int Orders;
int Loop=1;
static int Flip_Switch=0;
double Edge=0.0000138;
//+------------------------------------------------+
//+------------------------------------------------+
//+External Varables
extern double Lots=0.1;
extern double Macd_StopLoss=20;
extern double Macd_TrailingStop=50;
extern double Macd_TakeProfit=100;
extern double Fast_MA=1;
extern double Slow_MA=10;
extern double Signal=3;
//+------------------------------------------------+
//+------------------------------------------------+
//+Local Varables
int start()
{
//+------------------------------------------------+
//+------------------------------------------------+
//*Money Management
Lots=NormalizeDouble((AccountEquity()*Edge),1);
if (Lots < 0.1) Lots=0.1;
//+------------------------------------------------+
//+------------------------------------------------+
//+PriceSeries
double Price_Buy=NormalizeDouble(Ask,Digits)+
(0*Point);
double Price_Sell=NormalizeDouble(Bid,Digits)-
(0*Point);
//+------------------------------------------------+
//+------------------------------------------------+
//+Macd
double Macd_P=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_MAIN,1);
double Macd_C=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_MAIN,0);
double Macd_Sig_P=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_SIGNAL,1);
double Macd_Sig_C=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_SIGNAL,0);
//+------------------------------------------------+
//+------------------------------------------------+
//+OrderSelect Loop
for(int Loop=1; Loop<=OrdersTotal(); Loop++)
{
OrderSelect(Ticket,SELECT_BY_TICKET);
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Modify Macd Orders
if (OrdersTotal()>0)
if (OrderMagicNumber()==4)
{
//+Close Sell for Reverse
if (OrderType()==OP_SELL)
if (Macd_C<0)
if (Macd_P<=Macd_Sig_P && Macd_C>Macd_Sig_C)
{
OrderClose(Ticket,OrderLots(),
NormalizeDouble(Bid,Digits),4,Blue);
}
//+Close Buy for Reverse
if (OrderType()==OP_BUY)
if (Macd_C>0)
if (Macd_P>=Macd_Sig_P && Macd_C<Macd_Sig_C)
{
OrderClose(Ticket,OrderLots(),
NormalizeDouble(Ask,Digits),4,Blue);
}
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Macd Trailing Stop
if (Macd_TrailingStop>0)
if (OrdersTotal()>0)
if (OrderMagicNumber()==4)
{
//+TrailingStop Buy
if (OrderType()==OP_BUY)
if(Ask-OrderOpenPrice()>=Point)
{
if(OrderStopLoss()<Ask-Macd_TrailingStop*Point)
{
OrderModify(Ticket,OrderOpenPrice(),
NormalizeDouble(Ask,Digits)-Macd_TrailingStop*Point,
OrderTakeProfit(),0,White);
}
}
//+TrailingStop Sell
if (OrderType()==OP_SELL)
if(Bid-OrderOpenPrice()<=Point)
{
if(OrderStopLoss()>Bid+Macd_TrailingStop*Point)
{
OrderModify(Ticket,OrderOpenPrice(),
NormalizeDouble(Bid,Digits)+Macd_TrailingStop*Point,
OrderTakeProfit(),0,White);
}
}
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Macd Place Orders
if (OrdersTotal()<4)
{
//+------------------------------------------------+
//+------------------------------------------------+
//*Selling
if (Flip_Switch<=0)
if (Macd_C>0)
if (Macd_P>=Macd_Sig_P && Macd_C<Macd_Sig_C)
{
Ticket=OrderSend(Symbol(),OP_SELL,Lots,
NormalizeDouble(Bid,Digits),4,
Price_Sell+(Macd_StopLoss*Point),
Price_Sell-(Macd_TakeProfit*Point),
"Macd Short",4,0,Red);
Flip_Switch=1;
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Buying
if (Flip_Switch>=0)
if (Macd_C<0)
if (Macd_P<=Macd_Sig_P && Macd_C>Macd_Sig_C)
{
Ticket=OrderSend(Symbol(),OP_BUY,Lots,
NormalizeDouble(Ask,Digits),4,
Price_Buy-(Macd_StopLoss*Point),
Price_Buy+(Macd_TakeProfit*Point),
"Macd Long",4,0,Green);
Flip_Switch=-1;
}
}
//+------------------------------------------------+
//+------------------------------------------------+
return(0);
}
This EA is suppose to place Reverse orders only. So if my last order was a Buy the next one have to be a Sell. It works in back-test but does not in forward testing.
I believe when the Meta-Trader is Re-Started by Windows, my variable (static int Flip_Switch=0) resets back to Zero. I cannot figure out how to set the logic or variable and make the 1st order happen and Not set that switch to 0. Below is my code:
//+Global Varables
int Ticket;
int Orders;
int Loop=1;
static int Flip_Switch=0;
double Edge=0.0000138;
//+------------------------------------------------+
//+------------------------------------------------+
//+External Varables
extern double Lots=0.1;
extern double Macd_StopLoss=20;
extern double Macd_TrailingStop=50;
extern double Macd_TakeProfit=100;
extern double Fast_MA=1;
extern double Slow_MA=10;
extern double Signal=3;
//+------------------------------------------------+
//+------------------------------------------------+
//+Local Varables
int start()
{
//+------------------------------------------------+
//+------------------------------------------------+
//*Money Management
Lots=NormalizeDouble((AccountEquity()*Edge),1);
if (Lots < 0.1) Lots=0.1;
//+------------------------------------------------+
//+------------------------------------------------+
//+PriceSeries
double Price_Buy=NormalizeDouble(Ask,Digits)+
(0*Point);
double Price_Sell=NormalizeDouble(Bid,Digits)-
(0*Point);
//+------------------------------------------------+
//+------------------------------------------------+
//+Macd
double Macd_P=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_MAIN,1);
double Macd_C=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_MAIN,0);
double Macd_Sig_P=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_SIGNAL,1);
double Macd_Sig_C=iMACD
("EURUSD",PERIOD_H4,Fast_MA,Slow_MA,Signal,
PRICE_OPEN,MODE_SIGNAL,0);
//+------------------------------------------------+
//+------------------------------------------------+
//+OrderSelect Loop
for(int Loop=1; Loop<=OrdersTotal(); Loop++)
{
OrderSelect(Ticket,SELECT_BY_TICKET);
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Modify Macd Orders
if (OrdersTotal()>0)
if (OrderMagicNumber()==4)
{
//+Close Sell for Reverse
if (OrderType()==OP_SELL)
if (Macd_C<0)
if (Macd_P<=Macd_Sig_P && Macd_C>Macd_Sig_C)
{
OrderClose(Ticket,OrderLots(),
NormalizeDouble(Bid,Digits),4,Blue);
}
//+Close Buy for Reverse
if (OrderType()==OP_BUY)
if (Macd_C>0)
if (Macd_P>=Macd_Sig_P && Macd_C<Macd_Sig_C)
{
OrderClose(Ticket,OrderLots(),
NormalizeDouble(Ask,Digits),4,Blue);
}
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Macd Trailing Stop
if (Macd_TrailingStop>0)
if (OrdersTotal()>0)
if (OrderMagicNumber()==4)
{
//+TrailingStop Buy
if (OrderType()==OP_BUY)
if(Ask-OrderOpenPrice()>=Point)
{
if(OrderStopLoss()<Ask-Macd_TrailingStop*Point)
{
OrderModify(Ticket,OrderOpenPrice(),
NormalizeDouble(Ask,Digits)-Macd_TrailingStop*Point,
OrderTakeProfit(),0,White);
}
}
//+TrailingStop Sell
if (OrderType()==OP_SELL)
if(Bid-OrderOpenPrice()<=Point)
{
if(OrderStopLoss()>Bid+Macd_TrailingStop*Point)
{
OrderModify(Ticket,OrderOpenPrice(),
NormalizeDouble(Bid,Digits)+Macd_TrailingStop*Point,
OrderTakeProfit(),0,White);
}
}
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Macd Place Orders
if (OrdersTotal()<4)
{
//+------------------------------------------------+
//+------------------------------------------------+
//*Selling
if (Flip_Switch<=0)
if (Macd_C>0)
if (Macd_P>=Macd_Sig_P && Macd_C<Macd_Sig_C)
{
Ticket=OrderSend(Symbol(),OP_SELL,Lots,
NormalizeDouble(Bid,Digits),4,
Price_Sell+(Macd_StopLoss*Point),
Price_Sell-(Macd_TakeProfit*Point),
"Macd Short",4,0,Red);
Flip_Switch=1;
}
//+------------------------------------------------+
//+------------------------------------------------+
//+Buying
if (Flip_Switch>=0)
if (Macd_C<0)
if (Macd_P<=Macd_Sig_P && Macd_C>Macd_Sig_C)
{
Ticket=OrderSend(Symbol(),OP_BUY,Lots,
NormalizeDouble(Ask,Digits),4,
Price_Buy-(Macd_StopLoss*Point),
Price_Buy+(Macd_TakeProfit*Point),
"Macd Long",4,0,Green);
Flip_Switch=-1;
}
}
//+------------------------------------------------+
//+------------------------------------------------+
return(0);
}