Os_Pro8 V.1

Author: Copyright © 2022 - Boris Osipov
Orders Execution
It automatically opens orders when conditions are reachedChecks for the total of open ordersIt Closes Orders by itself
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
Os_Pro8 V.1
ÿþ//+------------------------------------------------------------------+

#property copyright "Copyright © 2022 - Boris Osipov"

#define IDENT  "Os_Pro8 V.3"

#property strict



 extern double MinBalance=100;       // @8A: 2 %% >B 10;0=A0

 extern int    Regim    = 0;         // 0 - 4> ?>;CG5=8O ?@>D8B0, 1 - ?@>4>;65=85 @01>BK

 extern double Lot      = 0.01;

 extern int    Delta    = 230;

 extern double KLot     = 2;

 extern double MinProfit= 0.1;

 extern int    MAGIC    = 1945;

    

double  OldBalance,OB,profit,orderLots;

string description;

int     buy,sell,sch;

double  Pokupka,Prodaga,Lots;

double  sva1,sva2;

datetime prevtime;

double  Expertremove;   // 0 - C40;8BL A>25B=8:0 A> 2A5E 3@0D8:>2

//---------------------------------------------------------------------

int init()

  {

    Comment("");

    OB=AccountBalance();

    GlobalVariableSet("OldBalance",OB);

    GlobalVariableSet("Expertremove",1);

    buy=0; sell=0; orderLots=0;

    description="";

    if(Regim==0) Lots=Lot; else Lots=Lots() ;

    prevtime=Time[0];

    Vals();

    Pokupka=ObjectGet("LineUp", OBJPROP_PRICE1);

    Prodaga=ObjectGet("LineDn", OBJPROP_PRICE1); 

    return(0);

  }

//------------------Start-----------------------------------------------

int start()

{

//   if(!IsDemo()) {Print(" 50;L=K9 AG5B "); return(0);}

   GlobalVariableGet("Expertremove",Expertremove);

   if (Expertremove==0) {CloseAll(); ExpertRemove();}  //#40;5=85 A>25B=8:0 A> 2A5E 3@0D8:>2



    Pokupka=ObjectGet("LineUp", OBJPROP_PRICE1);

    Prodaga=ObjectGet("LineDn", OBJPROP_PRICE1); 

          

   if (AccountEquity()<OB-OB*MinBalance/100)

      {

         Alert(Symbol()," >AB83=CB <8=8<0;L=>-4>?CAB8<K9 10;0=A");

         CloseAll(); ExpertRemove();

      }

    Comment("@>D8B = ",Profit());   

    if (Profit()>=MinProfit)

      {

          CloseAll(); Lots=Lot; buy=0; sell=0;

          GlobalVariableSet("OldBalance",0);

          GlobalVariableSet("OldBalance",AccountBalance());

          if(Regim==0) ExpertRemove();

          Vals();

          Pokupka=ObjectGet("LineUp", OBJPROP_PRICE1);

          Prodaga=ObjectGet("LineDn", OBJPROP_PRICE1); 

          return(0);

      }

    if(Bid>=Pokupka && buy==0)

      {

         if(!CheckVolumeValue(Lots,description)) {ExpertRemove(); return(0);}

         if(!CheckMoneyForTrade(Symbol(), Lots, OP_BUY)) { ExpertRemove(); return(0);}

         

         if(OrderSend(Symbol(),OP_BUY,Lots,Ask,30,0,0,NULL,MAGIC,0,Blue)==-1)

         {Print("H81:0 >B:@KB8O >@45@0 <<",GetLastError());}  // ExpertRemove();}

         else {Lots=Lots(); buy=1; sell=0;}

         return(0);

      }

   if(Ask<=Prodaga && sell==0)

      {

         if(!CheckVolumeValue(Lots,description)) {ExpertRemove(); return(0);}

         if(!CheckMoneyForTrade(Symbol(), Lots, OP_SELL)) { ExpertRemove(); return(0);}

         

         if(OrderSend(Symbol(),OP_SELL,Lots,Bid,30,0,0,NULL,MAGIC,0,Red)==-1)

         {Print("H81:0 >B:@KB8O >@45@0 <<",GetLastError());}  // ExpertRemove();}

         else {Lots=Lots(); sell=1; buy=0;}

         return(0);

      }

    return(0);

 } 

//+------------------------------------------------------------------+

//|  @>25@O5B >1J5< >@45@0 =0 :>@@5:B=>ABL                          |

//+------------------------------------------------------------------+

bool CheckVolumeValue(double volume,string &description)

  {

    description="  ";  

//--- <8=8<0;L=> 4>?CAB8<K9 >1J5< 4;O B>@3>2KE >?5@0F89

   double min_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MIN);

   if(volume<min_volume)

     {

      description=StringFormat("1J5< <5=LH5 <8=8<0;L=> 4>?CAB8<>3> SYMBOL_VOLUME_MIN=%.2f",min_volume);

      return(false);

     }



//--- <0:A8<0;L=> 4>?CAB8<K9 >1J5< 4;O B>@3>2KE >?5@0F89

   double max_volume=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_MAX);

   if(volume>max_volume)

     {

      description=StringFormat("1J5< 1>;LH5 <0:A8<0;L=> 4>?CAB8<>3> SYMBOL_VOLUME_MAX=%.2f",max_volume);

      return(false);

     }



//--- ?>;CG8< <8=8<0;L=CN 3@040F8N >1J5<0

   double volume_step=SymbolInfoDouble(Symbol(),SYMBOL_VOLUME_STEP);



   int ratio=(int)MathRound(volume/volume_step);

   if(MathAbs(ratio*volume_step-volume)>0.0000001)

     {

      description=StringFormat("1J5< =5 O2;O5BAO :@0B=K< <8=8<0;L=>9 3@040F88 SYMBOL_VOLUME_STEP=%.2f, 1;8609H89 :>@@5:B=K9 >1J5< %.2f",

                               volume_step,ratio*volume_step);

      return(false);

     }

   description=">@@5:B=>5 7=0G5=85 >1J5<0";

   return(true);

  }

//-------------------@>25@:0 A@54AB2---------------------------------------------- 

 

bool CheckMoneyForTrade(string symb, double lots,int type)

  {

   double free_margin=AccountFreeMarginCheck(symb,type,lots);

   //-- 5A;8 45=53 =5 E20B05B

   if(free_margin<0)

     {

      string oper=(type==OP_BUY)? "Buy":"Sell";

      Print("54>AB0B>G=> 45=53 4;O ", oper," ",lots, " ", symb, " Error code=",GetLastError());

      return(false);

     }

   //-- ?@>25@:0 ?@>H;0 CA?5H=>

   return(true);

  }

//+-------------------?@545;5=85 ;>B0----------- ------------------------------------+

double  Lots()

  {

     sch=0;

     int total  = OrdersTotal();

     for (int cnt = 0 ; cnt < total ; cnt++)

        {

          if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)

            {

                if (OrderMagicNumber() == MAGIC && OrderSymbol()==Symbol())

                   {

                     sch++;

                   }

            }   

        }

        if(sch==0) return(Lot);

        else 

            {

               double orderLots =  OrderLots()*KLot;

               return(orderLots); 

            }                    

 } 

//---------------------------->4AG5B ?@>D8B0------------------------

double Profit()

{

  profit=0;

  int total  = OrdersTotal();

  for (int cnt = 0 ; cnt < total ; cnt++)

  {

    if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)

      {

          if (OrderMagicNumber() == MAGIC && OrderSymbol()==Symbol())

             {

               profit+=OrderProfit(); //NormalizeDouble(OrderProfit(),2);

             }

      }    

  }

  return(profit);

}

//-------------------0:@KB85 2A5E >@45@>2 ?> ?@>D8BC----------------------------------

void CloseAll()

{

   int j,OT,error;

      for (j = OrdersTotal()-1; j >= 0; j--)

      {

         if (OrderSelect(j, SELECT_BY_POS))

         {

            if (OrderMagicNumber() == MAGIC && OrderSymbol()==Symbol())

            {

               OT = OrderType();

               if (OT>1) 

               {

                  if (!OrderDelete(OrderTicket())) Print("H81:0 C40;5=8O >@45@0 ",GetLastError());

               }

               if (OT==OP_BUY) 

               {

                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,Digits),3,Blue);

                  if (error==-1) Print("H81:0 70:@KB8O >@45@0 ",GetLastError());

               }

               if (OT==OP_SELL) 

               {

                  error=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Ask,Digits),3,Red);

                  if (error==-1) Print("H81:0 70:@KB8O >@45@0 ",GetLastError());

               }

             }

           }

        }       

  return;

  }

//-----------------------#AB0=>2:0 3@0=8F :>@84>@0------------------------

double Vals()

  { 

    ObjectDelete("LineUp"); ObjectDelete("LineDn");



      sva1=NormalizeDouble(Bid+Delta*Point,Digits);

      sva2=NormalizeDouble(Ask-Delta*Point,Digits);



      ObjectCreate("LineUp",OBJ_HLINE,0,0,sva1);        // #@>25=L

      ObjectSet("LineUp",OBJPROP_COLOR,Aqua);           // &25B

      ObjectSet("LineUp",OBJPROP_WIDTH,1);

      

      ObjectCreate("LineDn",OBJ_HLINE,0,0,sva2);         // #@>25=L 

      ObjectSet("LineDn",OBJPROP_COLOR,Red);             // &25B

      ObjectSet("LineDn",OBJPROP_WIDTH,1);



      return(0);

  }

//+------------------------------------------------------------------+

void deinit()

  {

      Comment("");  

    ObjectDelete("LineUp"); ObjectDelete("LineDn");

    return;

  }

//--------------------------------------------------------------------

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---