Could someone please add the following to my code? I will give you the EA for doing so
variable 1
1a after X amount of grid trades, increase grid size by X factor
1b also after the same amount of X trades as a, increase lot size by X factor (instead of master or the original factor)
variable 2
2a (after the first increase), after X amount of grid trades, increase grid size by X factor
2b also after the same amount of X trades as a, increase lot size by X factor (instead of the 1st adjusted factor above this one)
variable 3
31 (after the second increase), after X amount of grid trades, increase grid size by X factor
3b also after the same amount of X trades as a, increase lot size by X factor (instead of the 2nd adjusted factor above this one)
Maximum cash loss = X
If you can manage to do this in another save, I'd be impressed
1)
open x buy
open x short
2)
if price moves in the amount of the grid, sell all in profit positions, and open another position WITH TREND (the same type of position that just cashed out) on an X factor of the original long position.
this means if i cash out 1 long after the grid increases 10 pips, i will open up 1.5 longs (if the X factor has been adjusted by me to be set to 1.5)
also if prices moves to the amount the grid is set at, recovery position = X factor
(pretty standard for a grid EA)
THIS IS THE DEFAULT CODE (not the settings i use for the EA though)
//----
extern string GRID = " --- Grid Settings --- ";
extern int MaxOrders = 10;
extern double GridDistance = 50;
extern double PipProfitTarget = 10;
extern double $$ProfitTarget = 200;
extern double LotMultipler = 1.5;
extern double Emergencystop = 1000;
extern string TRAD = " --- Trading Condition Settings --- ";
extern int UniqeID = 200;
extern string FirstOpenType = "buy";
extern double OpenLot = 0.1;
extern double TakeProfit = 100;
extern string EAName = "CL-Martingale Base";
extern bool ScreenDisplay = true;
int dig, opentrades, lastticket, lastindex, lastopentype, count;
double pnt, totalpipPL, totaldollarPL;
string EAComment;
int OpenTicket[100];
int MagicNumber[100];
double OpenPrice[100];
double OpenLots[100];
//+------------------------------------------------------------------+
//| INITIALIZATION |
//+------------------------------------------------------------------+
int init() {
//----
pnt=MarketInfo(Symbol(),MODE_POINT);
dig=MarketInfo(Symbol(),MODE_DIGITS);
if (dig==3 || dig==5) {
pnt*=10;
}
//----
EAComment=EAName+", "+Symbol()+Period();
ArrayResize(OpenTicket,MaxOrders);
ArrayResize(MagicNumber,MaxOrders);
ArrayResize(OpenPrice,MaxOrders);
ArrayResize(OpenLots,MaxOrders);
countprimarytrades();
if (OpenTicket[0]!=0) {
if (OrderSelect(OpenTicket[0],SELECT_BY_TICKET)) {
if (OrderType()==OP_BUY) lastopentype=0;
else lastopentype=1;
}
}
else {
if (FirstOpenType=="buy") lastopentype=1;
else if (FirstOpenType=="sell") lastopentype=0;
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| DEINITIALIZATION |
//+------------------------------------------------------------------+
int deinit() {
//----
Comment("");
//----
return(0);
}
//+------------------------------------------------------------------+
//| MAIN LOOP |
//+------------------------------------------------------------------+
int start() {
//----
counttrades();
if (opentrades!=0) {
if (nextrecovery()) opennext_R(lastindex+1);
if (lastindex>0) totalclose();
}
countprimarytrades();
if (count==0) openprimaytrade();
if (ScreenDisplay) checkcomment();
//----
return(0);
}
//******************************************************************************************************
//******************************************************************************************************
//+------------------------------------------------------------------+
//| FUNCTIONS: Counting Trades |
//+------------------------------------------------------------------+
void assigndata(int r) {
//----
OpenTicket[r]=OrderTicket();
OpenPrice[r]=OrderOpenPrice();
OpenLots[r]=OrderLots();
MagicNumber[r]=OrderMagicNumber();
}
void deletedata(int r) {
//----
OpenTicket[r]=0;
OpenPrice[r]=0;
OpenLots[r]=0;
MagicNumber[r]=0;
}
void counttrades() {
//----
int total=OrdersTotal();
lastticket=0;
lastindex=0;
opentrades=0;
if (total==0) opentrades=0;
else {
for (int i=0; i<total; i++) {
if (!OrderSelect(i,SELECT_BY_POS)) continue;
if (OrderSymbol()!=Symbol()) continue;
if ((OrderMagicNumber()-UniqeID)>=0 && (OrderMagicNumber()-UniqeID)<MaxOrders) {
opentrades++;
lastticket=OrderTicket();
lastindex=OrderMagicNumber()-UniqeID;
assigndata(lastindex);
}
}
}
}
void countprimarytrades() {
//----
int total=OrdersTotal();
count=0;
deletedata(0);
if (total!=0) {
for (int i=0; i<total; i++) {
if (!OrderSelect(i,SELECT_BY_POS)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber()==UniqeID) {
OpenPrice[0]=OrderOpenPrice();
assigndata(0);
count++;
}
}
}
}
//+------------------------------------------------------------------+
//| FUNCTION: Recovery |
//+------------------------------------------------------------------+
bool nextrecovery() {
//----
if (OrderSelect(lastticket,SELECT_BY_TICKET)) {
if (OrderType()==OP_BUY) {
double price=OrderOpenPrice()-GridDistance*pnt;
if (Ask<=price) return (true);
else return (false);
}
else if (OrderType()==OP_SELL) {
price=OrderOpenPrice()+GridDistance*pnt;
if (Bid>=price) return (true);
else return (false);
}
else return (false);
}
}
void opennext_R(int r) {
//----
if (OrderSelect(lastticket,SELECT_BY_TICKET)) {
if (OrderType()==OP_BUY) opentrade(r,0);
else if (OrderType()==OP_SELL) opentrade(r,1);
if (r==MaxOrders-1) Alert ("Max number of trades reached!");
}
}
void totalclose() {
//----
totalpipPL=0;
totaldollarPL=0;
double totallot=0;
for (int r=0; r<=lastindex; r++) {
if (OrderSelect(OpenTicket[r],SELECT_BY_TICKET)) {
totallot=totallot+OrderLots();
totaldollarPL=totaldollarPL+OrderProfit()+OrderSwap();
}
}
double pointval=MarketInfo(Symbol(),MODE_TICKVALUE);
totalpipPL=(totaldollarPL*Point)/(pointval*pnt*totallot);
if ((totaldollarPL>=$$ProfitTarget) || (totalpipPL>=PipProfitTarget) || (totalpipPL<=-Emergencystop)) {
for (r=0; r<=lastindex; r++) {
if (OrderSelect(OpenTicket[r],SELECT_BY_TICKET)) {
closetrade();
deletedata(r);
totalpipPL=0;
totaldollarPL=0;
}
}
}
}
//+------------------------------------------------------------------+
//| FUNCTIONS: Order Excution |
//+------------------------------------------------------------------+
void openprimaytrade() {
//----
if (lastopentype==OP_BUY) opentrade(0,1);
else if (lastopentype==OP_SELL) opentrade(0,0);
}
void opentrade(int r,int type) {
//----
int magicnumber=UniqeID+r;
string identifier, ordertype;
double price, stoploss, takeprofit;
if (r==0) identifier=" Primary";
else identifier=" Recovery";
if (r==0) double lot=normalizelot(OpenLot);
else if (r>0 && r<MaxOrders) lot=normalizelot(MathPow(LotMultipler,r)*OpenLot);
if (type==OP_BUY) {
price=Ask;
ordertype=" BUY";
}
else if (type==OP_SELL) {
price=Bid;
ordertype=" SELL";
}
int ticket=OrderSend(Symbol(),type,lot,price,0,0,0,EAComment,magicnumber,0,Lime);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {
Alert(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+identifier+ordertype+" order opened : "+" @"+DoubleToStr(OrderOpenPrice(),dig));
if ((r==0) && (type==OP_BUY)) {
lastopentype=0;
//if (StopLoss!=0) stoploss=OrderOpenPrice()-StopLoss*pnt;
//else stoploss=0;
if (TakeProfit!=0) takeprofit=OrderOpenPrice()+TakeProfit*pnt;
else takeprofit=0;
}
else if ((r==0) && (type==OP_SELL)) {
lastopentype=1;
//if (StopLoss!=0) stoploss=OrderOpenPrice()+StopLoss*pnt;
//else stoploss=0;
if (TakeProfit!=0) takeprofit=OrderOpenPrice()-TakeProfit*pnt;
else takeprofit=0;
}
if (takeprofit!=0)
OrderModify(ticket,OrderOpenPrice(),0,takeprofit,0,CLR_NONE);
}
}
else {
Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Error opening order : ",GetLastError());
}
}
void closetrade() {
//----
double price;
if (OrderType()==OP_BUY) price=Bid;
else if (OrderType()==OP_SELL) price=Ask;
bool result=OrderClose(OrderTicket(),OrderLots(),price,1,MediumSeaGreen);
if (result) {
Alert(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Order closed : "+" @"+DoubleToStr(OrderClosePrice(),dig));
}
if (!result) {
Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Error closing order : ",GetLastError());
}
}
double normalizelot(double size) {
//----
if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) int digit=2;
else if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) digit=1;
else digit=0;
double val=NormalizeDouble(size,digit);
if (val<MarketInfo(Symbol(),MODE_MINLOT)) val=MarketInfo(Symbol(),MODE_MINLOT);
else if (val>MarketInfo(Symbol(),MODE_MAXLOT)) val=MarketInfo(Symbol(),MODE_MAXLOT);
//----
return(val);
}
//+------------------------------------------------------------------+
//| FUNCTION: Miscellaneous |
//+------------------------------------------------------------------+
void checkcomment() {
//----
string com;
for (int r=0; r<MaxOrders; r++) {
if (OpenTicket[r]!=0) {
string compr="\n-- "+"("+DoubleToStr(r,0)+") trade ticket: "+DoubleToStr(OpenTicket[r],0)+", open price: "+DoubleToStr(OpenPrice[r],Digits)+", order lots: "+DoubleToStr(OpenLots[r],2);
com=com+compr;
}
}
if (totaldollarPL==0) string a="X";
else a=DoubleToStr(totaldollarPL,2);
if (totalpipPL==0) string b="X";
else b=DoubleToStr(totalpipPL,1);
Comment("\n-----------------------------"+
"\n-- "+"Overall profit in $$: "+a+
"\n-- "+"Overall profit in pips: "+b+
"\n-----------------------------"+
"\n-- "+"List of current open trades: "+com);
}
THANK YOU!
variable 1
1a after X amount of grid trades, increase grid size by X factor
1b also after the same amount of X trades as a, increase lot size by X factor (instead of master or the original factor)
variable 2
2a (after the first increase), after X amount of grid trades, increase grid size by X factor
2b also after the same amount of X trades as a, increase lot size by X factor (instead of the 1st adjusted factor above this one)
variable 3
31 (after the second increase), after X amount of grid trades, increase grid size by X factor
3b also after the same amount of X trades as a, increase lot size by X factor (instead of the 2nd adjusted factor above this one)
Maximum cash loss = X
If you can manage to do this in another save, I'd be impressed
1)
open x buy
open x short
2)
if price moves in the amount of the grid, sell all in profit positions, and open another position WITH TREND (the same type of position that just cashed out) on an X factor of the original long position.
this means if i cash out 1 long after the grid increases 10 pips, i will open up 1.5 longs (if the X factor has been adjusted by me to be set to 1.5)
also if prices moves to the amount the grid is set at, recovery position = X factor
(pretty standard for a grid EA)
THIS IS THE DEFAULT CODE (not the settings i use for the EA though)
//----
extern string GRID = " --- Grid Settings --- ";
extern int MaxOrders = 10;
extern double GridDistance = 50;
extern double PipProfitTarget = 10;
extern double $$ProfitTarget = 200;
extern double LotMultipler = 1.5;
extern double Emergencystop = 1000;
extern string TRAD = " --- Trading Condition Settings --- ";
extern int UniqeID = 200;
extern string FirstOpenType = "buy";
extern double OpenLot = 0.1;
extern double TakeProfit = 100;
extern string EAName = "CL-Martingale Base";
extern bool ScreenDisplay = true;
int dig, opentrades, lastticket, lastindex, lastopentype, count;
double pnt, totalpipPL, totaldollarPL;
string EAComment;
int OpenTicket[100];
int MagicNumber[100];
double OpenPrice[100];
double OpenLots[100];
//+------------------------------------------------------------------+
//| INITIALIZATION |
//+------------------------------------------------------------------+
int init() {
//----
pnt=MarketInfo(Symbol(),MODE_POINT);
dig=MarketInfo(Symbol(),MODE_DIGITS);
if (dig==3 || dig==5) {
pnt*=10;
}
//----
EAComment=EAName+", "+Symbol()+Period();
ArrayResize(OpenTicket,MaxOrders);
ArrayResize(MagicNumber,MaxOrders);
ArrayResize(OpenPrice,MaxOrders);
ArrayResize(OpenLots,MaxOrders);
countprimarytrades();
if (OpenTicket[0]!=0) {
if (OrderSelect(OpenTicket[0],SELECT_BY_TICKET)) {
if (OrderType()==OP_BUY) lastopentype=0;
else lastopentype=1;
}
}
else {
if (FirstOpenType=="buy") lastopentype=1;
else if (FirstOpenType=="sell") lastopentype=0;
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| DEINITIALIZATION |
//+------------------------------------------------------------------+
int deinit() {
//----
Comment("");
//----
return(0);
}
//+------------------------------------------------------------------+
//| MAIN LOOP |
//+------------------------------------------------------------------+
int start() {
//----
counttrades();
if (opentrades!=0) {
if (nextrecovery()) opennext_R(lastindex+1);
if (lastindex>0) totalclose();
}
countprimarytrades();
if (count==0) openprimaytrade();
if (ScreenDisplay) checkcomment();
//----
return(0);
}
//******************************************************************************************************
//******************************************************************************************************
//+------------------------------------------------------------------+
//| FUNCTIONS: Counting Trades |
//+------------------------------------------------------------------+
void assigndata(int r) {
//----
OpenTicket[r]=OrderTicket();
OpenPrice[r]=OrderOpenPrice();
OpenLots[r]=OrderLots();
MagicNumber[r]=OrderMagicNumber();
}
void deletedata(int r) {
//----
OpenTicket[r]=0;
OpenPrice[r]=0;
OpenLots[r]=0;
MagicNumber[r]=0;
}
void counttrades() {
//----
int total=OrdersTotal();
lastticket=0;
lastindex=0;
opentrades=0;
if (total==0) opentrades=0;
else {
for (int i=0; i<total; i++) {
if (!OrderSelect(i,SELECT_BY_POS)) continue;
if (OrderSymbol()!=Symbol()) continue;
if ((OrderMagicNumber()-UniqeID)>=0 && (OrderMagicNumber()-UniqeID)<MaxOrders) {
opentrades++;
lastticket=OrderTicket();
lastindex=OrderMagicNumber()-UniqeID;
assigndata(lastindex);
}
}
}
}
void countprimarytrades() {
//----
int total=OrdersTotal();
count=0;
deletedata(0);
if (total!=0) {
for (int i=0; i<total; i++) {
if (!OrderSelect(i,SELECT_BY_POS)) continue;
if (OrderSymbol()!=Symbol()) continue;
if (OrderMagicNumber()==UniqeID) {
OpenPrice[0]=OrderOpenPrice();
assigndata(0);
count++;
}
}
}
}
//+------------------------------------------------------------------+
//| FUNCTION: Recovery |
//+------------------------------------------------------------------+
bool nextrecovery() {
//----
if (OrderSelect(lastticket,SELECT_BY_TICKET)) {
if (OrderType()==OP_BUY) {
double price=OrderOpenPrice()-GridDistance*pnt;
if (Ask<=price) return (true);
else return (false);
}
else if (OrderType()==OP_SELL) {
price=OrderOpenPrice()+GridDistance*pnt;
if (Bid>=price) return (true);
else return (false);
}
else return (false);
}
}
void opennext_R(int r) {
//----
if (OrderSelect(lastticket,SELECT_BY_TICKET)) {
if (OrderType()==OP_BUY) opentrade(r,0);
else if (OrderType()==OP_SELL) opentrade(r,1);
if (r==MaxOrders-1) Alert ("Max number of trades reached!");
}
}
void totalclose() {
//----
totalpipPL=0;
totaldollarPL=0;
double totallot=0;
for (int r=0; r<=lastindex; r++) {
if (OrderSelect(OpenTicket[r],SELECT_BY_TICKET)) {
totallot=totallot+OrderLots();
totaldollarPL=totaldollarPL+OrderProfit()+OrderSwap();
}
}
double pointval=MarketInfo(Symbol(),MODE_TICKVALUE);
totalpipPL=(totaldollarPL*Point)/(pointval*pnt*totallot);
if ((totaldollarPL>=$$ProfitTarget) || (totalpipPL>=PipProfitTarget) || (totalpipPL<=-Emergencystop)) {
for (r=0; r<=lastindex; r++) {
if (OrderSelect(OpenTicket[r],SELECT_BY_TICKET)) {
closetrade();
deletedata(r);
totalpipPL=0;
totaldollarPL=0;
}
}
}
}
//+------------------------------------------------------------------+
//| FUNCTIONS: Order Excution |
//+------------------------------------------------------------------+
void openprimaytrade() {
//----
if (lastopentype==OP_BUY) opentrade(0,1);
else if (lastopentype==OP_SELL) opentrade(0,0);
}
void opentrade(int r,int type) {
//----
int magicnumber=UniqeID+r;
string identifier, ordertype;
double price, stoploss, takeprofit;
if (r==0) identifier=" Primary";
else identifier=" Recovery";
if (r==0) double lot=normalizelot(OpenLot);
else if (r>0 && r<MaxOrders) lot=normalizelot(MathPow(LotMultipler,r)*OpenLot);
if (type==OP_BUY) {
price=Ask;
ordertype=" BUY";
}
else if (type==OP_SELL) {
price=Bid;
ordertype=" SELL";
}
int ticket=OrderSend(Symbol(),type,lot,price,0,0,0,EAComment,magicnumber,0,Lime);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {
Alert(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+identifier+ordertype+" order opened : "+" @"+DoubleToStr(OrderOpenPrice(),dig));
if ((r==0) && (type==OP_BUY)) {
lastopentype=0;
//if (StopLoss!=0) stoploss=OrderOpenPrice()-StopLoss*pnt;
//else stoploss=0;
if (TakeProfit!=0) takeprofit=OrderOpenPrice()+TakeProfit*pnt;
else takeprofit=0;
}
else if ((r==0) && (type==OP_SELL)) {
lastopentype=1;
//if (StopLoss!=0) stoploss=OrderOpenPrice()+StopLoss*pnt;
//else stoploss=0;
if (TakeProfit!=0) takeprofit=OrderOpenPrice()-TakeProfit*pnt;
else takeprofit=0;
}
if (takeprofit!=0)
OrderModify(ticket,OrderOpenPrice(),0,takeprofit,0,CLR_NONE);
}
}
else {
Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Error opening order : ",GetLastError());
}
}
void closetrade() {
//----
double price;
if (OrderType()==OP_BUY) price=Bid;
else if (OrderType()==OP_SELL) price=Ask;
bool result=OrderClose(OrderTicket(),OrderLots(),price,1,MediumSeaGreen);
if (result) {
Alert(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Order closed : "+" @"+DoubleToStr(OrderClosePrice(),dig));
}
if (!result) {
Print(TimeToStr(TimeCurrent(),TIME_DATE|TIME_SECONDS)+" | "+EAComment+" | "+" Error closing order : ",GetLastError());
}
}
double normalizelot(double size) {
//----
if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.01) int digit=2;
else if (MarketInfo(Symbol(),MODE_LOTSTEP)==0.1) digit=1;
else digit=0;
double val=NormalizeDouble(size,digit);
if (val<MarketInfo(Symbol(),MODE_MINLOT)) val=MarketInfo(Symbol(),MODE_MINLOT);
else if (val>MarketInfo(Symbol(),MODE_MAXLOT)) val=MarketInfo(Symbol(),MODE_MAXLOT);
//----
return(val);
}
//+------------------------------------------------------------------+
//| FUNCTION: Miscellaneous |
//+------------------------------------------------------------------+
void checkcomment() {
//----
string com;
for (int r=0; r<MaxOrders; r++) {
if (OpenTicket[r]!=0) {
string compr="\n-- "+"("+DoubleToStr(r,0)+") trade ticket: "+DoubleToStr(OpenTicket[r],0)+", open price: "+DoubleToStr(OpenPrice[r],Digits)+", order lots: "+DoubleToStr(OpenLots[r],2);
com=com+compr;
}
}
if (totaldollarPL==0) string a="X";
else a=DoubleToStr(totaldollarPL,2);
if (totalpipPL==0) string b="X";
else b=DoubleToStr(totalpipPL,1);
Comment("\n-----------------------------"+
"\n-- "+"Overall profit in $$: "+a+
"\n-- "+"Overall profit in pips: "+b+
"\n-----------------------------"+
"\n-- "+"List of current open trades: "+com);
}
THANK YOU!
Be hopeful in a winning position, and fearful in a losing position.