//+------------------------------------------------------------------+
//| Auto.mq4 |
//| Copyright © 2012, MetaQuotes Software Corp. |
//| |
//+------------------------------------------------------------------+
#define MAGICMA 20050610
extern double Lots = 0.1;
double MaximumRisk = 0.02;
double DecreaseFactor = 3;
double MovingPeriod = 12;
double MovingShift = 6;
extern double BUY_PRICE=0.0; // get out because we lost and reverse
extern double SELL_PRICE=0.0; // sell and continue forward
extern int TARGET=200; // sell and continue forward
extern int Commission=3;
string LastTrade = "Last_Trade";
string Buy = "Buy";
string Sell = "Sell";
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>20) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
//---- buy conditions
if (OrdersTotal()==0)
{
if(Bid==BUY_PRICE)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Bid,3,(SELL_PRICE-(Commission*Point)),(BUY_PRICE+(TARGET*Point)),"",MAGICMA,0,Blue);
LastTrade = Buy;
SendMail ("BUY "+Symbol(),"1st Buy Excuted");
PlaySound ("news.wav");
return;
}
}
if(Bid==BUY_PRICE && LastTrade ==Sell)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Bid,3,(SELL_PRICE-(Commission*Point)),(BUY_PRICE+(TARGET*Point)),"",MAGICMA,0,Blue);
LastTrade = Buy;
SendMail ("BUY "+Symbol(),"Buy Excuted");
PlaySound ("news.wav");
return;
}
//---- sell conditions
if (OrdersTotal()==0)
{
if(Ask==SELL_PRICE)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Ask,3,(BUY_PRICE+(Commission*Point)),(SELL_PRICE-(TARGET*Point)),"",MAGICMA,0,Red);
LastTrade = Sell;
SendMail ("SELL "+Symbol(),"1st Sell Excuted");
PlaySound ("news.wav");
return;
}
}
if(Ask==SELL_PRICE && LastTrade ==Buy)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Ask,3,(BUY_PRICE+(Commission*Point)),(SELL_PRICE-(TARGET*Point)),"",MAGICMA,0,Red);
LastTrade = Sell;
SendMail ("SELL "+Symbol(),"Sell Excuted");
PlaySound ("news.wav");
return;
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
//----
}
//+------------------------------------------------------------------+
BUY_PRICE is a variable, so i think i can change before run the script.
or i have to declare before begin?
or i have to declare before begin?
meroo_basha:
//+------------------------------------------------------------------+
//| Auto.mq4 |
//| Copyright © 2012, MetaQuotes Software Corp. |
//| |
//+------------------------------------------------------------------+
#define MAGICMA 20050610
extern double Lots = 0.1;
double MaximumRisk = 0.02;
double DecreaseFactor = 3;
double MovingPeriod = 12;
double MovingShift = 6;
extern double BUY_PRICE=0.0; // get out because we lost and reverse
extern double SELL_PRICE=0.0; // sell and continue forward
extern int TARGET=200; // sell and continue forward
extern int Commission=3;
string LastTrade = "Last_Trade";
string Buy = "Buy";
string Sell = "Sell";
//+------------------------------------------------------------------+
//| Calculate open positions |
//+------------------------------------------------------------------+
int CalculateCurrentOrders(string symbol)
{
int buys=0,sells=0;
//---- return orders volume
if(buys>0) return(buys);
else return(-sells);
}
//+------------------------------------------------------------------+
//| Calculate optimal lot size |
//+------------------------------------------------------------------+
double LotsOptimized()
{
double lot=Lots;
int orders=HistoryTotal(); // history orders total
int losses=0; // number of losses orders without a break
//---- select lot size
lot=NormalizeDouble(AccountFreeMargin()*MaximumRisk/1000.0,1);
//---- calcuulate number of losses orders without a break
if(DecreaseFactor>0)
{
for(int i=orders-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)==false) { Print("Error in history!"); }
if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue;
//----
if(OrderProfit()>0) break;
if(OrderProfit()<0) losses++;
}
if(losses>20) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1);
}
//---- return lot size
if(lot<0.1) lot=0.1;
return(lot);
}
//+------------------------------------------------------------------+
//| Check for open order conditions |
//+------------------------------------------------------------------+
void CheckForOpen()
{
int res;
//---- buy conditions
if (OrdersTotal()==0)
{
if(Bid==BUY_PRICE)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Bid,3,(SELL_PRICE-(Commission*Point)),(BUY_PRICE+(TARGET*Point)),"",MAGICMA,0,Blue);
LastTrade = Buy;
SendMail ("BUY "+Symbol(),"1st Buy Excuted");
PlaySound ("news.wav");
return;
}
}
if(Bid==BUY_PRICE && LastTrade ==Sell)
{
res=OrderSend(Symbol(),OP_BUY,LotsOptimized(),Bid,3,(SELL_PRICE-(Commission*Point)),(BUY_PRICE+(TARGET*Point)),"",MAGICMA,0,Blue);
LastTrade = Buy;
SendMail ("BUY "+Symbol(),"Buy Excuted");
PlaySound ("news.wav");
return;
}
//---- sell conditions
if (OrdersTotal()==0)
{
if(Ask==SELL_PRICE)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Ask,3,(BUY_PRICE+(Commission*Point)),(SELL_PRICE-(TARGET*Point)),"",MAGICMA,0,Red);
LastTrade = Sell;
SendMail ("SELL "+Symbol(),"1st Sell Excuted");
PlaySound ("news.wav");
return;
}
}
if(Ask==SELL_PRICE && LastTrade ==Buy)
{
res=OrderSend(Symbol(),OP_SELL,LotsOptimized(),Ask,3,(BUY_PRICE+(Commission*Point)),(SELL_PRICE-(TARGET*Point)),"",MAGICMA,0,Red);
LastTrade = Sell;
SendMail ("SELL "+Symbol(),"Sell Excuted");
PlaySound ("news.wav");
return;
}
//----
}
//+------------------------------------------------------------------+
//| Start function |
//+------------------------------------------------------------------+
void start()
{
//---- check for history and trading
if(IsTradeAllowed()==false) return;
//---- calculate open orders by current symbol
if(CalculateCurrentOrders(Symbol())==0) CheckForOpen();
//----
}
//+------------------------------------------------------------------+
if(IsTradeAllowed()==false) return;
IsTradeAllowed() ???????????
BUT for All Cases Alarm is generated, which mean that the condition true, the only thing that the order is never placed in live trading, Although it works fine on the tester?!!!!!!!!!!!
Ahhh i just noticed that if I allowed auto trading but after a manual confirmation it's working fine.
Can anybody help me? its really so strange!!

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello .. I just started using Auto traders few weeks ago, i start writing a small EA .. when i test it using strategy tester it works fine, but when applying it to the live trading it is not working!!
can anybody help me and tells me why?
bellow is my code: