Lavoro terminato
Specifiche
If the price touches the trendline, RSI >70 then Sell, otherwise RSI <30 then Buy.
--
Sample code
//+------------------------------------------------------------------+
//| Expert Advisor using ZigZag and drawing Trendlines |
//+------------------------------------------------------------------+
#property strict
#include <Trade\Trade.mqh>
input int ZigZagDepth=12;
input int ZigZagDeviation=5;
input int ZigZagBackstep=3;
input double LotSize=0.1;
input double StopLoss=50;
input double TakeProfit=100;
CTrade trade;
// ZigZag indicator
int zigzagHandle;
double zigzagBuffer[];
MqlRates PriceData[];
// Initialization function
int OnInit()
{
// Load ZigZag indicator
zigzagHandle = iCustom(Symbol(), PERIOD_CURRENT, "Examples\ZigZag", ZigZagDepth, ZigZagDeviation, ZigZagBackstep);
if(zigzagHandle == INVALID_HANDLE)
{
Print("Error loading ZigZag indicator");
return INIT_FAILED;
}
return INIT_SUCCEEDED;
}
// Function to check if the trendline crosses the price
bool IsTrendlineCrossPrice(datetime time1, double price1, datetime time2, double price2)
{
if(CopyRates(Symbol(), PERIOD_CURRENT, 0, 100, PriceData) <= 0)
return false;
int startIndex = iBarShift(Symbol(), PERIOD_CURRENT, time1, false);
int endIndex = iBarShift(Symbol(), PERIOD_CURRENT, time2, false);
for(int i = startIndex; i >= endIndex; i--)
{
double high = PriceData[i].high;
double low = PriceData[i].low;
double trendPrice = price1 + (price2 - price1) * ((PriceData[i].time - time1) / (double)(time2 - time1));
if(high >= trendPrice && low <= trendPrice)
{
return true; // Trendline crosses the price
}
}
return false;
}
// Main function
void OnTick()
{
if(CopyBuffer(zigzagHandle, 0, 0, 100, zigzagBuffer) <= 0)
{
Print("Failed to copy data from ZigZag");
return;
}
if(CopyRates(Symbol(), PERIOD_CURRENT, 0, 100, PriceData) <= 0)
{
Print("Failed to retrieve price data");
return;
}
int lastHighIndex = -1;
int lastLowIndex = -1;
double lastHigh = 0;
double lastLow = 0;
for(int i = 99; i >= 0; i--)
{
if(zigzagBuffer[i] > 0)
{
if(lastHighIndex == -1 && zigzagBuffer[i] > lastLow)
{
lastHigh = zigzagBuffer[i];
lastHighIndex = i;
}
else if(lastLowIndex == -1 && zigzagBuffer[i] < lastHigh)
{
lastLow = zigzagBuffer[i];
lastLowIndex = i;
}
}
}
if(lastHighIndex != -1 && lastLowIndex != -1)
{
datetime time1 = PriceData[lastHighIndex].time;
datetime time2 = PriceData[lastLowIndex].time;
if(!IsTrendlineCrossPrice(time1, lastHigh, time2, lastLow))
{
// Delete old trendlines
ObjectDelete(0, "Trendline");
// Draw trendline
ObjectCreate(0, "Trendline", OBJ_TREND, 0, time1, lastHigh, time2, lastLow);
ObjectSetInteger(0, "Trendline", OBJPROP_RAY_RIGHT, true);
ObjectSetInteger(0, "Trendline", OBJPROP_COLOR, clrRed);
// Get Bid/Ask prices
double Ask = SymbolInfoDouble(Symbol(), SYMBOL_ASK);
double Bid = SymbolInfoDouble(Symbol(), SYMBOL_BID);
// Check trading conditions
if(Ask > lastHigh) // Buy when price breaks ZigZag high
{
trade.PositionOpen(Symbol(), ORDER_TYPE_BUY, LotSize, Ask, StopLoss * _Point, TakeProfit * _Point, "Buy Order");
Print("Buy at price: ", Ask);
}
else if(Bid < lastLow) // Sell when price breaks ZigZag low
{
trade.PositionOpen(Symbol(), ORDER_TYPE_SELL, LotSize, Bid, StopLoss * _Point, TakeProfit * _Point, "Sell Order");
Print("Sell at price: ", Bid);
}
}
}
}