How to use a horizontal line as a trigger for buying and selling?

Avatar
  • updated

Friends, I'm trying to develop a very simple code (for those who understand the language), but unfortunately, I haven't been successful.

I have no programming knowledge, and I tried to create this code with the help of ChatGPT, but it didn't work.

I would greatly appreciate it if someone could help me with this.

The idea of the code is as follows:

I have two horizontal lines plotted on the chart, one line for buying and another line for selling.

The condition for buying is as follows: When the market touches the horizontal line from top to bottom, at the moment of the touch, it will place a market order 30 points above the line touch. So, when the market touches the line and the price reverses, it will go long, 30 points above.

The same logic applies to the selling line, meaning: When the market is coming from bottom to top, it will touch the selling line, and immediately a market order will be placed 30 points below the line touch.

I also wanted to define a safety stop loss of 300 points as soon as the order is executed, both for buying and selling, and execute it with 5 contracts.

So the idea is to have these lines plotted and be able to freely drag them during the trading period to automate my decision-making when I find an interesting entry point.

The code that ChatGPT generated for me is as follows: Didn't work)
-----------------------

#include

void OnTick()
{
double buyLinePrice = ObjectGetDouble(0, "BuyLine", OBJPROP_PRICE1);
double sellLinePrice = ObjectGetDouble(0, "SellLine", OBJPROP_PRICE1);
double currentPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);

if (currentPrice <= buyLinePrice && currentPrice > buyLinePrice - 30)
{
int ticket = OrderSend(_Symbol, OP_BUY, 5, currentPrice + 30 * SymbolInfoDouble(_Symbol, SYMBOL_POINT), 0, 0, 0, "Buy Order", 0, 0, CLR_NONE);

if (ticket > 0)
{
double stopLossPrice = currentPrice - 300 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
bool stopLossSet = OrderSetStopLoss(ticket, stopLossPrice);
if (!stopLossSet)
{
Print("Failed to set stop loss for buy order: ", ticket);
}
}
else
{
Print("Failed to send buy order");
}
}
else if (currentPrice >= sellLinePrice && currentPrice < sellLinePrice + 30)
{
int ticket = OrderSend(_Symbol, OP_SELL, 5, currentPrice - 30 * SymbolInfoDouble(_Symbol, SYMBOL_POINT), 0, 0, 0, "Sell Order", 0, 0, CLR_NONE);

if (ticket > 0)
{
double stopLossPrice = currentPrice + 300 * SymbolInfoDouble(_Symbol, SYMBOL_POINT);
bool stopLossSet = OrderSetStopLoss(ticket, stopLossPrice);
if (!stopLossSet)
{
Print("Failed to set stop loss for sell order: ", ticket);
}
}
else
{
Print("Failed to send sell order");
}
}
}

void OnStart()
{
ObjectCreate(0, "BuyLine", OBJ_HLINE, 0, 0, 0);
ObjectSetDouble(0, "BuyLine", OBJPROP_PRICE1, /* insert price buy order*/);

ObjectCreate(0, "SellLine", OBJ_HLINE, 0, 0, 0);
ObjectSetDouble(0, "SellLine", OBJPROP_PRICE1, /* insert price sell order */);
}

Avatar
Ulises Cune

You could make it place an order with a particular "Magic ID", for example 3040 and then control the orders with CP using the same "Magic ID", like placing a second order, for example at 30 points

Avatar
Ulises Cune

Review this code, do what you are needing.

https://www.mql5.com/en/code/20178