//+------------------------------------------------------------------+ //| Base : E04L-cover-07.mq5 E04LC07-v3 | //+------------------------------------------------------------------+ #property version "111.008" #property description "D'Alembert betting system :" #property description "The stake should be decreasing after the winning bet," #property description "... and increasing after the loosing one." input string WARNING="NOT all Input is check to be correct !"; input datetime LastRiskDate=D'2012.12.17 23:59:59'; // Do NOT take risk after this date ! (COMPETITION) input double MinMargin=200.1; // Don't increase risk below this MARGIN_LEVEL ! input double KTP=0.0025; // TakeProfit as part of price ( 0.01=1.0% ) same to all Symbol input double Limit_Position_Lot=2.5; // Maximal Position_volume per Symbol input double Limit_Exposure_Lot=1.25; // Maximal Exposure per Value ( total from all symbols ) input double LotAtRisk=1.75; // Maximal Exposure at send +/-(Valu1-Valu2) = Accepted Risk input bool Inverse = false; // Change position direction input bool ManageTP = true; // Quickly close profitable position input bool ManageLOSS=true; // Lower open price for loosing position input int NoWorkTime=9; // Time betwen two executions (seconds) = accelerate tests input int LotMX=3; // Multiply minimal order volume. Work only if 0.01 input int KStart=7; // Multiply Start volume enum SymbolPosition {PNon,PBuy,PSell}; // This is a enum TYPE declaration ! /* // STANDART SETTING const int Nmax = 10; // Used Symbol (valu-pair) ; max=10 const int Vmax=5; // index of last used Value +1 , first index=0 ! double V[5]; // Value exposure ; 0=EUR,1=USD,2=JPY,3=CHF,4=GBP,5=CAD ... no other for now. string S[10] = {"EURUSD","USDJPY","EURJPY","GBPJPY","GBPCHF","EURCHF","GBPUSD","EURGBP","USDCHF","CHFJPY"}; SymbolPosition A[10] = {PSell,PSell,PBuy,PBuy,PSell,PSell,PBuy,PBuy,PBuy,PSell}; // What is action for each symbol SymbolPosition SPos[10]; // What is position for each symbol */ /* const int Vmax=7; // index of last used Value +1 , first index=0 ! double V[7]; // Value exposure ; 0=EUR,1=USD,2=JPY,3=CHF,4=GBP,5=CAD,6=AUD. // COMPETITION SETTING const int Nmax = 12; // Used Symbol (valu-pair) ; string S[12] = {"USDCHF","GBPUSD","EURUSD","USDJPY","USDCAD","AUDUSD","EURGBP","EURAUD","EURCHF","EURJPY","GBPJPY","GBPCHF"}; SymbolPosition A[12] = {PBuy,PSell,PBuy,PSell,PSell,PSell,PSell,PSell,PSell,PBuy,PBuy,PSell}; // What is action for each symbol SymbolPosition SPos[12]; // What is position for each symbol */ // V3-setting const int Vmax=5; // index of last used Value +1 , first index=0 ! double V[5]; // Value exposure ; 0=EUR,1=USD,2=JPY,3=CHF,4=GBP,5=CAD ... no other for now. const int Nmax=3; // Used Symbol (valu-pair) ; string S[3]={"EURUSD","EURGBP","GBPUSD"}; SymbolPosition A[3]={PBuy,PSell,PSell}; // What is action for each symbol SymbolPosition SPos[3]; // What is position for each symbol . ==> MUST BE a HAMMILTON settings ! double LotV; // Sum all lots : EXPOSURE double LotP; // Sum all lots : POSITION string Val1,Val2; // Current Symbol ( valu-pair == Val1/Val2 ) datetime LastMessageTime; // When Last Message was print int I,J; // main program index uchar SDig; // Current symbol digits string Out; // Text on the screen MqlTick T; // Tick value MqlTradeRequest R; // Request - order MqlTradeResult D; // Deal = result of Request double LotMin; // Server setting double NLots; // Current symbol : Position volume double Take; // TakeProfit for open position (price). double TP; // TakeProfit for each order (delta_price) double SStop; // Market stop for current Symbol datetime LastWorkTime; // Used to accelerate tests , with "NoWorkTime" double BestBallance; // Used to protect from too much close with loss bool CloseOne; // Used to stop manual close after firt one double LotAtSend; // varible : Same as "LotAtRisk" string Reason; // Why make Close_With_Loss double FreeBalance; // Available money for correction double TPMove; string MaxExpSym; // Symbol with maximal position ( 1-"worst" position ) double MaxExpPos; // Volume of maximal existing position string MaxLosSym; // Symbol with maximal loss ( 2-"worst" position ) double MaxLosPos; // Profit(Loss) of most loosing position. string MaxProSym; // Symbol with maximal profit double MaxProPos; // Value of maximal profit //+------------------------------------------------------------------+ //| PrintNM | //+------------------------------------------------------------------+ void PrintNM(int NMTLimit,string NMS) // Print NMS with minimum interval NMTLimit seconds { if(LastMessageTime+NMTLimitPositionGetDouble(POSITION_PROFIT)) // Maximal LOSS position and Symbol { MaxLosPos=PositionGetDouble(POSITION_PROFIT); MaxLosSym=PositionGetSymbol(EI); } if(MaxProPos","OrderSend_BUY_",S[I],"@_",DoubleToString(R.price,SDig)," RetCode=",D.retcode); } //+------------------------------------------------------------------+ //| SELL | //+------------------------------------------------------------------+ void SELL() // // send order is SELL, Position can be ANY !!! { R.action=TRADE_ACTION_DEAL; R.symbol=S[I]; R.price=T.bid; R.sl=0.0; R.deviation=99; R.type=ORDER_TYPE_SELL; R.type_filling=ORDER_FILLING_FOK; // or ORDER_FILLING_RETURN switch(SPos[I]) { case PNon : { R.tp=NormalizeDouble(T.bid-KStart*TP,(uchar)SymbolInfoInteger(S[I],SYMBOL_DIGITS)); R.volume=KStart*LotMin; R.comment="OPEN_SELL "+DoubleToString(V[ValueToIndex(Val1)],3)+" : "+ DoubleToString(V[ValueToIndex(Val2)],3); break; } // New position case PBuy : { R.tp=PositionGetDouble(POSITION_TP); R.volume=LotMin; if(CloseOne) R.comment=Reason; // Losing close else R.comment="PART_CLOSE "+DoubleToString(PositionGetDouble(POSITION_PROFIT),2)+" / "+ DoubleToString(PositionGetDouble(POSITION_VOLUME),2); // Part close break; } case PSell : { R.tp=PositionGetDouble(POSITION_TP); R.volume=LotMin; if(CloseOne) R.comment=Reason; else R.comment="ADD_SELL "+DoubleToString(V[ValueToIndex(Val1)],3)+" : "+ DoubleToString(V[ValueToIndex(Val2)],3); break; } // Increase } // end :switch Exposure(); Out=" POSITIONS [EUR-lot] = "+DoubleToString(LotP,2)+" FreeBalance="+DoubleToString(FreeBalance,2)+ "\n EXPOSURE [EUR-lot] = "+DoubleToString(LotV,2)+" :"; for(J=0;J","OrderSend_SELL_",S[I],"@_",DoubleToString(R.price,SDig)," RetCode=",D.retcode); } //+------------------------------------------------------------------+ //| AcceptedLOSS | //+------------------------------------------------------------------+ bool AcceptedLOSS() // Keep Balance go up, after loosing close { if(PositionGetDouble(POSITION_PROFIT)<0.0 && FreeBalance+PositionGetDouble(POSITION_PROFIT)*LotMin/PositionGetDouble(POSITION_VOLUME)>0.0) return(true); else return(false); } //+------------------------------------------------------------------+ //| ORDER_TP_BUY | //+------------------------------------------------------------------+ void ORDER_TP_BUY() // Modify TP , position==BUY { R.action=TRADE_ACTION_SLTP; R.symbol=MaxProSym; R.tp=NormalizeDouble((1.0-KTP)*PositionGetDouble(POSITION_TP), (uchar)SymbolInfoInteger(MaxProSym,SYMBOL_DIGITS)); R.sl=0.0; R.deviation=99; if(R.tp-SymbolInfoDouble(MaxProSym,SYMBOL_ASK)> SymbolInfoInteger(MaxProSym,SYMBOL_TRADE_STOPS_LEVEL)*SymbolInfoDouble(MaxProSym,SYMBOL_POINT)) { Out=DoubleToString(PositionGetDouble(POSITION_VOLUME),2)+"*" +DoubleToString(KTP*PositionGetDouble(POSITION_TP),(uchar)SymbolInfoInteger(MaxProSym,SYMBOL_DIGITS)); R.comment="TP-BUY="+Out; OrderSend(R,D); Print("3->","OrderSend_MODIFY_TP_BUY_",MaxProSym," RetCode=",D.retcode); } } //+------------------------------------------------------------------+ //| ORDER_TP_SELL | //+------------------------------------------------------------------+ void ORDER_TP_SELL() // Modify TP , position==SELL { R.action=TRADE_ACTION_SLTP; R.symbol=MaxProSym; R.tp=NormalizeDouble((1.0+KTP)*PositionGetDouble(POSITION_TP), (uchar)SymbolInfoInteger(MaxProSym,SYMBOL_DIGITS)); R.sl=0.0; R.deviation=99; if(SymbolInfoDouble(MaxProSym,SYMBOL_BID)-R.tp> SymbolInfoInteger(MaxProSym,SYMBOL_TRADE_STOPS_LEVEL)*SymbolInfoDouble(MaxProSym,SYMBOL_POINT)) { Out=DoubleToString(PositionGetDouble(POSITION_VOLUME),2)+"*" +DoubleToString(KTP*PositionGetDouble(POSITION_TP),(uchar)SymbolInfoInteger(MaxProSym,SYMBOL_DIGITS)); R.comment="TP-SELL="+Out; OrderSend(R,D); Print("4->","OrderSend_MODIFY_TP_SELL_",MaxProSym," RetCode=",D.retcode); } } //+------------------------------------------------------------------+ //| CleanBad | //+------------------------------------------------------------------+ bool CleanBad() // Decreace Position_Volume for MaxLosSym with LotMin { bool CDone=false; for(I=0;I2.0*MathMax(MinMargin,400.0)); } //+------------------------------------------------------------------+ //| Expert initialization function | //+------------------------------------------------------------------+ int OnInit() { ZeroMemory(R); Print("Init"); Exposure(); LastMessageTime=TimeCurrent(); LastWorkTime=TimeCurrent()-NoWorkTime; BestBallance=AccountInfoDouble(ACCOUNT_BALANCE)-AccountInfoDouble(ACCOUNT_PROFIT); CloseOne=(0.9*AccountInfoDouble(ACCOUNT_BALANCE)>AccountInfoDouble(ACCOUNT_EQUITY)); LotMin=MathMax(SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN),0.01); if(LotMin<0.1) LotMin=NormalizeDouble(LotMin*LotMX,2); if(Inverse) for(I=0;ITimeCurrent()) LotAtSend=LotAtRisk; else { LotAtSend=LotMin; PrintNM(420,"!!! ==> No-risk mode <== !!!"); } // =============================== Trade start here ========================================== Exposure(); Extreme(); FreeBalance=AccountInfoDouble(ACCOUNT_BALANCE)-BestBallance; if(ManageLOSS && CloseOne) { Reason="Start_DrawDown"; CleanBad(); CloseOne=false; return; } // ========== Accepted loss ! ========== PositionSelect(MaxLosSym); if(ManageLOSS && (PositionGetDouble(POSITION_PROFIT)<0.0 && PositionGetDouble(POSITION_VOLUME)>LotMin)) { Reason="Adjust_Price="+DoubleToString(PositionGetDouble(POSITION_PRICE_OPEN), (uchar)SymbolInfoInteger(MaxLosSym,SYMBOL_DIGITS)); if(CleanBad()) { CloseOne=false; return; } } // ========== UN-Expected profit ! ========= if(ManageTP && (MaxProPos>(-MaxLosPos) || AccountInfoDouble(ACCOUNT_PROFIT)>0.0)) { PositionSelect(MaxProSym); if(PositionGetDouble(POSITION_VOLUME)>LotMin) switch(PositionGetInteger(POSITION_TYPE)) { case POSITION_TYPE_BUY : { ORDER_TP_BUY(); break; } case POSITION_TYPE_SELL : { ORDER_TP_SELL(); } } // end : switch } for(I=0;ISpread ) TP=NormalizeDouble(TP,SDig); // From here below ( and insite called function ) , SPos[I] == "POSITION_TYPE" bool FreeTrade=FreeTradeF(); switch(SPos[I]) { case PNon : { // New position BUY if(V[ValueToIndex(Val1)] Maximum Exposure SELL "+Val2); else PrintNM(300,"! ==> Maximum Exposure BUY "+Val1); // New position SELL if(V[ValueToIndex(Val2)] Maximum Exposure SELL "+Val1); else PrintNM(300,"! ==> Maximum Exposure BUY "+Val2); break; } // end : case PNon case PBuy : { // Part close if(T.bid>Take-(NLots/LotMin-1.0)*TP) // Minimal PriceToSend if(NLots>LotMin) { SELL(); return; } // Don't close LotMin ==> use TakeProfit if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) LOW FREE MARGIN + High exposure "); break; } if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)<123.4) { PrintNM(360,"!!! ==> LOW FREE MARGIN "); return; } // Increase position if(T.ask<=Take-(NLots/LotMin+1.0)*TP) // Maximal PriceToSend if(NLots+LotMin Maximum Exposure SELL "+Val2); else PrintNM(420,"! ==> Maximum Exposure BUY "+Val1); else PrintNM(360,"! ==> Maximum LOT_at_RISK : BUY_"+S[I]); else PrintNM(300,"! ==> Maximum POSITION_LOT "+S[I]); break; } // end : case PBuy case PSell : { // Part close if(T.askLotMin) { BUY(); return; } // Don't close LotMin ==> use TakeProfit if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL) LOW FREE MARGIN + High exposure "+S[I]); break; } if(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)<123.4) { PrintNM(360,"!!! ==> LOW FREE MARGIN "); return; } // Increase position if(T.bid>Take+(NLots/LotMin+1.0)*TP) // Minimal PriceToSend if(NLots+LotMin Maximum Exposure SELL "+Val1); else PrintNM(420,"! ==> Maximum Exposure BUY "+Val2); else PrintNM(360,"! ==> Maximum LOT_at_RISK : SELL_"+S[I]); else PrintNM(300,"! ==> Maximum POSITION_LOT "+S[I]); break; } // end : case PSell } // end : switch } // end : for } } //+------------------------------------------------------------------+ //| OnTrade | //+------------------------------------------------------------------+ void OnTrade() { Sleep(5555); } //+------------------------------------------------------------------+