//+------------------------------------------------------------------+ //| manov-singlesymbol.mq4 | //| Copyright © 2010, alohafx | //| http://alohafx.blog36.fc2.com/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2010, alohafx" #property link "http://alohafx.blog36.fc2.com/" //---- input parameters extern string symbol="EURUSD"; extern int MagicNumber = 2010112801; extern int direction=0; extern int nampin=110; extern int tp=135; extern int slippage=3; int stoplevel,POS_n_total,ticket; double AveragePrice,point; double OpenPrice[20]; //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() { //---- //digits if(Digits == 3 || Digits == 5) { nampin *= 10; tp *= 10; slippage *= 10; } stoplevel = MarketInfo(symbol, MODE_STOPLEVEL); point= MarketInfo(symbol, MODE_POINT); POS_n_total=0; //---- return(0); } //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| expert start function | //+------------------------------------------------------------------+ int start() { //---- if(Bars<100 || IsTradeAllowed() != true) return(0); //---- count_position(); if(POS_n_total > 0) { Allclose(); Nampin(); } //---- else { Call_Trade(); } //---- return(0); } //+---------------------------------------------------------------------+ void count_position() { POS_n_total=0; double temp=0; AveragePrice=0; for( int i = 0 ; i < OrdersTotal() ; i++ ) { if( OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber && OrderSymbol() == symbol) POS_n_total++; temp=temp+OrderOpenPrice(); } if(POS_n_total>0) AveragePrice=temp/POS_n_total; return(0); } //+------------------------------------------------------------------+ void Allclose() { bool Result; int i,Pos,Error; int tpLevel; double tpPrice; tpLevel=tp; if(POS_n_total==2) tpLevel=tp/2; if(POS_n_total>=3) tpLevel=tp/4; if(POS_n_total>=6) tpLevel=tp/8; if(POS_n_total>=10) tpLevel=tp/16; if(POS_n_total>=20) tpLevel=tp/32; if(direction==0) tpPrice=AveragePrice+tpLevel*point; else tpPrice=AveragePrice-tpLevel*point; if( (direction==0 && MarketInfo(symbol, MODE_BID)>tpPrice) || (direction==1 && MarketInfo(symbol, MODE_ASK)=0; i--) { if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == MagicNumber && OrderSymbol() == symbol) { Pos=OrderType(); if(Pos==OP_BUY || Pos==OP_SELL) { if(Pos==OP_BUY) Result=OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_BID), slippage, Blue); else Result=OrderClose(OrderTicket(), OrderLots(), MarketInfo(symbol, MODE_ASK), slippage, Red); if(Result!=true) { Error=GetLastError(); Print("LastError = ",Error); } else Error=0; } } } if(direction==0)direction=1; else direction=0; ArrayInitialize(OpenPrice,0); // Print(AveragePrice); } return(0); } //+------------------------------------------------------------------+ void Nampin() { double nPrice; if(POS_n_total>=20) return(0); if(direction==0) nPrice=AveragePrice-nampin*point; else nPrice=AveragePrice+nampin*point; if( (direction==0 && MarketInfo(symbol, MODE_ASK)nPrice) ) Call_Trade(); return(0); } //+------------------------------------------------------------------+ void Call_Trade() { if (direction==0) { ticket=OrderSend(symbol,OP_BUY,0.1,MarketInfo(symbol, MODE_ASK),slippage,0,0,"Buy",MagicNumber,0,Blue); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Buy order opened : ",OrderOpenPrice()," Free Margin = ", AccountFreeMargin()); // OpenPrice[POS_n_total]=OrderOpenPrice(); } else { Print("Error opening Buy order : ",GetLastError()); } } if (direction==1) { ticket=OrderSend(symbol,OP_SELL,0.1,MarketInfo(symbol, MODE_BID),slippage,0,0,"Sell",MagicNumber,0,Red); if (ticket>0) { if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("Sell order opened : ",OrderOpenPrice()," Free Margin = ", AccountFreeMargin()); // OpenPrice[POS_n_total]=OrderOpenPrice(); } else { Print("Error opening Sell order : ",GetLastError()); } } return(0); } //+------------------------------------------------------------------+ //EOF