Look at the below example: When Price actions touched the top TMA line I would want a "SELL" position to be open with SL to be set at the lower TMA line. Or if Price action touched the lower TMA line then a "BUY" position to be open with SL to be set at the top TMA line. I have included some code that was generated by a computer to make the above work. I don't know if you can use it but I have included it if you can make sense of the logic.
I have also included the FastTrueTMALine indicator as well....
Thanks....
Code: Select all
//
// Constants for crossing types...
//
static int Parallel=0;
static int SameLine=1;
static int InBounds=3;
static int OutBounds=4;
bool runAction0=false;
//
double lastPrice1_Condition0;
double lastPrice2_Condition0;
datetime lastTime1_Condition0;
datetime lastTime2_Condition0;
//
// Entry function. Each quote call it.
//
int start()
{
if(Condition0())
{
// Run actions...
// Action should run only once
if(!runAction0)
{
SubmitOrder("EUR/USD", 10001, 1.0, 1.31298, 1.31298, 10008, 10031, "48171", "LocalT", D'9999.12.12', "", "", 0.0, 0.0, -1.0, -2, 0, false);
runAction0=true;
};
Alert("buy lower line touched");
}
};
//
// EUR/USD:LocalT cross M5 TMA bands (50)
//
bool Condition0()
{
//
// To check whether object have crossed use geometry.
// X - time scale, Y - price scale
// Compare values on current time and last time...
// First line points
datetime curTime1;
double curPrice1;
curTime1 = TimeCurrent();
int fieldType1 = GetFieldType("EUR/USD:LocalT");
curPrice1 = GetPrice("EUR/USD:LocalT",5,0,1,fieldType1);
// Second line points
datetime curTime2;
double curPrice2;
curTime2 = TimeCurrent();
curPrice2 = iCustom("FastTrueTMALine","EUR/USD:LocalT",5,0,0,5,50,0,2,100,True,0.4,False,False,False,False,False,False,5000,14053594,255,32768);
// It is first comparing - skip (we have only one point)
if (lastPrice1_Condition0 == -1.0)
{
// ...but remember values to have next two points
lastPrice1_Condition0 = curPrice1;
lastPrice2_Condition0 = curPrice2;
lastTime1_Condition0 = curTime1;
lastTime2_Condition0 = curTime2;
return (false);
};
// Check crossing
int crossResult = 0;
// Point, where lines crossed
double crossX;
double crossY;
// Calculate cross point
crossResult = CrossLine(
// first object line
lastTime1_Condition0, lastPrice1_Condition0, curTime1, curPrice1,
// second object line
lastTime2_Condition0, lastPrice2_Condition0, curTime2, curPrice2,
crossX,
crossY);
// Remember last values
lastPrice1_Condition0 = curPrice1;
lastPrice2_Condition0 = curPrice2;
lastTime1_Condition0 = curTime1;
lastTime2_Condition0 = curTime2;
// Analize result
if (crossResult == InBounds)
return (true)
else
return (false);
};
//
// Determine point of crossing
//
int CrossLine(double line1_x1,double line1_y1,double line1_x2,double line1_y2,double line2_x1,double line2_y1,double line2_x2,double line2_y2,double& crossX, double& crossY)
{
double Z;
double Ca;
double Cb;
Z = (line1_y2 - line1_y1) * (line2_x1 - line2_x2) - (line2_y1 - line2_y2) * (line1_x2 - line1_x1);
Ca = (line1_y2 - line1_y1) * (line2_x1 - line1_x1) - (line2_y1 - line1_y1) * (line1_x2 - line1_x1);
Cb = (line2_y1 - line1_y1) * (line2_x1 - line2_x2) - (line2_y1 - line2_y2) * (line2_x1 - line1_x1);
// Same Line
if ((Z == 0) && (Ca == 0) && (Cb == 0.0))
return (SameLine);
// Parallel
if (Z==0.0)
return (Parallel);
double Ua;
double Ub;
Ua = Ca / Z;
Ub = Cb / Z;
// Calculate crossing point
crossX=(line1_x1 + (line1_x2 - line1_x1) * Ub);
crossY= (line1_y1 + (line1_y2 - line1_y1) * Ub);
// In or On bounds
if (
((Ua >= 0.0)
&& (Ua <= 1.0)
&& (Ub >= 0.0)
&& (Ub <= 1.0))
)
{
return (InBounds);
}
// out of bounds
else
return (OutBounds);
};