Orderstoploss in ordershistory

 

Upon the completion of a trade, I want to export the particulars of the trade to a CSV but I am having problems with orderstoploss(). During the trade, the stop loss is trailed. Ideally, I want to know what the stop was set at originally when the trade was entered. OrderStopLoss() is giving me the stop as it was set at the end of the trade, which often (although not often enough) is better than the opening price. Is there any way to get at this infomation? I was thinking of at open, converting the SL to a string and saving it in the comment field. Is there a better way

My code so far is below... just as a matter of convention / efficiency, I am currently, opening, writing and closing the file every loop. Is this the best way? or could / should I open the file, write to it in the loop and close the file at the end of the loop?

Comments appreciated

V

//Check for closed trades and export details    
   if (LastHistoryCount!=OrdersHistoryTotal())
      {
      for(int n = LastHistoryCount ; n < OrdersHistoryTotal(); n++)
         {
         OrderSelect(n, SELECT_BY_POS, MODE_HISTORY);
         double open=OrderOpenPrice();
         double close=OrderClosePrice();
         string date=TimeToStr(OrderOpenTime(),TIME_DATE|TIME_MINUTES);
         int    type=OrderType();
         string notes=OrderComment();
         double profit=OrderProfit();
         int    id=OrderTicket();
         double stop=OrderStopLoss();
         handle=FileOpen("tradehist.csv",FILE_CSV|FILE_READ|FILE_WRITE,';');
         if (handle>0)
            {
            FileSeek(handle, 0, SEEK_END);
            FileWrite(handle, id,date, type, open,close,profit,stop,notes);
            FileClose(handle);
            handle=0;
            }
         }
      }
  LastHistoryCount=OrdersHistoryTotal();
 

Viffer,

If variable stop is to have more than 1 value then it must be an array; bind this array with other variables container to an incrementing counter variable/header. When you call this counter, you can

access all particulars that is stored at that moment. hth

 
Viffer:

Upon the completion of a trade, I want to export the particulars of the trade to a CSV but I am having problems with orderstoploss(). During the trade, the stop loss is trailed. Ideally, I want to know what the stop was set at originally when the trade was entered. OrderStopLoss() is giving me the stop as it was set at the end of the trade, which often (although not often enough) is better than the opening price. Is there any way to get at this infomation? I was thinking of at open, converting the SL to a string and saving it in the comment field. Is there a better way

My code so far is below... just as a matter of convention / efficiency, I am currently, opening, writing and closing the file every loop. Is this the best way? or could / should I open the file, write to it in the loop and close the file at the end of the loop?

Comments appreciated

V

Edited : Sorry I missed the point of your question. When you first set your stoploss... you must have a stoploss value in hand. fill your csv's stop variable with that value. and simply don't update it when you trail it with new stoploss.

 

Yeah, it's the "don't update it" part that gets tricky when I start pyramiding. That's what got me to saving it in comment. The array idea got me thinking though for a 2d array to set ticket and SL on open and then on close search for ticket in the array to retrieve SL... Seems a lot of work for something that must be a fairly common need though.

Thanks

V