Auto Buy and Sell CBOT with risk% MM TP/SL ATR based
Created at 13 Nov 2015, 13:44
Auto Buy and Sell CBOT with risk% MM TP/SL ATR based
13 Nov 2015, 13:44
Can someone please help converting this MQL
//+------------------------------------------------------------------+ //| Manual Scalp Long.mq4 | //| Copyright © 2009, Kenny Hubbard | //| http://www.compu-forex.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, Kenny Hubbard" #property link "http://www.compu-forex.com" #property show_inputs //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ extern int MagicNumber = 100; extern double spreadlimit = 3.5; //<<------------set the spread limit here in pips extern double tradesize = 0.0; //<<------------set the default lot size here....set to 0 to use autorisk extern double takeprofit = 0; //<<------------set your take profit in pips here extern double ATR_TP_Factor = 4; extern double stoploss = 0; //<<------------set your stoploss in pips here extern double ATR_SL_Factor = 3; extern double risk = 1.0; //<<------------set your risk in % here double bidfill; int LotDigits; int Digit_Factor = 1; bool writetofile = true; int start() { string cmt = ""; if (stoploss == 0)stoploss = iATR(Symbol(),0, 20, 1) * ATR_SL_Factor; else stoploss *= Digit_Factor * Point; if(takeprofit == 0)takeprofit = iATR(Symbol(),0, 20, 1) * ATR_TP_Factor; else takeprofit *= Digit_Factor * Point; if (takeprofit < 0)takeprofit = 0; if (stoploss < 0)stoploss = 0; Print("Takeprofit = " + DoubleToStr(takeprofit,Digits)); Print("Stoploss = " + DoubleToStr(stoploss,Digits)); if(tradesize == 0 && stoploss == 0){ Alert("Unable to determine risk level. Please set a stoploss or a tradesize"); return(0); } //========================================================================================= Gather broker trade details 4 or 5 digits & volume information if (Digits == 3 || Digits == 5)Digit_Factor = 10; double One_Tick = MarketInfo(Symbol(),MODE_TICKVALUE) * Digit_Factor; double MaxLot = MarketInfo(Symbol(), MODE_MAXLOT); double MinLot = MarketInfo(Symbol(), MODE_MINLOT); if (MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01)LotDigits = 2; //========================================================================================= Calculate the trade volume based on the desired stoploss & risk if (tradesize == 0){ double Risk_In_Money = (stoploss/Point/Digit_Factor) * One_Tick; tradesize = NormalizeDouble(((AccountEquity() * risk/100)/Risk_In_Money),LotDigits); // %risk = $ loss >>> SL = allowed volume } if (tradesize > MaxLot)tradesize = MaxLot; if (tradesize < MinLot)tradesize = MinLot; //========================================================================================= Convert the pips for EA use using point & 4 or 5 digit conversion spreadlimit *= Digit_Factor * Point; //========================================================================================= Ok, let make the trade if ((Ask - Bid) < spreadlimit){ //check that the spread is within limit - if not = no trade PlaySound("tick.wav"); //let the user know that the script is running bidfill = Ask; //record the price for the csv file int ticket = OrderSend(Symbol(),OP_BUY,tradesize,Ask,0,0,0,"Manual Scalp",MagicNumber,0,White); //make the oredr with no stop for ECN if(ticket<0){ string Error_Text = ErrorDescription(GetLastError()); Alert("OrderSend failed with error "+Error_Text); //there is a problem.......alert the user return(0); } else{ //enter the stops with ordermodify if(OrderSelect(ticket,SELECT_BY_TICKET)){ if (stoploss != 0) double temp_stoploss = OrderOpenPrice()-stoploss; if (takeprofit != 0) double temp_takeprofit = OrderOpenPrice()+takeprofit; bool mod = OrderModify(ticket,OrderOpenPrice(),NormalizeDouble(temp_stoploss,Digits),NormalizeDouble(temp_takeprofit,Digits),0,CLR_NONE); if (!mod && GetLastError() > 1){ Error_Text = ErrorDescription(GetLastError()); Alert("Stoploss/Takeprofit OrderModify failed with error "+ Error_Text); } string Filename = StringConcatenate(AccountCompany(),"_Slippage"); double Fileslippage = ((OrderOpenPrice()-bidfill) / Point)/Digit_Factor; Print("Slippage on order " + OrderTicket() + " = " + DoubleToStr(Fileslippage,1)); if (writetofile) Sliptrack (OrderTicket(),TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES), OrderSymbol(), bidfill, OrderOpenPrice(), NormalizeDouble(((bidfill - OrderOpenPrice()) / Point)/10,1),Filename); } } } else { Alert("Spread is greater than ", spreadlimit/Point / Digit_Factor," pips"); } //---- cmt = cmt + "Last Trade Summary" + "\n"; cmt = cmt + "----------------------------" + "\n"; cmt = cmt + "Ticket no = " + ticket + "\n"; cmt = cmt + "Trade Open Time = " + TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES) + "\n"; cmt = cmt + "Risk level = " + DoubleToStr(risk,1) + "%\n"; cmt = cmt + "ATR Stoploss = " + DoubleToStr(stoploss/Point/Digit_Factor, 1) + " Pips\n"; cmt = cmt + "ATR Takeprofit = " + DoubleToStr(takeprofit/Point/Digit_Factor, 1) + " Pips\n"; double potentialloss = Risk_In_Money * tradesize; cmt = cmt + "Equity at risk = $" + DoubleToStr(potentialloss,2) + "\n"; cmt = cmt + "Trade Size = " + DoubleToStr(tradesize,LotDigits) + "\n"; cmt = cmt + "Slippage on order = " + DoubleToStr(Fileslippage,1) + " Pips (negative slippage is in your favour)\n"; double slippagecost = -Fileslippage * One_Tick * tradesize; if (slippagecost > 0)string slippagetext = " Profit"; else slippagetext = " LOSS"; if (slippagecost ==0) slippagetext = " neutral"; cmt = cmt + "Slippage cost = $" + DoubleToStr(MathAbs(slippagecost),2) + slippagetext + "\n"; Comment(cmt); return(0); } void Sliptrack(int Ordernum, string dateandtime, string currency, double filebidfill, double Fillprice, double Fileslippage, string File) { int handle; handle=FileOpen(File + ".csv",FILE_READ|FILE_WRITE, ';'); if (handle < 1) { handle=FileOpen(File + ".csv", FILE_CSV|FILE_WRITE, ';'); FileSeek(handle,0,0); FileSeek(handle,0,SEEK_END); FileWrite(handle,Ordernum,dateandtime, currency, filebidfill, Fillprice, Fileslippage); FileClose(handle); } else { FileSeek(handle,0,SEEK_END); FileWrite(handle,Ordernum,dateandtime, currency, filebidfill, Fillprice, Fileslippage); FileClose(handle); } }//Sliptrack end //---- //+------------------------------------------------------------------+ //| return error description | //+------------------------------------------------------------------+ string ErrorDescription(int error_code) { string error_string; //---- switch(error_code) { //---- codes returned from trade server case 0: case 1: error_string="no error"; break; case 2: error_string="common error"; break; case 3: error_string="invalid trade parameters"; break; case 4: error_string="trade server is busy"; break; case 5: error_string="old version of the client terminal"; break; case 6: error_string="no connection with trade server"; break; case 7: error_string="not enough rights"; break; case 8: error_string="too frequent requests"; break; case 9: error_string="malfunctional trade operation"; break; case 64: error_string="account disabled"; break; case 65: error_string="invalid account"; break; case 128: error_string="trade timeout"; break; case 129: error_string="invalid price"; break; case 130: error_string="invalid stops"; break; case 131: error_string="invalid trade volume"; break; case 132: error_string="market is closed"; break; case 133: error_string="trade is disabled"; break; case 134: error_string="not enough money"; break; case 135: error_string="price changed"; break; case 136: error_string="off quotes"; break; case 137: error_string="broker is busy"; break; case 138: error_string="requote"; break; case 139: error_string="order is locked"; break; case 140: error_string="long positions only allowed"; break; case 141: error_string="too many requests"; break; case 145: error_string="modification denied because order too close to market"; break; case 146: error_string="trade context is busy"; break; //---- mql4 errors case 4000: error_string="no error"; break; case 4001: error_string="wrong function pointer"; break; case 4002: error_string="array index is out of range"; break; case 4003: error_string="no memory for function call stack"; break; case 4004: error_string="recursive stack overflow"; break; case 4005: error_string="not enough stack for parameter"; break; case 4006: error_string="no memory for parameter string"; break; case 4007: error_string="no memory for temp string"; break; case 4008: error_string="not initialized string"; break; case 4009: error_string="not initialized string in array"; break; case 4010: error_string="no memory for array\' string"; break; case 4011: error_string="too long string"; break; case 4012: error_string="remainder from zero divide"; break; case 4013: error_string="zero divide"; break; case 4014: error_string="unknown command"; break; case 4015: error_string="wrong jump (never generated error)"; break; case 4016: error_string="not initialized array"; break; case 4017: error_string="dll calls are not allowed"; break; case 4018: error_string="cannot load library"; break; case 4019: error_string="cannot call function"; break; case 4020: error_string="expert function calls are not allowed"; break; case 4021: error_string="not enough memory for temp string returned from function"; break; case 4022: error_string="system is busy (never generated error)"; break; case 4050: error_string="invalid function parameters count"; break; case 4051: error_string="invalid function parameter value"; break; case 4052: error_string="string function internal error"; break; case 4053: error_string="some array error"; break; case 4054: error_string="incorrect series array using"; break; case 4055: error_string="custom indicator error"; break; case 4056: error_string="arrays are incompatible"; break; case 4057: error_string="global variables processing error"; break; case 4058: error_string="global variable not found"; break; case 4059: error_string="function is not allowed in testing mode"; break; case 4060: error_string="function is not confirmed"; break; case 4061: error_string="send mail error"; break; case 4062: error_string="string parameter expected"; break; case 4063: error_string="integer parameter expected"; break; case 4064: error_string="double parameter expected"; break; case 4065: error_string="array as parameter expected"; break; case 4066: error_string="requested history data in update state"; break; case 4099: error_string="end of file"; break; case 4100: error_string="some file error"; break; case 4101: error_string="wrong file name"; break; case 4102: error_string="too many opened files"; break; case 4103: error_string="cannot open file"; break; case 4104: error_string="incompatible access to a file"; break; case 4105: error_string="no order selected"; break; case 4106: error_string="unknown symbol"; break; case 4107: error_string="invalid price parameter for trade function"; break; case 4108: error_string="invalid ticket"; break; case 4109: error_string="trade is not allowed"; break; case 4110: error_string="longs are not allowed"; break; case 4111: error_string="shorts are not allowed"; break; case 4200: error_string="object is already exist"; break; case 4201: error_string="unknown object property"; break; case 4202: error_string="object is not exist"; break; case 4203: error_string="unknown object type"; break; case 4204: error_string="no object name"; break; case 4205: error_string="object coordinates error"; break; case 4206: error_string="no specified subwindow"; break; default: error_string="unknown error"; } //---- return(error_string); } //+------------------------------------------------------------------+ //************************************************************************************************************************* //+------------------------------------------------------------------+
//+------------------------------------------------------------------+ //| Openscalp - short.mq4 | //| Copyright © 2009, Kenny Hubbard | //| http://www.compu-forex.com | //+------------------------------------------------------------------+ #property copyright "Copyright © 2009, Kenny Hubbard" #property link "http://www.compu-forex.com" #property show_inputs //+------------------------------------------------------------------+ //| script program start function | //+------------------------------------------------------------------+ extern int MagicNumber = 100; extern double spreadlimit = 3.5; //<<------------set the spread limit here in pips extern double tradesize = 0; //<<------------set the default lot size here....set to 0 to use autorisk extern double takeprofit = 0; //<<------------set your take profit in pips here extern double ATR_TP_Factor = 4; extern double stoploss = 0; //<<------------set your stoploss in pips here extern double ATR_SL_Factor = 3; extern double risk = 1.0; //<<------------set your risk in % here double bidfill; int LotDigits = 1, Digit_Factor = 1; bool writetofile = true; int start() { string cmt = ""; if (stoploss == 0)stoploss = iATR(Symbol(),0, 20, 1) * ATR_SL_Factor; else stoploss *= Digit_Factor * Point; if(takeprofit == 0)takeprofit = iATR(Symbol(),0, 20, 1) * ATR_TP_Factor; else takeprofit *= Digit_Factor * Point; if (takeprofit < 0)takeprofit = 0; if (stoploss < 0)stoploss = 0; Print("Takeprofit = " + DoubleToStr(takeprofit,Digits)); Print("Stoploss = " + DoubleToStr(stoploss,Digits)); if(tradesize == 0 && stoploss == 0){ Alert("Unable to determine risk level. Please set a stoploss or a tradesize"); return(0); } //========================================================================================= Gather broker trade details 4 or 5 digits & volume information if (Digits == 3 || Digits == 5)Digit_Factor = 10; double One_Tick = MarketInfo(Symbol(),MODE_TICKVALUE) * Digit_Factor; double MaxLot = MarketInfo(Symbol(), MODE_MAXLOT); double MinLot = MarketInfo(Symbol(), MODE_MINLOT); if (MarketInfo(Symbol(),MODE_LOTSTEP) == 0.01)LotDigits = 2; //========================================================================================= Calculate the trade volume based on the desired stoploss & risk if (tradesize == 0){ double Risk_In_Money = (stoploss/Point/Digit_Factor) * One_Tick; tradesize = NormalizeDouble(((AccountEquity() * risk/100)/Risk_In_Money),LotDigits); // %risk = $ loss >>> SL = allowed volume } if (tradesize > MaxLot)tradesize = MaxLot; if (tradesize < MinLot)tradesize = MinLot; //========================================================================================= Convert the pips for EA use using point & 4 or 5 digit conversion spreadlimit *= Digit_Factor * Point; //========================================================================================= Ok, let make the trade if ((Ask - Bid) < spreadlimit){ //check that the spread is within limit - if not = no trade PlaySound("tick.wav"); //let the user know that the script is running bidfill = Bid; //record the price for the csv file int ticket = OrderSend(Symbol(),OP_SELL,tradesize,Bid,3,0,0,"Manual Scalp",MagicNumber,0,Yellow); //make the oredr with no stop for ECN if(ticket<0){ string Error_Text = ErrorDescription(GetLastError()); Alert("OrderSend failed with error "+Error_Text); //there is a problem.......alert the user return(0); } else{ //enter the stops with ordermodify if(OrderSelect(ticket,SELECT_BY_TICKET)){ if (stoploss != 0) double temp_stoploss = OrderOpenPrice()+stoploss; if (takeprofit != 0) double temp_takeprofit = OrderOpenPrice()-takeprofit; bool mod = OrderModify(ticket,OrderOpenPrice(),NormalizeDouble(temp_stoploss,Digits),NormalizeDouble(temp_takeprofit,Digits),0,CLR_NONE); if (!mod && GetLastError() > 1){ Error_Text = ErrorDescription(GetLastError()); Alert("Stoploss/Takeprofit OrderModify failed with error "+ Error_Text); } string Filename = StringConcatenate(AccountCompany(),"_Slippage"); double Fileslippage = ((bidfill - OrderOpenPrice()) / Point)/Digit_Factor; Print("Slippage on order " + OrderTicket() + " = " + DoubleToStr(Fileslippage,1)); if (writetofile) Sliptrack (OrderTicket(),TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES), OrderSymbol(), bidfill, OrderOpenPrice(), NormalizeDouble(((bidfill - OrderOpenPrice()) / Point)/10,1),Filename); } } } else { Alert("Spread is greater than ", spreadlimit/Point / Digit_Factor," pips"); } //---- cmt = cmt + "Last Trade Summary" + "\n"; cmt = cmt + "----------------------------" + "\n"; cmt = cmt + "Ticket no = " + ticket + "\n"; cmt = cmt + "Trade Open Time = " + TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES) + "\n"; cmt = cmt + "Risk level = " + DoubleToStr(risk,1) + "%\n"; cmt = cmt + "ATR Stoploss = " + DoubleToStr(stoploss/Point/Digit_Factor, 1) + " Pips\n"; cmt = cmt + "ATR Takeprofit = " + DoubleToStr(takeprofit/Point/Digit_Factor, 1) + " Pips\n"; double potentialloss = Risk_In_Money * tradesize; cmt = cmt + "Equity at risk = $" + DoubleToStr(potentialloss,2) + "\n"; cmt = cmt + "Trade Size = " + DoubleToStr(tradesize,LotDigits) + "\n"; cmt = cmt + "Slippage on order = " + DoubleToStr(Fileslippage,1) + " Pips (negative slippage is in your favour)\n"; double slippagecost = -Fileslippage * One_Tick * tradesize; if (slippagecost > 0)string slippagetext = " Profit"; else slippagetext = " LOSS"; if (slippagecost ==0) slippagetext = " neutral"; cmt = cmt + "Slippage cost = $" + DoubleToStr(MathAbs(slippagecost),2) + slippagetext + "\n"; Comment(cmt); return(0); } void Sliptrack(int Ordernum, string dateandtime, string currency, double filebidfill, double Fillprice, double Fileslippage, string File) { int handle; handle=FileOpen(File + ".csv",FILE_READ|FILE_WRITE, ';'); if (handle < 1) { handle=FileOpen(File + ".csv", FILE_CSV|FILE_WRITE, ';'); FileSeek(handle,0,0); FileSeek(handle,0,SEEK_END); FileWrite(handle,Ordernum,dateandtime, currency, filebidfill, Fillprice, Fileslippage); FileClose(handle); } else { FileSeek(handle,0,SEEK_END); FileWrite(handle,Ordernum,dateandtime, currency, filebidfill, Fillprice, Fileslippage); FileClose(handle); } }//Sliptrack end //---- //+------------------------------------------------------------------+ //| return error description | //+------------------------------------------------------------------+ string ErrorDescription(int error_code) { string error_string; //---- switch(error_code) { //---- codes returned from trade server case 0: case 1: error_string="no error"; break; case 2: error_string="common error"; break; case 3: error_string="invalid trade parameters"; break; case 4: error_string="trade server is busy"; break; case 5: error_string="old version of the client terminal"; break; case 6: error_string="no connection with trade server"; break; case 7: error_string="not enough rights"; break; case 8: error_string="too frequent requests"; break; case 9: error_string="malfunctional trade operation"; break; case 64: error_string="account disabled"; break; case 65: error_string="invalid account"; break; case 128: error_string="trade timeout"; break; case 129: error_string="invalid price"; break; case 130: error_string="invalid stops"; break; case 131: error_string="invalid trade volume"; break; case 132: error_string="market is closed"; break; case 133: error_string="trade is disabled"; break; case 134: error_string="not enough money"; break; case 135: error_string="price changed"; break; case 136: error_string="off quotes"; break; case 137: error_string="broker is busy"; break; case 138: error_string="requote"; break; case 139: error_string="order is locked"; break; case 140: error_string="long positions only allowed"; break; case 141: error_string="too many requests"; break; case 145: error_string="modification denied because order too close to market"; break; case 146: error_string="trade context is busy"; break; //---- mql4 errors case 4000: error_string="no error"; break; case 4001: error_string="wrong function pointer"; break; case 4002: error_string="array index is out of range"; break; case 4003: error_string="no memory for function call stack"; break; case 4004: error_string="recursive stack overflow"; break; case 4005: error_string="not enough stack for parameter"; break; case 4006: error_string="no memory for parameter string"; break; case 4007: error_string="no memory for temp string"; break; case 4008: error_string="not initialized string"; break; case 4009: error_string="not initialized string in array"; break; case 4010: error_string="no memory for array\' string"; break; case 4011: error_string="too long string"; break; case 4012: error_string="remainder from zero divide"; break; case 4013: error_string="zero divide"; break; case 4014: error_string="unknown command"; break; case 4015: error_string="wrong jump (never generated error)"; break; case 4016: error_string="not initialized array"; break; case 4017: error_string="dll calls are not allowed"; break; case 4018: error_string="cannot load library"; break; case 4019: error_string="cannot call function"; break; case 4020: error_string="expert function calls are not allowed"; break; case 4021: error_string="not enough memory for temp string returned from function"; break; case 4022: error_string="system is busy (never generated error)"; break; case 4050: error_string="invalid function parameters count"; break; case 4051: error_string="invalid function parameter value"; break; case 4052: error_string="string function internal error"; break; case 4053: error_string="some array error"; break; case 4054: error_string="incorrect series array using"; break; case 4055: error_string="custom indicator error"; break; case 4056: error_string="arrays are incompatible"; break; case 4057: error_string="global variables processing error"; break; case 4058: error_string="global variable not found"; break; case 4059: error_string="function is not allowed in testing mode"; break; case 4060: error_string="function is not confirmed"; break; case 4061: error_string="send mail error"; break; case 4062: error_string="string parameter expected"; break; case 4063: error_string="integer parameter expected"; break; case 4064: error_string="double parameter expected"; break; case 4065: error_string="array as parameter expected"; break; case 4066: error_string="requested history data in update state"; break; case 4099: error_string="end of file"; break; case 4100: error_string="some file error"; break; case 4101: error_string="wrong file name"; break; case 4102: error_string="too many opened files"; break; case 4103: error_string="cannot open file"; break; case 4104: error_string="incompatible access to a file"; break; case 4105: error_string="no order selected"; break; case 4106: error_string="unknown symbol"; break; case 4107: error_string="invalid price parameter for trade function"; break; case 4108: error_string="invalid ticket"; break; case 4109: error_string="trade is not allowed"; break; case 4110: error_string="longs are not allowed"; break; case 4111: error_string="shorts are not allowed"; break; case 4200: error_string="object is already exist"; break; case 4201: error_string="unknown object property"; break; case 4202: error_string="object is not exist"; break; case 4203: error_string="unknown object type"; break; case 4204: error_string="no object name"; break; case 4205: error_string="object coordinates error"; break; case 4206: error_string="no specified subwindow"; break; default: error_string="unknown error"; } //---- return(error_string); } //+------------------------------------------------------------------+ //************************************************************************************************************************* //+------------------------------------------------------------------+
to CTRAder it buys or sells and modify lot size based on specified risk% and SL based on ATR. I tried converting it using 2calgo but it cannot convert it. Here are the two files.
Thanks,
Spotware
13 Nov 2015, 16:09
Dear Trader,
We do not provide coding assistance services. We more than glad to assist you with specific questions about cAlgo.API. You can contact one of our Partners or post a job in Development Jobs section for further coding assistance.
@Spotware