When you post code please use the Code button (Alt+S) !
Improperly formatted code removed by moderator. Please EDIT your post and use the CODE button (Alt-S) when inserting code.
Hover your mouse over your post and select "edit" ...
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
I am trying to do a script that trails a market by 500 points and when it reverses and break out then activate a trade but just cant get the code to work.
Can someone please help me
void OnTick()
{
static double lastHigh = 0.0;
static double lastLow = 0.0;
static double trailingStop = 0.0;
static bool reversed = false;
double currentHigh, currentLow;
double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT);
double reverseLevel = 500 * point;
if (!CopyHigh(_Symbol, 0, 1, 1, currentHigh) || !CopyLow(_Symbol, 0, 1, 1, currentLow))
{
Print("Error copying high/low prices");
return;
}
if (!reversed)
{
if (lastHigh == 0.0 || currentHigh > lastHigh)
{
lastHigh = currentHigh;
trailingStop = lastHigh - reverseLevel;
}
if (lastLow == 0.0 || currentLow < lastLow)
{
lastLow = currentLow;
trailingStop = lastLow + reverseLevel;
}
double currentPrice = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID), Digits);
double stopLoss = NormalizeDouble(trailingStop, Digits);
if (currentPrice >= stopLoss)
{
// Place reverse trade logic here
// For example:
// OrderSend(Symbol(), OP_BUY, 1.0, currentPrice, 3, 0, 0, "Reverse Trade", 0, 0, Green);
// Reset variables
lastHigh = 0.0;
lastLow = 0.0;
trailingStop = 0.0;
reversed = true;
}
}
else
{
// Add logic for managing the reverse trade
// This could include setting a take profit or trailing stop for the reverse trade
}
}