How do you do this: if (marketposition == long)?? anyone?

 

For example, what I'd like to type is something like... if (marketposition == long) {}

How do you do this? Anyone?

 

if (ordertype == op_buy)

you may need to perform an order search first. 

 
ubzen:

if (ordertype == op_buy)

you may need to perform an order search first.


Thanks, but... I don't just want to know it for a single order, I want to know it for the whole account.

Aka, I want to know if my account is flat/short/long in the EUR/USD. How would I do that?

 
If you're placing multiple orders with longs and shorts then you'll have to decide. For example do you want the account position to be seen as bullish if you have more long orders than short. Or do you, if your volume is leaning toward long. Either way you'll need to loop through all your open orders using OrderSelect... and If(Lot*Takeprofit) ie Longs are > if(Lots*Takeprofit) Shorts. Or if(ordertype == op_buy) Long_Position++ (increments) and if(ordertype == op_sell) Short_Position++. If (Long_Position > Short_Position) then Account Position=Long.
 

Anyone got a simple function to loop through all open orders on the current symbol and return 0 if flat, 1 if long, 2 if short, etc?

Aka... (NOTE: The code below is fake, just to show what I'm sort-of wanting, I guess?)

Can anyone make the code below work for me? Thanks :)

int MarketPosition(??)

{

??? grab_orders = GrabAllMyOpenOrders(??);

int OverallPosition = 0;

for (loop till done??)

{

ThisOrder = looptilldone??;

If (Thisorder == op_buy) OverallPosition++;

If (Thisorder == op_sell) OverallPosition--;

}

if (OverallPosition > 0) return 1;

if (OverallPosition < 0) return 2;

return 0;

}
 

I haven't tested it, but this should get the job done...

Drop it at the end of your code outside of start() and then inside start call it and pass the magic number to it.

hth

V

bool NetPosition(int magic)
   {
   int count,type;
   bool  bullish=false;
   for( int i=OrdersTotal()-1; i>=0; i--)                                               
      {
      if (OrderSelect(i,SELECT_BY_POS)==true)                           
         {                                                                  
         type=OrderType();
         if (OrderSymbol()!=Symbol()||type>1 || OrderMagicNumber()!=magic)
            {
            continue;                                        
            }
            
         if (type==0)
            {
            count++;
            }
         else
            {
            count--;
            }
          }
       }    
   if (count>0)
      {
      bullish=true;
      }

   return(bullish);
   }      
 

BH

As ever this is OTTOMH

int NetPositionForMNandPair(int iMN, string sSymbol)
{ 
 // Returns 0 for flat; 1 for net long; 2 for net short 
 // Call as e.g.  NetPositionForMNandPair(MagicNumber, Symbol());
 
 int icnt, itotal, retval, iBuy, iSell;

 retval=0;
 iBuy=0;
 iSell=0;

 itotal=OrdersTotal();

   for(icnt=0;icnt<itotal;icnt++)
     {
      OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
       // check for opened position, symbol & MagicNumber
       if(OrderMagicNumber() == iMN)
        if (OrderSymbol() ==sSymbol)
         {
          if (OrderType() == OP_SELL) iSell++;
           
          if (OrderType() == OP_BUY) iBuy++;
         }
     }

  if (iBuy > iSell) retval=1;
  if (iBuy < iSell) retval=2;

 return(retval);
}

Good Luck

-BB-

 

Hmm, on reflection, I prefer BB's code. Mine doesn't deal with flat.

V

 
-BB- You're killing us with these short-hand lango... OTTOMH  (Off-the-top-of-my-head). Keeping up is getting harder than coding :)
 
ubzen:
-BB- You're killing us with these short-hand lango... OTTOMH (Off-the-top-of-my-head). Keeping up is getting harder than coding :)


This is nothing - try keeping with my kids on Facebook/Runescape/Twitter/MSN etc...

Its a whole new alphabet...

At least I try and use mainstream (does that mean old) acronyms!

-BB-

 

Thanks everyone for the comments.

Now, I'm wondering... I'd like to... (all on the same symbol)

Step 1: buy 1 contract.

Step 2: buy 1 more contract.

Step 3: buy 1 more contract.

Step 4: sell 1 contract, and have it close all those buys as well, so that now my overall position in the market is "1 short".

Can I call OrderCloseBy() multiple times in order to close multiple buy orders with a single sell order?

Reason: