Hello! I am testing extreme failures and reaming, the issue:
1-Open script in trade mode (script buys and sell every 5 min, wait until zorro open a trade at 10:00 NY
2-At 10:02 NY i close zorro with (CTRL+ALT+SUP)
3-At 10:03 NY i open zorro in trade mode and then a second bought is made
4-At 10:05 NY zorro close just the second bought but not the first.
Question is: How to avoid this? There is a proper rearming step guide? I have to code new solution to avoid this, comparing Broker with zorro number of shares? My code is wrong?
I copy the code, it is not any trading strategy, it just for testing:
//CODE BELLOW
// AMZN intraday: invests NAV/3 in AMZN in fixed New York time windows
// Backtest starts with 100,000 USD. Live/demo uses the account equity.
// Robust version: tolerates missing 1-minute bars (acts on the next bar).
#define NWIN 6
// NY time NowMin
// 10:00 600
// 11:00 660
// 12:00 720
// 13:00 780
// 14:00 840
// 15:00 900
// 16:00 (close) 960
// New York time in minutes since midnight (10:00 = 600)
int BuyTime[NWIN] = { 600, 610, 620, 630, 640, 650};
int SellTime[NWIN] = { 605, 615, 625, 635, 645, 655};
// TODO Meter bucle aqui debajo
BuyTime [0] = 800;
SellTime [0] = 800;
// per-day state: what has already been done in each window
int Bought[NWIN];
int Sold[NWIN];
int LastMin = 0;
function run()
{
BarPeriod = 1;
LookBack = 0;
StartDate = 2023;
EndDate = 2024;
int i, NowMin, Shares;
var NAV;
set(LOGFILE, PLOTNOW); // must be inside run()
set(NFA); //IBKR need this to close
if(!is(TRADEMODE))
Capital = 100000;
if(is(INITRUN))
assetList("History\\TEST\\AssetsTest.csv");
asset("AMZN");
if(is(LOOKBACK)) return;
NowMin = lhour(ET)*60 + minute();
if(is(TRADEMODE))
printf("\nNowMin %d = %02d:%02d NY", NowMin, lhour(ET), minute());
// New day detected: NY time went backwards -> reset the flags
if(NowMin < LastMin || is(INITRUN))
{
for(i = 0; i < NWIN; i++) {
Bought[i] = 0;
Sold[i] = 0;
}
}
LastMin = NowMin;
for(i = 0; i < NWIN; i++)
{
// SELL: window was entered, sell time reached, not yet sold
if(Bought[i] && !Sold[i] && NowMin >= SellTime[i])
{
Sold[i] = 1;
if(is(TRADEMODE))
printf("\n%02i:%02i NY SELL all SHARES (%i open)",
lhour(ET), minute(), NumOpenLong);
exitLong();
}
// BUY: inside the window [BuyTime, SellTime), not yet bought today, flat
if(!Bought[i] && NowMin >= BuyTime[i] && NowMin < SellTime[i] && NumOpenLong == 0)
{
NAV = Equity; // cash + open trades
Shares = floor(NAV/3./priceClose());
if(is(TRADEMODE))
printf("\n%02i:%02i NY BUY NAV %.0f price %.2f shares %i",
lhour(ET), minute(), NAV, priceClose(), Shares);
if(Shares > 0) {
Bought[i] = 1;
Amount = Shares; // number of IWM shares
enterLong();
}
}
// window passed without a buy (e.g. no bars): mark it as done
if(!Bought[i] && NowMin >= SellTime[i])
Bought[i] = Sold[i] = 1;
}
if(NowMin > SellTime[NWIN-1] && NumOpenLong > 0) // safety: flat after 10:55
exitLong();
plot("NAV", Equity, NEW, BLUE);
}