I'm practicing coding and I have already coded some very basic stuff (line color, type, size, extern defaults and inputs etc...) Can someone please help me, I'm stuck understanding how to reverse change the order type to the existing programming code
(example:
OpenSellStop changes to "open buy limit"
OpenBuyStop changes to "open sell limit"
Not sure how to go about this... basically trying to open the complete opposite order type that is being used at present
//Code
Code: Select all
double high,
low,
spread,
highOpen,
lowOpen,
highSL,
lowSL,
highTP,
lowTP;
string filename;
void OpenBuyStop()
{
int ticket, err, tries;
tries = 0;
if (!GlobalVariableCheck("InTrade"))
{
while (tries < 3)
{
GlobalVariableSet("InTrade", CurTime()); // set lock indicator
ticket = OrderSend(Symbol(), OP_BUYSTOP, LotsOptimized(), highOpen, Slippage,
highSL, highTP, "EA Order", Magic, 0, Red);
Write("In function OpenBuyStop OrderSend Executed, ticket = "+ticket);
GlobalVariableDel("InTrade"); // clear lock indicator
if (ticket <= 0)
{
Write("Error Occured : "+ErrorDescription(GetLastError())+" BuyStop @ "+highOpen+" SL @ "+highSL+" TakeProfit @"+highTP);
tries++;
}
else
tries = 3;
}
}
}
void OpenSellStop()
{
int ticket, err, tries;
tries = 0;
if (!GlobalVariableCheck("InTrade"))
{
while (tries < 3)
{
GlobalVariableSet("InTrade", CurTime()); // set lock indicator
ticket = OrderSend(Symbol(), OP_SELLSTOP, LotsOptimized(), lowOpen, Slippage,
lowSL, lowTP, "EA Order", Magic, 0, Red);
Write("In function OpenSellStop OrderSend Executed, ticket = "+ticket);
GlobalVariableDel("InTrade"); // clear lock indicator
if(ticket <= 0)
{
Write("Error Occured : "+ErrorDescription(GetLastError())+" BuyStop @ "+lowOpen+" SL @ "+lowSL+" TakeProfit @"+lowTP);
tries++;
}
else
tries = 3;
}
}
}
void SetBreakEven(int byPips)
{
for (int i=0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
// only look if mygrid and symbol...
if (OrderSymbol() == Symbol() && (OrderMagicNumber() == Magic))
{
double buyBreakEven = OrderOpenPrice() + byPips * Point;
double sellBreakEven = OrderOpenPrice() - byPips * Point;
if (OrderType() == OP_BUY && Bid > buyBreakEven && OrderStopLoss() != buyBreakEven)
{
Write("Moving StopLoss of Buy Order to BreakEven: "+buyBreakEven);
Comment("Moving StopLoss of Buy Order to BreakEven: ", buyBreakEven);
OrderModify(OrderTicket(), OrderOpenPrice(), buyBreakEven, OrderTakeProfit(), Green);
}
if (OrderType() == OP_SELL && Ask < sellBreakEven && OrderStopLoss() != sellBreakEven)
{
Write("Moving StopLoss of Sell Order to BreakEven:"+sellBreakEven);
Comment("Moving StopLoss of Sell Order to BreakEven:", sellBreakEven);
OrderModify(OrderTicket(), OrderOpenPrice(), sellBreakEven, OrderTakeProfit(), Red);
}
}
}
}
void DoTrail()
{
for (int i=0; i < OrdersTotal(); i++)
{
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
// only look if mygrid and symbol...
if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{
if (OrderType() == OP_BUY &&
Bid - OrderOpenPrice() > TrailingStop * Point &&
(OrderStopLoss() < Bid - TrailingStop * Point) || OrderStopLoss() == 0)
{
OrderModify(OrderTicket(), OrderOpenPrice(),
Bid - TrailingStop * Point, OrderTakeProfit(), 0, Green);
return(0);
}
if (OrderType() == OP_SELL &&
OrderOpenPrice() - Ask > TrailingStop * Point &&
(OrderStopLoss() > Ask + TrailingStop * Point) || OrderStopLoss() == 0)
{
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + TrailingStop * Point, OrderTakeProfit(), 0 ,Red);
return(0);
}
}
}
}
void DeletePendingOrders()
{
int myTkt;
int myTyp;
bool result = FALSE;
Comment("\nDeletePendingOrders: OrdersTotal = ", OrdersTotal());
for (int i=OrdersTotal(); i > 0; i--)
{
OrderSelect(i, SELECT_BY_POS);
myTkt = OrderTicket();
myTyp = OrderType();
switch( myTyp )
{
// Close pending orders
case OP_BUYLIMIT:
case OP_BUYSTOP:
case OP_SELLLIMIT:
case OP_SELLSTOP:
result = OrderDelete(myTkt);
break;
default: // Do nothing with open orders
break;
}
if (result == FALSE)
{
Alert( "Order ", myTkt, " failed to close. Error:", GetLastError() );
Print( "Order ", myTkt, " failed to close. Error:", GetLastError() );
Sleep(3000);
}
Sleep(1000);
}
}
int Write(string str)
{
int handle;
handle = FileOpen(filename, FILE_READ|FILE_WRITE|FILE_CSV, "/t");
FileSeek(handle, 0, SEEK_END);
FileWrite(handle, str + " Time " + TimeToStr(CurTime(), TIME_DATE|TIME_SECONDS));
FileClose(handle);
}
_____________________________________________________
// Open the Buy Stop and the Sell Stop for this candle
Write("Opening BuyStop & SellStop");
OpenBuyStop();
OpenSellStop();
return(0);
}