Order price execution
Posted: Jun 23 2024
Hi forum, I wrote a strategy that buy when price cross SMA and sell when price recross SMA, but can't understand why order are always executed on TOP or BOTTOM of bar and not on CROSS.
This prob with 15 min or 5 min chart is the same.
Thanks for help
MAX
This prob with 15 min or 5 min chart is the same.
Thanks for help
MAX
Code: Select all
// Parametri di input
Inputs:
LengthMALong(200), // Lunghezza del periodo per la media mobile lunga
LengthMAShort(50), // Lunghezza del periodo per la media mobile corta
ProfitTargetPercent(1.0); // Percentuale del target di profitto
// Variabili
Variables:
MALong(0),
MAShort(0),
EntryBarLow(0),
EntryPrice(0),
ProfitTarget(0),
CurrentTime(0);
// Calcola la media mobile semplice lunga e corta
MALong = Average(Close, LengthMALong);
MAShort = Average(Close, LengthMAShort);
// Inizializzazione della variabile CurrentTime
CurrentTime = Time;
// Filtro temporale: non operare tra le 00:01 e le 08:00
If CurrentTime > 0 and CurrentTime < 800 then
SetExitOnClose;
// Logica di entrata
If MarketPosition = 0 and (CurrentTime >= 800 or CurrentTime = 0) and MAShort < MALong then begin
If Close crosses over MALong then begin
Buy ("BuySignal") next bar at market;
EntryBarLow = Low[1]; // Registra il minimo della barra precedente
EntryPrice = Close; // Registra il prezzo di entrata
ProfitTarget = EntryPrice * (1 + ProfitTargetPercent / 100); // Calcola il target di profitto
end;
end;
// Gestione delle posizioni long
If MarketPosition = 1 then begin
// Se il prezzo scende sotto il minimo della barra precedente, chiudi la posizione long con stop loss
If Low <= EntryBarLow then begin
Sell ("StopLoss") next bar at market;
end;
// Se il prezzo raggiunge il target di profitto, chiudi la posizione long
If High >= ProfitTarget then begin
Sell ("ProfitTarget") next bar at market;
end;
// Se la SMA corta incrocia la SMA lunga verso il basso, chiudi la posizione long
If MAShort crosses under MALong then begin
Sell ("SellSignal") next bar at market;
end;
end;