???????? ?????????????? mql4 ? C# ??? cAlgo API

Created at 23 Oct 2013, 09:07
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
AL

alex379

Joined 23.10.2013

???????? ?????????????? mql4 ? C# ??? cAlgo API
23 Oct 2013, 09:07


Please help programmers to rewrite adviser from MQL4 to C # for cAlgo API

Advisor to the MT4 demo works fine.

For efficient operation of the adviser to the perfect, fast execution without re-quotes and slippage, low spreads and good liquidity at the right time. Advisor is not a commercial.

I would be very grateful for the work done!


#include 
#include 
extern double   DeltaExtrim =13;//?????????? ?????????? ? ???
extern double     Dbar = 13;//???.????? ????
extern double     DbarHL = 13;//???.????? ????
extern int     TIME          =  1;//????? ??????? ??????????? ?????
extern double  StopLoss       =  2;  //????????? ???????? 
extern double  TP       =     30;  //???? ??????
extern double  TrSt           =  1; //????
extern double porogP           =0;//??? ????? ??????? ????? . ???
extern double porogS           =0;//??? ????? ??????? ????? . ???
extern double MinProfit       =1;//???. ?????? ??????????? ????????
extern int     Speed1          =  1;//?????. ????????????? ?????
extern int     Speed2          =  1;//?????. ????????????? ?????
extern int     Speed3          =  3;//?????. ????????????? ?????
extern int     SpeedDel      =  3;//?????. ????????????? ?????
extern double Dist = 0.5;   // ????????? ??????????? ??????????
extern int Expiration       = 0;//????.????? ????? ???????? ( ?? ????? ) ???????
extern int ExpirationAll       = 5;//????.????? ????? ???? ???????
extern int   NbarExtrim = 15;//?????. ????? ?????????? ???????????
extern bool  OneOrderBar = false;//?????????? ?????? ?????? ?????? ? ????
extern double Lot = 0.1;
extern double MaxLot = 1000;
extern double MinLot = 0.01;
extern int NormalizeLot =2;//???????????? ??????? ?????? ????? ???. ? ???????? ???? 1 - 0.1 ....2 - 0.01
extern bool Kmin =false;//  ?????????? ???? ?? ???.? ?????? ??????. ?????? =0 ?? ????.(??????????)
extern bool   TickFiltr = false;//????????? ??????? ??????? ??????????? ?????
extern bool   LotOptimized = false;//????????? ????????? ? ???? ?? ????????? ???????
extern double    Risk = 0.2;//?????? ???? ????????? ??????? ??? LotOptimized = true
extern double    max_spread =1.5;//????. ???????? ???????????? ?????? ? ???
extern double comis =6;//????????
extern double Klot = 1.1;
extern bool  ContrOrder = true;
extern bool  Stop1=true;//????.?????? ?? ??????????? ?? ?????
extern bool  Stop2=true;//????.?????? ?? ????? ?? ?????
extern bool  Stop3=true;//????.?????? ?? ????? ? ????? ????
extern bool Market1=false;//
extern bool Market2=false;// ?? ?? ?????? ??????
extern bool Market3=false;//
extern bool Sound = true;
extern string DigString="2 for jpy, 4 for other crosses";
extern int     Dig = 5;
extern string com = "N";
extern string com1 = "N1";
extern string com2 = "N2";
extern string com3 = "N3";
extern int Magic = 7777;


double         Point_;
datetime       nextTime;
int            cnt, total, lastBars,i,vol;
int            up=0, down=0, TimeSpeedUp, TimeSpeedDown;
bool           TimBoolUp,TimBoolDown, OrderSal;
double         priceUp=0, priceDown=0,profit,eee,lots=0;
datetime time;

int              PipMultiplier=0;


void closOrders()
{  
int bo=0,so=0,cb=0,cs=0;
   for (int q = 0; q < total; q++)   
   {
      OrderSelect(q, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic&&SpeedDel>0)
      {
       if(OrderType()==OP_BUYSTOP&&up <= down-SpeedDel&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
       if(OrderType()==OP_SELLSTOP&&up >= down+SpeedDel&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
       
      }
   }
   return;
   }

void openOrders()
{  
int b1=0,s1=0,b2=0,s2=0,b3=0,s3=0,bo1=0,so1=0,bo2=0,so2=0,bo3=0,so3=0,cb=0,cs=0,zb=0,zs=0,p=0;

 if(Kmin==true)
 {
  for(i=OrdersHistoryTotal();i>=0;i--){
  if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)){
    if(OrderSymbol()==Symbol()){
      if(OrderMagicNumber()==Magic){
        if(OrderCloseTime()!=0)
        {
         if(OrderCloseTime()>time){
          time=OrderCloseTime();
          eee=OrderLots();
          profit=OrderProfit();
            //??????
          if (profit<0)lots=MinLot;//NormalizeDouble(GlobalVariableGet("lot"+com)/Kmin,NormalizeLot);
          else lots = NormalizeDouble(GlobalVariableGet("lot"+com),NormalizeLot);
          
          }
        }
      }
    }
  }
}
}
  if(Kmin==0)lots = NormalizeDouble(GlobalVariableGet("lot"+com),NormalizeLot);
  if(lots>MaxLot)lots= MaxLot;
  if(lots= 0; q--)   
   {
      OrderSelect(q, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
     
       if(OrderType()==OP_BUYSTOP&&profit<0&&OrderLots()>MinLot&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
       if(OrderType()==OP_SELLSTOP&&profit<0&&OrderLots()>MinLot&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
       if(OrderType()==OP_BUYSTOP&&OrderComment()==com1)bo1++;
       if(OrderType()==OP_SELLSTOP&&OrderComment()==com1)so1++;
       if(OrderType()==OP_BUYSTOP&&OrderComment()==com2)bo2++;
       if(OrderType()==OP_SELLSTOP&&OrderComment()==com2)so2++;
       if(OrderType()==OP_BUYSTOP&&OrderComment()==com3)bo3++;
       if(OrderType()==OP_SELLSTOP&&OrderComment()==com3)so3++;
       
       if(OrderType()==OP_BUY&&OrderComment()==com1+com)b1++;
       if(OrderType()==OP_SELL&&OrderComment()==com1+com)s1++;
       if(OrderType()==OP_BUY&&OrderComment()==com2+com)b2++;
       if(OrderType()==OP_SELL&&OrderComment()==com2+com)s2++;
       if(OrderType()==OP_BUY&&OrderComment()==com3+com)b3++;
       if(OrderType()==OP_SELL&&OrderComment()==com3+com)s3++;
       
      }
   }
   
      for (int q1 = 0; q1 < total; q1++)   
      {
      OrderSelect(q1, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic&&zb==1&&OrderType()==OP_BUYSTOP&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic&&zs==1&&OrderType()==OP_SELLSTOP&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
      }


   
   
   
   int try;
   if(Ask-Bid0&&TimeCurrent()-GlobalVariableGet("td"+com)0&&(Ask+Bid)/2-DeltaExtrim*Point*PipMultiplier>iHigh(Symbol(),1,iHighest(Symbol(),1,2,NbarExtrim,1)))
      {           
       RefreshRates(); 
       if(so1==0&&Stop1==true)
       OrderSend(Symbol(),OP_SELLSTOP,lots,Bid-Dist*Point*PipMultiplier,2,Bid-Dist*Point*PipMultiplier+(Ask-Bid)+StopLoss*Point*PipMultiplier,0 ,com1,Magic,0,Red);
       if(s1==0&&Market1==true)
       OrderSend(Symbol(),OP_SELL,lots,Bid,2,0,0 ,com1+com,Magic,0,Red);
       cb=1;
       if(Sound==true)PlaySound("stops.wav" );
      } 
      if(up <= down-Speed2&&GlobalVariableCheck("td"+com)&&GlobalVariableGet("td"+com)>0&&TimeCurrent()-GlobalVariableGet("td"+com)0&&Close[0]>Open[0]+Dbar*Point*PipMultiplier)
      {           
       RefreshRates(); 
       if(so2==0&&Stop2==true)
       OrderSend(Symbol(),OP_SELLSTOP,lots,Bid-Dist*Point*PipMultiplier,2,Bid-Dist*Point*PipMultiplier+(Ask-Bid)+StopLoss*Point*PipMultiplier,0 ,com2,Magic,0,Red);
       if(s2==0&&Market2==true)
       OrderSend(Symbol(),OP_SELL,lots,Bid,2,0,0 ,com2+com,Magic,0,Red);
       cb=1;
       if(Sound==true)PlaySound("stops.wav" );
      }  
      if(up <= down-Speed3&&GlobalVariableCheck("td"+com)&&GlobalVariableGet("td"+com)>0&&TimeCurrent()-GlobalVariableGet("td"+com)0&&High[0]-Low[0]>DbarHL*Point*PipMultiplier )
      {           
       RefreshRates(); 
       if(so3==0&&Stop3==true)
       OrderSend(Symbol(),OP_SELLSTOP,lots,Bid-Dist*Point*PipMultiplier,2,Bid-Dist*Point*PipMultiplier+(Ask-Bid)+StopLoss*Point*PipMultiplier,0 ,com3,Magic,0,Red);
       if(s3==0&&Market3==true)
       OrderSend(Symbol(),OP_SELL,lots,Bid,2,0,0 ,com3+com,Magic,0,Red);
       cb=1;
       if(Sound==true)PlaySound("stops.wav" );
      }
   }
   }
    else 
   {  
      if(zb==0)
      {
      if(up >= down+Speed1&&GlobalVariableCheck("tu"+com)&&GlobalVariableGet("tu"+com)>0&&TimeCurrent()-GlobalVariableGet("tu"+com)0&&(Ask+Bid)/2+DeltaExtrim*Point*PipMultiplier= down+Speed2&&GlobalVariableCheck("tu"+com)&&GlobalVariableGet("tu"+com)>0&&TimeCurrent()-GlobalVariableGet("tu"+com)0&&Close[0]= down+Speed3&&GlobalVariableCheck("tu"+com)&&GlobalVariableGet("tu"+com)>0&&TimeCurrent()-GlobalVariableGet("tu"+com)0&&High[0]-Low[0]>DbarHL*Point*PipMultiplier)
      {           
         RefreshRates();    
         if(bo3==0&&Stop3==true)
         OrderSend(Symbol(),OP_BUYSTOP,lots,Ask+Dist*Point*PipMultiplier,2,Ask+Dist*Point*PipMultiplier-(Ask-Bid)-StopLoss*Point*PipMultiplier,0,com3, Magic, 0, Green);
         if(b3==0&&Market3==true)
         OrderSend(Symbol(),OP_BUY,lots,Ask,2,0,0,com3+com, Magic, 0, Green);
         cs=1;
         if(Sound==true)PlaySound("stops.wav" );
       }
       } 
   
   }    
     int zdb=0,zds=0,ddb=0,dds=0;
     double bst=0,sst=0;
     for (int n =OrdersTotal(); n >=0; n--)   
   {
      OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
       if(OrderType()==OP_BUY&&OrderProfit()+OrderCommission()>0&&cb==1)OrderClose(OrderTicket(),OrderLots(),Bid,5,CLR_NONE);
       if(OrderType()==OP_SELL&&OrderProfit()+OrderCommission()>0&&cs==1)OrderClose(OrderTicket(),OrderLots(),Ask,5,CLR_NONE);
      }
   } 
 
}
return;
}
int init()
{
   
 if (Digits==3 || Digits==5)
         PipMultiplier=10;
   else  PipMultiplier=1;
   return(0);
}

int deinit()
{
   GlobalVariableDel("tu"+com);
   GlobalVariableDel("td"+com);
   GlobalVariableDel("tk"+com);
   GlobalVariableDel("rtk"+com);
   GlobalVariableDel("vol"+com);
   GlobalVariableDel("lot"+com);
   return(0);
}



int start()
{   
double p=porogP*Point;
double p1=porogS*Point;
   double ask = (Ask+Bid)/2;//iAD(NULL,0,0);//
   double ask1= GlobalVariableGet("tk"+com);
  // double tk=(Ask+Bid)/2;
   total = OrdersTotal();
   int   i, currentSymbolOrderPos = -1,q;
   int   l, currentSymbolOrderPos2 = -1;
   int b=0,s=0; 
   vol = Volume[0];  
   if(DayOfWeek()==0||DayOfWeek()==6)return(0);

   for (i = 0; i < total; i++)   
   {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
      b++;
      }
   }
   //if(DayOfWeek()==1&&b==0&& TimeCurrent()GlobalVariableGet("rtk"+com)
      {
         up = up + 1;
         priceUp = ask;//-p;
         if(!GlobalVariableCheck("tu"+com))GlobalVariableSet("tu"+com,TimeCurrent());
         if(TimBoolUp == false)
         {
            TimeSpeedUp = TimeCurrent();
            TimBoolUp = true;
         }   
      }
   //else
     if(priceUp > ask+p1)
      {
        priceUp = 0;
       
        up  = 0;
        TimBoolUp = false;
        TimeSpeedUp = 0;
        GlobalVariableDel("tu"+com);
      }
    if(TickFiltr==true&&GlobalVariableCheck("vol"+com)&&vol>GlobalVariableGet("vol"+com)+1)
      {
        priceUp = 0;
       
        up  = 0;
        TimBoolUp = false;
        TimeSpeedUp = 0;
        GlobalVariableDel("tu"+com);
      }  

 
      if(priceDown > ask+p)//&&MathAbs(tk-GlobalVariableGet("tk"+com))>GlobalVariableGet("rtk"+com)
      {
         down = down + 1;
         priceDown = ask;//+p;
         if(!GlobalVariableCheck("td"+com))GlobalVariableSet("td"+com,TimeCurrent());
         if(TimBoolDown == false)
         {
            TimeSpeedDown = TimeCurrent();
            TimBoolDown = true;
         }   
      }
     //else
     if(priceDown < ask-p1)
      {
         priceDown = 0;
         down      = 0;
         TimBoolDown = false;
         TimeSpeedDown = 0; 
         GlobalVariableDel("td"+com);  
      }
    if(TickFiltr==true&&GlobalVariableCheck("vol"+com)&&vol>GlobalVariableGet("vol"+com)+1)
      {
         priceDown = 0;
         down      = 0;
         TimBoolDown = false;
         TimeSpeedDown = 0; 
         GlobalVariableDel("td"+com);
      } 

     
      if(up >=1  || down >=1)//Speed
      {          
            openOrders();                  
      } 
      if(up >= SpeedDel || down >= SpeedDel)
      {          
            closOrders();                  
      }
      GlobalVariableSet("vol"+com,Volume[0]);
      if(GlobalVariableCheck("tk"+com))GlobalVariableSet("rtk"+com,MathAbs(ask-GlobalVariableGet("tk"+com)));
      GlobalVariableSet("tk"+com,ask);
      
    ObjectCreate("U", OBJ_LABEL, 0, 0, 0, 0, 0);
    ObjectSet("U", OBJPROP_CORNER, 2);
    ObjectSet("U", OBJPROP_YDISTANCE, 30);
    ObjectSet("U", OBJPROP_XDISTANCE, 10);
    ObjectSetText("U", "U " + DoubleToStr(up,2),12, "Tahoma",Lime );
    ObjectCreate("D", OBJ_LABEL, 0, 0, 0, 0, 0);
    ObjectSet("D", OBJPROP_CORNER, 2);
    ObjectSet("D", OBJPROP_YDISTANCE, 15);
    ObjectSet("D", OBJPROP_XDISTANCE, 10);
    ObjectSetText("D", "D " + DoubleToStr(down,2),12, "Tahoma",Red );
         
      if(priceUp == 0)
      {
         priceUp   = ask;         
      }
      if(priceDown == 0)
      {        
         priceDown = ask;
      }
   //}   
//else // ???? ???????? ????? ?? ???????? ???????
if(b>0)
   {
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    int zdb=0,zds=0,ddb=0,dds=0;
    string tb="",ts="";
     double bst=0,sst=0;
     for (int n =OrdersTotal(); n >=0; n--)   
   {
      OrderSelect(n, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
       if(OrderType()==OP_BUY)bst=OrderStopLoss();
       if(OrderType()==OP_SELL)sst=OrderStopLoss();
       if(OrderType()==OP_BUY)tb=DoubleToStr(OrderTicket(),0);
       if(OrderType()==OP_SELL)ts=DoubleToStr(OrderTicket(),0);
       if(OrderType()==OP_BUYSTOP&&OrderComment()==ts)zdb=1;
       if(OrderType()==OP_SELLSTOP&&OrderComment()==tb)zds=1;
      }
   } 
   
        for (int n1 =OrdersTotal(); n1 >=0; n1--)   
   {
      OrderSelect(n1, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
       if(OrderType()==OP_BUYSTOP&&OrderComment()==ts)zdb=1;
       if(OrderType()==OP_SELLSTOP&&OrderComment()==tb)zds=1;       
       
       if(OrderType()==OP_BUY&&OrderStopLoss()>OrderOpenPrice()&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))dds=1;
       if(OrderType()==OP_SELL&&OrderStopLoss()=0; n2--)   
   {
      OrderSelect(n2, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {
       if(OrderType()==OP_BUYSTOP&&ddb==1)OrderDelete(OrderTicket()); 
       if(OrderType()==OP_SELLSTOP&&dds==1)OrderDelete(OrderTicket());
       if(ContrOrder==true&&OrderType()==OP_BUY&&zds==0&&OrderStopLoss()OrderOpenPrice()&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))
       OrderSend(Symbol(),OP_BUYSTOP,Klot*OrderLots(),OrderStopLoss(),2,0,0,DoubleToStr(OrderTicket(),0), Magic, 0, Green);
      }
   }    
  /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////    
      for (int m = 0; m < OrdersTotal(); m++)   
      {
      OrderSelect(m, SELECT_BY_POS, MODE_TRADES);
      if (OrderSymbol() == Symbol()&&OrderMagicNumber()==Magic)
      {    
       if(OrderType()==OP_BUYSTOP&&OrderOpenPrice()-Ask>Dist*Point*PipMultiplier&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderModify(OrderTicket(),Ask+Dist*Point*PipMultiplier,Ask+Dist*Point*PipMultiplier-(Ask-Bid)-StopLoss*Point*PipMultiplier,0,0,CLR_NONE);
       if(OrderType()==OP_SELLSTOP&&Bid-OrderOpenPrice()>Dist*Point*PipMultiplier&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderModify(OrderTicket(),Bid-Dist*Point*PipMultiplier,Bid-Dist*Point*PipMultiplier+(Ask-Bid)+StopLoss*Point*PipMultiplier,0,0,CLR_NONE);
       if(Expiration>0&&(OrderType()==OP_SELLSTOP||OrderType()==OP_BUYSTOP)&&TimeCurrent()-OrderOpenTime()>Expiration&&(OrderComment()==com1||OrderComment()==com2||OrderComment()==com3))OrderDelete(OrderTicket());
       if(ExpirationAll>0&&(OrderType()==OP_SELLSTOP||OrderType()==OP_BUYSTOP)&&TimeCurrent()-OrderOpenTime()>ExpirationAll)OrderDelete(OrderTicket());
      if (OrderType() == OP_BUY)
      {
         if(OrderStopLoss()==0)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Bid - StopLoss*Point*PipMultiplier,Digits),'', 0, Red);
         }
         if(OrderTakeProfit()==0)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Ask+TP*Point*PipMultiplier, 0, Red);
         }
         if (Bid-OrderOpenPrice()-comis*Point>TrSt*Point*PipMultiplier)
         {
            if (OrderStopLoss()=MinProfit*Point*PipMultiplier)
            {
              OrderModify(OrderTicket(),OrderOpenPrice(),Bid - (TrSt)*Point*PipMultiplier,'', 0, Red);
            }
         }        
      }
      if (OrderType() == OP_SELL)
      {   
         if(OrderStopLoss()==0)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),NormalizeDouble(Ask + StopLoss*Point*PipMultiplier, Digits),'', 0, Red);
         }  
         if(OrderTakeProfit()==0)
         {
            OrderModify(OrderTicket(),OrderOpenPrice(),OrderStopLoss(),Bid-TP*Point*PipMultiplier, 0, Red);
         }    
         if (OrderOpenPrice()-comis*Point-Ask > TrSt*Point*PipMultiplier)
         {
           if (OrderStopLoss() > Ask +(TrSt)*Point*PipMultiplier&&OrderOpenPrice()-Ask>=MinProfit*Point*PipMultiplier) 
           {
              OrderModify(OrderTicket(),OrderOpenPrice(),Ask + (TrSt)*Point*PipMultiplier,'', 0, Red);
           }
         }            
      }
   }
   }
   }   
   return(0);
}
double GetLotSize()
{
if(LotOptimized == TRUE){
double Loty;
//........................................................................
        int m;
        double MG;
        if(Lot>0)
        {
        if(AccountFreeMargin()>AccountMargin())MG=AccountFreeMargin();       
        double Min_Lot = MarketInfo(Symbol(), MODE_LOTSTEP);             
        if(Risk>0)
        {
          m=MG/MarketInfo (Symbol(), MODE_MARGINREQUIRED)*Risk/Min_Lot;
          Loty = m*Min_Lot;
        if(Loty < MarketInfo(Symbol(), MODE_MINLOT))
        Loty =MarketInfo(Symbol(), MODE_MINLOT);            
        if(Loty > MarketInfo (Symbol(), MODE_MAXLOT))
        Loty = MarketInfo (Symbol(), MODE_MAXLOT);                  
        }
        }
        if(Risk==0)Loty= Lot;

//......................................................................................, 
}
else
{
Loty=Lot;
}
   return (Loty);
}

 


@alex379
Replies

virtuesoft
23 Oct 2013, 15:35

RE:

What exactly is your question?

Do you want to know whether the code you pasted is C# or MQL4? If so, then I can say that I think it is MQL4.

 


@virtuesoft

virtuesoft
23 Oct 2013, 17:46

RE: RE:

I see you've updated your post now. 

Have you tried the converter on the 2cAlgo website. It might help...

http://2calgo.com/


@virtuesoft

fzlogic
25 Oct 2013, 17:02

Robots are not supported yet in 2calgo. Try /jobs/. It's hard to get people to work for free :)


@fzlogic