//+------------------------------------------------------------------+ //| trade.mq4 | //| Copyright © 2004, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2004, MetaQuotes Software Corp." #property link "http://www.metaquotes.net/" #include #include #property show_inputs string ExpertComment = "OSIRIS"; int Slippage = 3; double StopLoss = 20; double TakeProfit = 10; bool MoneyManagement = true; double Lots = 1; extern double risk = 2.0; extern bool UseOldMM_Code = true; double myPoint; // this handles 5 digit pricing correctly int MagicNumber=8746910; string TradeComment = "TM Sell MM"; int init() { myPoint = SetPoint(); } //+------------------------------------------------------------------+ //| Get number of lots for this trade | //| If MoneyManagement is true the function will return the risk | //| percent based on AccountBalance. Corrections are made for | //| Standard and Mini accounts | //| If MoneyManagement if false then the value of mLots is returned | //| This value is also corrected for the type of account. | //| A check is also made to make certain that the maximum lots | //| allowed by the broker is not exceeded | //+------------------------------------------------------------------+ double GetLots(bool mMoneyManagement, double mRisk, double mLots) { double lot, MinLot, MaxLot; MinLot = MarketInfo(Symbol(), MODE_MINLOT); MaxLot = MarketInfo(Symbol(), MODE_MAXLOT); if(mMoneyManagement) { lot=NormalizeDouble(MathFloor(AccountFreeMargin()*risk/30000),1); // Check if mini or standard Account if(MinLot < 1.0) { lot = MathFloor(lot*10)/10; if (lot < 0.1) lot = 0.1; } else { if (lot >= 1.0) lot = MathFloor(lot); else lot = 1.0; } } else { lot = mLots; } if (lot > MaxLot) lot = MaxLot; return(lot); } //+------------------------------------------------------------------+ //| Get number of lots for this trade | //+------------------------------------------------------------------+ double GetLotsNew(double StopInPips) { int Decimals = 0; double lot; double myMaxLot = MarketInfo(Symbol(), MODE_MAXLOT); double myMinLot = MarketInfo(Symbol(), MODE_MINLOT); double LotStep = MarketInfo(Symbol(), MODE_LOTSTEP); double LotSize = MarketInfo(Symbol(), MODE_LOTSIZE); double LotTickValue = MarketInfo(Symbol(), MODE_TICKVALUE); if(LotStep == 0.01) Decimals = 2; if(LotStep == 0.1) Decimals = 1; if (MoneyManagement == false) return(Lots); //+------------------------------------------------------------------+ //| Calculate optimal lot size | //+------------------------------------------------------------------+ lot = ( AccountFreeMargin() * (risk/100) )/( LotTickValue * StopInPips * 10.0 ); lot = StrToDouble(DoubleToStr(lot,Decimals)); if (lot < myMinLot) lot = myMinLot; if (lot > myMaxLot) lot = myMaxLot; return(lot); } //+------------------------------------------------------------------+ //| script | //+------------------------------------------------------------------+ int start() { double mSpread = Ask - Bid; double mStopLoss = (Bid + StopLoss * myPoint) - mSpread; double mTakeProfit = (Bid - TakeProfit * myPoint) + mSpread; double mRisk = risk; double mLots; if (UseOldMM_Code) mLots = GetLots(MoneyManagement, risk, Lots); else mLots = GetLotsNew(StopLoss); // Lots needed to be mLots // int ticket = OpenTrade(OP_SELL, Lots, Slippage, mStopLoss, mTakeProfit, TradeComment, MagicNumber, CLR_NONE); int ticket = OpenTrade(OP_SELL, mLots, Slippage, mStopLoss, mTakeProfit, TradeComment, MagicNumber, CLR_NONE); if(ticket<0) { int error = GetLastError(); Print("Error = ", ErrorDescription(error)); return; } //---- OrderPrint(); return(0); } int OpenTrade(int signal, double mLots, int mSlippage, double mStopLoss, double mTakeProfit, string TradeComment, int MagicNumber, color mColor) { int ticket, err; double TPprice,STprice; RefreshRates(); if (signal==OP_BUY) { ticket=OrderSend(Symbol(),OP_BUY,mLots,Ask,mSlippage,0,0,TradeComment,MagicNumber,0,mColor); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (mStopLoss != 0 || mTakeProfit != 0) { TPprice = 0; if (mTakeProfit > 0) { TPprice = mTakeProfit; TPprice = ValidTakeProfit(OP_BUY,Ask, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = mStopLoss; STprice = ValidStopLoss(OP_BUY,Bid, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, mColor); } } } } else if (signal==OP_SELL) { ticket=OrderSend(Symbol(),OP_SELL,mLots,Bid,mSlippage,0,0,TradeComment,MagicNumber,0,mColor); if (ticket > 0) { if (OrderSelect( ticket,SELECT_BY_TICKET, MODE_TRADES) ) { if (mStopLoss != 0 || mTakeProfit != 0) { TPprice = 0; if (mTakeProfit > 0) { TPprice=mTakeProfit; TPprice = ValidTakeProfit(OP_SELL,Bid, TPprice); } STprice = 0; if (mStopLoss > 0) { STprice = mStopLoss; STprice = ValidStopLoss(OP_SELL,Ask, STprice); } // Normalize stoploss / takeprofit to the proper # of digits. if (Digits > 0) { STprice = NormalizeDouble( STprice, Digits); TPprice = NormalizeDouble( TPprice, Digits); } OrderModify(ticket, OrderOpenPrice(), STprice, TPprice, 0, mColor); } } } } return(ticket); } double ValidStopLoss(int type, double price, double SL) { double newSL, temp; if (SL < 0.1) return(SL); temp = MarketInfo(Symbol(),MODE_STOPLEVEL) * myPoint; newSL = SL; if (type == OP_BUY) { if((price - newSL) < temp) newSL = price - temp; } if (type == OP_SELL) { if((newSL-price) < temp) newSL = price + temp; } newSL = NormalizeDouble(newSL,Digits); return(newSL); } double ValidTakeProfit(int type, double price, double TP) { double newTP, temp; if (TP < 0.1) return(TP); temp = MarketInfo(Symbol(), MODE_STOPLEVEL) * myPoint; newTP = TP; if (type == OP_BUY) { if((TP - price) < temp) newTP = price + temp; } if (type == OP_SELL) { if((price - TP) < temp) newTP = price - temp; } newTP = NormalizeDouble(newTP,Digits); return(newTP); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } //+------------------------------------------------------------------+