Placing BuyStop and SellStop Orders

Posted By: steyr

Placing BuyStop and SellStop Orders - 10/19/23 18:41

Hello dear community,

I am working with the MT5 bridge, and currently I am struggling to place Buy Limit and Sell Limit orders.

When I run my code, the Buy/Sell Limit order is placed almost directly at the current price, instead of at a 0.2% distance from the current price.

Furthermore, I am wondering how to program Buy/Sell Stop orders in Zorro. The manual doesn't seem to provide proper code examples. Can anyone of you help me out with this?

Best regards,
steyr




Code
		if(priceC(0) <=  priceO(0) and NumPendingShort <= 0)
			{
				OrderLimit = priceO(0)-(priceC(1)*(0.2/100)); // 0.2% Distance from current Price
				MaxShort = 1;
				enterShort(OrderLimit);
			}

			else if(priceC(0) >= priceO(0) and NumPendingLong <= 0)
			{
					
				OrderLimit = priceO(0)+(priceC(1)*(0.2/100));// 0.2% Distance from current Price
				MaxLong = 1;
				enterLong(OrderLimit);
					
			}
Posted By: AndrewAMD

Re: Placing BuyStop and SellStop Orders - 10/19/23 20:05

OrderLimit is a global variable and not an argument for enterLong/short. Just set OrderLimit before you call enterLong.
Posted By: steyr

Re: Placing BuyStop and SellStop Orders - 10/20/23 09:46

Thank you for your hint, Andrew.

However, it's the case that it doesn't place the Limit Order around +- 0.2% from the current price but rather in close proximity to the price...

I don't understand this logic.
Code
		if(priceC(0) <=  priceO(0) and NumPendingShort <= 0)
			{
				OrderLimit = priceO(0)-(priceC(1)*(0.2/100)); // 0.2% Distance from current Price
				MaxShort = 1;
				enterShort();
			}

			else if(priceC(0) >= priceO(0) and NumPendingLong <= 0)
			{
					
				OrderLimit = priceO(0)+(priceC(1)*(0.2/100));// 0.2% Distance from current Price
				MaxLong = 1;
				enterLong();
					
			}
Posted By: AndrewAMD

Re: Placing BuyStop and SellStop Orders - 10/20/23 12:36

Your code does not match your description. It is relative to both the current bar and the previous bar, which is wrong. Don’t call priceC(1).
Posted By: steyr

Re: Placing BuyStop and SellStop Orders - 10/20/23 15:30

I see, unfortunately, changing the approach as you said before hasn't resolved the issue with the inexplicable placement of OrderLimit.
Even though the correct price level is displayed in the print statement, it's not being placed accurately in the market.

In the attached image, priceO(0) and the OrderLimit variable are printed, showing a difference of 0.02%. Despite this, a sell position is opened at 0.00006 immediately afterward. What could be going wrong here?


Code

		if(priceC(0) <=  priceO(0) and NumPendingShort <= 0)
			{
				OrderLimit = priceO(0)-(priceO(0)*(0.2/100)); // 0.2% Distance from current Price
				MaxShort = 1;
				enterShort();
			}

			else if(priceC(0) >= priceO(0) and NumPendingLong <= 0)
			{
					
				OrderLimit = priceO(0)+(priceO(0)*(0.2/100));// 0.2% Distance from current Price
				MaxLong = 1;
				enterLong();
					
			}




Attached picture Orderlimit issue.jpg
Posted By: AndrewAMD

Re: Placing BuyStop and SellStop Orders - 10/20/23 17:49

It still doesn't match your description. The latest price is priceC(0). run() is called when a bar closes.

Also, try rounding OrderLimit to the nearest PIP, and if that doesn't work, contact support.
Posted By: steyr

Re: Placing BuyStop and SellStop Orders - 10/23/23 08:57

Rounding the OrderLimit variable using round() unfortunately doesn't change the outcome. I will now contact the support team.

Thank you for your assistance until now.

This is how the final code looks like

Code
                      tick()
                      {		
                        if(priceC(0) <=  priceO(0) and NumPendingShort <= 0)
			{
				OrderLimit = roundto( (priceO(0) - (priceO(0) * (BarRange/100)) ),PIP); // 0.2% Distance from current Price
				MaxShort = 1;
				enterShort(OrderLimit);
			}

			else if(priceC(0) >= priceO(0) and NumPendingLong <= 0)
			{
					
				OrderLimit = roundto( (priceO(0) + (priceC(1) * (0.2/100)) ),PIP); // 0.2% Distance from current Price
				MaxLong = 1;
				enterLong(OrderLimit);
					
			}
                      }
Posted By: AndrewAMD

Re: Placing BuyStop and SellStop Orders - 10/23/23 15:26

You did it again... Don't do this:
Code
enterShort(OrderLimit);
enterLong(OrderLimit);
enterLong() and enterShort() are documented here.
https://zorro-project.com/manual/en/buylong.htm
Posted By: steyr

Re: Placing BuyStop and SellStop Orders - 10/24/23 20:11

Oh sorry, my fault..

in the running script its correct..

Code
            tick()
                      {		
                        if(priceC(0) <=  priceO(0) and NumPendingShort <= 0)
			{
				OrderLimit = roundto( (priceO(0) - (priceO(0) * (BarRange/100)) ),PIP); // 0.2% Distance from current Price
				MaxShort = 1;
				enterShort();
			}

			else if(priceC(0) >= priceO(0) and NumPendingLong <= 0)
			{
					
				OrderLimit = roundto( (priceO(0) + (priceC(1) * (0.2/100)) ),PIP); // 0.2% Distance from current Price
				MaxLong = 1;
				enterLong();
					
			}
                      }
© 2024 lite-C Forums