//+------------------------------------------------------------------+
//|                                                        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 <stdlib.mqh>
#include <WinUser32.mqh>
//+------------------------------------------------------------------+
//| script "trading for all money"                                   |
//+------------------------------------------------------------------+
int start()
  {

//Script Parameters
int OrderTypes = OP_BUY;
double Lots = 0.1;
int MagicNumber = 10;
bool MoneyManagement = true;
int Risk = 2;
bool UseStopLoss = true;
int StopLoss = 20;
bool UseTakeProfit = true;
int TakeProfit = 20;
bool UseBlockTrade = true;
double MaxLotSize = 9999;
int MaxTrades = 10;
int Slippage = 5;

double Price;
int Ticket1;
int Ticket2;

double StopLossLevel, TakeProfitLevel;

//----Starting Variables

   if (MoneyManagement)
   {
      if (Risk<1 || Risk>100)
      {
         Comment("Invalid Risk Value.");
         return(0);
      }
      else
      {
         Lots=MathFloor((AccountFreeMargin()*AccountLeverage()*Risk*Point*100)/(Ask*MarketInfo(Symbol(),MODE_LOTSIZE)*MarketInfo(Symbol(),MODE_MINLOT)))*MarketInfo(Symbol(),MODE_MINLOT);
      }
   }
   

      
if (Lots == 0 && MoneyManagement == true) Lots = MarketInfo(Symbol(), MODE_MINLOT);   
if (!UseBlockTrade && Lots > MarketInfo(Symbol(), MODE_MAXLOT)) Lots = MODE_MAXLOT;
   
int OpenTradeCount = 0;
double Lots1 = 0;
double Lots2 = 0;

   Lots2 = Lots;
   
if (UseBlockTrade) {   
   if (Lots2 > MaxLotSize) {
         Lots1 = MaxLotSize;
         OpenTradeCount = MathFloor(Lots / MaxLotSize);
         Lots2 = Lots - (OpenTradeCount * MaxLotSize); 
            if(OpenTradeCount > MaxTrades) {
               Lots2 = 0;
               OpenTradeCount = MaxTrades;
               }
}
}

         if (UseStopLoss) StopLossLevel = Ask - StopLoss * Point; else StopLossLevel = 0.0;
         if (UseTakeProfit) TakeProfitLevel = Ask + TakeProfit * Point; else TakeProfitLevel = 0.0;


   if (OrderTypes == OP_BUY) Price = Ask; else {
      if (OrderTypes == OP_SELL) Price = Bid; }


   if (Lots2 > 0) Ticket2=OrderSend(Symbol(), OrderTypes, Lots2, Price, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ") " + Symbol() + " " + Period() + "", 255, 0, CLR_NONE);
   if (Lots1 > 0) {for (int i = 0; i < OpenTradeCount; i ++) Ticket1=OrderSend(Symbol(), OrderTypes, Lots1, Price, Slippage, StopLossLevel, TakeProfitLevel, "Buy(#" + MagicNumber + ") " + Symbol() + " " + Period() + "", 255, 0, CLR_NONE);}
   


     }
//----
   OrderPrint();
   return(0);
     
//+------------------------------------------------------------------+