[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[amibroker] Re: AB system into TradeSim Database



PureBytes Links

Trading Reference Links

--- In amibroker@xxxxxxxxxxxxxxx, "Greg" <crootster@xxx> wrote:
>
> I have a system or two that I have been working on and being that I 
> have TradeSim I would like to put it across to its database to test it 
> there as well as in AB.Any help would be greatly appreciated.
> 
> Cheers Greg
>
Greg,
This is a function called from AFL exploration ( backtest
whatever)that I wrote some time ago.  It worked for long only trades.
I understand Tradesim allows text file input that include short
trades. Perhaps you could modify? Hope this is what you were looking for.
franc

/////////////////////////////////////////////////////////////////////////////////////////////////////

// Procedure ABtoMS( sFileName, Shift ): TextFile

/////////////////////////////////////////////////////////////////////////////////////////////////////
//Function ABtoMS() writes a Tradesim compatible Trade Database
//textfile from a Scan/Exploration
//The completed buy/sell trades are within the AA Range setting
//Tradesim cannot accept incomplete trades; trades are closed at AA
//ToRange setting or lastBar.

// NOTES:
//1) output writen to msFileName = "C:\\Temp\\ABtoMS.trt"
//2) The output is appended to an existing file OR creates a new file.
//3) InitialStop MUST BE CALCULATED (global variable)
//4) AA Range read, only quotations between the range are written to
//   the file
//5) Open Trades are completed using the ToRange Date values
//6) Amibroker outputs buy/sell signals. Need delay for actual trade

/*
--------------------------------------------------------------------------------------------------
   TRADESIM FIELD			AMIBROKER			FORMAT				COMMENT
  
--------------------------------------------------------------------------------------------------
   ticker						Name()				string
	Type						"L"					string				(L)ong trades only
	Entry Date				YYYYMMDD			string				Buy Date
	Exit Date					YYYYMMDD			string				Sell Date
	InitialStop				InitialStop		7.4					MUST be calculated
	BuyPrice					BuyPrice			7.4
	SellPrice					SellPrice			7.4
	EntryLowPrice				Low					7.4					optional - Low at Buy Date
	EntryHighPrice			High				7.4					optional - High at Buy Date
	ExitLowPrice				Low					7.4					optional - Low at Sell Date
	ExitHighPrice				High				7.4					optional - High at Sell Date
	TradedSize				Volume				3.0					optional - Volume at Buy Date
//---------------------------------------------------------------------------------------------------
   TRADE RECORD CONSTRUCTION:
   a single space between each field required

   sTicker			symbol + " L"
   sEntryDate			EntryDate
   sExitDate			ExitDate
   sEntryPrice		InitialStop, Buyprice
   sExitPrice			Sellprice,
   sBuyHiLO			Low, High
   sSellHiLo			Low, High
   sVolume			Volume
**/
/////////////////////////////////////////////////////////////////////////////////////////////////////

procedure ABtoMS( msFileName, Shift )
{
  local bfirst, bRange;
  local sTicker, sTradedSize;
  local sPageDesc, sHeader;
  local sEntryDate, sEntryPrice, sBuyHiLo;
  local sExitDate, sExitPrice, sSellHiLo;
  local y, m, d, i, fh;

  global Buy, Sell, BuyPrice, SellPrice, InitialStop, TradedVol;


  if( msFileName == "" ) { msFileName =
"c:\\Temp\\ABtoMS-TradeFile.trt"; }

  //Read InBarRange; true when between From/To dates or n quotations
  bRange = Status("barinrange");

  // generate the year, month, day arrays for all the quotations
  y = Year();
  m = Month();
  d = Day();

//---------------------------------------------------------------------------------------------------
/** this is an extract of the format Tradesim requires...
**# Text Trade Database Example
**# Comments always begin with the # character
**
**# The compulsory fields are always required in the order shown on
the next line,
**# [Symbol][Trade Position][Entry Date][Exit Date][Initial
Stop][Entry Price][Exit Price]
**
**# The optional fields which are NOT used in this example should be
ordered as follows,
**# [Low Entry Price][High Entry Price][Low Exit Price][High Exit
Price][Traded Volume]
**
**BHP L 19960419 19960607 0.0000 19.2200 18.5800
**BHP S 19960607 19961018 0.0000 18.5800 17.0400
*/
// create a new or open an existing file to append trade details
// make sure the directory exists

  // file does not exist, so create a new file
  fh =fopen( msFileName, "a");
  if ( fh )
  {
    //a buy must occur before a sell, only the first buy and sell
    //accepted
    //subsequent multiple Buy/Sell signals ignored, until trade
    //details complete
    //ignore signals until Buy found, then follow with Sell to
    //construct trade record
    bfirst = 0;

    for( i = 0; i < BarCount; i++ )
    {
      //find the first buy, then construct Buy details
      j = i - Shift;
      if( j < 0 ) J = 0;

      if( Buy[j] AND bRange[i] == 1 AND bfirst == 0 )
      {
        // initialise the output strings to default
        bfirst      = 1;
        sTicker     = "      ";							// (6)
        sEntryDate  = "         ";						// (9)
        sExitDate   = "         ";						// (9)
        sEntryPrice = "                        ";// (16)
        sExitPrice  = "        ";// (8)
        sBuyHiLo    = "                ";//(16)
        sSellHiLo   = "                ";// (16)
        sVolume     = "         ";// (8+1)

        // Construct the Trade BUY details
        sTicker     = Name() + " L ";
        sEntryDate  = StrFormat("%04.0f%02.0f%02.0f ",y[i],m[i],d[i]);
        sEntryPrice = StrFormat("%07.4f %07.4f ",
                                 InitialStop[j],BuyPrice[i] );
        sBuyHiLo    = StrFormat( "%07.4f %07.4f ",L[i], H[i] );
        sVolume     = StrFormat( "%3.0f ",TradedVol[j] );
      }  // end if buy

      //find the first sell after buy,
      //the (symbol lastbar->[i]<barcount) OR (AA ToRange) could occur 
      //before Sell Signal
      //construct the Trade Sell details to complete the trade record.
//      if( ( Sell[i-Shift] OR ( bRange[i] == 0 ) OR ( i >= BarCount-1 
//             ) ) AND bfirst == 1 ) 
      if((Sell[j] OR (bRange[i]==0) OR (i>=BarCount-1)) AND bfirst==1)
      {
        bfirst = 0;
        // construct SELL details
        sExitDate   = StrFormat("%04.0f%02.0f%02.0f ",y[i],m[i],d[i]);
        sExitPrice  = StrFormat( "%07.4f ",SellPrice[i] );
        sSellHiLo   = StrFormat( "%07.4f %07.4f ",L[i],H[i] );

        //buy & sell details complete, now to write the trade record
        fputs(sTicker + sEntryDate + sExitDate +
                 sEntryPrice + sExitPrice +
                  sBuyHiLo + sSellHiLo +
                    sVolume +"\n", fh );
      }  // end if sell

    }  //end for

    fclose( fh );
  }  //end fh

}  //end function ABtoMS
/////////////////////////////////////////////////////////////////////////////////////////////////////



Please note that this group is for discussion between users only.

To get support from AmiBroker please send an e-mail directly to 
SUPPORT {at} amibroker.com

For NEW RELEASE ANNOUNCEMENTS and other news always check DEVLOG:
http://www.amibroker.com/devlog/

For other support material please check also:
http://www.amibroker.com/support.html
 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/amibroker/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/amibroker/join
    (Yahoo! ID required)

<*> To change settings via email:
    mailto:amibroker-digest@xxxxxxxxxxxxxxx 
    mailto:amibroker-fullfeatured@xxxxxxxxxxxxxxx

<*> To unsubscribe from this group, send an email to:
    amibroker-unsubscribe@xxxxxxxxxxxxxxx

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/