Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 356
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Thank you, but I would like a precise solution. So that the current bar is not counted, but only the previous bars are counted.
Thank you, but I would like a precise solution. So that the current bar is not taken into account, but only previous bars are taken into account.
Damn, I've dug into someone else's code, again at a glance try to change this piece. In this case probably PRICE_CLOSE or 0 should be...
Here's the code, changed the shifts from 0 to 1. Will that help?
//============================================================================================
//
//
//
//
//
//============================================================================================
inttern MA1_Period=7; // 1st MA period
extern int intMA2_Period=13; // Period of the 2nd MA
extern int MA1_Method=1; // MA1 calculation method (SMA=0,EMA=1,SMMA=2,LWMA=3)
extern int MA2_Method=1; // MA2 calculation method (SMA=0,EMA=1,SMMA=2,LWMA=3)
extern int MA1_Price=0; // MA1 price calculation method
extern int MA2_Price=0; // MA2 price calculation method
extern int MA1_Shift=1; // MA1 time shift
extern int MA2_Shift=1; // MA2 time shift
extern double Lot = 0.01; // Fixed lot
extern int slippage = 0; // Price deviation for market orders
int New_Bar; // 0/1 New bar forming
int Time_0; // Start time of a new bar
int PosClose; // Crossing direction
int total; // Number of open orders
double MA1_0; // Current value of the 1st МА (pink)
double MA1_1; // Previous value of the 1st MA (pink)
double MA2_0; // Current value of the 2nd MA (blue)
double MA2_1; // Previous value of the 2nd MA (blue)
int orderBuy; // 1 = fact of presence of the Buy order
int orderSell; // 1 = fact of presence of Sell order
//============================================================================================
int init()
{
}
//============================================================================================
int start()
{
double price;
int total=OrdersTotal(); // total orders
for(int i=total-1;i>=0;i--)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true) // Select order
{
if(OrderType()==OP_BUY) // If a Buy order
{
orderBuy=1;
if(CrossPositionClose()==1) // Close the order if it satisfies
{ // condition CrossPositionClose()=1
price=MarketInfo(Symbol(),MODE_BID);
OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
}
}
if(OrderType()==OP_SELL) // If a Buy order is being placed
{
orderSell=1;
if(CrossPositionClose()==2) // Close the order if it satisfies
{ // condition CrossPositionClose()=2
price=MarketInfo(Symbol(),MODE_ASK);
OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
}
}
}
}
New_Bar=0; // Let's zeroize it first
if (Time_0 != Time[0]) // If the bar start time is already different
{
New_Bar= 1; // Here is a new bar
Time_0 = Time[0]; // Remember the new bar start time
}
MA1_0=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 0); // Current value of the 1st MA
MA1_1=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 1); // Previous value of the 1st MA
MA2_0=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 0); // Current value of the 2nd MA
MA2_1=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 1); // Previous value of the 2nd MA
return;
}
//============================================================================================
int CrossPositionClose()
{
PosClose=0; // this is where the dog is buried!!:)
if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0)) // CrossPosition Close {
{
PosClose=1;
}
if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0)) // Downwards intersection
{
PosClose=2;
}
return(PosClose); // return the intersection direction.
}
//============================================================================================
//============================================================================================
int MAMethod(int MA_Method)
{
switch(MA_Method)
{
case 0: return(0); //return MODE_SMA=0
case 1: return(1); // return MODE_EMA=1
case 2: return(2); // Return MODE_SMMA=2
case 3: return(3); //return MODE_LWMA=3
}
}
//============================================================================================
int MAPrice(int MA_Price)
{
switch(MA_Price)
{
case 0: return(PRICE_CLOSE); // Return PRICE_CLOSE=0
case 1: return(PRICE_OPEN); //return PRICE_OPEN=1
case 2: return(PRICE_HIGH); //return PRICE_HIGH=2
case 3: return(PRICE_LOW); //return PRICE_LOW=3
case 4: return(PRICE_MEDIAN); //return PRICE_MEDIAN=4
case 5: return(PRICE_TYPICAL); //return PRICE_TYPICAL=5
case 6: return(PRICE_WEIGHTED); //return PRICE_WEIGHTED=6
}
}
//============================================================================================
Sepulca, I already tried that, the compiler swears.
It can't be, I'll try it in mine now......
Sepulca, thanks, your code works. Only I've put the shifts back in their place.
I will check it on longer timeframes, I will torture it for a few days to be sure this code works.
picture for example ...
help with code condition ...................................................................................