#04 Trying out "Nanchatte EA"
October 13, 2025: Note
I have finally ported external historical data to MT4, so I'm going to test whether the EA will run.
Since it just needs to move, I’ll use a very rough, improvised logic.
Pseud EA – Logic (lol)
0.
Capital: 100,000 yen. Order size: 0.01 lots. Period: 2024-10-05 to 2025-10-04
1.
Sell short at the opening price of the most recent bullish candle that is closest in time to now (hereafter called “target”).
Record the target’s lowest and highest prices.
2.
Do not open a new position until the current one is closed.
3.
If you hold a position, close when the target reaches its low or high.
4.
If the above “3.” is not hit, when the current candle finishes, close at the open price of the next candle.
“4.” should really be: close at 23:59:50 if none of the above is hit, but I only have daily data now, so I gave up.
This is only a test to see if the EA runs...
※ Why would I want to “close at 23:59:50”?
Because if markets gap on Monday, it would cause massive losses.
Well, this time it’s fine (laugh)
Anyway, I’ll paste the code for now.
#property strict
#property copyright "Ore"
#property version "1.00"
#property description "Test_EA"
//--- External variables ---
extern double Lots = 0.01; // Position size
//--- Global variables ---
double target_open;
double target_low;
double target_close;
//--- Variable to check for a new bar formation
datetime last_bar_time;
//+------------------------------------------------------------------+
//| Expert Advisor initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
last_bar_time = iTime(Symbol(), PERIOD_CURRENT, 0);
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert Advisor termination function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
}
//+------------------------------------------------------------------+
//| Processing when a new tick arrives |
//+------------------------------------------------------------------+
void OnTick()
{
//--- Logic to close when a new bar is confirmed ---
if(last_bar_time != iTime(Symbol(), PERIOD_CURRENT, 0))
{
last_bar_time = iTime(Symbol(), PERIOD_CURRENT, 0);
//--- Close all positions
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS) == true)
{
if(OrderClose(OrderTicket(), OrderLots(), Bid, 3, Blue) == true)
{
Print("Because a new bar was formed, the position was closed.");
}
}
}
}
//--- Entry logic when there are no positions ---
if(OrdersTotal() == 0)
{
//--- Find the most recent bullish candle
for(int i = 1; i < 1000; i++) {
if(Open[i] < Close[i]) // Bullish candle found {
target_open = Open[i];
target_low = Low[i];
target_close = Close[i];
//--- Place a sell limit order at the opening price ---
if(Open[0] > target_open)
{
int ticket = OrderSend(Symbol(), OP_SELLLIMIT, Lots, target_open, 3, 0, 0, "MyEA", 0, 0, Red);
if(ticket > 0)
{
Print("Sell limit order placed. Price:", target_open);
}
} Return; // Exit loop after finding the first bullish candle
}
}
}
//--- Profit-taking / stop-loss logic when holding a position ---
else
{
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS) == true)
{
if(Bid <= target_low || Bid >= target_close)
{
if(OrderClose(OrderTicket(), OrderLOTS(), Bid, 3, Blue) == true)
{
Print("Position closed. Price:", Bid);
}
}
}
}
}
==
→ Next, in MT4's MetaEditor, create a new Expert Advisor and paste the above code.
→ Click the "Compile" button to ensure there are no errors.
→ In MT4, select this EA in the Strategy Tester and run a backtest.
It ran successfully! ヽ(;▽;)ノ
× ![]()