how to code for : trade ONLY on a close of the current candle
You don't know when the candle is going to close until it opens a new one.
So you trade on the first tick of a new candle, that should achieve the same thing (or as close as you can get).
if (Volume[0]==1) ...
You don't know when the candle is going to close until it opens a new one.
So you trade on the first tick of a new candle, that should achieve the same thing (or as close as you can get).
if (Volume[0]==1) ...
thank you for your reply. do Iinsert that into my trading criteria or somewhere else...in other words do I put it in like this...:
if(ptma < pfma && tma > fma && cci > 0 && stochmain > 20 && ready == 0 && Volume[0] ==1 )
{ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-7*Point,Ask+5*Point,0,0,0,Green);
}
or do I place the volume if statement seperate to the trading criteria?
sorry for the newbie question, but I started learning to code only 3 weeks ago by reading Anotov Oleg's tutorials, they are good but he leaves alot out.
thanx
I have added it like this for now and am testing to see if this is the right way to use it.
if(Open[0]> hma && ptma < pfma && tma > fma && cci > 0 && adxdiplus > adxdiminus && stochmain > 20 && ready == 0)
{if(Volume[0]==1)
{
ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,0,magic,0,Green);
if(ticket>0)
thank you for your reply. do Iinsert that into my trading criteria or somewhere else...in other words do I put it in like this...:
if(ptma < pfma && tma > fma && cci > 0 && stochmain > 20 && ready == 0 && Volume[0] ==1 )
{ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-7*Point,Ask+5*Point,0,0,0,Green);
}
or do I place the volume if statement seperate to the trading criteria?
sorry for the newbie question, but I started learning to code only 3 weeks ago by reading Anotov Oleg's tutorials, they are good but he leaves alot out.
thanx
thanks to all for your replies, I really appreciate it as I really want to become a good coder...
I have another question that may sound stupid but could someone please tell me why this ea I wrote as practice will not place sell trades? it only goes long, I think its a problem with my code but I cant seem to identify the problem...
thanks in advance for the help.
here is the code...
{
//----
int rsi,prsi,ticket;
int ready,sml,hma;
ready = OrdersTotal();
for(int a=0;a<100;a++)
{
rsi = iRSI(NULL,0,4,PRICE_CLOSE,a);
prsi = iRSI(NULL,0,4,PRICE_CLOSE,a+1);
sml = iStochastic(NULL,0,5,3,3,1,PRICE_CLOSE,MODE_MAIN,a);
hma = iMA(NULL,0,50,0,MODE_SMA,PRICE_CLOSE,a);
if(prsi<50 && rsi>50 && sml>20 && Close[1]>hma && ready == 0)
{
if(Volume[0]==1)
{
ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,0,0,0,Blue);
return(0);
}
else
if(prsi>50 && rsi<50 && sml<80 && Close[1]<hma && ready ==0)
{
if(Volume[0]==1)
{
ticket = OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,0,0,0,Red);
return(0);
}
}
}
}
//----
return(0);
}
use bars
TWO things:
Effeceincy - Test for tick first - this is more CPU efficinet why do lots of processing if it is not the right tick so:
if (Volume[0] ==1 )
{
// Test for trade
// if trade
ticket =
if (pma > 9)
{
Do some stuff
{
}else
{
// update MA values etc
}
TWO - indent your code to denote structure and flow. (a little as above)
NEW B REL,
thank you very much for your reply, I will try to apply what you have mentioned.
thanx again for the help!
Rory
NEW B REL,
thank you also for your good advice...if I understand you correctly I must code it something like this...
If(Volume==1)
{
if(ready==0)
{
if(prsi<50 && rsi>50 && sml>20 && Close[1]>hma)
{
ticket = OrderSend(Symbol(),OP_SELL,0.1,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,0,0,0,Red);return(0);
}
If this is not what you meant, then please correct me. thank you for your advice so far, I look forward to understanding how to fix this problem properly.
thanx again
Rory
SO I have done my best to impliment all that everyone has taught me, I think I have done it right as my EA makes trades as I wanted it to BUT I have a problem...
I have added a trailing stop and think I made a mistake somewhere, please can someone tell me what I did wrong here cos the trailing stop does not work...here is the code from my EA...
//+------------------------------------------------------------------+
//| Market Chaser.mq4 |
//| Rory Kruger |
//| |
//+------------------------------------------------------------------+
#property copyright "Rory Kruger"
#property link
//---- input parameters
extern double LotSize=0.1;
extern double StopLoss=100.0;
extern double TakeProfit=300.0;
extern double TrailingStop=50.0;
#define BTA 100
//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
//----
double sma; // Signal MA (10 SMA)
double mma; // Main MA (30 SMA)
double psma; // Previous candle Signal MA (10 SMA) value
double pmma; // Previous candle Main MA (30 SMA) value
double hma; // Filter EA = 100 SMA
double macdm; // Filter MACD Main Line = 10
double macds; // Filter MACD Signal Line =30
double pmacdm; // Previous candles MACD Main Line value for filtering close orders
double pmacds; // Previous candles MACD MSignal Line value for filtering close orders
int cct,ticket,total; // cct = close current trade variable, ticket = OrderSend variable, total = OrdersTotal variable
if (Bars<100) // Conditions of history check
{
Alert("Not enough history to analyze");
return(0);
}
if(TakeProfit<10) // Broker stipulated MINIMUM Take Profit level
{
Alert("Take Profit too small");
return(0);
}
sma = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0);
psma = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1);
mma = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,0);
pmma = iMA(NULL,0,30,0,MODE_SMA,PRICE_CLOSE,1);
hma = iMA(NULL,0,200,0,MODE_SMA,PRICE_CLOSE,0);
macdm = iMACD(Symbol(),0,10,30,8,1,MODE_MAIN,0);
macds = iMACD(Symbol(),0,10,30,8,1,MODE_SIGNAL,0);
pmacdm = iMACD(Symbol(),0,10,30,8,1,MODE_MAIN,1);
pmacds = iMACD(Symbol(),0,10,30,8,1,MODE_SIGNAL,1);
total = OrdersTotal(); // Total currently open orders
if(Volume[0]==1) // To prevent the EA trading multiple positions on the same candle
{
if(total<1) // no open orders currently
{
if(AccountFreeMargin()<(1000*LotSize)) // To calculate if your accounts free margin is enough to open trades
{
Print("Not enough money to trade, free margin = ",AccountFreeMargin());
return(0);
}
if(psma<pmma && sma>mma && Open[0]>hma && macdm>0 && total == 0) // !!!BUY CRITERIA!!!
{
ticket=OrderSend(Symbol(),OP_BUY,LotSize,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,0,0,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
Print("BUY order opened",OrderOpenPrice());
}
else Print("Order FAILED :",GetLastError());
return(0);
}
else
if(Volume[0]==1)
{
if(psma>pmma && sma<mma && Open[0]<hma && macdm<0 && total == 0) // !!!SELL CRITERIA!!!
{
ticket=OrderSend(Symbol(),OP_SELL,LotSize,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,0,0,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) // To put a record of the trade into the journal
Print("SELL order opened",OrderOpenPrice());
}
else Print("Order FAILED :",GetLastError());
return(0);
}
for(cct=0;cct<total;cct++)
{
OrderSelect(Symbol(),SELECT_BY_POS,MODE_TRADES); // To determine the close order preferances
if(OrderType()<=OP_SELL&&OrderSymbol()==Symbol())
{
if(OrderType()==OP_BUY)
{
if(pmacdm<0) // If MACD Main Line goes UNDER the 0 line during an open BUY position
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet);
return(0);
}
if(TrailingStop>0) // Check if BUY Trailing Stop needs to be adjusted
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
else
{
if(pmacdm>0) // If MACD Main Line goes OVER the 0 line during an open SELL position
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet);
return(0);
}
if(TrailingStop>0) // Check if SELL Trailing Stop needs to be adjusted
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
return(0);
}
}
}
}
}
}
}
}
}
}
}
I do not mean to be lazy, I am very wiling to do the work neccessary to learn what I did wrong with the Trailing Stop, so even if someone can refer me to an article that will teach me to use trailing stops properly then I will be grateful.
Thanx
Rory
Hi,
Can anyone please tell me what code to add to my OrderSend criteria to make sure my EA trades ONLY on a CLOSE of the current candle?
eg:
if(ptma < pfma && tma > fma && cci > 0 && stochmain > 20 && ready == 0)
{
ticket = OrderSend(Symbol(),OP_BUY,0.1,Ask,3,Ask-7*Point,Ask+5*Point,0,0,0,Green);
}
here are my variables:
ptma = previous candles Moving Average value (20MA)
pfma = previous candles Moving Average value (50MA)
tma = current candles 20 MA
fma = current candles 50 MA.
ready = OrdersTotal()
The EA places many trades but I want it to trade only on the close of the current candle if the entry criteria is met.
thanx in advance for the help.
Rory