Code:
/*[[
Name := Vérification de Backtest 2
Author := Syl20
Link :=
www.mataf.net
Notes := Error proof of purchase underneath a sale in an even bar...
Lots := 1.00
Stop Loss := 10
Take Profit := 20
Trailing Stop := 20
]]*/
var: Ordre(0), TrailStop(0), CloseTime(0);
var: cnt(0);
var: UT (0);// times unity of the motto
UT = period;
TrailStop = TrailingStop*Point;
// --------------------------------------
// Exclusion conditions
// --------------------------------------
If Bars < 100 then Exit;
If FreeMargin < Equity/10 then Exit;
If TotalTrades > 0 then // Takes some notes the hour of the last trade
{
CloseTime = CurTime;
};
// ------------------
// Order passage
// ------------------
If TotalTrades < 1 then // Verify if it there already has a trade
{
//Solution
If CurTime-CloseTime > UT*60 and CurTime-LastTradeTime > UT*60 then // Verify if it there has a trade in the bar.
SetOrder(OP_BUYSTOP,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,Lime); // Buy
Exit;
};
// -------------------------------
// Movements of the Trailing Stops
// -------------------------------
For cnt=1 to TotalTrades
{
Ordre=OrderValue(cnt,VAL_TYPE);
If OrderValue(cnt,VAL_SYMBOL)=Symbol and CurTime-LastTradeTime> UT*60 then // One verifies if one modifies the good order and if the last bar is finished.
If OrderValue(cnt,VAL_TYPE)>OP_BUY then
{
If OrderValue(cnt,VAL_STOPLOSS)>Bid then
{
DeleteOrder(OrderValue(cnt,VAL_TICKET),Orange);
Exit;
}
}
// ---------------------
// Trailing orders LONG
// ---------------------
If Ordre=OP_BUY then
{
If (close[1]-OrderValue(cnt,VAL_OPENPRICE)) > TrailStop then //vérifie si la fermeture est plus grande que l'achat.
{
If OrderValue(cnt,VAL_STOPLOSS) < (close[1]-TrailStop) then // Verify if the new stop will be higher than the last one.
{
ModifyOrder(OrderValue(cnt,VAL_TICKET),OrderValue(cnt,VAL_OPENPRICE),close[1]-TrailStop,OrderValue(cnt,VAL_TAKEPROFIT),Pink);
Exit; // Déplace le stop
};
};
};
};
/*
N.B. The stops and trailing stop in a BT can give false results, but this is nonetheless slight.
If the new bar opens under the stop the sale price calculated will be the price of the stop...
To oppose that it would be necessary to program a stop that closes the position has the opening of the bar.
//End
A model of reference will follow…