Help in straightening out this code? please

 

Hi to all,

This is an attempt, but truly lost now. There are more functions to add, but there are 28 errors so far.

Can someone help out.

Cheers Huckleberry

-------------+
//| My System ver1.mq4 |
//| Copyright © 2010, Ben banta |
//| https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Ben banta"
#property link "https://www.metaquotes.net/"


//----Custom function variables

extern int TradeSize=20; int Slip=3;
extern int ShtTP; extern int ShtSL;

//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| Buy Entry / See Entry |
//+------------------------------------------------------------------+
int start()
{
if (Bid<(Low[iLowest(NULL,0,MODE_LOW,20,1)]))
{
if (OrdersTotal() == 0 );
{
LaunchBuy; //See User Defined
}
{
else
if (Ask>(High[iHighest(NULL,0,MODE_HIGH,20,1)]))
{
if (OrdersTotal() == 0);
{
LaunchSell; //See User Defined
}

return(0);
}
//+------------------------------------------------------------------+
// Custom Funtion for Buy Entry |
//+------------------------------------------------------------------+
//============================
//
// Custom Function No. 1
//
//============================ |

//------------1-------------
bool LaunchLong()
{ //OpenBrace to Start the LaunchLong() function
double Min_Dist; double Min_Lot; double Step;
double Free; double Lot; double TP; double SL;
double Dist_SL; int ticket; double Dist_TP;
int Error;

//-----------2--------------
TP=Bid+LongTP*Point;
SL=Ask-LongSL*Point;

while (true)
{ //OpenBrace for beginging of 'while' operator
Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL); // Min. Distance
Min_Lot=MarketInfo(Symbol(),MODE_MINLOT); //Min. Volume (Min=0.1 @ Alpari)
Step=MarketInfo(Symbol(),MODE_LOTSTEP); //Step to change lots (Lot Step=0.1 @ Alpari)
Free=AccountFreeMargin(); //Free Margin

//-----------3--------------

Lot=MathFloor(Free/(TradeSize*1000)*(1/Step))*Step; //Calculates position size
if (Lot < Min_Lot) //If it is less than allowed
{
Alert("Not enough money for", Min_Lot, "lots");
break; //exit cycle
}

//-----------4--------------

if ((SL+Bid)/Point < Min_Dist) //If it is less than allowed
{
SL=Bid-Min_Dist*Point; //Set the allowed
Alert("Increased the distance of SL = ",Dist_SL,"pt");
}

//-----------5--------------

if ((Bid+TP)/Point < Min_Dist) //If it is less than allowed
{
TP=Ask+Min_Dist*Point; //Set the allowed
Alert("Increased the distance of TP = ",Dist_TP," pt");
}

//-----------6--------------

Alert("The request was sent to the server. Waiting for reply..");
ticket=OrderSend(Symbol(),OP_BUY,Lot, Ask, Slip, SL, TP, "", 0, 0, Red);

//-----------7--------------

if (ticket>0) //Got it!
{
Alert ("Opened order Sell ",ticket);
break; //exit cycle
}
//-------------8-----------

Error=GetLastError(); //Failed
switch(Error) // Overcomable errors
{
case 135: Alert ("The price has change. Retrying..");
RefreshRates(); //Update data
continue; //At the next iteration
case 136: Alert ("No prices. Waiting for a new tick..");
while(RefreshRates()==false) //Up to a new tick
Sleep(1); //cycle a delay
continue; //At the next iteration
case 146: Alert("Trading subsystem is busy. Retrying..");
Sleep(500); //Simple solution
RefreshRates(); //Update data
continue; //At the next iteration
}
switch(Error)
{
case 2: Alert("Common error.");
break; //exit 'switch'
case 5: Alert("Outdated version of the client terminal.");
break; //Exit 'switch"
case 64: Alert("The account is blocked.");
break; //Exit 'switch'
case 133: Alert("Trading forbidden");
break;
default: Alert("Occurred error ", Error); //Other alternatives
}
break; //Exit cycle
} //End 'while' operator
} //End 'LaunchLong()' function

//+------------------------------------------------------------------+
// Custom Function for Sell Entry |
//+------------------------------------------------------------------+
//============================
//
// Custom Function No. 2
//
//============================ |

//------------1-------------
bool LaunchShort() //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{ //OpenBrace to Start the LaunchShort() function
double Min_Dist; double Min_Lot; double Step;
double Free; double Lot; double TP; double SL;
double Dist_SL; int ticket; double Dist_TP;
int Error;

//-----------2--------------
TP=Bid-ShtTP*Point; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
SL=Ask-ShtSL*Point; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

while (true)
{ //OpenBrace for beginging of 'while' operator
Min_Dist=MarketInfo(Symbol(),MODE_STOPLEVEL); // Min. Distance
Min_Lot=MarketInfo(Symbol(),MODE_MINLOT); //Min. Volume (Min=0.1 @ Alpari)
Step=MarketInfo(Symbol(),MODE_LOTSTEP); //Step to change lots (Lot Step=0.1 @ Alpari)
Free=AccountFreeMargin(); //Free Margin
//-----------3--------------
Lot=MathFloor(Free/(TradeSize*1000)*(1/Step))*Step; //Calculates position size <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
if (Lot < Min_Lot) //If it is less than allowed
{
Alert("Not enough money for", Min_Lot, "lots");
break; //exit cycle
}

//-----------4--------------

if ((SL-Ask)/Point < Min_Dist) //If it is less than allowed<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
SL=Ask+Min_Dist*Point; //Set the allowed<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Alert("Increased the distance of SL = ",Dist_SL,"pt");
}

//-----------5--------------

if ((Ask-TP)/Point < Min_Dist) //If it is less than allowed<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
TP=Ask-Min_Dist*Point; //Set the allowed<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Alert("Increased the distance of TP = ",Dist_TP," pt");
}

//-----------6--------------
{
Alert("The request was sent to the server. Waiting for reply..");
ticket=OrderSend(Symbol(),OP_SELL,Lot, Bid, Slip, SL, TP, "", 0, 0, Red); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
//-----------7--------------

if (ticket>0) //Got it!
{
Alert ("Opened order Sell ",ticket); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
break; //exit cycle
}
//-------------8-----------

Error=GetLastError(); //Failed
switch(Error) // Overcomable errors
{
case 135: Alert ("The price has change. Retrying..");
RefreshRates(); //Update data
continue; //At the next iteration
case 136: Alert ("No prices. Waiting for a new tick..");
while(RefreshRates()==false) //Up to a new tick
Sleep(1); //cycle a delay
continue; //At the next iteration
case 146: Alert("Trading subsystem is busy. Retrying..");
Sleep(500); //Simple solution
RefreshRates(); //Update data
continue; //At the next iteration
}
switch(Error)
{
case 2: Alert("Common error.");
break; //exit 'switch'
case 5: Alert("Outdated version of the client terminal.");
break; //Exit 'switch"
case 64: Alert("The account is blocked.");
break; //Exit 'switch'
case 133: Alert("Trading forbidden");
break;
default: Alert("Occurred error ", Error); //Other alternatives
}
break; //Exit cycle
} //End 'while' operator
} //End 'LaunchShort()' function

//+---------------------------------------------------------+
// Custom Function No. 3 |
//+---------------------------------------------------------+
bool CloseTrade()
{ //Begin CloseTrade
if(OrdersTotal()>0)
{
OrderSelect(0,SELECT_BY_POS);
OrderClose(OrderTicket(),OrderLots(),Ask,Slip); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
} //End CloseTrade() function

 
Hi to all,

This is an attempt, but truly lost now. There are more functions to add, but there are 28 errors so far.

Can someone help out.

Cheers Huckleberry


H,

attached file will now compile. You will notice xxx wherever code was changed/moded. (nothing else was changed(*)) oh yes, please also learn to (1)format your code and (2)use the SRC button when including any code in a post. Remember, that others attempting to help you often times are put at big disadvantage when confronted with non formatted source code...

(*)I did mod start() so that could understand the nesting of conditions so that mods could be made.


but please consider taking some time to read up on MQL4 language syntax and the extensive BOOK which all site pages have link to at top.


there is much work to do from the observation of your code...


enjoy, but seriously consider reading site docs!

Files:
29192.mq4  9 kb
 

Hello fbj,

Thank you for taking the time. Sorry for not using the SRC. I had used the SRC, but it constantly came back with the error, that the text was to long. I'll be more careful when asking for help.

Yes, the code needs alot more work. It is more complicated than what I initially imagined. To close out the trade is by using the ATR times 2. But I have not yet figured out how to insert this script into the code. Once the close can be inserted, I have yet to figure how to insert additional buys or sells on the initial position.

Thanks again.

Cheers

Huckleberry

Reason: