//+------------------------------------------------------------------+ // //+------------------------------------------------------------------+ #property link "http://www.forexfactory.com/" #property version "1.00" #property description "Programmer Voldemar227" #property strict //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum OnOff { Off, On }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum Trade { Only_BUY, // Only BUY Only_SELL, // Only SELL Only_All // BUY and SELL }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum ma { BUY_MA = 0, SELL_MA = 1, OFF = 2 }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ enum typelot { fix_lot = 0, // Fixed Start Lot 0.01 / 0.01 / 0.01 / 0.01 / 0.01 /............. Summ_lot = 1, // Summ Sart Lot 0.01 / 0.02 / 0.03 / 0.04 / 0.05 /............. Martingale= 2, // Martingale Lot 0.01 / 0.02 / 0.04 / 0.08 / 0.16 /............. Step_lot = 3 // Step Lot 0.01 / 0.01 / 0.01 / 0.02 / 0.02 / 0.02 / 0.03 / 0.03 / 0.03 / 0.04 / 0.04 / 0.04 /............ }; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ input Trade TypeOrders = Only_SELL; input typelot TypeLot = fix_lot; // Type Lot input OnOff TradeTrend = Off; // TradeTrend input double StartLot = 0.01; // Lot input int MinStep = 400; // Step order input int MinStepPlus = 0; // Add minimal step input double MinProfit = 10.00; // Minimal Profit Close input int Magic = 227; input int Slippage = 30; //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Expert tick function | //+------------------------------------------------------------------+ int b=0,s=0; double BuyProfit = 0,SellProfit = 0,BuyMinPrice = 0,SellMaxPrice = 0,BuyMinLot = 0,SellMaxLot = 0,BuyMaxPrice = 0,SellMinPrice = 0,SellTicProfit = 0,BuyTicProfit = 0; int BuyMaxTic = 0,SellMinTic = 0; double buylot=0,sellot=0; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void OnTick() { b=0;s=0; BuyProfit = 0; SellProfit = 0; BuyMinPrice= 0; SellMaxPrice=0; BuyMinLot = 0; SellMaxLot = 0; BuyMaxPrice= 0; SellMinPrice=0; SellTicProfit = 0; BuyTicProfit = 0; BuyMaxTic = 0; SellMinTic = 0; for(int i=OrdersTotal()-1; i>=0; i--) if(OrderSelect(i,SELECT_BY_POS)) if(OrderSymbol()==Symbol()) if(OrderMagicNumber()==Magic) { if(OrderType()==OP_BUY) { b++; if(OrderOpenPrice()BuyMaxPrice || BuyMaxPrice==0) { BuyMaxPrice=OrderOpenPrice(); BuyMaxTic=OrderTicket(); BuyTicProfit=OrderProfit(); } if(OrderProfit()>0)BuyProfit+=OrderProfit()+OrderCommission()+OrderSwap(); } if(OrderType()==OP_SELL) { s++; if(OrderOpenPrice()>SellMaxPrice || SellMaxPrice==0) { SellMaxPrice=OrderOpenPrice(); SellMaxLot=OrderLots(); } if(OrderOpenPrice()0)SellProfit+=OrderProfit()+OrderCommission()+OrderSwap(); } } // Summ profit final double ProfitBuy=BuyTicProfit+BuyProfit; double ProfitSel=SellTicProfit+SellProfit; // --- double BuyDist = NormalizeDouble(MinStep*Point()+MinStepPlus*Point()*b,Digits()); double SellDist = NormalizeDouble(MinStep*Point()+MinStepPlus*Point()*s,Digits()); // --- buylot=b==0?StartLot:CalcLot(OP_BUY); sellot=s==0?StartLot:CalcLot(OP_SELL); // --- if(TypeOrders==Only_BUY || TypeOrders==Only_All) if((b==0) || (b>0 && (BuyMinPrice-Ask)>=BuyDist) || ((b>0 && (Ask-BuyMaxPrice)>=BuyDist) && TradeTrend==On)) if(OrderSend(Symbol(),OP_BUY,NormalizeDouble(buylot,2),NormalizeDouble(Ask,Digits()),Slippage,0,0,"My order",Magic,0,clrGreen)<0) Print(" Error open Buy N ",GetLastError()); if(TypeOrders==Only_SELL || TypeOrders==Only_All) if((s==0) || (s>0 && (Bid-SellMaxPrice)>=SellDist) || ((s>0 && (SellMinPrice-Bid)>=SellDist) && TradeTrend==On)) if(OrderSend(Symbol(),OP_SELL,NormalizeDouble(sellot,2),NormalizeDouble(Bid,Digits()),Slippage,0,0,"My order",Magic,0,clrGreen)<0) Print(" Error open Sell N ",GetLastError()); // --- if(ProfitBuy>=MinProfit && b>=2) CloseAll(OP_BUY,BuyMaxTic); if(ProfitSel>=MinProfit && s>=2) CloseAll(OP_SELL,SellMinTic); } //+------------------------------------------------------------------+ //| Expert deinitialization function | //+------------------------------------------------------------------+ void OnDeinit(const int reason) { } //+------------------------------------------------------------------+ void CloseAll(int aType,int ticket) { for(int i=OrdersTotal()-1; i>=0; i--) if(OrderSelect(i,SELECT_BY_POS)) if(OrderSymbol()==Symbol()) if(OrderMagicNumber()==Magic) { if(OrderType()==aType && OrderType()==OP_BUY) if(OrderProfit()>0 || OrderTicket()==ticket) if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits()),Slippage,clrRed)) Print(" OrderClose OP_BUY Error N",GetLastError()); if(OrderType()==aType && OrderType()==OP_SELL) if(OrderProfit()>0 || OrderTicket()==ticket) if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits()),Slippage,clrRed)) Print(" OrderClose OP_SELL Error N",GetLastError()); } } //+------------------------------------------------------------------+ double CalcLot(int TypeOrder) { double rezult=0; switch(TypeLot) { case 0: // Standart lot if(TypeOrder==OP_BUY || TypeOrder==OP_SELL)rezult=StartLot; break; case 1: // Summ lot if(TypeOrder==OP_BUY && Ask < BuyMinPrice ) rezult=BuyMinLot+StartLot; if(TypeOrder==OP_BUY && Ask > BuyMaxPrice ) rezult=StartLot; if(TypeOrder==OP_SELL&& Bid > SellMaxPrice ) rezult=SellMaxLot+StartLot; if(TypeOrder==OP_SELL&& Bid < SellMinPrice ) rezult=StartLot; break; case 2: // Martingale lot if(TypeOrder==OP_BUY && Ask < BuyMinPrice ) rezult=BuyMinLot*2; if(TypeOrder==OP_BUY && Ask > BuyMaxPrice ) rezult=StartLot; if(TypeOrder==OP_SELL&& Bid > SellMaxPrice ) rezult=SellMaxLot*2; if(TypeOrder==OP_SELL&& Bid < SellMinPrice ) rezult=StartLot; break; case 3: // Step lot if(TypeOrder==OP_BUY && Ask < BuyMinPrice && b%3==0 ) rezult=BuyMinLot+StartLot; if(TypeOrder==OP_BUY && Ask > BuyMaxPrice ) rezult=StartLot; if(TypeOrder==OP_SELL&& Bid > SellMaxPrice && s%3==0) rezult=SellMaxLot+StartLot; if(TypeOrder==OP_SELL&& Bid < SellMinPrice ) rezult=StartLot; break; } return rezult; } //+------------------------------------------------------------------+