• Home
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 7:38am
Menu
  • Forums
  • Trades
  • News
  • Calendar
  • Market
  • Brokers
  • Login
  • Join
  • 7:38am
Sister Sites
  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Options

Bookmark Thread

First Page First Unread Last Page Last Post

Print Thread

Similar Threads

Difference between buy/sell stops & buy/sell limits 34 replies

How to make lots of money using micro lots 16 replies

how to change buy to sell, sell to buy for EA 1 reply

the buy/buy or sell/sell currency broker?? 5 replies

I am looking for EA to put Buy Stops, Buy limits, Sell limits, and Sell Stops. I will 0 replies

  • Platform Tech
  • /
  • Reply to Thread
  • Subscribe
  • 2
Attachments: How to make 1 trade so sell lots = buy lots?
Exit Attachments
Tags: How to make 1 trade so sell lots = buy lots?
Cancel

How to make 1 trade so sell lots = buy lots?

  • Post #1
  • Quote
  • First Post: Aug 16, 2011 12:02pm Aug 16, 2011 12:02pm
  •  dancingphil
  • Joined May 2009 | Status: Member | 667 Posts
Sorry dudes, I'm very new to coding, but trying hard - a little help needed.

I use hedging, not stop losses. I always have many trades open.

I want to "balance trades" so that "sell lots = buy lots" when a trigger is reached (either when a % equity profit is reached, or a % equity loss is reached).

Example: I have many open trades which are 2.04 lots of Sell, 0.16 lots of Buy. When TargetBestEquity is reached, I need to balance open and sell trades with an OP_BUY of 1.88 lots....thus making the both sides equal to 2.04 lots.

But I can't seem to use it as a lots variable in the OrderSend() balancing trade.

Inserted Code
   double total_selllots =0;   //define variable total_sellots, starting at =0
   double total_buylots =0;   //define variable total_buylots, starting at =0
 
   //Calculate total_buylots
 
   for (int i = OrdersTotal()-1; i >=0; i--)   //scanning all trades
   {
      if (! OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) { Sleep(10); continue; }
      if(OrderSymbol() == Symbol() && OrderMagicNumber()==Magic) //check the currency and magic number are correct
      {         
         if (OrderType() == OP_BUY)  //with OP_BUY trades
         {
            total_buylots = total_buylots + OrderLots();  //add them up and keep a tally of them
 
         }
   //Calculate total_selllots
 
         if (OrderType() == OP_SELL) //with the OP_SELL trades
         {
            total_selllots = total_selllots + OrderLots();    //add them up and keep a tally of them too
         }

So, (as an example) my EA fails when I try to use (total_buylots-total_selllots) as the lots quantity in a Ordersend().

I can print to the chart the correct lots count, but I must be doing something silly to not be able to use it in the OrderSend().

Inserted Code
    string strTotalLots;
   strTotalLots="Total BUY  LOTS = " + DoubleToStr(total_buylots,2) + "\n";
   strTotalLots=strTotalLots+"Total SELL LOTS = " + DoubleToStr(total_selllots,2) + "\n";
   strTotalLots=strTotalLots+"Total ALL  LOTS = " + DoubleToStr(total_selllots+total_buylots,2) + "\n";

All ideas and help gratefully appreciated.

Phil
  • Post #2
  • Quote
  • Aug 16, 2011 12:24pm Aug 16, 2011 12:24pm
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
Can not be sure from what you have shown but I suggest that you try
NormalizeDouble( whatevervalueOfLots, 2) when you send an order or modify it.

(or 1 in place of 2 if broker only accepts minilots)
If you think I'm mad, I must be mad
 
 
  • Post #3
  • Quote
  • Aug 16, 2011 6:12pm Aug 16, 2011 6:12pm
  •  dancingphil
  • Joined May 2009 | Status: Member | 667 Posts
Thanks, but the question is about how to calculate the "whatevervalueOfLots".

Eg, say I have 20 open trades, I need to have a Total_SellLots and a Total_BuyLots so that I can OrderSend one trade the makes Buy and Sell equal.

I can get the correct answers on the Chart as text, just not as a Lots value in the OrderSend()
 
 
  • Post #4
  • Quote
  • Aug 17, 2011 1:25am Aug 17, 2011 1:25am
  •  futurespec
  • Joined May 2011 | Status: If you think I'm mad, I must be mad | 1,058 Posts
Cannot really see what is wrong?

Are you trying to send a -ve value of lots??
(if total_selllots >= total_buylots )
If you think I'm mad, I must be mad
 
 
  • Post #5
  • Quote
  • Aug 17, 2011 7:11am Aug 17, 2011 7:11am
  •  tigpips
  • | Joined May 2011 | Status: Member | 108 Posts
You can try adding this at the end:

PHP Code
 if(total_buylots<total_selllots)

{
   
lotdifference = total_selllots - total_buylots; //Check difference in lots, if buylots is smaller then we will buy
   
t = OrderSend(Symbol(),OP_BUY,lotdifference,Ask,Slippage,0,0,NULL,Magic,0,Blue);
   if (
t > 1){
   if (
OrderSelect(t, SELECT_BY_TICKET))OrderModify(OrderTicket(),OrderOpenPrice(),Ask - Stoploss*Point,Ask + Takeprofit*Point,0,Blue ); 
   else Print(
"OrderModify() ticket=",t," failed with error code ",GetLastError());
   }
   else Print(
"OrderSend ticket=",t," failed with error code ",GetLastError()); 

}
if(
total_buylots>total_selllots)
{
   
lotdifference = total_buylots - total_selllots; //Check difference in lots, if buylots is smaller then we will sell
   
t = OrderSend(Symbol(),OP_SELL,lotdifference,Bid,Slippage,0,0,NULL,Magic,0,Red);
   if (
t > 1){
   if (
OrderSelect(t, SELECT_BY_TICKET))OrderModify(OrderTicket(),OrderOpenPrice(),Bid + Stoploss*Point,Bid - Takeprofit*Point,0,Red ); 
   else Print(
"OrderModify() ticket=",t," failed with error code ",GetLastError());
   }
   else Print(
"OrderSend ticket=",t," failed with error code ",GetLastError());    
} 
Attached is the complete version of it.
Attached File(s)
File Type: mq4 Balance Hedge Buy Sell Lots.mq4   4 KB | 476 downloads
 
 
  • Post #6
  • Quote
  • Aug 19, 2011 1:38am Aug 19, 2011 1:38am
  •  dancingphil
  • Joined May 2009 | Status: Member | 667 Posts
@ Tigpips ... I love it - thanks heaps for that!

Phil
 
 
  • Post #7
  • Quote
  • Last Post: Sep 16, 2011 11:12am Sep 16, 2011 11:12am
  •  dancingphil
  • Joined May 2009 | Status: Member | 667 Posts
Again there must be something stupid I'm doing wrong with my code.

This works fine with OP_BUY and OP_SELL balance trades. But it goes crazy and opens many orders when I change the order type to OP_BUYSTOP and OP_SELLSTOP

I've taken Tigpips great help above in his lots balancing EA, and added triggers for when I want this to happen.

Basically, it is to open a OP_BUYSTOP order whatever buy lots are needed to equal the sell lots when a UpFractal is formed at just above the UpFractal price.

And the same in reverse on the sell side.

It works fine, except that it opens many such pending orders endlessly.

How to make it open only the one such order? I think it is opening many because it can still see the lots imbalance - as it's not counting pending orders in the lots count.

As always, any help gratefully appreciated.
Attached File(s)
File Type: mq4 Balance Hedge Buy Sell Lots.mq4   5 KB | 575 downloads
 
 
  • Platform Tech
  • /
  • How to make 1 trade so sell lots = buy lots?
  • Reply to Thread
0 traders viewing now
Top of Page
  • Facebook
  • Twitter
About FF
  • Mission
  • Products
  • User Guide
  • Media Kit
  • Blog
  • Contact
FF Products
  • Forums
  • Trades
  • Calendar
  • News
  • Market
  • Brokers
  • Trade Explorer
FF Website
  • Homepage
  • Search
  • Members
  • Report a Bug
Follow FF
  • Facebook
  • Twitter

FF Sister Sites:

  • Metals Mine
  • Energy EXCH
  • Crypto Craft

Forex Factory® is a brand of Fair Economy, Inc.

Terms of Service / ©2023