//+------------------------------------------------------------------+ //| NTC_Satik02.mq4 | //| Copyright 2019, NTC | //| | //+------------------------------------------------------------------+ #property copyright "Copyright 2019, NTC" #property version "1.00" #property script_show_inputs #property strict #property description "Daily continuation of an initial trend" #property description "Place two (Long and Short) OCO stops at 10:00 (optional) New York Close Chart time" #property description "Stop's distance is 400 (optional) points away from Open price (at the specified time)" #property description "Set StopLoss to 800 (optional) points and TakeProfit to 2400 (optional) points" #property description "Stop's expiration is set to 19:00 NY Close chart time" #property description "Order closure at 23:59 (Last bar close) NY Close chart time" #property description "Tested on H1 GBPUSD." #define CLR_BUY clrDarkGreen #define CLR_SELL clrMaroon #define CLR_SL clrDeepPink #define CLR_TP clrDarkTurquoise #define LINE_WIDTH 2 #define EXPIRATION_HOUR 19 #define CLOSE_HOUR 0 #define CLOSE_FRIDAY_HOUR 0 string name="Satik02"; long chartID=ChartID(); int visibility; input datetime inpFromDate = 0; // From date input datetime inpToDate = 0; // To date input int inpInitialHour = 10; // Initial hour input int inpStopDistance = 400; // Stop distance from open price input int inpStopLoss = 800; // SL in points input int inpTakeProfit = 2400; // TP in points input int inpSpread = 15; // Spread in points input bool inpClear = false; // Just clear lines from the chart //+------------------------------------------------------------------+ //| Script program start function | //+------------------------------------------------------------------+ void OnStart() { DeleteLines(); Comment(""); if(inpClear) return; visibility=Visibility(); double longStop, shortStop, longPrice, shortPrice; datetime openStop, expiration; int sumPoints=0, points, longProfitCount=0, longLossCount=0, shortProfitCount=0, shortLossCount=0, longProfitSum=0, longLossSum=0, shortProfitSum=0, shortLossSum=0, startBar=inpFromDate>0?iBarShift(_Symbol,_Period,inpFromDate):(Bars-1), endBar=inpToDate>0?iBarShift(_Symbol,_Period,inpToDate)-1:-1, i; while(Time[startBar]-1) startBar--; i=startBar; for(;i>endBar && i>-1;i--) { if(TimeHour(Time[i])==inpInitialHour) { openStop=Time[i]; CheckStop(longStop,shortStop,longPrice,shortPrice,expiration,i); DrawStop(openStop,expiration,longStop,shortStop); if(longPrice>0.0) { CheckLong(longPrice,points,i); sumPoints+=points; if(points<0) { longLossCount++; longLossSum+=points; } else { longProfitCount++; longProfitSum+=points; } } else if(shortPrice>0.0) { CheckShort(shortPrice,points,i); sumPoints+=points; if(points<0) { shortLossCount++; shortLossSum+=points; } else { shortProfitCount++; shortProfitSum+=points; } } } } string comment1=StringFormat("From %s to %s ; Initial hour=%d , Stop distance=%d , StopLoss=%d , TakeProfit=%d , Spread=%d\n", TimeToString(Time[startBar]),TimeToString(Time[endBar+1]),inpInitialHour,inpStopDistance,inpStopLoss,inpTakeProfit,inpSpread), comment2=StringFormat("Long trades: %d => %d (%.2f%%) profit, %d (%.2f%%) loss ; Profit points=%d ; Loss points=%d ; Sum points=%d\n", longProfitCount+longLossCount, longProfitCount,100.0*longProfitCount/double(longProfitCount+longLossCount), longLossCount,100.0*longLossCount/double(longProfitCount+longLossCount), longProfitSum,-longLossSum,longProfitSum+longLossSum), comment3=StringFormat("Short trades: %d => %d (%.2f%%) profit, %d (%.2f%%) loss ; Profit points=%d ; Loss points=%d ; Sum points=%d\n", shortProfitCount+shortLossCount, shortProfitCount,100.0*shortProfitCount/double(shortProfitCount+shortLossCount), shortLossCount,100.0*shortLossCount/double(shortProfitCount+shortLossCount), shortProfitSum,-shortLossSum,shortProfitSum+shortLossSum), comment4=StringFormat("Total trades: %d => %d (%.2f%%) profit, %d (%.2f%%) loss ; LPR= 1 : %.2f ; Sum points=%d\n", longProfitCount+longLossCount+shortProfitCount+shortLossCount, longProfitCount+shortProfitCount,100.0*(longProfitCount+shortProfitCount)/double(longProfitCount+longLossCount+shortProfitCount+shortLossCount), longLossCount+shortLossCount,100.0*(longLossCount+shortLossCount)/double(longProfitCount+longLossCount+shortProfitCount+shortLossCount), double(longProfitSum+shortProfitSum)/double(longLossSum+shortLossSum)*-1.0,sumPoints); Comment(comment1,comment2,comment3,comment4); } void CheckStop(double& longStop,double& shortStop,double& longPrice,double& shortPrice,datetime& expiration,int& i) { longStop=Open[i]+(double)inpStopDistance*_Point; shortStop=Open[i]-(double)inpStopDistance*_Point; longPrice=shortPrice=0.0; expiration=0; bool valid=true; for(;valid && i>-1;i--) { valid=TimeHour(Time[i])!=EXPIRATION_HOUR; if(High[i]>=longStop) { longPrice=longStop; valid=false; } if(Low[i]<=shortStop) { shortPrice=shortStop; valid=false; } } i++; if(valid) expiration=Time[0]; else expiration=Time[i]; } void CheckLong(double openPrice,int& points,int& i) { openPrice+=(double)inpSpread*_Point; points=0; datetime closeTime=0, openTime=Time[i]; double sl=openPrice-(double)inpStopLoss*_Point, tp=openPrice+(double)inpTakeProfit*_Point, closePrice=0.0; int closeHour=TimeDayOfWeek(Time[i])==5?CLOSE_FRIDAY_HOUR:CLOSE_HOUR; bool valid=true; for(;valid && i>-1;i--) { valid=TimeHour(Time[i])!=closeHour; if(High[i]>=tp) { closePrice=tp; valid=false; } else if(Low[i]<=sl) { closePrice=sl; valid=false; } } i++; closeTime=Time[i]; if(closePrice-1;i--) { valid=TimeHour(Time[i])!=closeHour; if(Low[i]<=tp) { closePrice=tp; valid=false; } else if(High[i]>=sl) { closePrice=sl; valid=false; } } i++; closeTime=Time[i]; if(closePrice-1;i--) { objectName=ObjectName(i); if(StringFind(objectName,name)>-1) ObjectDelete(chartID,objectName); } }