stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Order type modify (Help request)
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=4851
Page 1 of 1
Author:  JenniePussier [ Wed Aug 03, 2016 2:33 pm ]
Post subject:  Order type modify (Help request)

Hi,

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);
}
Author:  McNish [ Thu Aug 04, 2016 6:03 pm ]
Post subject:  Order type modify (Help request)

OrderSend() is the function to send orders.
In the order send, replace OP_BUYSTOP with OP_SELLLIMIT and vice versa.

am basic too myself but i believe that is correct.

Cheers, :-)
Author:  JenniePussier [ Thu Aug 04, 2016 6:16 pm ]
Post subject:  Order type modify (Help request)

Thx, will give it a try :cheer:

been bugging me all day! :smile:
Author:  JenniePussier [ Thu Aug 04, 2016 6:30 pm ]
Post subject:  Order type modify (Help request)

There might be more to it...

Makes logical sense in part but there must be more mods needed,
Been trying stuff like find and replace, so for every OP_BUYSTOP
find and replace to OP_SELLLIMIT and the inverse applies for OP_SELLSTOP.
Made sure to go though the whole code and change everything with no success yet.

wonder what it could be? I'm thinking that the symbols >,<,= along with bid and ask
as well as the void functions are giving an ordersend error in backtester
Author:  JenniePussier [ Thu Aug 04, 2016 6:40 pm ]
Post subject:  Order type modify (Help request)

Snippit 1

Code: Select all

void OpenBuyStop()
{
    int ticket, err, tries;
    tries = 0;
    if (!GlobalVariableCheck("InTrade")) 
    {
        while (tries < 3)
        {
            GlobalVariableSet("InTrade", CurTime());  // set lock indicator
            ticket = OrderSend(Symbol(), OP_SELLLIMIT, 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;
        }
    }
}
Snippit 2

Code: Select all

void OpenSellStop()
 {
    int ticket, err, tries;
    tries = 0;
    if (!GlobalVariableCheck("InTrade")) 
    {
        while (tries < 3)
        {
            GlobalVariableSet("InTrade", CurTime());  // set lock indicator
            ticket = OrderSend(Symbol(), OP_BUYLIMIT, 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;
        }
    }
}
Snippit 3

Code: Select all

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);
            }
        }
    }
snippit 4

Code: Select all

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);
            }
        }
    }
}
snippit 5

Code: Select all

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);
    }
}
I'm 99% sure that those snippit codes are where the problem is in reverse order programming
Author:  JenniePussier [ Thu Aug 04, 2016 6:43 pm ]
Post subject:  Order type modify (Help request)

nearly forgot this one

Code: Select all

    // Open the Buy Stop and the Sell Stop for this candle
    Write("Opening BuyStop & SellStop");
    OpenBuyStop();
    OpenSellStop();
    
    return(0);
}
Author:  tomele [ Thu Aug 04, 2016 6:59 pm ]
Post subject:  Order type modify (Help request)

JenniePussier ยป 04 Aug 2016, 20:30 wrote:There might be more to it...
A lot more. You cant just replace OP_BUYSTOP with OP_SELLLIMIT and leave the remaining unchanged. This is complete nonsense. Please educate yourself about the parameters of the SendOrder function and basic coherences in trading.
All times are UTC Page 1 of 1