Orders Execution
0
Views
0
Downloads
0
Favorites
Mighty_mGRIDrepair
//+------------------------------------------------------------------+
//| mightyhitter |
//+------------------------------------------------------------------+
#property copyright "Copyright 2006, mightyhitter"
#property link ""
extern double TakeProfit = 20;
extern double StopLoss = 20;
extern double Lots = 0.1;
extern double MaxLots = 1000.00;
extern int MagicNumber = 8030097;
extern bool Debug=false;
//global vars
double NewBar=0;
bool IsNewBar=true;
double MyLots;
//array
int gl_OrderTicket[100];
int gl_OrderType[100];
double gl_OrderPips[100];
double gl_OrderLots[100];
int start(){
int cnt, ticket;
if(Bars<300){
Print("bars less than 100");
return(0);
}
if(Bars>NewBar)
{
IsNewBar=true;
NewBar=Bars;
}else{IsNewBar=false;}
CheckOrders();
if(Debug){
PrintVars();
}
}
int EnterOrder(int bs)
{
int ticket;
//check margin
if(AccountFreeMargin()<(1000*Lots)){
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
//buy
if (bs==1)
{
ticket=OrderSend(Symbol(),OP_BUY,MyLots,Ask,3,0,0, "mmatingale Long",MagicNumber,0,Green);
if(ticket>0){
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {Print("BUY order opened : ",OrderOpenPrice());}
}
else Print("Error opening BUY order : ",GetLastError());
return(ticket);
}
//sell
if (bs==2)
{
ticket=OrderSend(Symbol(),OP_SELL,MyLots,Bid,3,0,0, "mmatingale Short",MagicNumber,0,Red);
if(ticket>0) {
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) {Print("SELL order opened : ",OrderOpenPrice());}
}
else Print("Error opening SELL order : ",GetLastError());
return(ticket);
}
}
void CheckOrders()
{
int cnt,ticket;
int total=OrdersTotal();
ArrayInitialize(gl_OrderTicket,0);
ArrayInitialize(gl_OrderType,0);
ArrayInitialize(gl_OrderPips,0);
ArrayInitialize(gl_OrderLots,0);
int mc=0;
for(cnt=0;cnt<total;cnt++) {//3
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) {//2
if((OrderType()==OP_SELL||OrderType()==OP_BUY)&&OrderMagicNumber()==MagicNumber&&OrderSymbol()==Symbol())
{
gl_OrderTicket[mc]=OrderTicket();
if(OrderType()==OP_SELL)
{
gl_OrderType[mc]=2;
gl_OrderPips[mc]=(OrderOpenPrice()-Ask)/Point;
}
if(OrderType()==OP_BUY)
{
gl_OrderType[mc]=1;
gl_OrderPips[mc]=(Bid-OrderOpenPrice())/Point;
}
gl_OrderLots[mc]=OrderLots();
mc++;
}
}//2
}//3
int LastValue=GetLastValue();
//Print(LastValue);
if(LastValue==0)
{
UpdateVars();
EnterOrder(2);
}
else
{
if(gl_OrderPips[LastValue-1]>=TakeProfit)CloseOrdersAll();
if(gl_OrderPips[LastValue-1]<=(0-StopLoss)&&gl_OrderType[LastValue-1]==1){UpdateVars();EnterOrder(2);}
if(gl_OrderPips[LastValue-1]<=(0-StopLoss)&&gl_OrderType[LastValue-1]==2){UpdateVars();EnterOrder(1);}
}
}
int TotalOrders(int BSType)
{
int cnt,bs1=0,bs2=0;
for(cnt=0;cnt<OrdersTotal();cnt++) {
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()==OP_BUY && BSType==1 && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())bs1++;
if(OrderType()==OP_SELL && BSType==2 && OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())bs2++;
}
if(BSType==1)return(bs1);
if(BSType==2)return(bs2);
return(-1);
}
void UpdateVars()
{
int cnt;
int LastValue=GetLastValue();
if(LastValue==0)MyLots=Lots;
if(LastValue==1)MyLots=gl_OrderLots[LastValue-1]*3;
if(LastValue>1)MyLots=gl_OrderLots[LastValue-1]*2+gl_OrderLots[LastValue-2];
}
void CloseOrdersAll()
{
PrintVars();
int total = OrdersTotal();
for(int cnt=total-1;cnt>=0;cnt--)
{
OrderSelect(cnt, SELECT_BY_POS);
if(OrderMagicNumber()==MagicNumber && OrderSymbol()==Symbol())
{
int type = OrderType();
bool result = false;
switch(type)
{
//Close opened long positions
case OP_BUY : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5, Red );
break;
//Close opened short positions
case OP_SELL : result = OrderClose( OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_ASK), 5, Red );
}
if(result == false)
{
Print("Order " , OrderTicket() , " failed to close. Error:" , GetLastError() );
Sleep(3000);
}
}
}
if(TotalOrders(1)>0||TotalOrders(2)>0)CloseOrdersAll();
}
void PrintVars()
{
string msg="";
string otype="";
double WLots=0,LLots=0;
int LastValue=GetLastValue();
if(LastValue>0){
for(int j=0;j<LastValue;j++)
{
if(gl_OrderType[j]==1)otype="BUY";
if(gl_OrderType[j]==2)otype="SELL";
msg=msg+"["+gl_OrderTicket[j]+","+otype+","+DoubleToStr(gl_OrderPips[j],0)+","+DoubleToStr(gl_OrderLots[j],2)+"] ";
if(gl_OrderPips[j]>0)WLots=WLots+gl_OrderPips[j]*gl_OrderLots[j];
if(gl_OrderPips[j]<0)LLots=LLots+gl_OrderPips[j]*gl_OrderLots[j];
}
msg=msg+"(H/L: "+DoubleToStr(gl_OrderPips[ArrayMaximum(gl_OrderPips)],0)+"/"+DoubleToStr(gl_OrderPips[ArrayMinimum(gl_OrderPips)],0)+") ";
msg=msg+"(W/L: "+DoubleToStr(WLots,0)+"/"+DoubleToStr(LLots,0)+") ";
Print(msg);}
}
int GetLastValue()
{
int cnt,LastValue=0;
for(cnt=ArraySize(gl_OrderTicket)-1;cnt>=0;cnt--)
{
if(gl_OrderTicket[cnt]!=0)
{
LastValue=cnt+1;
cnt=-1;
}
}
return(LastValue);
}
Comments
Markdown Formatting Guide
# H1
## H2
### H3
**bold text**
*italicized text*
[title](https://www.example.com)

`code`
```
code block
```
> blockquote
- Item 1
- Item 2
1. First item
2. Second item
---