Manual Take Profit and Stop Loss

 

Hello fellow Traders.

In a recent programming attempt I wanted to try out manual Take Profits and Stop Losses.
I'd like you to tell me if I'm grasping this concept right or is there a change that is due. Note: In Take Profit I include the Spread which I have to in order to compensate for the Broker, but with Stop Loss I don't. Is this ok? Also I am using the Ask price since I've read it's the price the Broker offers and considers when entering Buy Positions.

bool BuyTradeOpen; //A pretend check to see if a Trade is open
double AskPN; //Ask Price New
double OrderPrice; //Price at which we have opened the Trade
double TakeProfit = 0.00500; //50 Pip Take Profit (EUR/USD)
double StopLoss = 0.00250; //25 Pip Stop Loss (EUR/USD)
double SpreadC; //A pretend Spread Value

void OnTick()
   {
      if (BuyTradeOpen == True)
         {
            AskPN = Ask;
            if (AskPN >= (OrderPrice + TakeProfit + SpreadC)) //Take Profit
               {
                  //Close Trade with 50 Pips of Profit
               }
            else if (AskPN <= (OrderPrice - StopLoss)) //Stop Loss
               {
                  //Close Trade with 25 Pips of Loss
               }
         }
   }
 

I don't understand what "I include the Spread which I have to in order to compensate for the Broker " means

if (OrderPrice >= (AskPN + TakeProfit + SpreadC)) //Take Profit

 will be true when the trade is in loss

You need sometyhing like

if(Bid-(OrderPrice+TakeProfit)>=0)) //Take Profit

Buy orders open at Ask and close at Bid 

and you will need to select the trade somewhere in your code

 

Hello GumRai, thank you so much for your response.

I just realised I messed up the code when I posted the example. I have edited it and it should be in order now.
Yes, your suggestion works just as it should, but can you perhaps explain why I would open at Ask and close at Bid?

If that is true with Buy positions then I will be reaching my Take Profit even faster as the Bid Price is generally lower than the Ask?

BidPN = Bid;
if (BidPN >= (OrderPrice + TakeProfit + SpreadC)) //Take Profit
   {
      //Close Trade with 50 Pips of Profit
   }
else if (BidPN <= (OrderPrice - StopLoss)) //Stop Loss
   {
      //Close Trade with 25 Pips of Loss
   }

That way this should be as such?


Also about the spread. I'm just stating that I am putting Spread into my Take Profit calculation because the price must rise 50 Pips + Spread to hit my Take Profit, but when I am calculating my Stop Loss I do not include the Spread. I believe this is right.

 
lovromirnik:

 but can you perhaps explain why I would open at Ask and close at Bid?

If that is true with Buy positions then I will be reaching my Take Profit even faster as the Bid Price is generally lower than the Ask?

Because Buy trades are opened at ask and closed at bid, you have no choice in the matter.

When price is moving in an upward direction, a level is hit by the Ask first, because the Bid price is lower than the Ask 

 
lovromirnik:

Also about the spread. I'm just stating that I am putting Spread into my Take Profit calculation because the price must rise 50 Pips + Spread to hit my Take Profit, but when I am calculating my Stop Loss I do not include the Spread. I believe this is right.

Using Bid in your calculation will automatically factor in the spread
 

Maybe you could consider using something like this in your code?

   If(OrderSelect() )    //Insert whatever way you are selecting the order
      {
      if(OrderType()==OP_BUY)
         if(OrderClosePrice()-OrderOpenPrice()>=TakeProfit)
            if(!OrderClose(OrderTicket(),OrderLots,OrderClosePrice(),slippage,clrNONE))
               Print("Error Closing Order#"+(string)OrderTicket()+". Error Code "+(string)GetLastError());
      }
   
Reason: