//+------------------------------------------------------------------+ //| Version 1.0 info.mq4 | //+------------------------------------------------------------------+ #property indicator_chart_window extern double Lots = 0.1, // fractional lots are allowed MaximumRisk = 0.05, DecreaseFactor = 0.03; int init() {return(0);} //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { Comment(""); return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { string swaplong = DoubleToStr(MarketInfo(Symbol(),18),2); string swapshort = DoubleToStr(MarketInfo(Symbol(),19),2); string pipvalue = DoubleToStr(PipCost(Symbol()),2); int AcctLeverage = AccountLeverage(); Comment("\n\n Account Leverage: ",AcctLeverage,":1" + "\n Pair Spread: " + DoubleToStr(((Ask-Bid)*10000),0) + "\n PipValue in $: " + pipvalue + "\n max StopLoss: " + DoubleToStr(maxLoss(),0) + "\n\n LotSize: " + DoubleToStr(LotsOptimized(), 2) + "\n Riskfactor: " + DoubleToStr((MaximumRisk*100), 0)+" %" + "\n Decrease: " + DoubleToStr(DecreaseFactor*100, 0)+" %" + "\n\n LongSwap : " + swaplong + "\n ShortSwap: " + swapshort ); return(0); } //+------------------------------------------------------------------+ //| Calculate optimal lot size //+------------------------------------------------------------------+ double LotsOptimized() {double lot=Lots; int losses=0,orders=HistoryTotal(); // number of losses orders without a break // history orders total //---- 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!"); break; } if(OrderSymbol()!=Symbol() || OrderType()>OP_SELL) continue; //---- if(OrderProfit()>0) break; if(OrderProfit()<0) losses++; } if(losses>1) lot=NormalizeDouble(lot-lot*losses/DecreaseFactor,1); } //---- return lot size if(lot<0.1) lot=0.1; return(lot); } //+------------------------------------------------------------------+ //| Calculate max loss //+------------------------------------------------------------------+ double maxLoss() {double SL= (AccountFreeMargin()*MaximumRisk)/PipCost(Symbol())/LotsOptimized(); return(SL); } //+--------- --------- --------- --------- --------- --------- ----+ //+ Calculate cost in USD of 1pip of given symbol //+--------- --------- --------- --------- --------- --------- ----+ double PipCost (string TradeSymbol) { double Base, Cost; string TS_13, TS_46, TS_4L; TS_13 = StringSubstr (TradeSymbol, 0, 3); TS_46 = StringSubstr (TradeSymbol, 3, 3); TS_4L = StringSubstr (TradeSymbol, 3, StringLen(TradeSymbol)-3); Base = MarketInfo (TradeSymbol, MODE_LOTSIZE) * MarketInfo (TradeSymbol, MODE_POINT); if ( TS_46 == "USD" ) Cost = Base; else if ( TS_13 == "USD" ) Cost = Base / MarketInfo (TradeSymbol, MODE_BID); else if ( PairExists ("USD"+TS_4L) ) Cost = Base / MarketInfo ("USD"+TS_4L, MODE_BID); else Cost = Base * MarketInfo (TS_46+"USD" , MODE_BID); return(Cost) ; } //+--------- --------- --------- --------- --------- --------- ----+ //+ Returns true if given symbol exists //+--------- --------- --------- --------- --------- --------- ----+ bool PairExists (string TradeSymbol) { return ( MarketInfo (TradeSymbol, MODE_LOTSIZE) > 0 ); }