//+------------------------------------------------------------------+ //| VininE_Cyber Cycle | //| Copyright © 2008, Victor Nicolaev | //| e-mail: vinin@mail.ru | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Victor Nicolaev" #property link "e-mail: vinin@mail.ru" //=================================================================== extern string S1 = "Параметры индикатора"; extern double alpha=0.01; extern double betta=1.0; extern double level_Buy_Open=0.5; extern double level_Sell_Open=-0.5; extern double level_Buy_Close=0.5; extern double level_Sell_Close=-0.5; extern string S2 = "Параметры MM"; extern double Lots = 1 ; extern int StopLoss = 0 ; // Стоплосс extern int TakeProfit = 0 ; // Стоплосс extern int Slippage=50; extern string S3 = "Параметры Советника"; extern int Magic = 20090309; extern string _comment = ""; // Комментарий color clOpenBuy=Red; color clOpenSell=Blue; color clCloseBuy=Red; color clCloseSell=Blue; int Order_Count[6]; string NameOP[]={"BUY","SELL","BUYLIMMIT","SELLLIMIT","BUYSTOP","SELLSTOP"}; #include #include //+------------------------------------------------------------------+ //| expert initialization function | //+------------------------------------------------------------------+ int init() {//0 GetLastError(); // Очистка последней ошибки return(0);}//0 //+------------------------------------------------------------------+ //| expert deinitialization function | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //Работа по отдельной ТС //+------------------------------------------------------------------+ int start() { if (!NevBar()) return(0); if (CheckClose()) Order_Close(); int cmd1=CheckOpen(); if (cmd1==-1) return(0); SetOrder(cmd1); return(0); } bool SetOrder(int OP){ bool Res=false; int cmd=MathAbs(OP-1); Order_Count_Calculate(); // Подсчет открытых позиций if (Order_Count[cmd]>0) // Контроль позиций для закрытия Order_Close(cmd); if (Order_Count[OP]==0) // Контроль позиций для открытия Order_Open( OP); return(Res); } int CheckOpen(){ int Res=-1; double tmp1=iCustom(NULL,0,"VininI_Cyber Cycle(V2)", alpha, betta,PRICE_OPEN,0,1); double tmp2=iCustom(NULL,0,"VininI_Cyber Cycle(V2)", alpha, betta,PRICE_OPEN,0,2); if (tmp1>level_Buy_Open) Res=OP_BUY; if (tmp1level_Sell_Close) Res=true; return(Res); } bool NevBar() { static int PrevBar=0; int NevBar=Time[0]; if (PrevBar==NevBar) return(false); PrevBar=NevBar; return(true); } //====================================================================================================== // Функция логического сравнения //===================================================================================================== double iif(bool A, double B, double C) {if(A) return(B); return(C); } //====================================================================================================== // Функция открытия ордеров //===================================================================================================== void Order_Open(int OP){ int err=GetLastError(); RefreshRates(); int cmd=iif(OP==OP_BUY,1,-1); double _ask =MarketInfo(Symbol(),MODE_ASK); double _bid =MarketInfo(Symbol(),MODE_BID); double _point =MarketInfo(Symbol(),MODE_POINT); double _digits =MarketInfo(Symbol(),MODE_DIGITS); double price= iif(OP==OP_BUY,_ask,_bid); double _sl=NormalizeDouble(iif(StopLoss==0,0,price-cmd*StopLoss*_point),_digits); double _tp=NormalizeDouble(iif(TakeProfit==0,0,price+cmd*TakeProfit*_point),_digits); double lots=GetLots(); OrderSend(Symbol(), OP, lots, price, 3,_sl,_tp,_comment,Magic, 0); err=GetLastError(); if (err>0) Print(Ask," ;", Bid, " ;", price," ;",_sl," ;",_tp); } //====================================================================================================== // Функция закрытия ордеров //===================================================================================================== void Order_Close(int OP=-1){ bool res; GetLastError(); for(int i = OrdersTotal() - 1; i >= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; if (OrderType()!=OP && OP!=-1) continue; RefreshRates(); double _ask =MarketInfo(Symbol(),MODE_ASK); double _bid =MarketInfo(Symbol(),MODE_BID); double _point =MarketInfo(Symbol(),MODE_POINT); double _digits =MarketInfo(Symbol(),MODE_DIGITS); double ClosePrice=NormalizeDouble(iif(OrderType()==OP_BUY,_bid, _ask),_digits); res=OrderClose(OrderTicket(), OrderLots(), ClosePrice, 3, iif(OrderType()==OP_BUY,clCloseBuy,clCloseSell)); if (!res) { int err=GetLastError(); Print("Error(",err,") delete order ",NameOP[OP],": ",ErrorDescription(err)); } } } double NormalizePrice(double Price){ RefreshRates(); double Res=Price; double MinStop=MarketInfo(Symbol(),MODE_STOPLEVEL); if (MathAbs(Price-Close[0])= 0; i--) { if (!OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() != Symbol()) continue; if (OrderMagicNumber() != Magic) continue; Order_Count[OrderType()]++; } } //=============================================================================== // Функция расчитывает объем лота исходя из заданного уровня риска // лот=Э/(ДД/10/(%Э*%Р)+М) // // М - маржа, залог на 1 лот // //=============================================================================== double GetLots() { double LotMin=MarketInfo(Symbol(),MODE_MINLOT); double LotMax=MarketInfo(Symbol(),MODE_MAXLOT); double LotStep=MarketInfo(Symbol(),MODE_LOTSTEP); double lot=MathRound(Lots/LotStep)*LotStep; lot=MathMax(LotMin,MathMin(LotMax,lot)); return(lot); }