
#property copyright "Copyright 2019, Example Inc."
#property link "https://www.example.com"
input int LotSize = 1;
input int Period = 30;
input double VolatilityThreshold = 0.1;
input int ExitAfterMinutes = 60;
int buyOrderId;
int sellOrderId;
datetime entryTime;
void OnTick()
{
// Get the last Period candlesticks
ArraySetAsSeries(candles, true);
CopyRates(Symbol(), PERIOD_M1, TimeCurrent() - Period, Period, candles);
// Calculate the maximum and minimum prices
double maxPrice = High(candles);
double minPrice = Low(candles);
// Calculate the standard deviation of the closing prices
double stdev = iStdDev(candles, MODE_CLOSE, 0);
// Check if the volatility is above the threshold
if (stdev > VolatilityThreshold)
{
// Check if the current ask price is higher than the maximum price
if (Ask > maxPrice)
{
// Place a buy order
if (OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, 0, 0, "My order", 16384, 0, Green))
{
Print("Buy order placed");
buyOrderId = OrderTicket();
entryTime = TimeCurrent();
}
else
{
Print("Error placing buy order : ", ErrorDescription(GetLastError()));
}
}
// Check if the current bid price is lower than the minimum price
if (Bid < minPrice)
{
// Place a sell order
if (OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, 0, 0, "My order", 16384, 0, Red))
{
Print("Sell order placed");
sellOrderId = OrderTicket();
entryTime = TimeCurrent();
}
else
{
Print("Error placing sell order : ", ErrorDescription(GetLastError()));
}
}
}
if (buyOrderId > 0)
{
if (TimeCurrent() - entryTime >= ExitAfterMinutes * 60)
{
if (OrderClose(buyOrderId, LotSize, Bid, 3, clrNONE))
{
Print("Buy order closed");
buyOrderId = 0;
}
else
{
Print("Error closing buy order : ", ErrorDescription(GetLastError()));
}
}
}
if (sellOrderId > 0)
{
if (TimeCurrent() - entryTime >= ExitAfterMinutes * 60)
{
if (OrderClose(sellOrderId, LotSize, Ask, 3, clrNONE))
{
Print("Sell order closed");
sellOrderId = 0;
}
else
{
Print("Error closing sell order : ", ErrorDescription(GetLastError()));
}
}
}
}
Settings =
{
Name = «DHLM»,
line =
{
{
Name = «High»,
Color = RGB(0,200,64),
Type = TYPET_BAR,
Width = 1
},
{
Name = «Low»,
Color = RGB(200,0,64),
Type = TYPET_BAR,
Width = 1
},
{
Name = «Median»,
Color = RGB(0,64,200),
Type = TYPET_BAR,
Width = 1
}
}
}
local hlm = {}
local math_max = math.max
local math_min = math.minfunction Init()
return #Settings.line
end
function OnCalculate(index)
local dt = T(index)if O(index) then
if dt.day ~= hlm.day or
dt.month ~= hlm.month or
dt.year ~= hlm.year then
hlm.year = dt.year
hlm.day = dt.day
hlm.month = dt.month
hlm.high = H(index)
hlm.low = L(index)
else
hlm.high = math_max(hlm.high,H(index))
hlm.low = math_min(hlm.low,L(index))
hlm.median = (hlm.high + hlm.low)/2
end
end
return hlm.high,hlm.low,hlm.median
end

// Scalping strategy for algotrading
// Define variables for strategy
double stop_loss = 0.5; // stop loss in percentage
double take_profit = 2; // take profit in percentage
// On every tick
void OnTick()
{
// Get the current bid and ask prices
double bid = Bid;
double ask = Ask;
// Get the previous bid and ask prices
double prev_bid = iBars(Symbol(), PERIOD_M1, 0);
double prev_ask = iBars(Symbol(), PERIOD_M1, 0);
// Check if the current bid price is higher than the previous ask price
if (bid > prev_ask)
{
// Open a long position with a stop loss and take profit
double lot_size = NormalizeDouble(AccountFreeMargin() * 0.01 / MarketInfo(Symbol(), MODE_STOPLEVEL), 2);
OrderSend(Symbol(), OP_BUY, lot_size, ask, 3, bid * (1 - stop_loss/100), bid * (1 + take_profit/100));
}
// Check if the current ask price is lower than the previous bid price
else if (ask < prev_bid)
{
// Open a short position with a stop loss and take profit
double lot_size = NormalizeDouble(AccountFreeMargin() * 0.01 / MarketInfo(Symbol(), MODE_STOPLEVEL), 2);
OrderSend(Symbol(), OP_SELL, lot_size, bid, 3, ask * (1 + stop_loss/100), ask * (1 - take_profit/100));
}
}