Shorting instead of selling

 

I can't seem to sell any of the items I am buying. If I run the following two lines I end up with a buy for 8 lots, then a short for 8 lots. I want to buy 8 then sell 8. Any help would be appreciated.


OrderSend (Symbol(),OP_BUY,8.0,Ask,0,0,0,"A real buy", 0, Blue);
OrderSend (Symbol(),OP_SELL,8.0,Bid,0,0,0,"A real sell", 0, Blue);

 

Hmm, I think you need to sort out the terminology. Do you mean, first you buy 8 lots, and then later, you want to close the order?

If so use OrderClose.

The words "sell" and "short" are the same thing... opening a new short order.

Opening a new sell order does not cancel your current buy orders because each time you open an order, you are borrowing more money from the broker (and maybe paying commission) so you position will continue to get larger.

 
JTWrenn:

I can't seem to sell any of the items I am buying. If I run the following two lines I end up with a buy for 8 lots, then a short for 8 lots. I want to buy 8 then sell 8. Any help would be appreciated.


OrderSend (Symbol(),OP_BUY,8.0,Ask,0,0,0,"A real buy", 0, Blue);
OrderSend (Symbol(),OP_SELL,8.0,Bid,0,0,0,"A real sell", 0, Blue);

Use OrderClose() as already mentioned . .. or OrderCloseBy() if you have the 2 Orders opened.
 
JTWrenn:

I can't seem to sell any of the items I am buying. If I run the following two lines I end up with a buy for 8 lots, then a short for 8 lots. I want to buy 8 then sell 8. Any help would be appreciated.

OrderSend (Symbol(),OP_BUY,8.0,Ask,0,0,0,"A real buy", 0, Blue);
OrderSend (Symbol(),OP_SELL,8.0,Bid,0,0,0,"A real sell", 0, Blue);

alladir:

Hmm, I think you need to sort out the terminology. . . .

@JTWrenn:

If you buy a position at the Ask price using OrderSend(), you exit all or a portion of that position by selling at the Bid price (a) when the Bid price becomes equal to or less than the stoploss price, (b) when the Bid price becomes equal to or greater than the takeprofit price, or (c) using OrderClose() (or the less common OrderCloseBy()).

If you sell a position at the Bid price using OrderSend(), you exit all or a portion of that position by buying at the Ask price (a) when the Ask price becomes equal to or greater than the stoploss price, (b) when the Ask price becomes equal to or less than the takeprofit price, or (c) using OrderClose() (or the less common OrderCloseBy()).

Remember that a FX transaction has two parts (or "legs"). If the first part is a buy, the second must be a sell; if the first part is a sell, the second must be a buy.

 

Thanks for the info all, I sorted out my problem and got it sell/closing now. Was getting confused on the different terms in program vs general terms when talking about things.


Now I am trying to figure out why my ima's are different when comparing two ima's vs what the line looks like. The numbers are all over the place and I can't quite figure it out. If anyone has any ideas on it I would greatly appreciate it. Think it is a period vs tick/pip issue but I am really not sure. It is fun being a noob:)


Thanks again

 
JTWrenn:

Thanks for the info all, I sorted out my problem and got it sell/closing now. Was getting confused on the different terms in program vs general terms when talking about things.


Now I am trying to figure out why my ima's are different when comparing two ima's vs what the line looks like. The numbers are all over the place and I can't quite figure it out. If anyone has any ideas on it I would greatly appreciate it. Think it is a period vs tick/pip issue but I am really not sure. It is fun being a noob:)

You will need to show your iMA() calls and a screen shot to illustrate what you mean . . . most likely you are doing something wrong, check the iMA() documentation carefully . . .
 
JTWrenn: Now I am trying to figure out why my ima's are different when comparing two ima's vs what the line looks like.
If the parameters don't match exactly, they'll be different. That's why I wrote/use my polyline code, to see the actual line, not what the unrelated indicator draws.
 

I started typing it all up then realized that the time on the graph is tied to the period I selected. So I can point to my ima on the graph and it will say it is at 5:30 but I may actually be pointing to 5:23 but I selected a 30M period. I think this is what was messing up my numbers when I was trying to compare it all.


Not sure but I think I will dig a bit more to make sure I am not just looking at it wrong before bugging you guys. Will take a look at that polyline code as well, but I think for right now it is over my head. Got a lot of learning to do.

 

Here is all my code though just in case anyone feels like telling me I am wrong on anything:) Greatly appreciate any advice especially on the slippage, stoploss, and take profit ideas.



//+------------------------------------------------------------------+
//| Learner.mq4 |
//| Copyright 2013, MetaQuotes Software Corp. |
//| https://www.metaquotes.net |
//+------------------------------------------------------------------+

extern int Slow_Period; // Slow Avg
extern int Slow_Method; // Type of Avg(SMA=0,EMA=1,SMMA=2,LWMA=3)
extern int Slow_Shift; // Time shift of avg
double Slow; // Stores current Slow moving average number
double SlowLast; // Stores Slow moving average from last tick
double SlowOld; // Stores the slow from 2 ticks before
int Slow_Price=0; // Type of price Applied

extern int Fast_Period; // Fast Avg
extern int Fast_Method; // Type of Avg(SMA=0,EMA=1,SMMA=2,LWMA=3)
extern int Fast_Shift; // Time shift of avg
double Fast; // Stores current fast moving average number
double FastLast; // Stores Fast moving average from last tick
double FastOld; // Stores the Fast from 2 ticks before
int Fast_Price=0; // Type of price Applied

double BTC; // Number of BTC to buy or sell
double Bought; // Price at buy

string lastbuyorsell="sell"; // Was the last action a buy or sell. Set to sell at start to ensure first action is buy.
int switchtimer=0; // Minimum time between choosing a buy or sell
int ticket=0; // Stores ticket number or -1 on failed buy/sell


//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
//----

//----
}
//+------------------------------------------------------------------+
//| expert deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
return(0);
}
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
if(IsOptimization() && VarOptimTest ==2) return(0);
//----
//ticket=0; // resets ticket to allow only 1 error per pass
Slow=iMA(NULL,0, Slow_Period, Slow_Shift,Slow_Method, Slow_Price, 0); // Pulls current slow avg
Fast=iMA(NULL,0, Fast_Period, Fast_Shift,Fast_Method, Fast_Price, 0); // Pulls current fast avg

SlowLast=iMA(NULL,0, Slow_Period, Slow_Shift,Slow_Method, Slow_Price, 1); // Pulls current slow avg from previous tick
FastLast=iMA(NULL,0, Fast_Period, Fast_Shift,Fast_Method, Fast_Price, 1); // Pulls current fast avg from previous tick

SlowOld=iMA(NULL,0, Slow_Period, Slow_Shift,Slow_Method, Slow_Price, 2); // Pulls current slow avg from previous tick
FastOld=iMA(NULL,0, Fast_Period, Fast_Shift,Fast_Method, Fast_Price, 2); // Pulls current fast avg from previous tick


if(switchtimer > 1000)
{ //if (((Slow<Fast && SlowLast>=Fast) || (Slow<Fast && SlowLast>Fast)) && (lastbuyorsell=="sell"))
if ((Slow<Fast) && (SlowLast<FastLast) && (SlowOld<FastOld) && (lastbuyorsell=="sell")) //if fast MA is above the slow and was for last 2 ticks buy
{
Print ("buy after waiting --",switchtimer,"slow --",Slow,"fast --",Fast);
BTC = NormalizeDouble(AccountBalance()/Ask,1);
lastbuyorsell="buy";
Bought=Ask;
ticket=OrderSend (Symbol(),OP_BUY,BTC,Ask,0,0,Ask*1.05,"A real buy", 0, Blue);
switchtimer=0;
} //if (((Fast<Slow && FastLast>=Slow) || (Fast<Slow && FastLast>Slow)) && (lastbuyorsell=="buy"))
else if ((Fast<Slow) && (FastLast<SlowLast) && (FastOld<SlowOld) && (lastbuyorsell=="buy")) // if fast MA is below slow and was for last 2 ticks sell
{
Print ("sell after waiting --",switchtimer,"slow --",Slow,"fast --",Fast);
lastbuyorsell="sell";
OrderClose(ticket,BTC,Bid,0,Red);
//OrderSend (Symbol(),OP_SELL,8.0,Bid,0,0,0,"A real sell", 0, Blue);
switchtimer=0;
}
}

switchtimer=switchtimer+1; //counts the loop and adds to the switchtimer to make sure I don't bounce back and forth

//if(ticket<0)
// {
// Print("OrderSend failed with error: ",GetLastError());
// return(0);
// }
}
//+------------------------------------------------------------------+

 

slippage is something I did a bit of studying on recently.. turns out for most of my brokers, it doesn't matter what figure you input.. it still accepts the order at any slippage.

Also, check if your broker is ECN. If so, you can't set stops in the initial OrderSend. You need to send the order then use OrderModify to set stops.

I'm not going to error check your code, but all in all, I would never risk money with code that fits on a page... it's sensible to have a lot of error checking at every stage, with a course of action for every possible thing that could go wrong.

Give it a go with a demo account!

 
JTWrenn:

Here is all my code though just in case anyone feels like telling me I am wrong on anything:) Greatly appreciate any advice especially on the slippage, stoploss, and take profit ideas.


<CODE REMOVED>

Please read some other posts before posting . . .

Please edit your post . . . please use the SRC button to post code: How to use the SRC button.

Reason: